From f1a5d42eb4434985778fa2ec57db13db62dfbe86 Mon Sep 17 00:00:00 2001 From: Keith Fahlgren Date: Mon, 23 Jun 2008 17:09:11 +0000 Subject: [PATCH] Adding CSS support to .epub target & dbtoepub: -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 | 7 +++- xsl/epub/bin/lib/docbook.rb | 33 ++++++++++----- xsl/epub/bin/spec/epub_regressions_spec.rb | 2 +- xsl/epub/bin/spec/epub_small_smoketest.rb | 2 +- xsl/epub/bin/spec/epub_smoketest_spec.rb | 2 +- xsl/epub/bin/spec/epub_spec.rb | 48 ++++++++++++++-------- xsl/epub/bin/spec/files/test.css | 4 ++ xsl/epub/docbook.xsl | 9 ++++ 8 files changed, 74 insertions(+), 33 deletions(-) create mode 100644 xsl/epub/bin/spec/files/test.css diff --git a/xsl/epub/bin/dbtoepub b/xsl/epub/bin/dbtoepub index c94a1072b..5a41094b8 100755 --- a/xsl/epub/bin/dbtoepub +++ b/xsl/epub/bin/dbtoepub @@ -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) diff --git a/xsl/epub/bin/lib/docbook.rb b/xsl/epub/bin/lib/docbook.rb index 33f5e605f..827ff875d 100755 --- a/xsl/epub/bin/lib/docbook.rb +++ b/xsl/epub/bin/lib/docbook.rb @@ -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 = [] diff --git a/xsl/epub/bin/spec/epub_regressions_spec.rb b/xsl/epub/bin/spec/epub_regressions_spec.rb index b34db8ad8..ac6a9cedb 100755 --- a/xsl/epub/bin/spec/epub_regressions_spec.rb +++ b/xsl/epub/bin/spec/epub_regressions_spec.rb @@ -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 diff --git a/xsl/epub/bin/spec/epub_small_smoketest.rb b/xsl/epub/bin/spec/epub_small_smoketest.rb index a2feac678..6834792a5 100755 --- a/xsl/epub/bin/spec/epub_small_smoketest.rb +++ b/xsl/epub/bin/spec/epub_small_smoketest.rb @@ -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 diff --git a/xsl/epub/bin/spec/epub_smoketest_spec.rb b/xsl/epub/bin/spec/epub_smoketest_spec.rb index 27f389cf3..9d81469a3 100755 --- a/xsl/epub/bin/spec/epub_smoketest_spec.rb +++ b/xsl/epub/bin/spec/epub_smoketest_spec.rb @@ -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 diff --git a/xsl/epub/bin/spec/epub_spec.rb b/xsl/epub/bin/spec/epub_spec.rb index f5a90492b..54cdf3977 100755 --- a/xsl/epub/bin/spec/epub_spec.rb +++ b/xsl/epub/bin/spec/epub_spec.rb @@ -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 =~ /]*#{@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 =~ /]*#{@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 index 000000000..2301cef9f --- /dev/null +++ b/xsl/epub/bin/spec/files/test.css @@ -0,0 +1,4 @@ +h2 { + text-align: center; + color: #dda0dd; +} diff --git a/xsl/epub/docbook.xsl b/xsl/epub/docbook.xsl index 020e69109..825b59cd3 100644 --- a/xsl/epub/docbook.xsl +++ b/xsl/epub/docbook.xsl @@ -649,6 +649,15 @@ + + + http://www.idpf.org/2007/opf + test/css + css + + + + http://www.idpf.org/2007/opf -- 2.40.0