# 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)
verbose = false
debug = false
css_file = nil
+customization_layer = nil
# Set up the OptionParser
opts = OptionParser.new
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
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)
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")
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
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
--- /dev/null
+<?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>