Skip to content

Commit 8519ed5

Browse files
author
Sanders Kleinfeld
committed
Refinements to XREF handling and added handling for non-XREF href processing.
1 parent ecc5af5 commit 8519ed5

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

htmlbook-xsl/chunk.xsl

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<!-- Chunk template used to split content among multiple .html files -->
1212

13+
<!-- ToDo: Refactor to eliminate duplicate code around href generation for XREF vs. non-XREF <a> elems -->
14+
1315
<!-- ToDo: Add "previous" and "next" links as in the docbook-xsl stylesheets? -->
1416

1517
<!-- Imports htmlbook.xsl -->
@@ -320,7 +322,7 @@ sect5:s
320322

321323
<!-- Custom XREF template in chunk.xsl, because we need to take chunk filename into account, and update hrefs. -->
322324
<!-- All XREFs must be tagged with a @data-type containing XREF -->
323-
<xsl:template match="h:a[contains(@data-type, 'xref')]">
325+
<xsl:template match="h:a[contains(@data-type, 'xref')]" name="process-as-xref">
324326
<xsl:param name="autogenerate-xrefs" select="$autogenerate-xrefs"/>
325327
<xsl:variable name="calculated-output-href">
326328
<xsl:call-template name="calculate-output-href">
@@ -334,9 +336,9 @@ sect5:s
334336
</xsl:call-template>
335337
</xsl:variable>
336338
<xsl:copy>
337-
<xsl:apply-templates select="@*[not(local-name() = 'href')]"/>
339+
<xsl:apply-templates select="@*[not(name() = 'href')]"/>
338340
<xsl:choose>
339-
<xsl:when test="count(key('id', $href-anchor)) > 0">
341+
<xsl:when test="(count(key('id', $href-anchor)) &gt; 0) and ($is-xref = 1)">
340342
<xsl:variable name="target" select="key('id', $href-anchor)[1]"/>
341343
<!-- Regenerate the href here, to ensure it accurately points to correct location, including chunk filename) -->
342344
<xsl:attribute name="href">
@@ -384,6 +386,63 @@ sect5:s
384386
</xsl:copy>
385387
</xsl:template>
386388

389+
<!-- href and content handling for a elements that are not indexterms, xrefs, or footnoterefs -->
390+
<xsl:template match="h:a[not((contains(@data-type, 'xref')) or
391+
(contains(@data-type, 'footnoteref')) or
392+
(contains(@data-type, 'indexterm')))][@href]">
393+
<!-- If the element is empty, does not have data-type="link", and is a valid XREF, go ahead and treat it like an <a> element with data-type="xref" -->
394+
<xsl:variable name="is-xref">
395+
<xsl:call-template name="href-is-xref">
396+
<xsl:with-param name="href-value" select="@href"/>
397+
</xsl:call-template>
398+
</xsl:variable>
399+
<xsl:variable name="calculated-output-href">
400+
<xsl:call-template name="calculate-output-href">
401+
<xsl:with-param name="source-href-value" select="@href"/>
402+
</xsl:call-template>
403+
</xsl:variable>
404+
<xsl:variable name="href-anchor" select="substring-after($calculated-output-href, '#')"/>
405+
<xsl:choose>
406+
<xsl:when test="(not(node())) and
407+
($is-xref = 1) and
408+
not(@data-type='link')">
409+
<xsl:call-template name="process-as-xref"/>
410+
</xsl:when>
411+
<!-- Otherwise just process href and apply-templates for everything else -->
412+
<xsl:otherwise>
413+
<xsl:copy>
414+
<xsl:apply-templates select="@*[not(name(.) = 'href')]"/>
415+
<xsl:attribute name="href">
416+
<xsl:choose>
417+
<xsl:when test="(count(key('id', $href-anchor)) &gt; 0) and ($is-xref = 1)">
418+
<xsl:variable name="target" select="key('id', $href-anchor)[1]"/>
419+
<!-- Regenerate the href here, to ensure it accurately points to correct location, including chunk filename) -->
420+
<xsl:call-template name="href.target">
421+
<xsl:with-param name="object" select="$target"/>
422+
<xsl:with-param name="source-link-node" select="."/>
423+
</xsl:call-template>
424+
</xsl:when>
425+
<xsl:otherwise>
426+
<xsl:if test="$is-xref = 1">
427+
<xsl:call-template name="log-message">
428+
<xsl:with-param name="type" select="'WARNING'"/>
429+
<xsl:with-param name="message">
430+
<xsl:text>Unable to locate target for a element with @href value:</xsl:text>
431+
<xsl:value-of select="@href"/>
432+
</xsl:with-param>
433+
</xsl:call-template>
434+
</xsl:if>
435+
<!-- Oh well, just copy any existing href to output -->
436+
<xsl:apply-templates select="@href"/>
437+
</xsl:otherwise>
438+
</xsl:choose>
439+
</xsl:attribute>
440+
<xsl:apply-templates/>
441+
</xsl:copy>
442+
</xsl:otherwise>
443+
</xsl:choose>
444+
</xsl:template>
445+
387446
<!-- Generate target @href value pointing to given node, in the appropriate chunk -->
388447
<!-- Borrowed and adapted from xhtml/html.xsl and xhtml/chunk-common.xsl in docbook-xsl stylesheets -->
389448
<xsl:template name="href.target">

0 commit comments

Comments
 (0)