Commit 34e2dc1f authored by jhodgdon's avatar jhodgdon

Issue #2859434 by jhodgdon: Fix extra space from index entries

parent fff86752
......@@ -228,4 +228,105 @@
<xsl:apply-templates mode="book.titlepage.recto.auto.mode" select="info/abstract"/>
</xsl:template>
<!-- Override index term output, to change the link title. -->
<xsl:template match="indexterm" mode="reference">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:param name="position"/>
<xsl:param name="separator" select="''"/>
<xsl:variable name="term.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.term.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="number.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.number.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="range.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.range.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$separator != ''">
<xsl:value-of select="$separator"/>
</xsl:when>
<xsl:when test="$position = 1">
<xsl:value-of select="$term.separator"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$number.separator"/>
</xsl:otherwise>
</xsl:choose>
<!-- This line is added. -->
<xsl:variable name="insection" select="ancestor-or-self::section" />
<xsl:choose>
<xsl:when test="@zone and string(@zone)">
<xsl:call-template name="reference">
<xsl:with-param name="zones" select="normalize-space(@zone)"/>
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<a>
<xsl:apply-templates select="." mode="class.attribute"/>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:apply-templates select="ancestor-or-self::section[last()]" mode="title.markup"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]" mode="title.markup"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="ancestor-or-self::section[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="$title"/> <!-- text only -->
</a>
<xsl:variable name="id" select="(@id|@xml:id)[1]"/>
<xsl:if test="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))]">
<xsl:apply-templates select="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))][last()]" mode="reference">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:with-param name="separator" select="$range.separator"/>
</xsl:apply-templates>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
......@@ -20,4 +20,105 @@
<xsl:template match="simpara[@role='summary']">
</xsl:template>
<!-- Override index term output, to change the link title. -->
<xsl:template match="indexterm" mode="reference">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:param name="position"/>
<xsl:param name="separator" select="''"/>
<xsl:variable name="term.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.term.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="number.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.number.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="range.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.range.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$separator != ''">
<xsl:value-of select="$separator"/>
</xsl:when>
<xsl:when test="$position = 1">
<xsl:value-of select="$term.separator"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$number.separator"/>
</xsl:otherwise>
</xsl:choose>
<!-- This line is added. -->
<xsl:variable name="insection" select="ancestor-or-self::section" />
<xsl:choose>
<xsl:when test="@zone and string(@zone)">
<xsl:call-template name="reference">
<xsl:with-param name="zones" select="normalize-space(@zone)"/>
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<a>
<xsl:apply-templates select="." mode="class.attribute"/>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:apply-templates select="ancestor-or-self::section[last()]" mode="title.markup"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]" mode="title.markup"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="ancestor-or-self::section[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="$title"/> <!-- text only -->
</a>
<xsl:variable name="id" select="(@id|@xml:id)[1]"/>
<xsl:if test="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))]">
<xsl:apply-templates select="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))][last()]" mode="reference">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:with-param name="separator" select="$range.separator"/>
</xsl:apply-templates>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
......@@ -198,4 +198,105 @@
<xsl:apply-templates mode="book.titlepage.recto.auto.mode" select="info/abstract"/>
</xsl:template>
<!-- Override index term output, to change the link title. -->
<xsl:template match="indexterm" mode="reference">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:param name="position"/>
<xsl:param name="separator" select="''"/>
<xsl:variable name="term.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.term.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="number.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.number.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="range.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.range.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$separator != ''">
<xsl:value-of select="$separator"/>
</xsl:when>
<xsl:when test="$position = 1">
<xsl:value-of select="$term.separator"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$number.separator"/>
</xsl:otherwise>
</xsl:choose>
<!-- This line is added. -->
<xsl:variable name="insection" select="ancestor-or-self::section" />
<xsl:choose>
<xsl:when test="@zone and string(@zone)">
<xsl:call-template name="reference">
<xsl:with-param name="zones" select="normalize-space(@zone)"/>
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<a>
<xsl:apply-templates select="." mode="class.attribute"/>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:apply-templates select="ancestor-or-self::section[last()]" mode="title.markup"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]" mode="title.markup"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="ancestor-or-self::section[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="$title"/> <!-- text only -->
</a>
<xsl:variable name="id" select="(@id|@xml:id)[1]"/>
<xsl:if test="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))]">
<xsl:apply-templates select="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))][last()]" mode="reference">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:with-param name="separator" select="$range.separator"/>
</xsl:apply-templates>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
......@@ -21,8 +21,12 @@ do
langconf="-f lang-$lang.conf"
fi
# Run the preprocessor that fixes index entries.
php preprocess._php ../source/$lang ../output/ebooks/$lang i
cp ../source/$lang/guide-docinfo.xml ../output/ebooks/$lang
# Run the AsciiDoc processor to convert to DocBook for ebooks.
asciidoc -d book -b docbook -f std.conf -a docinfo -a lang=$lang $langconf -o ../output/ebooks/$lang/guide.docbook ../source/$lang/guide.txt
asciidoc -d book -b docbook -f std.conf -a docinfo -a lang=$lang $langconf -o ../output/ebooks/$lang/guide.docbook ../output/ebooks/$lang/guide.txt
# Copy image files to e-book directory.
cp ../source/$lang/images/*.png ../output/ebooks/$lang/images
......
......@@ -22,7 +22,7 @@ do
fi
# Run the preprocessor that puts file names into the files under each header.
php preprocess._php ../source/$lang ../output/html_feed/$lang f
php preprocess._php ../source/$lang ../output/html_feed/$lang fi
cp ../source/$lang/guide-docinfo.xml ../output/html_feed/$lang
# Run the AsciiDoc processor to convert to DocBook format. Syntax:
......@@ -48,7 +48,7 @@ done
mkdir -p ../output/html_feed/guidelines
mkdir -p ../output/html_feed/guidelines/images
php preprocess._php ../guidelines ../output/html_feed/guidelines f
php preprocess._php ../guidelines ../output/html_feed/guidelines fi
asciidoc -d book -b docbook -f std.conf -f guidelines.conf -o ../output/html_feed/guidelines/guidelines.docbook ../output/html_feed/guidelines/guidelines.txt
......
......@@ -21,7 +21,7 @@ do
fi
# Run the preprocessor that puts file names into the files under each header.
php preprocess._php ../source/$lang ../output/html/$lang f
php preprocess._php ../source/$lang ../output/html/$lang fi
cp ../source/$lang/guide-docinfo.xml ../output/html/$lang
# Run the AsciiDoc processor to convert to DocBook format. Syntax:
......@@ -47,7 +47,7 @@ done
mkdir -p ../output/html/guidelines
mkdir -p ../output/html/guidelines/images
php preprocess._php ../guidelines ../output/html/guidelines f
php preprocess._php ../guidelines ../output/html/guidelines fi
asciidoc -d book -b docbook -f std.conf -f guidelines.conf -o ../output/html/guidelines/guidelines.docbook ../output/html/guidelines/guidelines.txt
......
......@@ -124,4 +124,105 @@
<xsl:template match="simpara[@role='summary']">
</xsl:template>
<!-- Override index term output, to change the link title. -->
<xsl:template match="indexterm" mode="reference">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:param name="position"/>
<xsl:param name="separator" select="''"/>
<xsl:variable name="term.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.term.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="number.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.number.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="range.separator">
<xsl:call-template name="index.separator">
<xsl:with-param name="key" select="'index.range.separator'"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$separator != ''">
<xsl:value-of select="$separator"/>
</xsl:when>
<xsl:when test="$position = 1">
<xsl:value-of select="$term.separator"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$number.separator"/>
</xsl:otherwise>
</xsl:choose>
<!-- This line is added. -->
<xsl:variable name="insection" select="ancestor-or-self::section" />
<xsl:choose>
<xsl:when test="@zone and string(@zone)">
<xsl:call-template name="reference">
<xsl:with-param name="zones" select="normalize-space(@zone)"/>
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<a>
<xsl:apply-templates select="." mode="class.attribute"/>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:apply-templates select="ancestor-or-self::section[last()]" mode="title.markup"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]" mode="title.markup"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- This is a changed part! Will only work for AsciiDoc output. -->
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="count($insection) &gt; 0">
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="ancestor-or-self::section[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="(ancestor-or-self::glossary|ancestor-or-self::chapter|ancestor-or-self::preface|ancestor-or-self::appendix|ancestor-or-self::part|ancestor-or-self::book)[last()]"/>
<xsl:with-param name="context" select="(//index[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))] | //setindex[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))])[1]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="$title"/> <!-- text only -->
</a>
<xsl:variable name="id" select="(@id|@xml:id)[1]"/>
<xsl:if test="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))]">
<xsl:apply-templates select="key('endofrange', $id)[count(ancestor::node()|$scope) = count(ancestor::node()) and ($role = @role or $type = @type or (string-length($role) = 0 and string-length($type) = 0))][last()]" mode="reference">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:with-param name="separator" select="$range.separator"/>
</xsl:apply-templates>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
......@@ -5,6 +5,9 @@
<!-- Note that PDF is normally made with the fo stylesheets
from the docbook-xsl project. -->
<!-- Strip unnecessary whitespace. -->
<xsl:strip-space elements="*"/>
<!-- Use outline numbering for sections. -->
<xsl:param name="section.autolabel" select="1"/>
<xsl:param name="section.autolabel.max.depth">1</xsl:param>
......
......@@ -17,6 +17,8 @@
* pass:[<remark>Source file: (filename)</remark>]
* @endcode
* after each 2nd-level and 3rd-level header found in the file.
* - i: Fixes extra whitespace from index entries, by moving them to the
* first regular paragraph in each topic (except glossary files).
*/
if (count($argv) < 4) {
......@@ -39,6 +41,9 @@ if ($dh = opendir($source)) {
while (($file = readdir($dh)) !== FALSE) {
if (preg_match('|^[a-zA-Z_.\-]+\.txt$|', $file)) {
$text = file_get_contents($source . '/' . $file);
if ($text && strpos($steps, 'i') !== FALSE) {
$text = move_index_entries($text, $file);
}
if ($text && strpos($steps, 'f') !== FALSE) {
$text = add_filenames($text, $file);
}
......@@ -46,7 +51,7 @@ if ($dh = opendir($source)) {
$count = file_put_contents($output . '/' . $file, $text);
}
if (!$text || !$count) {
exit("Could not write to $file");
exit("Empty file or could not write to output for $file");
}
}
}
......@@ -77,7 +82,7 @@ function add_filenames($text, $filename, $depths = array(2,3)) {
}
$match = array_filter($match);
if (!count($match)) {
return FALSE;
exit ("No headers in $filename");
}
$match = '/^(' . implode('|', $match) . ')[^\=].*$/m';
......@@ -90,3 +95,60 @@ function add_filenames($text, $filename, $depths = array(2,3)) {
$text = implode("----\n", $codes);
return $text;
}
/**
* Fixes index entries in the text of a file.
*
* Except in files containing glossary entries, moves all index entries to
* the next regular paragraph (not a header or special paragraph).
*
* @param string $text
* Text of the file.
* @praam string $filename
* Name of the file (only used for error message).
*
* @return string
* Processed text of the file.
*/
function move_index_entries($text, $filename) {
// See if this is a glossary file. If so, do not disturb it.
if (strpos($text, '[glossary]') !== FALSE) {
return $text;
}
// Split the text into paragraphs (on two or more line breaks).
$paragraphs = preg_split('|\R{2,}|', $text);
// Put it back together, moving index-entry-only paragraphs to the front of
// the next regular paragraph.
$output = '';
$saved_indexes = '';
foreach ($paragraphs as $paragraph) {
// If the first character is a "letter" character, it's a "regular"
// paragraph. Output any saved indexes and the paragraph.
$first_char = mb_substr($paragraph, 0, 1, 'utf-8');
if (preg_match('|\pL|', $first_char)) {
$output .= "\n\n" . $saved_indexes . $paragraph;
$saved_indexes = '';
continue;
}
// See if the paragraph consists entirely of index entries and
// whitespace. If so, save and continue.
if (preg_match('|^(\s*\(\(\([^\(]+\)\)\))+|', $paragraph)) {
$saved_indexes = $paragraph;
continue;
}
// It's some kind of a header or other special paragraph. Output and
// continue.
$output .= "\n\n" . $paragraph;
}
// If there were index entries left over, we failed.
if ($saved_indexes) {
exit("Could not fix indexes in $filename");
}
return trim($output);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment