| 1 |
#!/usr/bin/env ruby |
| 2 |
# This program converts DocBook documents into .epub files. |
| 3 |
# |
| 4 |
# Usage: dbtoepub [OPTIONS] [DocBook Files] |
| 5 |
# |
| 6 |
# .epub is defined by the IDPF at www.idpf.org and is made up of 3 standards: |
| 7 |
# - Open Publication Structure (OPS) |
| 8 |
# - Open Packaging Format (OPF) |
| 9 |
# - Open Container Format (OCF) |
| 10 |
# |
| 11 |
# Specific options: |
| 12 |
# -c, --css [FILE] Use FILE for CSS on generated XHTML. |
| 13 |
# -d, --debug Show debugging output. |
| 14 |
# -f, --font [OTF FILE] Embed OTF FILE in .epub. |
| 15 |
# -h, --help Display usage info. |
| 16 |
# -s, --stylesheet [XSL FILE] Use XSL FILE as a customization |
| 17 |
# layer (imports epub/docbook.xsl). |
| 18 |
# -v, --verbose Make output verbose. |
| 19 |
|
| 20 |
lib = File.expand_path(File.join(File.dirname(__FILE__), 'lib')) |
| 21 |
$LOAD_PATH.unshift(lib) if File.exist?(lib) |
| 22 |
|
| 23 |
require 'fileutils' |
| 24 |
require 'optparse' |
| 25 |
require 'tmpdir' |
| 26 |
|
| 27 |
require 'docbook' |
| 28 |
|
| 29 |
verbose = false |
| 30 |
debug = false |
| 31 |
css_file = nil |
| 32 |
otf_files = [] |
| 33 |
customization_layer = nil |
| 34 |
output_file = nil |
| 35 |
|
| 36 |
#$DEBUG=true |
| 37 |
|
| 38 |
# Set up the OptionParser |
| 39 |
opts = OptionParser.new |
| 40 |
opts.banner = "Usage: #{File.basename($0)} [OPTIONS] [DocBook Files] |
| 41 |
|
| 42 |
#{File.basename($0)} converts DocBook <book> and <article>s into to .epub files. |
| 43 |
|
| 44 |
.epub is defined by the IDPF at www.idpf.org and is made up of 3 standards: |
| 45 |
- Open Publication Structure (OPS) |
| 46 |
- Open Packaging Format (OPF) |
| 47 |
- Open Container Format (OCF) |
| 48 |
|
| 49 |
Specific options:" |
| 50 |
opts.on("-c", "--css [FILE]", "Use FILE for CSS on generated XHTML.") {|f| css_file = f} |
| 51 |
opts.on("-d", "--debug", "Show debugging output.") {debug = true; verbose = true} |
| 52 |
opts.on("-f", "--font [OTF FILE]", "Embed OTF FILE in .epub.") {|f| otf_files << f} |
| 53 |
opts.on("-h", "--help", "Display usage info.") {puts opts.to_s; exit 0} |
| 54 |
opts.on("-o", "--output [OUTPUT FILE]", "Output ePub file as OUTPUT FILE.") {|f| output_file = f} |
| 55 |
opts.on("-s", "--stylesheet [XSL FILE]", "Use XSL FILE as a customization layer (imports epub/docbook.xsl).") {|f| customization_layer = f} |
| 56 |
opts.on("-v", "--verbose", "Make output verbose.") {verbose = true} |
| 57 |
|
| 58 |
db_files = opts.parse(ARGV) |
| 59 |
if db_files.size == 0 |
| 60 |
puts opts.to_s |
| 61 |
exit 0 |
| 62 |
end |
| 63 |
|
| 64 |
db_files.each {|docbook_file| |
| 65 |
dir = File.expand_path(File.join(Dir.tmpdir, ".epubtmp#{Time.now.to_f.to_s}")) |
| 66 |
FileUtils.mkdir_p(dir) |
| 67 |
e = DocBook::Epub.new(docbook_file, dir, css_file, customization_layer, otf_files) |
| 68 |
|
| 69 |
if output_file |
| 70 |
epub_file = output_file |
| 71 |
else |
| 72 |
epub_file = File.basename(docbook_file, ".xml") + ".epub" |
| 73 |
end |
| 74 |
puts "Rendering DocBook file #{docbook_file} to #{epub_file}" if verbose |
| 75 |
e.render_to_file(epub_file) |
| 76 |
} |