]> granicus.if.org Git - docbook-dsssl/commitdiff
Adding CSS support to .epub target & dbtoepub:
authorKeith Fahlgren <abdelazer@users.sourceforge.net>
Mon, 23 Jun 2008 17:09:11 +0000 (17:09 +0000)
committerKeith Fahlgren <abdelazer@users.sourceforge.net>
Mon, 23 Jun 2008 17:09:11 +0000 (17:09 +0000)
    -c, --css [FILE]                 Use FILE for CSS on generated XHTML.

DocBook::Epub
...
- should include a CSS link in HTML files when a CSS file has been provided
- should include CSS file in .epub when a CSS file has been provided
- should include a CSS link in OPF file when a CSS file has been provided

xsl/epub/bin/dbtoepub
xsl/epub/bin/lib/docbook.rb
xsl/epub/bin/spec/epub_regressions_spec.rb
xsl/epub/bin/spec/epub_small_smoketest.rb
xsl/epub/bin/spec/epub_smoketest_spec.rb
xsl/epub/bin/spec/epub_spec.rb
xsl/epub/bin/spec/files/test.css [new file with mode: 0644]
xsl/epub/docbook.xsl

index c94a1072bbe6a21c136e4ff6c7da5a0096ddd978..5a41094b8e6884fc31081a18b0036a995d6bb481 100755 (executable)
@@ -9,6 +9,7 @@
 # - Open Container Format (OCF)
 #
 # 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
@@ -17,11 +18,13 @@ lib = File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
 $LOAD_PATH.unshift(lib) if File.exist?(lib)
 
 require 'optparse'
+require 'tmpdir'
 
 require 'docbook'
 
 verbose = false
 debug = false
+css_file = nil
 
 # Set up the OptionParser
 opts = OptionParser.new
@@ -35,6 +38,7 @@ opts.banner = "Usage: #{File.basename($0)} [OPTIONS] [DocBook Files]
 - Open Container Format (OCF)
 
 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}
@@ -46,7 +50,8 @@ if db_files.size == 0
 end
 
 db_files.each {|docbook_file|
-  e = DocBook::Epub.new(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)
   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 33f5e605f46a795842e95fd377abf4693546428e..827ff875d7f1cd19f7ca99e715518821530cbb61 100755 (executable)
@@ -1,5 +1,6 @@
-require 'rexml/parsers/pullparser'
 require 'fileutils'
+require 'rexml/parsers/pullparser'
+
 module DocBook
 
   class Epub
@@ -18,11 +19,12 @@ module DocBook
 
     attr_reader :output_dir
 
-    def initialize(docbook_file, output_dir=OUTPUT_DIR)
+    def initialize(docbook_file, output_dir=OUTPUT_DIR, css_file=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 = []
 
       unless File.exist?(@docbook_file)
@@ -51,14 +53,15 @@ module DocBook
 
     private
     def render_to_epub(output_file, verbose)  
-      chunk_quietly = "--stringparam chunk.quietly " + (verbose ? '0' : '1')
-      callout_path =  "--stringparam callout.graphics.path #{CALLOUT_PATH}/"
-      callout_limit = "--stringparam callout.graphics.number.limit #{CALLOUT_LIMIT}"
-      callout_ext =   "--stringparam callout.graphics.extension #{CALLOUT_EXT}" 
-      base =          "--stringparam base.dir #{@oebps_dir}/" 
-      meta =          "--stringparam epub.metainf.dir #{@meta_dir}/" 
-      oebps =         "--stringparam epub.oebps.dir #{@oebps_dir}/" 
-      options = "--xinclude #{chunk_quietly} #{callout_path} #{callout_limit} #{callout_ext} #{base} #{meta} #{oebps}"
+      chunk_quietly =   "--stringparam chunk.quietly " + (verbose ? '0' : '1')
+      callout_path =    "--stringparam callout.graphics.path #{CALLOUT_PATH}/"
+      callout_limit =   "--stringparam callout.graphics.number.limit #{CALLOUT_LIMIT}"
+      callout_ext =     "--stringparam callout.graphics.extension #{CALLOUT_EXT}" 
+      html_stylesheet = "--stringparam html.stylesheet #{File.basename(@css_file)}" if @css_file
+      base =            "--stringparam base.dir #{@oebps_dir}/" 
+      meta =            "--stringparam epub.metainf.dir #{@meta_dir}/" 
+      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}\""
       STDERR.puts db2epub_cmd if $DEBUG
@@ -71,9 +74,10 @@ module DocBook
     def bundle_epub(output_file, verbose)  
       quiet = verbose ? "" : "-q"
       mimetype_filename = write_mimetype()
-      meta  = File.basename(@meta_dir)
+      meta   = File.basename(@meta_dir)
       oebps  = File.basename(@oebps_dir)
       images = copy_images()
+      csses  = copy_csses()
       callouts = copy_callouts()
       # zip -X -r ../book.epub mimetype META-INF OEBPS
       # Double-quote stylesheet & file to help Windows cmd.exe
@@ -100,6 +104,13 @@ module DocBook
       return new_callout_images
     end
 
+    def copy_csses
+      if @css_file 
+        css_new_filename = File.join(@oebps_dir, File.basename(@css_file))
+        FileUtils.cp(@css_file, css_new_filename)
+      end
+    end
+
     def copy_images
       image_references = get_image_refs()
       new_images = []
index b34db8ad862063e6534713d95700e23c74c9bbbf..ac6a9cedb6dd477ad239ae7ccbd666efa3574308 100755 (executable)
@@ -20,7 +20,7 @@ $DEBUG = false
 describe DocBook::Epub do
   before(:all) do
     @filedir = File.expand_path(File.join(File.dirname(__FILE__), 'files'))
-    @testdocsdir = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs'))
+    @testdocsdir = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs', 'tests'))
     @tmpdir = File.join(Dir::tmpdir(), "epubregressions"); Dir.mkdir(@tmpdir) rescue Errno::EEXIST
   end
 
index a2feac678f326f12351e42758f8ec7c8c4b7ea6b..6834792a50d7948ee3a765b7b83ab7267bc9e106 100755 (executable)
@@ -16,7 +16,7 @@ require 'docbook'
 
 $DEBUG = false
 
-TESTDOCSDIR = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs'))
+TESTDOCSDIR = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs', 'tests'))
 NUMBER_TO_TEST = 15
 
 describe DocBook::Epub do
index 27f389cf38aa6ea05907b2b4827f8da30e6b821d..9d81469a33c6ef4d9942bfa81b4c87b3388fcf01 100755 (executable)
@@ -17,7 +17,7 @@ require 'docbook'
 
 $DEBUG = false
 
-TESTDOCSDIR = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs'))
+TESTDOCSDIR = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs', 'tests'))
 
 describe DocBook::Epub do
 
index f5a90492b8bca6c1449a61e29ca689e0dbb2bf91..54cdf397784e519e31250b7fc4da9f7afd8e3c4f 100755 (executable)
@@ -19,13 +19,11 @@ $DEBUG = false
 describe DocBook::Epub do
   before(:all) do
     @filedir = File.expand_path(File.join(File.dirname(__FILE__), 'files'))
-    @testdocsdir = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs'))
+    @testdocsdir = File.expand_path(File.join(ENV['DOCBOOK_SVN'], 'testdocs', 'tests'))
     exampledir = File.expand_path(File.join(File.dirname(__FILE__), 'examples'))
     @valid_epub = File.join(exampledir, "AMasqueOfDays.epub")
     @tmpdir = File.join(Dir::tmpdir(), "epubspec"); Dir.mkdir(@tmpdir) rescue Errno::EEXIST
 
-    @css_file_base = "test.css"
-    @css_file = File.join(@filedir, @css_file_base)
 
     @simple_bookfile = File.join(@testdocsdir, "book.001.xml")
     @simple_epub = DocBook::Epub.new(@simple_bookfile, @tmpdir)
@@ -36,6 +34,13 @@ describe DocBook::Epub do
     @manygraphic_epubfile  = File.join(@tmpdir, "manygraphicepub.epub")
     @manygraphic_epub.render_to_file(@manygraphic_epubfile, $DEBUG)
 
+    @css_file_base = "test.css"
+    @css_file = File.join(@filedir, @css_file_base)
+    @css_epub = DocBook::Epub.new(File.join(@testdocsdir, "book.002.xml"), @tmpdir, @css_file)
+    @css_epubfile = File.join(@tmpdir, "css.epub")
+    @css_epub.render_to_file(@css_epubfile, $DEBUG)
+
+    FileUtils.copy(@css_epubfile, ".css.epub") if $DEBUG
     FileUtils.copy(@simple_epubfile, ".t.epub") if $DEBUG
     FileUtils.copy(@manygraphic_epubfile, ".mg.epub") if $DEBUG
   end
@@ -145,16 +150,12 @@ describe DocBook::Epub do
     end  
   end
 
-  it "should include a CSS link in HTML files when CSS files have been provided" do
+  it "should include a CSS link in HTML files when a CSS file has been provided" do
     begin
       tmpdir = File.join(Dir::tmpdir(), "epubcsshtmltest"); Dir.mkdir(tmpdir) rescue Errno::EEXIST
-      
-      epub = DocBook::Epub.new(File.join(@testdocsdir, "book.002.xml"), @tmpdir, @css_file)
-      epubfile = File.join(tmpdir, "csslink.epub")
-      epub.render_to_file(epubfile, $DEBUG)
 
-      success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{epubfile}")
-      raise "Could not unzip #{epubfile}" unless success
+      success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{@css_epubfile}")
+      raise "Could not unzip #{@css_epubfile}" unless success
       html_files = Dir.glob(File.join(tmpdir, "**", "*.html"))
       html_links = html_files.find_all {|html_file| File.open(html_file).readlines.to_s =~ /<link [^>]*#{@css_file_base}/}
       html_links.size.should == html_files.size
@@ -165,19 +166,30 @@ describe DocBook::Epub do
     end
   end
 
-  it "should include a CSS link in OPF file when CSS files have been provided" do
+  it "should include CSS file in .epub when a CSS file has been provided" do
+    begin
+      tmpdir = File.join(Dir::tmpdir(), "epubcssfiletest"); Dir.mkdir(tmpdir) rescue Errno::EEXIST
+
+      success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{@css_epubfile}")
+      raise "Could not unzip #{@css_epubfile}" unless success
+      css_files = Dir.glob(File.join(tmpdir, "**", "*.css"))
+      css_files.should_not be_empty
+    rescue => e
+      raise e
+    ensure
+      FileUtils.rm_r(tmpdir, :force => true)
+    end
+  end
+
+  it "should include a CSS link in OPF file when a CSS file has been provided" do
     begin
       tmpdir = File.join(Dir::tmpdir(), "epubcsshtmltest"); Dir.mkdir(tmpdir) rescue Errno::EEXIST
       
-      epub = DocBook::Epub.new(File.join(@testdocsdir, "book.002.xml"), @tmpdir, @css_file)
-      epubfile = File.join(tmpdir, "csslink.epub")
-      epub.render_to_file(epubfile, $DEBUG)
-
-      success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{epubfile}")
-      raise "Could not unzip #{epubfile}" unless success
+      success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{@css_epubfile}")
+      raise "Could not unzip #{@css_epubfile}" unless success
       opf_files = Dir.glob(File.join(tmpdir, "**", "*.opf"))
       opf_links = opf_files.find_all {|opf_file| File.open(opf_file).readlines.to_s =~ /<item [^>]*#{@css_file_base}/}
-      opf_links.size.should == opf_files.size
+      opf_links.should_not be_empty
     rescue => e
       raise e
     ensure
diff --git a/xsl/epub/bin/spec/files/test.css b/xsl/epub/bin/spec/files/test.css
new file mode 100644 (file)
index 0000000..2301cef
--- /dev/null
@@ -0,0 +1,4 @@
+h2 {
+  text-align: center;
+  color: #dda0dd;
+}
index 020e6910977acd819726a96bbe32a1e86a896be6..825b59cd31988516ebcf1f15a4cbfed55299dd73 100644 (file)
         </xsl:element>
       </xsl:if>  
 
+      <xsl:if test="$html.stylesheet != ''">
+        <xsl:element name="item">
+          <xsl:attribute name="xmlns">http://www.idpf.org/2007/opf</xsl:attribute>
+          <xsl:attribute name="media-type">test/css</xsl:attribute>
+          <xsl:attribute name="id">css</xsl:attribute>
+          <xsl:attribute name="href"><xsl:value-of select="$html.stylesheet"/></xsl:attribute>
+        </xsl:element>
+      </xsl:if>
+
       <xsl:if test="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"> 
         <xsl:element name="item">
           <xsl:attribute name="xmlns">http://www.idpf.org/2007/opf</xsl:attribute>