-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepub_resources
110 lines (94 loc) · 3.17 KB
/
epub_resources
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env ruby
# frozen_string_literal: true
# Script for embedding resource links
# epub_process -n -f embed_link -m manifest.csv book.epub
require 'optparse'
require 'ostruct'
require 'os'
# Determine the root directory of the code base.
script_dir = File.expand_path(File.dirname(__FILE__))
root_dir = File.dirname(script_dir)
require_relative File.join(root_dir, "lib", "logger")
script_logger = UMPTG::Logger.create(logger_fp: STDOUT)
# Process the script parameters.
options = OpenStruct.new
options.manifest_file = nil
options.normalize = false
options.filter_list = []
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [-n] [-m manifest_file] [-f filter] epub_file [epub_file...]"
opts.on('-f', '--filter filter', 'Filter name') do |filter|
options.filter_list << filter.to_sym
end
opts.on('-m', '--manifest manifest_file', 'Resource manifest file') do |manifest_file|
options.manifest_file = manifest_file
end
opts.on('-n', '--normalize', 'Use which link as resource') do |flag|
options.normalize = true
end
opts.on_tail('-h', '--help', 'Print this help message') do
script_logger.info(opts)
exit 0
end
end
option_parser.parse!(ARGV)
if ARGV.count < 1
script_logger.info(option_parser.help)
return
end
# Process the command line parameters.
epub_file_list = ARGV
require_relative File.join(root_dir, "lib", "epub")
require_relative File.join(root_dir, "lib", "fulcrum")
require_relative File.join(root_dir, "lib", "xml", "pipeline")
manifest = nil
unless options.manifest_file.nil?
manifest_file = File.expand_path(options.manifest_file)
unless File.file?(manifest_file)
script_logger.error("#{manifest_file} is not a file.")
exit 1
end
manifest = UMPTG::Fulcrum::Manifest::Document.new(
csv_file: manifest_file
)
end
filter_list = options.filter_list.empty? ? [ :embed_link ] : options.filter_list
ops = {}
filter_list.each {|f| ops[f] = true }
fixer_processor = UMPTG::Fulcrum::Resources::Processor.new(
name: "Fixer",
manifest: manifest,
process_figures: true,
options: ops
)
epub_processor = UMPTG::Fulcrum::Resources.EPUBProcessor(
xml_processor: fixer_processor
)
epub_file_list.each do |epub_file|
unless File.file?(epub_file)
script_logger.error("invalid EPUB file #{epub_file}")
exit 1
end
script_logger.info("processing EPUB file #{File.basename(epub_file)}")
epub = UMPTG::EPUB::Archive.new(epub_file: epub_file)
logger_file = File.join(
File.dirname(epub_file),
File.basename(epub_file, ".*") + "_" + File.basename(__FILE__) + ".log"
)
epub_processor.logger = UMPTG::Logger.create(logger_file: logger_file)
epub_processor.logger.info("Processing EPUB file #{File.basename(epub_file)}")
epub_processor.run(
epub,
{
normalize: options.normalize
}
)
if epub.modified
new_epub_file = File.join(
File.dirname(epub_file),
File.basename(epub_file, ".*") + "_" + File.basename(__FILE__) + File.extname(epub_file)
)
epub.save(epub_file: new_epub_file)
script_logger.info("Saved #{new_epub_file}.")
end
end