Skip to content

Commit

Permalink
Refactor px:html-chunker
Browse files Browse the repository at this point in the history
Give the px:chunker a "mapping" output instead of the
"link-attribute-name" option and apply the mapping afterwards with
px:html-update-links.

See #16
  • Loading branch information
bertfrees committed Apr 2, 2020
1 parent 923f237 commit 2ee2f06
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 190 deletions.
190 changes: 133 additions & 57 deletions common/fileset-utils/src/main/resources/xml/xslt/fileset-compose.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +25,136 @@
<xsl:template match="d:fileset">
<xsl:copy>
<xsl:sequence select="@* except @xml:base"/>
<xsl:variable name="files" as="element(d:file)*">
<xsl:apply-templates select="d:file"/>
<xsl:sequence select="$b/d:file[not((@original-href,@href)[1]=$a/d:file/(@href,@original-href))]"/>
<xsl:variable name="files-and-anchors" as="element()*"> <!-- element(d:file|d:anchor)* -->
<xsl:for-each select="d:file|d:anchor">
<xsl:choose>
<xsl:when test="self::d:file">
<xsl:variable name="a-file" as="element(d:file)" select="."/>
<xsl:variable name="b-file" as="element(d:file)*"
select="$b/d:file[@original-href=$a-file/@href]"/>
<xsl:choose>
<xsl:when test="exists($b-file)">
<xsl:for-each select="$b-file">
<xsl:copy>
<xsl:sequence select="@href|$a-file/@original-href"/>
</xsl:copy>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="a-anchor" as="element(d:anchor)" select="."/>
<xsl:variable name="b-anchor" as="element(d:anchor)?"
select="$b/d:anchor[@original-id=$a-anchor/@id and
@original-href=$a-anchor/@href][1]"/>
<xsl:choose>
<xsl:when test="exists($b-anchor)">
<xsl:copy>
<xsl:sequence select="@original-id|@original-href|$b-anchor/(@id|@href)"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="b-file" as="element(d:file)?"
select="$b/d:file[@original-href=$a-anchor/@href][1]"/>
<xsl:choose>
<xsl:when test="exists($b-file)">
<xsl:copy>
<xsl:sequence select="@original-id|@id|@original-href|$b-file/@href"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:sequence select="$b/d:file[not(@original-href=$a/d:file/(@href,@original-href))]"/>
<xsl:for-each select="$b/d:anchor[not(some $anc in $a/d:anchor
satisfies $anc/@href=@original-href and
$anc/@id=@original-id)]
[not(some $anc in $a/d:anchor
satisfies $anc/@original-href=@original-href and
$anc/@original-id=@original-id)]">
<xsl:variable name="b-anchor" as="element(d:anchor)" select="."/>
<xsl:variable name="a-file" as="element(d:file)?"
select="$a/d:file[@href=$b-anchor/@original-href][1]"/>
<xsl:choose>
<xsl:when test="exists($a-file)">
<xsl:copy>
<xsl:sequence select="@original-id|@id|@href|$a-file/@original-href"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="base" as="xs:string?" select="($b/@xml:base,@xml:base)[1]"/>
<xsl:choose>
<xsl:when test="exists($base)">
<xsl:attribute name="xml:base" select="$base"/>
<xsl:apply-templates mode="relativize" select="$files">
<xsl:with-param name="base" tunnel="yes" select="$base"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$files"/>
</xsl:otherwise>
</xsl:choose>
<xsl:variable name="files-with-anchors" as="element(d:file)*">
<xsl:for-each-group select="$files-and-anchors" group-by="@original-href">
<xsl:for-each-group select="current-group()" group-by="@href">
<d:file>
<xsl:sequence select="@href|@original-href"/>
<xsl:sequence select="current-group()[self::d:anchor]"/>
</d:file>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:variable>
<xsl:variable name="base" as="attribute()?" select="($b/@xml:base,@xml:base)[1]"/>
<xsl:sequence select="$base"/>
<xsl:apply-templates mode="relativize" select="$files-with-anchors">
<xsl:with-param name="base" tunnel="yes" select="$base"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="d:file">
<xsl:variable name="a-file" as="element(d:file)" select="."/>
<xsl:variable name="b-file" as="element(d:file)?" select="$b/d:file[(@original-href,@href)[1]=$a-file/@href][1]"/>
<xsl:choose>
<xsl:when test="exists($b-file)">
<xsl:copy>
<xsl:attribute name="href" select="$b-file/@href"/>
<xsl:sequence select="(@original-href,$b-file/@original-href)[1]"/>
<xsl:apply-templates select="d:anchor">
<xsl:with-param name="b-file" tunnel="yes" select="$b-file"/>
</xsl:apply-templates>
<xsl:sequence select="$b-file/d:anchor[not(@original-id=$a-file/d:anchor/(@id,@original-id))]"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="d:anchor">
<xsl:param name="b-file" as="element(d:file)" tunnel="yes" required="yes"/>
<xsl:variable name="a-anchor" as="element(d:anchor)" select="."/>
<xsl:variable name="b-anchor" as="element(d:anchor)?" select="$b-file/d:anchor[@original-id=$a-anchor/@id][1]"/>
<xsl:choose>
<xsl:when test="exists($b-anchor)">
<xsl:copy>
<xsl:attribute name="id" select="$b-anchor/@id"/>
<xsl:sequence select="@original-id"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template mode="normalize"
match="/d:fileset|
/d:fileset/d:file[@href]|
/d:fileset/d:file[@original-href or d:anchor[@id and @original-id]]|
/d:fileset/d:file/d:anchor[@id and @original-id]|
/d:fileset/d:file/d:anchor/@id|
/d:fileset/d:file/d:anchor/@original-id">
<xsl:copy>
<xsl:apply-templates mode="#current" select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template mode="normalize"
match="/d:fileset/d:file[@href]">
<xsl:param name="base" tunnel="yes" required="yes"/>
<xsl:variable name="normalized-file" as="element(d:file)">
<xsl:copy>
<xsl:variable name="normalized-attrs" as="attribute()*">
<xsl:apply-templates mode="#current" select="@*"/>
</xsl:variable>
<xsl:sequence select="$normalized-attrs"/>
<xsl:if test="not(@original-href)">
<xsl:attribute name="original-href" select="$normalized-attrs[name()='href']"/>
</xsl:if>
</xsl:copy>
</xsl:variable>
<xsl:sequence select="$normalized-file"/>
<xsl:apply-templates mode="#current">
<xsl:with-param name="normalized-parent" select="$normalized-file"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template mode="normalize"
match="/d:fileset/d:file/d:anchor[@id]">
<xsl:param name="normalized-parent" as="element(d:file)" required="yes"/>
<xsl:copy>
<xsl:apply-templates mode="#current" select="@*"/>
<xsl:if test="not(@original-id)">
<xsl:attribute name="original-id" select="@id"/>
</xsl:if>
<xsl:sequence select="$normalized-parent/(@href|@original-href)"/>
</xsl:copy>
</xsl:template>

<xsl:template mode="normalize"
match="d:fileset/@xml:base|
d:file/@href|
Expand All @@ -101,11 +163,25 @@
<xsl:attribute name="{name()}" select="pf:normalize-uri(resolve-uri(.,$base))"/>
</xsl:template>

<xsl:template mode="relativize" match="d:anchor/@href|
d:anchor/@original-href|
d:anchor/@original-id[.=../@id]|
d:file/@original-href[.=../@href]"/>

<xsl:template mode="relativize" match="d:file/@href">
<xsl:param name="base" tunnel="yes" required="yes"/>
<xsl:attribute name="href" select="pf:relativize-uri(.,$base)"/>
<xsl:param name="base" tunnel="yes" required="yes" as="xs:string?"/>
<xsl:choose>
<xsl:when test="exists($base)">
<xsl:attribute name="href" select="pf:relativize-uri(.,$base)"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template mode="normalize" match="@*|node()" priority="0.4"/>

<xsl:template mode="#default relativize" match="@*|node()">
<xsl:copy>
<xsl:apply-templates mode="#current" select="@*|node()"/>
Expand Down
35 changes: 30 additions & 5 deletions common/fileset-utils/src/test/xprocspec/fileset-compose.xprocspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,34 @@
</d:file>
<d:file href="other.xml">
<d:anchor id="f" original-id="e"/>
<d:anchor id="g"/>
<d:anchor id="h"/>
</d:file>
<d:file href="yet_another.xml" original-href="yet_another.html"/>
</d:fileset>
</x:document>
<x:document type="inline">
<d:fileset xml:base="file:/Users/me/out/">
<d:file href="merged.html" original-href="file:/Users/me/in/part1.html"/>
<d:file href="merged.xml" original-href="file:/Users/me/in/part1.xml"/>
<d:file href="merged.xml" original-href="../in/part2.xml">
<d:anchor id="g" original-id="b"/>
<d:anchor id="i" original-id="b"/>
</d:file>
<d:file href="merged.xml" original-href="../in/part3.xml"/>
<d:file href="../in/other.xml">
<d:anchor id="h" original-id="f"/>
<d:anchor id="i" original-id="e"/>
<d:anchor id="j" original-id="f"/>
<d:anchor id="k" original-id="e"/>
</d:file>
<d:file href="other.xml" original-href="../in/other.xml">
<d:anchor id="g"/>
<d:anchor id="l" original-id="h"/>
</d:file>
<d:file href="yet_another_split_1.xml" original-href="../in/yet_another.xml">
<d:anchor id="m"/>
</d:file>
<d:file href="yet_another_split_2.xml" original-href="../in/yet_another.xml"/>
<d:file href="yet_another_split_3.xml" original-href="../in/yet_another.xml">
<d:anchor id="n"/>
</d:file>
<d:file href="images/img.png" original-href="../in/img.png"/>
</d:fileset>
Expand All @@ -46,13 +60,24 @@
<d:fileset xml:base="file:/Users/me/out/">
<d:file href="merged.xml" original-href="file:/Users/me/in/part1.html"/>
<d:file href="merged.xml" original-href="file:/Users/me/in/part2.html">
<d:anchor id="g" original-id="a"/>
<d:anchor id="i" original-id="a"/>
</d:file>
<d:file href="merged.xml" original-href="file:/Users/me/in/part3.xml">
<d:anchor id="d" original-id="c"/>
</d:file>
<d:file href="../in/other.xml">
<d:anchor id="h" original-id="e"/>
<d:anchor id="j" original-id="e"/>
</d:file>
<d:file href="other.xml" original-href="file:/Users/me/in/other.xml">
<d:anchor id="g"/>
<d:anchor id="l" original-id="h"/>
</d:file>
<d:file href="yet_another_split_1.xml" original-href="file:/Users/me/in/yet_another.html">
<d:anchor id="m"/>
</d:file>
<d:file href="yet_another_split_2.xml" original-href="file:/Users/me/in/yet_another.html"/>
<d:file href="yet_another_split_3.xml" original-href="file:/Users/me/in/yet_another.html">
<d:anchor id="n"/>
</d:file>
<d:file href="images/img.png" original-href="file:/Users/me/in/img.png"/>
</d:fileset>
Expand Down
Loading

0 comments on commit 2ee2f06

Please sign in to comment.