-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepub2html
61 lines (48 loc) · 1.8 KB
/
epub2html
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# HELIO-3510, generate basic HTML instance from an EPUB,
# either reflowable or fixed layout.
require 'optparse'
require 'ostruct'
# Process the script parameters.
options = OpenStruct.new
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [-h] epub_file [epub_file...]"
opts.on_tail('-h', '--help', 'Print this help message') do
puts opts
exit 0
end
end
option_parser.parse!(ARGV)
if ARGV.count < 1
puts option_parser.help
return
end
# Determine the root directory of the code base.
script_dir = File.expand_path(File.dirname(__FILE__))
#root_dir = File.dirname(script_dir)
root_dir = File.dirname(File.expand_path(File.join(script_dir, "..", "..")))
epub_file_list = ARGV
require_relative File.join(root_dir, "lib", "epub")
epub_file_list.each do |epub_file|
epub_file = File.expand_path(epub_file)
unless File.exist?(epub_file)
puts "Error: invalid EPUB file path #{epub_file}."
next
end
puts "Processing #{File.basename(epub_file)}"
epub = UMPTG::EPUB::Archive.new(epub_file: epub_file)
if epub.renditions.empty?
puts "Warning: no renditions for EPUB #{File.basename(epub_file)}"
next
end
# If EPUB has a text rendition, use it. Otherwise, use first rendition.
text_renditions = epub.renditions.select {|r| r.text_rendition? }
rendition = text_renditions.first unless text_renditions.empty?
rendition = epub.renditions.first if text_renditions.empty?
puts "\tUsing rendition \"#{rendition.label}\"" unless rendition.label.nil?
puts "\tUsing rendition (no label)" if rendition.label.nil?
xhtml_markup = epub.to_xhtml(rendition: rendition)
html_file = File.join(File.dirname(epub_file), File.basename(epub_file, ".*") + ".xhtml")
File.write(html_file, xhtml_markup)
end