From ab335711f7914833a6f3d917f450047805966c71 Mon Sep 17 00:00:00 2001 From: Keith Fahlgren Date: Tue, 8 Jul 2008 01:36:10 +0000 Subject: [PATCH] Adding support for user-specified customization layers in dbtoepub --- xsl/epub/bin/dbtoepub | 14 +++++++++----- xsl/epub/bin/lib/docbook.rb | 10 ++++++++-- xsl/epub/bin/spec/epub_spec.rb | 26 ++++++++++++++++++++++++++ xsl/epub/bin/spec/files/test_cust.xsl | 8 ++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 xsl/epub/bin/spec/files/test_cust.xsl diff --git a/xsl/epub/bin/dbtoepub b/xsl/epub/bin/dbtoepub index 5a41094b8..abe176def 100755 --- a/xsl/epub/bin/dbtoepub +++ b/xsl/epub/bin/dbtoepub @@ -11,8 +11,10 @@ # Specific options: # -c, --css [FILE] Use FILE for CSS on generated XHTML. # -d, --debug Show debugging output. -# -h, --help Display usage info -# -v, --verbose Make output verbose +# -h, --help Display usage info. +# -s, --stylesheet [XSL FILE] Use XSL FILE as a customization +# layer (imports epub/docbook.xsl). +# -v, --verbose Make output verbose. lib = File.expand_path(File.join(File.dirname(__FILE__), 'lib')) $LOAD_PATH.unshift(lib) if File.exist?(lib) @@ -25,6 +27,7 @@ require 'docbook' verbose = false debug = false css_file = nil +customization_layer = nil # Set up the OptionParser opts = OptionParser.new @@ -40,8 +43,9 @@ opts.banner = "Usage: #{File.basename($0)} [OPTIONS] [DocBook Files] Specific options:" opts.on("-c", "--css [FILE]", "Use FILE for CSS on generated XHTML.") {|f| css_file = f} opts.on("-d", "--debug", "Show debugging output.") {debug = true; verbose = true} -opts.on("-h", "--help", "Display usage info") {puts opts.to_s; exit 0} -opts.on("-v", "--verbose", "Make output verbose") {verbose = true} +opts.on("-h", "--help", "Display usage info.") {puts opts.to_s; exit 0} +opts.on("-s", "--stylesheet [XSL FILE]", "Use XSL FILE as a customization layer (imports epub/docbook.xsl).") {|f| customization_layer = f} +opts.on("-v", "--verbose", "Make output verbose.") {verbose = true} db_files = opts.parse(ARGV) if db_files.size == 0 @@ -51,7 +55,7 @@ end db_files.each {|docbook_file| dir = File.expand_path(File.join(Dir.tmpdir, ".epubtmp#{Time.now.to_f.to_s}")) - e = DocBook::Epub.new(docbook_file, dir, css_file) + e = DocBook::Epub.new(docbook_file, dir, css_file, customization_layer) epub_file = File.basename(docbook_file, ".xml") + ".epub" puts "Rendering DocBook file #{docbook_file} to #{epub_file}" if verbose e.render_to_file(epub_file) diff --git a/xsl/epub/bin/lib/docbook.rb b/xsl/epub/bin/lib/docbook.rb index 827ff875d..96ed0cf40 100755 --- a/xsl/epub/bin/lib/docbook.rb +++ b/xsl/epub/bin/lib/docbook.rb @@ -19,13 +19,19 @@ module DocBook attr_reader :output_dir - def initialize(docbook_file, output_dir=OUTPUT_DIR, css_file=nil) + def initialize(docbook_file, output_dir=OUTPUT_DIR, css_file=nil, customization_layer=nil) @docbook_file = docbook_file @output_dir = output_dir @meta_dir = File.join(@output_dir, META_DIR) @oebps_dir = File.join(@output_dir, OEBPS_DIR) @css_file = css_file ? File.expand_path(css_file) : css_file @to_delete = [] + + if customization_layer + @stylesheet = File.expand_path(customization_layer) + else + @stylesheet = STYLESHEET + end unless File.exist?(@docbook_file) raise ArgumentError.new("File #{@docbook_file} does not exist") @@ -63,7 +69,7 @@ module DocBook oebps = "--stringparam epub.oebps.dir #{@oebps_dir}/" options = "--xinclude #{chunk_quietly} #{callout_path} #{callout_limit} #{callout_ext} #{base} #{meta} #{oebps} #{html_stylesheet}" # Double-quote stylesheet & file to help Windows cmd.exe - db2epub_cmd = "#{XSLT_PROCESSOR} #{options} \"#{STYLESHEET}\" \"#{@docbook_file}\"" + db2epub_cmd = "#{XSLT_PROCESSOR} #{options} \"#{@stylesheet}\" \"#{@docbook_file}\"" STDERR.puts db2epub_cmd if $DEBUG success = system(db2epub_cmd) raise "Could not render as .epub to #{output_file} (#{db2epub_cmd})" unless success diff --git a/xsl/epub/bin/spec/epub_spec.rb b/xsl/epub/bin/spec/epub_spec.rb index 4c734f49a..17b62a572 100755 --- a/xsl/epub/bin/spec/epub_spec.rb +++ b/xsl/epub/bin/spec/epub_spec.rb @@ -218,6 +218,32 @@ describe DocBook::Epub do end end + it "should allow for the stylesheets to be overridden by a customization layer" do + begin + tmpdir = File.join(Dir::tmpdir(), "epubcusttest"); Dir.mkdir(tmpdir) rescue Errno::EEXIST + + css_file = nil + customization_layer = File.join(@filedir, "test_cust.xsl") + epub = DocBook::Epub.new(File.join(@testdocsdir, "xref.001.xml"), @tmpdir, css_file, customization_layer) + epubfile = File.join(tmpdir, "cust.epub") + epub.render_to_file(epubfile, $DEBUG) + FileUtils.copy(epubfile, ".cust.epub") if $DEBUG + + success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{epubfile}") + raise "Could not unzip #{epubfile}" unless success + glob = Dir.glob(File.join(tmpdir, "**", "*.html")) + # The customization layer changes the style of cross references to _not_ + # include the title, so it should only appear in the part file and the + # TOC + files_including_part_title = glob.find_all {|html_file| File.open(html_file).readlines.to_s =~ />[^<]*Part One Title/} + files_including_part_title.length.should == 2 + rescue => e + raise e + ensure + FileUtils.rm_r(tmpdir, :force => true) + end + end + after(:all) do FileUtils.rm_r(@tmpdir, :force => true) end diff --git a/xsl/epub/bin/spec/files/test_cust.xsl b/xsl/epub/bin/spec/files/test_cust.xsl new file mode 100644 index 000000000..6e66a58b7 --- /dev/null +++ b/xsl/epub/bin/spec/files/test_cust.xsl @@ -0,0 +1,8 @@ + + + + + + -- 2.49.0