Skip to content

Instantly share code, notes, and snippets.

@LdBeth
Created October 25, 2022 17:20
Show Gist options
  • Save LdBeth/93ec3b8cd6c12501d8a88547d2ee6fd0 to your computer and use it in GitHub Desktop.
Save LdBeth/93ec3b8cd6c12501d8a88547d2ee6fd0 to your computer and use it in GitHub Desktop.
xslTNG customization used to build my website.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:v="http://docbook.org/ns/docbook/variables"
xmlns:m="http://docbook.org/ns/docbook/modes"
xmlns:f="http://docbook.org/ns/docbook/functions"
xmlns:fp="http://docbook.org/ns/docbook/functions/private"
xmlns:t="http://docbook.org/ns/docbook/templates"
xmlns:tp="http://docbook.org/ns/docbook/templates/private"
xmlns:db="http://docbook.org/ns/docbook"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="array h v m f fp t tp db xs mml"
version="3.0">
<xsl:import href="../docbook/xslt/docbook.xsl"/>
<xsl:param name="orderedlist-item-numeration"
select="'1'"/>
<xsl:param name="debug"
select="'chunk-cleanup'"/>
<xsl:param name="use-docbook-css"
select="'false'"/>
<xsl:param name="verbatim-syntax-highlight-languages"
select="'apl xml html cl rnc'"/>
<xsl:param name="user-css-links"
select="('css/artng.css')"/>
<xsl:param name="resource-base-uri"
select="'/'"/>
<xsl:param name="date-dateTime-format"
select="'[D01] [MNn,*-3] [Y0001]
at [H01]:[m01]'"/>
<xsl:param name="verbatim-number-first-line"
select="'false'"/>
<xsl:param name="chunk-nav"
select="'false'"/>
<xsl:variable name="v:admonition-icons">
<db:tip>&#x261e;&#xFE0E;</db:tip>
<db:note>&#x270e;&#xFE0E;</db:note>
<db:important>&#x2757;&#xFE0E;</db:important>
<db:caution>&#9888;&#xFE0E;</db:caution>
<db:warning>&#x26a1;&#xFE0E;</db:warning>
<db:danger>&#x2620;&#xFE0E;</db:danger>
</xsl:variable>
<xsl:param name="v:mediaobject-input-base-uri" as="xs:string"
select="'media/'" />
<xsl:param name="v:mediaobject-output-base-uri" as="xs:string"
select="'./'"/>
<xsl:function name="f:resolve-object-uri" as="xs:string">
<xsl:param name="uri" as="xs:string"/>
<xsl:variable name="input-uri"
select="substring-after($uri, $v:mediaobject-input-base-uri)"/>
<xsl:variable name="output-uri"
select="if (exists($v:mediaobject-output-base-uri))
then $v:mediaobject-output-base-uri || $input-uri
else $input-uri"/>
<xsl:sequence select="$output-uri"/>
</xsl:function>
<xsl:template match="*[@db-chunk]" mode="m:chunk-cleanup" priority="10">
<xsl:param name="docbook" as="document-node()" tunnel="yes"/>
<xsl:variable name="self" select="."/>
<xsl:message use-when="'chunk-cleanup' = $debug"
select="'Chunk cleanup (print override):',
local-name(.), @db-chunk/string()"/>
<xsl:variable name="footnotes" as="element(h:db-footnote)*">
<xsl:for-each select=".//h:db-footnote[not(ancestor::h:table)]">
<xsl:variable name="chunk" select="ancestor::*[@db-chunk][1]"/>
<xsl:if test="$chunk is $self">
<xsl:sequence select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="annotations" as="xs:string*">
<xsl:for-each select=".//h:db-annotation-marker">
<xsl:variable name="chunk" select="ancestor::*[@db-chunk][1]"/>
<xsl:if test="$chunk is $self">
<xsl:sequence select="@target/string()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="head" select="/h:html/h:head"/>
<xsl:variable name="rbu" select="fp:root-base-uri(.)"/>
<xsl:variable name="cbu" select="fp:chunk-output-filename(.)"/>
<html db-chunk="{fp:chunk-output-filename(.)}">
<xsl:attribute name="lang" select="f:language($docbook)"/>
<xsl:variable name="ctype" select="$head/h:meta[@http-equiv='Content-Type']"/>
<xsl:variable name="title" select="$head/h:title"/>
<head>
<xsl:apply-templates select="$ctype" mode="m:chunk-cleanup"/>
<title>
<xsl:value-of select="f:chunk-title(.)"/>
</title>
<xsl:apply-templates select="$head/node() except ($ctype|$title)"
mode="m:chunk-cleanup">
<xsl:with-param name="rootbaseuri" select="$rbu"/>
<xsl:with-param name="chunkbaseuri" select="$cbu"/>
</xsl:apply-templates>
<xsl:if test="exists(.//mml:*)"
xmlns:mml="http://www.w3.org/1998/Math/MathML">
<xsl:apply-templates select="/h:html/h:db-mathml-script/*"
mode="m:chunk-cleanup">
<xsl:with-param name="rootbaseuri" select="$rbu"/>
<xsl:with-param name="chunkbaseuri" select="$cbu"/>
</xsl:apply-templates>
</xsl:if>
</head>
<body>
<xsl:copy-of select="*/@*"/>
<xsl:apply-templates select="*/h:header" mode="m:chunk-cleanup"/>
<main>
<xsl:apply-templates select="*/* except */h:header"
mode="m:chunk-cleanup"/>
</main>
<xsl:if test="$footnotes or exists($annotations)">
<footer>
<xsl:if test="$footnotes">
<xsl:call-template name="t:chunk-footnotes">
<xsl:with-param name="footnotes" select="$footnotes"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="exists($annotations)">
<xsl:variable name="style"
select="key('hanno', $annotations[1])[1]/@style/string()"/>
<div class="annotations">
<div class="annotation-wrapper title"
>Annotations</div>
<xsl:for-each select="$annotations">
<xsl:apply-templates select="key('hanno', ., root($self))/node()"
mode="m:docbook"/>
</xsl:for-each>
</div>
</xsl:if>
</footer>
</xsl:if>
</body>
</html>
</xsl:template>
<xsl:template name="tp:verbatim-lines" as="element()">
<xsl:param name="highlight" as="xs:string*" required="yes"/>
<xsl:param name="numbered" as="xs:boolean" required="yes"/>
<xsl:param name="trim-trailing" as="xs:boolean" required="yes"/>
<xsl:param name="inject" as="array(*)?" select="()"/>
<xsl:variable name="lines" as="array(*)">
<xsl:call-template name="tp:verbatim-array">
<xsl:with-param name="highlight" select="$highlight"/>
<xsl:with-param name="numbered" select="$numbered"/>
<xsl:with-param name="trim-trailing" select="$trim-trailing"/>
<xsl:with-param name="inject" select="$inject"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="no-code"
select="self::db:address or self::db:literallayout"/>
<xsl:variable name="starting-line-number" as="xs:integer">
<xsl:choose>
<xsl:when test="@startinglinenumber">
<xsl:sequence select="xs:integer(@startinglinenumber)"/>
</xsl:when>
<xsl:when test="@continuation = 'continues'">
<xsl:variable name="name" select="node-name(.)"/>
<xsl:variable name="prec" select="preceding::*[node-name(.) = $name][1]"/>
<xsl:choose>
<xsl:when test="empty($prec)">
<xsl:sequence select="1"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="formatted" as="element()">
<xsl:apply-templates select="$prec"/>
</xsl:variable>
<xsl:sequence select="if ($formatted/@db-startinglinenumber
and $formatted/@db-numberoflines)
then
xs:integer($formatted/@db-startinglinenumber)
+ xs:integer($formatted/@db-numberoflines)
else
1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="every-nth" as="xs:integer">
<xsl:choose>
<xsl:when test="f:pi(., 'linenumbering-everyNth',
fp:verbatim-properties(.)?everyNth)">
<xsl:sequence
select="xs:integer(f:pi(., 'linenumbering-everyNth',
fp:verbatim-properties(.)?everyNth))"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$v:verbatim-number-every-nth"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="number-first" as="xs:boolean">
<xsl:choose>
<xsl:when test="f:pi(., 'linenumbering-first',
fp:verbatim-properties(.)?first)">
<xsl:sequence
select="f:is-true(f:pi(., 'linenumbering-first',
fp:verbatim-properties(.)?first))"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$v:verbatim-number-first-line"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="minlines" as="xs:integer">
<xsl:choose>
<xsl:when test="f:pi(., 'linenumbering-minlines',
fp:verbatim-properties(.)?minlines)">
<xsl:sequence
select="xs:integer(f:pi(., 'linenumbering-minlines',
fp:verbatim-properties(.)?minlines))"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$v:verbatim-number-minlines"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="syntax-highlight"
select="f:highlight-verbatim(.)"/>
<div class="pre-wrap{if ($syntax-highlight) then ' highlight' else ''}">
<xsl:if test="$numbered">
<xsl:attribute name="db-startinglinenumber" select="$starting-line-number"/>
<xsl:attribute name="db-numberoflines" select="array:size($lines)"/>
</xsl:if>
<table>
<xsl:apply-templates select="." mode="m:attributes">
<xsl:with-param name="style" select="'lines'"/>
<xsl:with-param name="numbered" select="$numbered"/>
<xsl:with-param name="long"
select="array:size($lines) ge $minlines"/>
</xsl:apply-templates>
<tbody><tr>
<xsl:if test="$numbered">
<td class="gutter">
<pre>
<xsl:for-each select="1 to array:size($lines)">
<xsl:variable name="index" select="."/>
<xsl:variable name="ln" select=". + $starting-line-number - 1"/>
<span class="ln">
<xsl:sequence select="if ((array:size($lines) ge $minlines)
and (($index = 1 and $number-first)
or ($ln mod $every-nth = 0)))
then $ln else '&#160;'"/>
</span>
<xsl:text>&#10;</xsl:text>
</xsl:for-each>
</pre>
</td>
</xsl:if>
<td class="code"><pre>
<xsl:for-each select="1 to array:size($lines)">
<xsl:variable name="index" select="."/>
<xsl:variable name="ln" select=". + $starting-line-number - 1"/>
<xsl:variable name="line"
select="if (count($lines(.)) = 1 and $lines(.) = '')
then ' '
else $lines(.)"/>
<xsl:variable name="callouts"
select="$line[. instance of element()
and contains-token(@class, 'callout-bug')]"/>
<xsl:variable name="line" as="item()*">
<xsl:for-each select="$line">
<xsl:call-template name="tp:filter-callouts">
<xsl:with-param name="highlight" select="$highlight"/>
<xsl:with-param name="line" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="classes" as="xs:string+">
<xsl:sequence select="'line'"/>
<xsl:if test="(('lines' = $highlight)
and $callouts[.[contains-token(@class, 'defcol')]])
or
(('lineranges' = $highlight
or 'lineranges-first' = $highlight
or 'lineranges-all' = $highlight)
and $callouts[.[contains-token(@class, 'linerange')]])">
<xsl:sequence select="'highlight'"/>
<xsl:sequence select="'line'||."/>
</xsl:if>
</xsl:variable>
<span class="{string-join($classes,' ')}" db-line="{.}">
<xsl:choose>
<xsl:when test="$no-code">
<xsl:sequence select="$line"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$line"/>
</xsl:otherwise>
</xsl:choose>
</span>
<xsl:text>&#10;</xsl:text>
</xsl:for-each>
</pre></td>
</tr></tbody></table>
</div>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment