Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #579 removes unnecessary down-up fragments in URIs #580

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading