Skip to content

Commit

Permalink
see Issue docbook#579 removes unnecessary down-up fragments in URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteimke committed Jan 8, 2025
1 parent 4ff16dc commit ae3b57a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/main/xslt/modules/variable.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,33 @@
</xsl:variable>

<!-- Make sure we've resolved it so that file:///, file://, file:/, etc.
get normalized because later on we're going to want to compare
the prefix of this base URI with the prefix of another URI. -->
get normalized and down-up steps like /a/b/../c will be shortened to a/c
because later on we're going to want to compare
the prefix of this base URI with the prefix of another URI.
(see mode mp:copy-patch-toc in chunk-output.xsl) -->
<xsl:variable name="vp:chunk-output-base-uri" as="xs:anyURI?">
<xsl:choose>
<xsl:when use-when="function-available('ext:cwd')"
test="true()">
<xsl:if test="'chunks' = $v:debug">
<xsl:message select="'Chunk output base uri:',
resolve-uri($chunk-output-base-uri, ext:cwd())"/>
</xsl:if>
<xsl:sequence select="resolve-uri($chunk-output-base-uri, ext:cwd())"/>
</xsl:when>
<xsl:when test="$v:chunk">
<xsl:if test="'chunks' = $v:debug">
<xsl:message select="'Chunk output base uri:', $chunk-output-base-uri"/>
</xsl:if>
<xsl:sequence select="xs:anyURI($chunk-output-base-uri)"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="xs:anyURI($chunk-output-base-uri)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:variable name="result" as="xs:anyURI">
<xsl:choose>
<xsl:when test="not($chunk-output-base-uri) castable as xs:anyURI">
<xsl:message
select="'Error: ' || $chunk-output-base-uri || ' is not valid as $chunk-output-base-uri.'"
/>
</xsl:when>
<xsl:when use-when="function-available('ext:cwd')" test="true()">
<xsl:sequence select="
let $cobu := xs:anyURI($chunk-output-base-uri) => fp:shorten-uri()
return
resolve-uri($cobu, ext:cwd())"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="xs:anyURI($chunk-output-base-uri) => fp:shorten-uri()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="'chunks' = $v:debug">
<xsl:message select="'Chunk output base uri: ' || $result"/>
</xsl:if>
<xsl:sequence select="$result"/>
</xsl:variable>

<!-- I tinkered a bit to find images that would display across
Expand Down
24 changes: 24 additions & 0 deletions src/main/xslt/standalone-functions.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@
select="('glossary-collection', 'bibliography-collection',
'annotation-collection')"/>

<!-- ====================================================================================
| Functions for general use
|================================================================================== -->

<!-- Remove down-up fragments in $uri: /a/b/c/../../d becomes /a/d -->
<xsl:function name="fp:shorten-uri" as="xs:anyURI">
<xsl:param name="uri" as="xs:anyURI"/>
<xsl:variable name="result" as="xs:string*" >
<xsl:analyze-string select="$uri" regex="/[^/]+/\.\.">
<xsl:non-matching-substring>
<xsl:sequence select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:choose>
<xsl:when test="string-join($result) eq $uri">
<xsl:sequence select="xs:anyURI($uri)"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="string-join($result) => xs:anyURI() => fp:shorten-uri()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

<!-- ====================================================================================
| Functions for processing instructions and their pseudo attributes
|=================================================================================== -->
Expand Down

0 comments on commit ae3b57a

Please sign in to comment.