-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
# - 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
$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
- 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}
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)
-require 'rexml/parsers/pullparser'
require 'fileutils'
+require 'rexml/parsers/pullparser'
+
module DocBook
class Epub
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)
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
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
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 = []
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
$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
$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
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)
@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
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
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
--- /dev/null
+h2 {
+ text-align: center;
+ color: #dda0dd;
+}
</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>