dbtoepub 2.6 KB

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