]> granicus.if.org Git - docbook-dsssl/commitdiff
Adding support for user-specified customization layers in dbtoepub
authorKeith Fahlgren <abdelazer@users.sourceforge.net>
Tue, 8 Jul 2008 01:36:10 +0000 (01:36 +0000)
committerKeith Fahlgren <abdelazer@users.sourceforge.net>
Tue, 8 Jul 2008 01:36:10 +0000 (01:36 +0000)
xsl/epub/bin/dbtoepub
xsl/epub/bin/lib/docbook.rb
xsl/epub/bin/spec/epub_spec.rb
xsl/epub/bin/spec/files/test_cust.xsl [new file with mode: 0644]

index 5a41094b8e6884fc31081a18b0036a995d6bb481..abe176def2840b560c6d02b1978ef9f06e005420 100755 (executable)
 # 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)
index 827ff875d7f1cd19f7ca99e715518821530cbb61..96ed0cf40efca08e893eb31d79f93e050cca3cd2 100755 (executable)
@@ -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
index 4c734f49abc551bb636e1a7070c766713d43762e..17b62a5725c112530826183a2991ab6a067c23b7 100755 (executable)
@@ -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 (file)
index 0000000..6e66a58
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0"?>
+<xsl:stylesheet
+version="1.0"
+xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+  <xsl:import href="../../../../epub/docbook.xsl" />
+  <xsl:param name="xref.with.number.and.title" select="0"/>
+
+</xsl:stylesheet>