]> granicus.if.org Git - docbook-dsssl/commitdiff
Patch contributed by Liza Daly to make ePub cover handling more generic. Additionally
authorKeith Fahlgren <abdelazer@users.sourceforge.net>
Tue, 17 Feb 2009 04:41:34 +0000 (04:41 +0000)
committerKeith Fahlgren <abdelazer@users.sourceforge.net>
Tue, 17 Feb 2009 04:41:34 +0000 (04:41 +0000)
DocBook 5.0's <cover> now has some limited support:

- should reference a cover in the OPF guide for a DocBook 5.0 test document

xsl/epub/bin/spec/epub_regressions_spec.rb
xsl/epub/bin/spec/epub_spec.rb
xsl/epub/bin/spec/files/v5cover.xml [new file with mode: 0644]
xsl/epub/bin/spec/spec_helper.rb
xsl/epub/docbook.xsl

index 9f6d55572301489bce391206dabbf927b956add5..1b152b3c41105f188dd6166f43b3184c37f64505 100755 (executable)
@@ -17,6 +17,7 @@ require 'docbook'
 
 $DEBUG = false
 
+
 describe DocBook::Epub do
   before(:all) do
     @filedir = File.expand_path(File.join(File.dirname(__FILE__), 'files'))
@@ -103,6 +104,11 @@ describe DocBook::Epub do
     xhtml_dtd_in_opf_file.should_not be_true
   end
 
+  it "should reference a cover in the OPF guide for a DocBook 5.0 test document" do
+    opf_lns = opf_lines('v5cover.xml', @filedir)
+    opf_lns.to_s.should =~ /reference[^>]+type=['"]cover['"]/
+  end
+
   after(:all) do
     FileUtils.rm_r(@tmpdir, :force => true)
   end  
index 22143ddd6bcbdb579fe340576348f476372d96cd..e4ea0f635539c428d9e917c5bf79704436864cd9 100755 (executable)
@@ -16,19 +16,6 @@ require 'docbook'
 
 $DEBUG = false
 
-def opf_lines(filename, filedir)
-  shortname = filename.gsub(/\W/, '')
-  tmpdir = File.join(Dir::tmpdir(), shortname); Dir.mkdir(tmpdir) rescue Errno::EEXIST
-  epub = DocBook::Epub.new(File.join(filedir, filename), tmpdir)
-  epubfile  = File.join(tmpdir, shortname + ".epub")
-  epub.render_to_file(epubfile, $DEBUG)
-  FileUtils.copy(epubfile, "." + shortname + ".epub") if $DEBUG
-  success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{File.expand_path(epubfile)}")
-  raise "Could not unzip #{epubfile}" unless success
-  opf_file = Dir.glob(File.join(tmpdir, "**", "*.opf")).first
-  opf_lines = File.open(opf_file).readlines
-  return opf_lines
-end
 
 describe DocBook::Epub do
   before(:all) do
diff --git a/xsl/epub/bin/spec/files/v5cover.xml b/xsl/epub/bin/spec/files/v5cover.xml
new file mode 100644 (file)
index 0000000..082f047
--- /dev/null
@@ -0,0 +1,25 @@
+<book xmlns="http://docbook.org/ns/docbook" version="5.0">
+  <info>
+    <title>v5 test</title>
+    <author>
+      <personname>
+        <firstname>Liza</firstname>
+        <surname>Daly</surname>
+      </personname>
+    </author>
+    <cover>   
+      <mediaobject>
+        <imageobject>
+          <imagedata fileref="covers/cvr_lrg.jpg"/>
+        </imageobject>
+      </mediaobject>
+      <para>Additional content on cover page. </para>
+    </cover>
+  </info>
+  <chapter>
+    <info>
+      <title>DocBook v5 Regression Cover Test</title>
+    </info>
+    <para>Some content</para>
+  </chapter>
+</book>
index d3cfe75bae0bd102afeaaf9c7eba282429f2d267..51b755d38ca94cda137dd645adef9f3450a062ae 100755 (executable)
@@ -3,6 +3,7 @@ $LOAD_PATH.unshift(lib) if File.exist?(lib)
 
 require 'docbook'
 
+# Helper classes
 class BeValidEpub
   
   def initialize
@@ -35,3 +36,19 @@ end
 def be_valid_epub
   BeValidEpub.new
 end
+
+
+# Helper Functions
+def opf_lines(filename, filedir)
+  shortname = filename.gsub(/\W/, '')
+  tmpdir = File.join(Dir::tmpdir(), shortname); Dir.mkdir(tmpdir) rescue Errno::EEXIST
+  epub = DocBook::Epub.new(File.join(filedir, filename), tmpdir)
+  epubfile  = File.join(tmpdir, shortname + ".epub")
+  epub.render_to_file(epubfile, $DEBUG)
+  FileUtils.copy(epubfile, "." + shortname + ".epub") if $DEBUG
+  success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{File.expand_path(epubfile)}")
+  raise "Could not unzip #{epubfile}" unless success
+  opf_file = Dir.glob(File.join(tmpdir, "**", "*.opf")).first
+  opf_lines = File.open(opf_file).readlines
+  return opf_lines
+end
index 3502827aef5c455055ea7423592ebfabe2037336..5c81361d4a73752f8846d422e56feebcdcad09d1 100644 (file)
@@ -36,7 +36,7 @@
   <xsl:param name="epub.cover.filename" select="concat($epub.oebps.dir, 'cover', $html.ext)"/> 
   <xsl:param name="epub.cover.id" select="'cover'"/> 
   <xsl:param name="epub.cover.image.id" select="'cover-image'"/> 
-
+  <xsl:param name="epub.cover.linear" select="0" />
   <xsl:param name="epub.ncx.toc.id">ncxtoc</xsl:param>
   <xsl:param name="epub.html.toc.id">htmltoc</xsl:param>
   <xsl:param name="epub.metainf.dir" select="'META-INF/'"/> 
               <xsl:call-template name="l10n.language"/>
             </xsl:element>
 
-            <xsl:if test="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"> 
+            <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
               <xsl:element name="meta">
                 <xsl:attribute name="name">cover</xsl:attribute>
                 <xsl:attribute name="content">
             type="cover" pointing to it AND there is a logical cover specified in a
             <meta name="cover"> tag, THEN, the HTML cover is discarded. -->
           <xsl:element name="head">
-            <xsl:if test="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"> 
+            <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
               <xsl:element name="meta">
                 <xsl:attribute name="name">cover</xsl:attribute>
                 <xsl:attribute name="content">
 
   <xsl:template name="opf.guide">
     <xsl:if test="contains($toc.params, 'toc') or 
-                  /*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"> 
+                  /*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
       <xsl:element name="guide">
         <xsl:attribute name="xmlns">http://www.idpf.org/2007/opf</xsl:attribute>
-        <xsl:if test="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"> 
+        <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
           <xsl:element name="reference">
             <xsl:attribute name="href">
               <!-- TODO: Figure out how to get this to work right with generation but also not be hardcoded -->
         <xsl:value-of select="$epub.ncx.toc.id"/>
       </xsl:attribute>
 
-      <xsl:if test="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"> 
+      <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
         <xsl:element name="itemref">
           <xsl:attribute name="idref">
             <xsl:value-of select="$epub.cover.id"/>
           </xsl:attribute>
-          <xsl:attribute name="linear">no</xsl:attribute>
+          <xsl:attribute name="linear">
+          <xsl:choose>
+            <xsl:when test="$epub.cover.linear">
+              <xsl:text>yes</xsl:text>
+            </xsl:when>
+            <xsl:otherwise>no</xsl:otherwise>
+          </xsl:choose>
+          </xsl:attribute>
         </xsl:element>
       </xsl:if>
 
         </xsl:element>
       </xsl:if>
 
-      <xsl:if test="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"> 
+      <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
         <xsl:element name="item">
           <xsl:attribute name="xmlns">http://www.idpf.org/2007/opf</xsl:attribute>
           <xsl:attribute name="id"> <xsl:value-of select="$epub.cover.id"/> </xsl:attribute>
           <xsl:attribute name="xmlns">http://www.idpf.org/2007/opf</xsl:attribute>
           <xsl:attribute name="id"> 
             <xsl:choose>
-              <!-- TODO: Remove hardcoded 'front' -->
-              <xsl:when test="ancestor::mediaobject[@role='cover'] and ../@role='front-large'">
+              <xsl:when test="(ancestor::mediaobject[@role='cover'] or ancestor::cover) and (../@role='front-large' or count(ancestor::mediaobject/descendant::imageobject) = 1)">
                 <xsl:value-of select="$epub.cover.image.id"/>
               </xsl:when>
               <xsl:otherwise>
         <xsl:attribute name="xmlns">http://www.idpf.org/2007/opf</xsl:attribute>
         <xsl:attribute name="id"> 
           <xsl:choose>
-            <!-- TODO: Remove hardcoded 'front' -->
-            <xsl:when test="ancestor::mediaobject[@role='cover'] and ../@role='front-large'">
+            <xsl:when test="(ancestor::mediaobject[@role='cover'] or ancestor::cover) and (../@role='front-large' or count(ancestor::mediaobject/descendant::imageobject) = 1)">
               <xsl:value-of select="$epub.cover.image.id"/>
             </xsl:when>
             <xsl:otherwise>
   </xsl:template>  
 
   <xsl:template name="cover">
-    <xsl:apply-templates select="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']"/>
+    <xsl:apply-templates select="/*/*[contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"/>
   </xsl:template>  
 
-  <xsl:template match="/*/*[contains(name(.), 'info')]/mediaobject[@role='cover']">
+  <xsl:template match="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]">
     <xsl:call-template name="write.chunk">
       <xsl:with-param name="filename">
         <xsl:value-of select="$epub.cover.filename" />
               <xsl:attribute name="id">
                 <xsl:value-of select="$epub.cover.image.id"/>
               </xsl:attribute>
-              <!-- TODO: Remove hardcoded 'front' -->
-              <xsl:apply-templates select="imageobject[@role='front-large']"/>
+              <xsl:choose>
+                <xsl:when test="imageobject[@role='front-large']">
+                  <xsl:apply-templates select="imageobject[@role='front-large']"/>
+                </xsl:when>
+                <xsl:otherwise>
+                  <xsl:apply-templates select="imageobject[1]"/>
+                </xsl:otherwise>
+              </xsl:choose>
             </xsl:element>
+            <!-- If this is defined as an explicit cover page, then process
+            any remaining text -->
+            <xsl:if test="ancestor::cover">
+              <xsl:apply-templates select="ancestor::cover/para"/>
+            </xsl:if>
           </xsl:element>
         </xsl:element>
       </xsl:with-param>