Skip to content

Commit

Permalink
create-hidden-headlines: don't use content from subsections when gene…
Browse files Browse the repository at this point in the history
…rating headlines
  • Loading branch information
josteinaj committed Mar 26, 2021
1 parent 9a75dff commit a351f84
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 17 deletions.
21 changes: 13 additions & 8 deletions xslt/prepare-for-ebook/create-hidden-headlines.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@

<xsl:choose>
<xsl:when test="($matter = $alwaysFromText) or ($section/f:types(.) = $alwaysFromText)">
<xsl:value-of select="f:chapter-first-text($section)"/>
<xsl:variable name="first-text" select="f:chapter-first-text($section)"/>
<xsl:value-of select="if ($first-text != '') then $first-text else f:chapter-translation($matter, $section)"/>
</xsl:when>

<xsl:when test="$matter = 'cover'">
Expand All @@ -76,7 +77,8 @@
<xsl:value-of select="f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:when test="$cover-headlines = 'from-text'">
<xsl:value-of select="f:chapter-first-text($section)"/>
<xsl:variable name="first-text" select="f:chapter-first-text($section)"/>
<xsl:value-of select="if ($first-text != '') then $first-text else f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
Expand All @@ -90,7 +92,8 @@
<xsl:value-of select="f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:when test="$frontmatter-headlines = 'from-text'">
<xsl:value-of select="f:chapter-first-text($section)"/>
<xsl:variable name="first-text" select="f:chapter-first-text($section)"/>
<xsl:value-of select="if ($first-text != '') then $first-text else f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
Expand All @@ -104,7 +107,8 @@
<xsl:value-of select="f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:when test="$bodymatter-headlines = 'from-text'">
<xsl:value-of select="f:chapter-first-text($section)"/>
<xsl:variable name="first-text" select="f:chapter-first-text($section)"/>
<xsl:value-of select="if ($first-text != '') then $first-text else f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
Expand All @@ -118,7 +122,8 @@
<xsl:value-of select="f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:when test="$backmatter-headlines = 'from-text'">
<xsl:value-of select="f:chapter-first-text($section)"/>
<xsl:variable name="first-text" select="f:chapter-first-text($section)"/>
<xsl:value-of select="if ($first-text != '') then $first-text else f:chapter-translation($matter, $section)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
Expand All @@ -145,7 +150,7 @@
<xsl:function name="f:chapter-first-text">
<xsl:param name="section" as="element()"/>

<xsl:variable name="text" select="string-join(($section//text()[normalize-space()])[position() le 10], ' ')" as="xs:string"/>
<xsl:variable name="text" select="string-join((($section//text() except $section//section//text())[normalize-space()])[position() le 10], ' ')" as="xs:string"/>
<xsl:variable name="text" select="normalize-space($text)" as="xs:string"/>
<xsl:variable name="text" select="string(replace($text, '^(.*?[a-zA-ZæøåÆØÅ].*?)\..*', '$1'))" as="xs:string"/>

Expand All @@ -160,8 +165,8 @@
```
</xsl:text>
</xsl:message>
<xsl:message terminate="yes" select="concat(
'No usable text content for headline available at: ',
<xsl:message select="concat(
'No usable text content for headline available. Falling back to type-based headline. At: ',
'/', string-join((for $e in ($section/ancestor-or-self::*) return concat('*[', count($e/preceding-sibling::*) + 1, ']')), '/'),
if ($section/@id) then concat(' (', $section/name(), '[@id=''', $section/@id, '''])') else '')"/>
</xsl:when>
Expand Down
120 changes: 111 additions & 9 deletions xslt/prepare-for-ebook/create-hidden-headlines.xspec
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,21 @@
<p>Chapter</p>
</section>
<section epub:type="bodymatter chapter">
<h1 id="..." class="generated-headline hidden-headline">Chapter Chapter Chapter...</h1>
<h1 id="..." class="generated-headline hidden-headline">Chapter</h1>
<p>Chapter</p>
<section>
<h2 id="..." class="generated-headline hidden-headline">Chapter Chapter Chapter...</h2>
<h2 id="..." class="generated-headline hidden-headline">Chapter</h2>
<p>Chapter</p>
<section>
<h3 id="..." class="generated-headline hidden-headline">Chapter Chapter</h3>
<h3 id="..." class="generated-headline hidden-headline">Chapter</h3>
<p>Chapter</p>
<section>
<h4 id="..." class="generated-headline hidden-headline">Chapter</h4>
<p>Chapter</p>
</section>
</section>
<section>
<h3 id="..." class="generated-headline hidden-headline">Chapter Chapter</h3>
<h3 id="..." class="generated-headline hidden-headline">Chapter</h3>
<p>Chapter</p>
<section>
<h4 id="..." class="generated-headline hidden-headline">Chapter</h4>
Expand All @@ -223,7 +223,7 @@
</section>
</section>
<section epub:type="bodymatter part">
<h1 id="..." class="generated-headline hidden-headline">Chapter Chapter Chapter...</h1>
<h1 id="..." class="generated-headline hidden-headline">Part</h1>
<section epub:type="chapter">
<h2 id="..." class="generated-headline hidden-headline">Chapter</h2>
<p>Chapter</p>
Expand All @@ -237,13 +237,13 @@
<p>Chapter</p>
</section>
<section epub:type="chapter">
<h2 id="..." class="generated-headline hidden-headline">Chapter Chapter Chapter...</h2>
<h2 id="..." class="generated-headline hidden-headline">Chapter</h2>
<p>Chapter</p>
<section epub:type="subchapter">
<h3 id="..." class="generated-headline hidden-headline">Chapter Chapter Chapter</h3>
<h3 id="..." class="generated-headline hidden-headline">Chapter</h3>
<p>Chapter</p>
<section>
<h4 id="..." class="generated-headline hidden-headline">Chapter Chapter</h4>
<h4 id="..." class="generated-headline hidden-headline">Chapter</h4>
<p>Chapter</p>
<section>
<h5 id="..." class="generated-headline hidden-headline">Chapter</h5>
Expand Down Expand Up @@ -289,7 +289,7 @@
</section>
</section>
<section epub:type="bodymatter part">
<h1 id="..." class="generated-headline hidden-headline">Chapter</h1>
<h1 id="..." class="generated-headline hidden-headline">Part</h1>
<section epub:type="chapter">
<h2 id="..." class="generated-headline hidden-headline">Chapter</h2>
<p>Chapter</p>
Expand Down Expand Up @@ -1259,4 +1259,106 @@
</x:expect>
</x:scenario>

<x:scenario label="Don't use content from subsections when generating headlines">
<x:context>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
</head>
<body>
<section id="chapter_1_section" epub:type="bodymatter chapter">
<section epub:type="z3998:poem" class="poem" id="poem_1">
<div class="linegroup">
<p class="line">Lorem ipsum dolor sit amet</p>
<p class="line">consectetur adipisicing elit</p>
<p class="line">sed do eiusmod tempor incididunt</p>
<p class="line">ut labore et dolore magna aliqua</p>
</div>
</section>
<section epub:type="z3998:poem" class="poem" id="poem_2">
<div class="linegroup">
<p class="line">Ut enim ad minim veniam</p>
<p class="line">quis nostrud exercitation ullamco</p>
<p class="line">laboris nisi ut aliquip</p>
<p class="line">ex ea commodo consequat</p>
</div>
</section>
</section>
<section id="chapter_2_section" epub:type="bodymatter chapter">
<p>This section has text before the first subsection.</p>
<section epub:type="z3998:poem" class="poem" id="poem_3">
<div class="linegroup">
<p class="line">ut labore et dolore magna aliqua</p>
<p class="line">sed do eiusmod tempor incididunt</p>
<p class="line">consectetur adipisicing elit</p>
<p class="line">Lorem ipsum dolor sit amet</p>
</div>
</section>
<section epub:type="z3998:poem" class="poem" id="poem_4">
<div class="linegroup">
<p class="line">ex ea commodo consequat</p>
<p class="line">laboris nisi ut aliquip</p>
<p class="line">quis nostrud exercitation ullamco</p>
<p class="line">Ut enim ad minim veniam</p>
</div>
</section>
</section>
</body>
</html>
</x:context>

<x:expect label="The result should be as expected'">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
</head>
<body>
<section id="chapter_1_section" epub:type="bodymatter chapter">
<h1 id="..." class="generated-headline hidden-headline">Chapter</h1>
<section epub:type="z3998:poem" class="poem" id="poem_1">
<h2 id="..." class="generated-headline hidden-headline">Lorem ipsum dolor...</h2>
<div class="linegroup">
<p class="line">Lorem ipsum dolor sit amet</p>
<p class="line">consectetur adipisicing elit</p>
<p class="line">sed do eiusmod tempor incididunt</p>
<p class="line">ut labore et dolore magna aliqua</p>
</div>
</section>
<section epub:type="z3998:poem" class="poem" id="poem_2">
<h2 id="..." class="generated-headline hidden-headline">Ut enim ad...</h2>
<div class="linegroup">
<p class="line">Ut enim ad minim veniam</p>
<p class="line">quis nostrud exercitation ullamco</p>
<p class="line">laboris nisi ut aliquip</p>
<p class="line">ex ea commodo consequat</p>
</div>
</section>
</section>
<section id="chapter_2_section" epub:type="bodymatter chapter">
<h1 id="..." class="generated-headline hidden-headline">This section has...</h1>
<p>This section has text before the first subsection.</p>
<section epub:type="z3998:poem" class="poem" id="poem_3">
<h2 id="..." class="generated-headline hidden-headline">ut labore et...</h2>
<div class="linegroup">
<p class="line">ut labore et dolore magna aliqua</p>
<p class="line">sed do eiusmod tempor incididunt</p>
<p class="line">consectetur adipisicing elit</p>
<p class="line">Lorem ipsum dolor sit amet</p>
</div>
</section>
<section epub:type="z3998:poem" class="poem" id="poem_4">
<h2 id="..." class="generated-headline hidden-headline">ex ea commodo...</h2>
<div class="linegroup">
<p class="line">ex ea commodo consequat</p>
<p class="line">laboris nisi ut aliquip</p>
<p class="line">quis nostrud exercitation ullamco</p>
<p class="line">Ut enim ad minim veniam</p>
</div>
</section>
</section>
</body>
</html>
</x:expect>
</x:scenario>

</x:description>

0 comments on commit a351f84

Please sign in to comment.