Skip to content

Commit

Permalink
Fix footnote links
Browse files Browse the repository at this point in the history
Affects: #13
  • Loading branch information
io7m committed Jul 4, 2021
1 parent 274ce78 commit e4ab568
Show file tree
Hide file tree
Showing 24 changed files with 264 additions and 101 deletions.
Expand Up @@ -22,6 +22,7 @@
import com.io7m.xstructural.xml.SXMLResources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -199,8 +200,14 @@ private static boolean checkLinks(
final var anchorsLocal = new ArrayList<Node>();
for (int index = 0; index < anchors.getLength(); ++index) {
final var anchor = anchors.item(index);
final var href = anchor.getAttributes().getNamedItem("href");
final var attributes = anchor.getAttributes();

final var href = attributes.getNamedItem("href");
final var hrefText = href.getTextContent();

if (!checkFootnoteLink(file, attributes, hrefText)) {
failed = true;
}
if (hrefText.startsWith("#")) {
anchorsLocal.add(anchor);
}
Expand Down Expand Up @@ -234,6 +241,29 @@ private static boolean checkLinks(
}
}

private static boolean checkFootnoteLink(
final Path file,
final NamedNodeMap attributes,
final String hrefText)
{
final var cssClass = attributes.getNamedItem("class");
if (cssClass == null) {
return true;
}

final var cssClassText = cssClass.getTextContent();
if (cssClassText.contains("stLinkFootnote")) {
if (!hrefText.startsWith("#")) {
if (!hrefText.contains(file.getFileName().toString())) {
LOG.error("a footnote link must link to the same file name ({})",
hrefText);
return false;
}
}
}
return true;
}

private static boolean findId(
final NodeList withIds,
final String idName)
Expand Down
Expand Up @@ -33,7 +33,7 @@
--stMetaFontSize: 90%;
--stNavigationRowGap: 0.25em;
--stVerbatimBorder: 1px solid #dddddd;
--stVerbatimFontSize: 90%;
--stVerbatimFontSize: 80%;

--stH1Size: 1.1em;
--stH2Size: 1.066em;
Expand Down
Expand Up @@ -105,7 +105,8 @@

<xsl:apply-templates select="."
mode="xstructural.tableOfContentsOptional">
<xsl:with-param name="withTitle" select="false()"/>
<xsl:with-param name="withTitle"
select="false()"/>
</xsl:apply-templates>

<xsl:apply-templates select="s:Section|s:Subsection|s:Paragraph|s:FormalItem"
Expand Down
Expand Up @@ -36,7 +36,8 @@

<xsl:apply-templates select="."
mode="xstructural.tableOfContentsOptional">
<xsl:with-param name="withTitle" select="false()"/>
<xsl:with-param name="withTitle"
select="false()"/>
</xsl:apply-templates>

<xsl:apply-templates select="s:Section|s:Subsection|s:Paragraph|s:FormalItem"
Expand Down
Expand Up @@ -162,6 +162,7 @@
<xsl:value-of select="$stNumber"/>
</xsl:element>
</td>

<td class="stFootnoteContentCell">
<div class="stFootnoteContent">
<xsl:apply-templates select="child::node()"
Expand All @@ -176,7 +177,7 @@
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="."/>
</xsl:call-template>
</xsl:attribute>
Expand All @@ -186,6 +187,7 @@
select="."/>
</xsl:call-template>
</xsl:attribute>
<xsl:value-of select="position()"/>
</xsl:element>
</xsl:for-each>
</div>
Expand Down
Expand Up @@ -25,18 +25,7 @@

<xsl:import href="xstructural7-links.xsl"/>
<xsl:import href="xstructural7-text.xsl"/>

<xsl:key name="xstructural.footnotes.referenceKey"
match="/s:Document//s:LinkFootnote"
use="@target"/>

<xsl:key name="xstructural.footnotes.key"
match="/s:Document//s:Footnote"
use="@id"/>

<xsl:key name="xstructural.links.key"
match="/s:Document//(s:Paragraph|s:FormalItem|s:Section|s:Subsection)"
use="@id"/>
<xsl:import href="xstructural7-keys.xsl"/>

<xsl:template match="text()"
mode="xstructural.inlines">
Expand All @@ -53,7 +42,7 @@

<xsl:attribute name="href">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="key('xstructural.links.key',@target)"/>
</xsl:call-template>
</xsl:attribute>
Expand Down Expand Up @@ -112,7 +101,7 @@

<xsl:attribute name="href">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$targetFootnote"/>
</xsl:call-template>
</xsl:attribute>
Expand Down
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8" ?>

<!--
Copyright © 2021 Mark Raynsford <code@io7m.com> https://www.io7m.com
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="urn:com.io7m.structural:7:0"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all"
version="2.0">

<xsl:key name="xstructural.footnotes.referenceKey"
match="/s:Document//s:LinkFootnote"
use="@target"/>

<xsl:key name="xstructural.footnotes.key"
match="/s:Document//s:Footnote"
use="@id"/>

<xsl:key name="xstructural.links.key"
match="/s:Document//(s:Paragraph|s:FormalItem|s:Section|s:Subsection|s:Footnote)"
use="@id"/>

</xsl:stylesheet>
Expand Up @@ -29,7 +29,7 @@

<xsl:template name="xstructural.links.anchorOf"
as="xsd:string">
<xsl:param name="node"
<xsl:param name="target"
as="element()"
required="yes"/>
<xsl:message terminate="yes">
Expand Down
Expand Up @@ -84,7 +84,7 @@
select="$sectionPrevFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionPrev"/>
</xsl:call-template>
</xsl:attribute>
Expand All @@ -103,7 +103,7 @@
select="$sectionUpFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionUp"/>
</xsl:call-template>
</xsl:attribute>
Expand All @@ -122,7 +122,7 @@
select="$sectionNextFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionNext"/>
</xsl:call-template>
</xsl:attribute>
Expand Down Expand Up @@ -189,7 +189,7 @@
select="$sectionPrevFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionPrev"/>
</xsl:call-template>
</xsl:attribute>
Expand All @@ -208,7 +208,7 @@
select="$sectionUpFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionUp"/>
</xsl:call-template>
</xsl:attribute>
Expand All @@ -227,7 +227,7 @@
select="$sectionNextFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionNext"/>
</xsl:call-template>
</xsl:attribute>
Expand Down Expand Up @@ -286,7 +286,7 @@
select="$sectionNextFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionNext"/>
</xsl:call-template>
</xsl:attribute>
Expand Down Expand Up @@ -341,7 +341,7 @@
select="$sectionNextFile"/>
<xsl:attribute name="title">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="$sectionNext"/>
</xsl:call-template>
</xsl:attribute>
Expand Down
Expand Up @@ -47,7 +47,7 @@
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="."/>
</xsl:call-template>
</xsl:attribute>
Expand Down Expand Up @@ -114,7 +114,7 @@
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:call-template name="xstructural.links.anchorOf">
<xsl:with-param name="node"
<xsl:with-param name="target"
select="."/>
</xsl:call-template>
</xsl:attribute>
Expand Down
Expand Up @@ -41,27 +41,60 @@
<!-- Top-level web multi templates. -->
<!-- -->

<xsl:template name="xstructural.fileOf"
as="xsd:string">
<xsl:param name="target"
as="element()"/>

<xsl:variable name="targetOwner"
as="element()">
<xsl:choose>
<xsl:when test="count($target/ancestor-or-self::s:Section) > 0">
<xsl:sequence select="$target/ancestor-or-self::s:Section[1]"/>
</xsl:when>
<xsl:when test="count($target/ancestor-or-self::s:Document) > 0">
<xsl:sequence select="$target/ancestor-or-self::s:Document[1]"/>
</xsl:when>
</xsl:choose>
</xsl:variable>

<xsl:value-of select="concat(generate-id($targetOwner), '.xhtml')"/>
</xsl:template>

<xdoc:doc>
An override of the anchorOf function that works for single-page XHTML files.
An override of the anchorOf function that works for multi-page XHTML files.
</xdoc:doc>

<xsl:template name="xstructural.links.anchorOf"
as="xsd:string">
<xsl:param name="node"
<xsl:param name="target"
as="element()"/>

<xsl:variable name="file"
as="xsd:string"
select="concat(generate-id($node), '.xhtml')"/>
<xsl:message select="concat('anchorOf: ', local-name($target))"/>

<xsl:variable name="completeHref"
<xsl:variable name="owningFile"
as="xsd:string">
<xsl:choose>
<xsl:when test="$node/attribute::id">
<xsl:value-of select="concat($file, '#id_',$node/attribute::id[1])"/>
<xsl:when test="$target/ancestor-or-self::s:Section[1]">
<xsl:variable name="owningSection"
as="element()">
<xsl:sequence select="$target/ancestor-or-self::s:Section[1]"/>
</xsl:variable>
<xsl:value-of select="concat(generate-id($owningSection),'.xhtml')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$xstructural.web.indexMulti"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:variable name="completeHref">
<xsl:choose>
<xsl:when test="$target/attribute::id">
<xsl:value-of select="concat($owningFile,'#id_',$target/attribute::id[1])"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($file, '#',generate-id($node))"/>
<xsl:value-of select="concat($owningFile,'#',generate-id($target))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Expand Down Expand Up @@ -123,7 +156,8 @@

<xsl:apply-templates select="."
mode="xstructural.tableOfContentsOptional">
<xsl:with-param name="withTitle" select="true()"/>
<xsl:with-param name="withTitle"
select="true()"/>
</xsl:apply-templates>

<xsl:apply-templates select="s:Section|s:Subsection|s:Paragraph|s:FormalItem"
Expand Down
Expand Up @@ -46,16 +46,16 @@

<xsl:template name="xstructural.links.anchorOf"
as="xsd:string">
<xsl:param name="node"
<xsl:param name="target"
as="element()"/>

<xsl:variable name="completeHref">
<xsl:choose>
<xsl:when test="$node/attribute::id">
<xsl:value-of select="concat('#id_',$node/attribute::id[1])"/>
<xsl:when test="$target/attribute::id">
<xsl:value-of select="concat('#id_',$target/attribute::id[1])"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('#',generate-id($node))"/>
<xsl:value-of select="concat('#',generate-id($target))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Expand Down Expand Up @@ -114,7 +114,8 @@

<xsl:apply-templates select="."
mode="xstructural.tableOfContentsOptional">
<xsl:with-param name="withTitle" select="true()"/>
<xsl:with-param name="withTitle"
select="true()"/>
</xsl:apply-templates>

<xsl:apply-templates select="s:Section|s:Subsection|s:Paragraph|s:FormalItem"
Expand Down

0 comments on commit e4ab568

Please sign in to comment.