|
13 | 13 |
|
14 | 14 | <!-- Default rule for TOC generation -->
|
15 | 15 |
|
16 |
| - <!-- ToDo: Add support for separate handling for data-type="link", where it's an internal cross-reference, but you definitely |
17 |
| - do not want to override text node --> |
18 |
| - |
19 | 16 | <!-- All XREFs must be tagged with a @data-type containing XREF -->
|
20 |
| - <xsl:template match="h:a[contains(@data-type, 'xref')]"> |
21 |
| - <xsl:variable name="href-anchor"> |
22 |
| - <xsl:choose> |
23 |
| - <!-- If href contains an # (as it should), we're going to assume the subsequent text is the referent id --> |
24 |
| - <xsl:when test="contains(@href, '#')"> |
25 |
| - <xsl:value-of select="substring-after(@href, '#')"/> |
26 |
| - </xsl:when> |
27 |
| - <!-- Otherwise, we'll just assume the entire href is the referent id --> |
28 |
| - <xsl:otherwise> |
29 |
| - <xsl:value-of select="@href"/> |
30 |
| - </xsl:otherwise> |
31 |
| - </xsl:choose> |
| 17 | + <xsl:template match="h:a[contains(@data-type, 'xref')]" name="process-as-xref"> |
| 18 | + <xsl:param name="autogenerate-xrefs" select="$autogenerate-xrefs"/> |
| 19 | + <xsl:variable name="calculated-output-href"> |
| 20 | + <xsl:call-template name="calculate-output-href"> |
| 21 | + <xsl:with-param name="source-href-value" select="@href"/> |
| 22 | + </xsl:call-template> |
| 23 | + </xsl:variable> |
| 24 | + <xsl:variable name="href-anchor" select="substring-after($calculated-output-href, '#')"/> |
| 25 | + <xsl:variable name="is-xref"> |
| 26 | + <xsl:call-template name="href-is-xref"> |
| 27 | + <xsl:with-param name="href-value" select="@href"/> |
| 28 | + </xsl:call-template> |
32 | 29 | </xsl:variable>
|
33 | 30 | <xsl:copy>
|
34 |
| - <xsl:apply-templates select="@*"/> |
35 |
| - <xsl:choose> |
36 |
| - <!-- Generate XREF text node if <a> is either empty or $xref-placeholder-overwrite-contents = 1 --> |
37 |
| - <xsl:when test="$autogenerate-xrefs = 1 and (. = '' or $xref-placeholder-overwrite-contents = 1)"> |
| 31 | + <xsl:apply-templates select="@*[not(name(.) = 'href')]"/> |
| 32 | + <xsl:attribute name="href"> |
| 33 | + <xsl:value-of select="$calculated-output-href"/> |
| 34 | + </xsl:attribute> |
| 35 | + <xsl:choose> |
| 36 | + <!-- Generate XREF text node if $autogenerate-xrefs is enabled --> |
| 37 | + <xsl:when test="($autogenerate-xrefs = 1) and ($is-xref = 1)"> |
38 | 38 | <xsl:choose>
|
39 | 39 | <!-- If we can locate the target, process gentext with "xref-to" -->
|
40 | 40 | <xsl:when test="count(key('id', $href-anchor)) > 0">
|
|
65 | 65 | </xsl:copy>
|
66 | 66 | </xsl:template>
|
67 | 67 |
|
| 68 | + <!-- href and content handling for a elements that are not indexterms, xrefs, or footnoterefs --> |
| 69 | + <xsl:template match="h:a[not((contains(@data-type, 'xref')) or |
| 70 | + (contains(@data-type, 'footnoteref')) or |
| 71 | + (contains(@data-type, 'indexterm')))][@href]"> |
| 72 | + <!-- 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" --> |
| 73 | + <xsl:variable name="is-xref"> |
| 74 | + <xsl:call-template name="href-is-xref"> |
| 75 | + <xsl:with-param name="href-value" select="@href"/> |
| 76 | + </xsl:call-template> |
| 77 | + </xsl:variable> |
| 78 | + <xsl:choose> |
| 79 | + <xsl:when test="(not(node())) and |
| 80 | + ($is-xref = 1) and |
| 81 | + not(@data-type='link')"> |
| 82 | + <xsl:call-template name="process-as-xref"/> |
| 83 | + </xsl:when> |
| 84 | + <!-- Otherwise just process href and apply-templates for everything else --> |
| 85 | + <xsl:otherwise> |
| 86 | + <xsl:copy> |
| 87 | + <xsl:apply-templates select="@*[not(name(.) = 'href')]"/> |
| 88 | + <xsl:attribute name="href"> |
| 89 | + <xsl:call-template name="calculate-output-href"> |
| 90 | + <xsl:with-param name="source-href-value" select="@href"/> |
| 91 | + </xsl:call-template> |
| 92 | + </xsl:attribute> |
| 93 | + <xsl:apply-templates/> |
| 94 | + </xsl:copy> |
| 95 | + </xsl:otherwise> |
| 96 | + </xsl:choose> |
| 97 | + </xsl:template> |
| 98 | + |
68 | 99 | <!-- Adapted from docbook-xsl templates in xhtml/xref.xsl -->
|
69 | 100 | <xsl:template match="*" mode="xref-to">
|
70 | 101 | <xsl:param name="referrer"/>
|
|
225 | 256 | <!-- ============================================================ -->
|
226 | 257 |
|
227 | 258 | <!-- Adapted from docbook-xsl templates in common/gentext.xsl -->
|
228 |
| -<!-- For reasons of simplicity and relevancy, only supporting %n, %t, and %d substitutions --> |
| 259 | +<!-- For reasons of simplicity and relevance, only supporting %n, %t, and %d substitutions --> |
229 | 260 | <xsl:template name="substitute-markup">
|
230 | 261 | <xsl:param name="template" select="''"/>
|
231 | 262 | <xsl:param name="allow-anchors" select="'0'"/>
|
|
484 | 515 | </xsl:choose>
|
485 | 516 | </xsl:template>
|
486 | 517 |
|
| 518 | +<!-- Utility template for determining whether an @href is a valid XREF --> |
| 519 | +<!-- Returns 1 if href is an XREF, and 0 if not --> |
| 520 | +<xsl:template name="href-is-xref"> |
| 521 | + <xsl:param name="href-value" select="@href"/> |
| 522 | + <xsl:choose> |
| 523 | + <xsl:when test="starts-with($href-value, '#')">1</xsl:when> |
| 524 | + <xsl:when test="starts-with($href-value, 'mailto:')">0</xsl:when> |
| 525 | + <!-- If we weren't worried about XSLT 1.0 compatibility, might be better to use a regex here --> |
| 526 | + <xsl:when test="contains($href-value, '://')">0</xsl:when> |
| 527 | + <xsl:otherwise>1</xsl:otherwise> |
| 528 | + </xsl:choose> |
| 529 | +</xsl:template> |
| 530 | + |
| 531 | +<!-- Utility template for processing @href attributes on <a> elements --> |
| 532 | +<!-- For XREFs, grab either the text content after the last # sign, or all the content if there is no # sign --> |
| 533 | +<!-- For non-XREFs, don't touch at all --> |
| 534 | +<xsl:template name="calculate-output-href"> |
| 535 | + <xsl:param name="source-href-value" select="@href"/> |
| 536 | + <xsl:param name="href-is-xref"/> |
| 537 | + |
| 538 | + <xsl:variable name="is-xref"> |
| 539 | + <xsl:choose> |
| 540 | + <xsl:when test="($href-is-xref = 0) or ($href-is-xref = 1)"> |
| 541 | + <xsl:value-of select="$href-is-xref"/> |
| 542 | + </xsl:when> |
| 543 | + <xsl:otherwise> |
| 544 | + <xsl:call-template name="href-is-xref"> |
| 545 | + <xsl:with-param name="href-value" select="$source-href-value"/> |
| 546 | + </xsl:call-template> |
| 547 | + </xsl:otherwise> |
| 548 | + </xsl:choose> |
| 549 | + </xsl:variable> |
| 550 | + |
| 551 | + <xsl:choose> |
| 552 | + <xsl:when test="$is-xref = 1"> |
| 553 | + <xsl:choose> |
| 554 | + <!-- If there is more than one # sign in content, recursively call template to get content after first # --> |
| 555 | + <xsl:when test="contains(substring-after($source-href-value, '#'), '#')"> |
| 556 | + <xsl:call-template name="calculate-output-href"> |
| 557 | + <xsl:with-param name="source-href-value" select="substring-after($source-href-value, '#')"/> |
| 558 | + <xsl:with-param name="href-is-xref" select="1"/> |
| 559 | + </xsl:call-template> |
| 560 | + </xsl:when> |
| 561 | + <!-- If there is a # sign in content, grab the # and all content thereafter --> |
| 562 | + <xsl:when test="contains($source-href-value, '#')"> |
| 563 | + <xsl:value-of select="concat('#', substring-after($source-href-value, '#'))"/> |
| 564 | + </xsl:when> |
| 565 | + <!-- Otherwise, just use all the text as is, with a # sign prepended--> |
| 566 | + <xsl:otherwise> |
| 567 | + <xsl:value-of select="concat('#', $source-href-value)"/> |
| 568 | + </xsl:otherwise> |
| 569 | + </xsl:choose> |
| 570 | + </xsl:when> |
| 571 | + <xsl:otherwise> |
| 572 | + <!-- Just use the text as is --> |
| 573 | + <xsl:value-of select="$source-href-value"/> |
| 574 | + </xsl:otherwise> |
| 575 | + </xsl:choose> |
| 576 | +</xsl:template> |
| 577 | + |
487 | 578 | </xsl:stylesheet>
|
0 commit comments