File: template.xsl

package info (click to toggle)
postgresql-pllua 1%3A2.0.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,316 kB
  • sloc: ansic: 14,369; sql: 2,181; makefile: 163; sh: 59; javascript: 38
file content (443 lines) | stat: -rw-r--r-- 15,226 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" version="5.0" doctype-system="about:legacy-compat" />

  <!-- any variables we might want to use later -->
  <xsl:variable name="canon_url">https://pllua.github.io/pllua/</xsl:variable>
  <xsl:variable name="canon_site">https://github.com/pllua/pllua/</xsl:variable>

  <!--
      general purpose utility templates
  -->

  <!--
      copy-add-class(newclass, [attr], [copyattr], [copychild])

      Adds string "newclass" to a class-like (i.e. space-delimited)
      attribute named "attr" of the current node, which defaults to
      "class" if not specified. The result is applied to the current
      result node. If copyattr is not specified as false, copies all
      other attributes too; if copychild is not false, copy the child
      elements.
  -->
  <xsl:template name="copy-add-class">
    <xsl:param name="newclass" />
    <xsl:param name="attr" select="string('class')" />
    <xsl:param name="copyattr" select="true()" />
    <xsl:param name="copychild" select="true()" />
    <xsl:if test="$copyattr">
      <xsl:apply-templates select="@*" />
    </xsl:if>
    <xsl:attribute name="{$attr}">
      <xsl:value-of select="normalize-space(concat(string(attribute::*[name() = $attr]),' ',$newclass))"/>
    </xsl:attribute>
    <xsl:if test="$copychild">
      <xsl:apply-templates />
    </xsl:if>
  </xsl:template>

  <!--
      We want to munge certain cases of <ul><li>..<br/>..</li></ul>
      from the input into <dl><dt>..</dt><dd>..</dd></dl>, possibly
      with multiple <br/> tags leading to multiple <dt> elements.

      We do this as follows: for all nodes immediately under <li>, we
      tag the node with the id of the nearest following <br/> tag.
      This includes text nodes but the <br/> nodes themselves are
      excluded.

      Then, to output the <li> element, we traverse each child <br/>,
      and collect into a <dt> element all the nodes tagged to that
      <br/>. Then the remaining nodes (distinguished by not having a
      following <br/> sibling) are just processed into the <dd>
      element.

      We end up tagging nodes that we don't actually intend to munge,
      but that's harmless.
  -->

  <xsl:key
      name="dl-br-key"
      match="li/node()[not(self::br) and following-sibling::br]"
      use="generate-id(following-sibling::br[1])"
      />

  <!--
      match the <ul> instances we wish to munge, and enter mode
      "dl-br" for them.
  -->
  <xsl:template match="ul[child::li[1][child::br] and not(parent::div[@class='no-dl-fudge'])]">
    <dl>
      <xsl:call-template name="copy-add-class">
        <xsl:with-param name="newclass" select="string('dl-br')" />
        <xsl:with-param name="copychild" select="false()" />
      </xsl:call-template>
      <xsl:apply-templates mode="dl-br" />
    </dl>
  </xsl:template>

  <!--
      MODE: dl-br

      This is used at the immediate top level under a <ul> we're
      munging; we process the <li> elements specially and copy
      anything else.
  -->

  <xsl:template mode="dl-br" match="li[child::br]">
    <xsl:for-each select="./br">
      <dt>
        <xsl:apply-templates
            select="key('dl-br-key',generate-id())[* or normalize-space(text())]"
            />
      </dt>
    </xsl:for-each>
    <xsl:if test="child::br[position()=last() and (following-sibling::* or normalize-space(following-sibling::text()))]">
      <dd>
        <xsl:apply-templates select="node()[not(following-sibling::br or self::br)]" />
      </dd>
    </xsl:if>
  </xsl:template>

  <!-- default action for this mode is just to punt to the normal mode -->
  <xsl:template mode="dl-br" match="*|@*">
    <xsl:apply-templates select="." />
  </xsl:template>

  <!--
      END MODE: dl-br
  -->

  <!--
      We also want to munge some cases of <ul><li>...</li></ul> where
      the <li> elements contain paragraph text. These also become <dl>
      elements, with the first paragraph as <dt> (losing its <p> tag)
      and all following ones collected inside a <dd> with their <p>
      tags intact.
  -->

  <!--
      match the <ul> instances we wish to munge, and enter mode
      "dl-p" for them.
  -->
  <xsl:template match="ul[child::li[1][child::p] and not(parent::div[@class='no-dl-fudge'])]">
    <dl>
      <xsl:call-template name="copy-add-class">
        <xsl:with-param name="newclass" select="string('dl-p')" />
        <xsl:with-param name="copychild" select="false()" />
      </xsl:call-template>
      <xsl:apply-templates mode="dl-p" />
    </dl>
  </xsl:template>

  <!--
      MODE: dl-p

      Used immediately at top level under a <ul>, we munge the <li>
      elements and copy the rest.
  -->

  <xsl:template mode="dl-p" match="li">
    <dt>
      <xsl:apply-templates select="p[1]/@*" />
      <xsl:apply-templates select="p[1]/node()" />
    </dt>
    <xsl:if test="child::p[following-sibling::p]">
      <dd>
        <xsl:apply-templates select="node()[preceding-sibling::p]" />
      </dd>
    </xsl:if>
  </xsl:template>

  <!-- default action for this mode is just to punt to the normal mode -->
  <xsl:template mode="dl-p" match="*|@*">
    <xsl:apply-templates select="." />
  </xsl:template>

  <!--
      END MODE: dl-p
  -->

  <!--
      External hrefs in <a> tags; add keywords to rel= attribute.
  -->
  <xsl:template match='a[starts-with(@href,"http://") or starts-with(@href,"https://")]'>
    <a>
      <xsl:call-template name="copy-add-class">
        <xsl:with-param name="attr" select="string('rel')" />
        <xsl:with-param name="newclass" select="string('external nofollow')" />
      </xsl:call-template>
    </a>
  </xsl:template>

  <!--
      We want to move "footer" around in the skeleton, so make copying
      it conditional.
  -->
  <xsl:template match="div[@id='footer']">
    <xsl:param name="footer" select="false()" />
    <xsl:if test="$footer">
      <xsl:copy>
        <xsl:call-template name="copy-add-class">
          <xsl:with-param name="newclass" select="string('maincolumn footer')" />
        </xsl:call-template>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <!--
      Assign classes to some elements to simplify CSS.
  -->

  <!--
      <code> not inside <pre> is assigned a "shortcode" or "longcode"
      class according to its content length.
  -->
  <xsl:template match="code[not(ancestor::pre)]">
    <xsl:copy>
      <xsl:call-template name="copy-add-class">
        <xsl:with-param name="newclass">
          <xsl:choose>
            <xsl:when test="string-length() &lt; 40">shortcode</xsl:when>
            <xsl:otherwise>longcode</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <!--
      <pre> containing <code> is a codeblock class.
  -->
  <xsl:template match="pre[child::code]">
    <xsl:copy>
      <xsl:call-template name="copy-add-class">
        <xsl:with-param name="newclass" select="string('codeblock')" />
      </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <!--
      Table of Contents.

      We generate a structured listing (currently <ol>s) from the flat
      list of <h*> nodes in the document. We do this with another
      tagging trick: we tag every h(N) node with the generated id of
      the h(N-1) node that precedes it in document order. This allows
      us to easily collect the h(N+1) nodes for a given h(N) node.

      Currently the hard limit is <h5>, but a maxdepth parameter is
      used to limit this further.
  -->

  <!--
      keys for Hn nodes
      key name format matters, for incrementing in toc-recurse
  -->
  <xsl:key name="hkey-2" match="h2" use="generate-id(preceding::h1[1])"/>
  <xsl:key name="hkey-3" match="h3" use="generate-id(preceding::h2[1])"/>
  <xsl:key name="hkey-4" match="h4" use="generate-id(preceding::h3[1])"/>
  <xsl:key name="hkey-5" match="h5" use="generate-id(preceding::h4[1])"/>

  <!--
      mode "toc-label" matches any Hn node of a level suitable for TOC
      inclusion, and returns text content (only) with the node's own
      label (not including prefix)
  -->
  <xsl:template mode="toc-label" match="h1" name="toc-label-h1">
    <xsl:number level="any" count="h1" />
  </xsl:template>
  <xsl:template mode="toc-label" match="h2" name="toc-label-h2">
    <xsl:number level="any" count="h2" from="h1" />
  </xsl:template>
  <xsl:template mode="toc-label" match="h3" name="toc-label-h3">
    <xsl:number level="any" count="h3" from="h2" />
  </xsl:template>
  <xsl:template mode="toc-label" match="h4" name="toc-label-h4">
    <xsl:number level="any" count="h4" from="h3" />
  </xsl:template>
  <xsl:template mode="toc-label" match="h5" name="toc-label-h5">
    <xsl:number level="any" count="h5" from="h4" />
  </xsl:template>
  <xsl:template mode="toc-label" match="@*|text()" />

  <!--
      mode "toc-id" matches any Hn node of a level suitable for TOC
      inclusion, and returns text content (only) with the id tag to be
      used for the node
  -->
  <xsl:template mode="toc-id" match="h1" name="toc-id-h1">
    <xsl:text>S</xsl:text>
    <xsl:call-template name="toc-label-h1" />
  </xsl:template>
  <xsl:template mode="toc-id" match="h2" name="toc-id-h2">
    <xsl:call-template name="toc-id-h1" />
    <xsl:text>.</xsl:text>
    <xsl:call-template name="toc-label-h2" />
  </xsl:template>
  <xsl:template mode="toc-id" match="h3" name="toc-id-h3">
    <xsl:call-template name="toc-id-h2" />
    <xsl:text>.</xsl:text>
    <xsl:call-template name="toc-label-h3" />
  </xsl:template>
  <xsl:template mode="toc-id" match="h4" name="toc-id-h4">
    <xsl:call-template name="toc-id-h3" />
    <xsl:text>.</xsl:text>
    <xsl:call-template name="toc-label-h4" />
  </xsl:template>
  <xsl:template mode="toc-id" match="h5" name="toc-id-h5">
    <xsl:call-template name="toc-id-h4" />
    <xsl:text>.</xsl:text>
    <xsl:call-template name="toc-label-h5" />
  </xsl:template>
  <xsl:template mode="toc-id" match="@*|text()" />

  <!--
      mode "toc" is applied to the document's H1 elements to create
      the TOC.

      Since the document's Hn tags are unstructured and might not even
      be at top level, we have to generate the nested structure
      ourselves. Accordingly, match only on the H1 tags, and for each
      one, collect the associated H2 tags and recurse.

      Parameter "maxdepth" can be supplied by the caller.
  -->
  <xsl:template mode="toc" match="h1" name="toc-recurse">
    <xsl:param name="depth" select="2" />
    <xsl:param name="maxdepth" select="5" />
    <xsl:variable name="id">
      <xsl:apply-templates mode="toc-id" select="." />
    </xsl:variable>
    <xsl:variable name="label">
      <xsl:apply-templates mode="toc-label" select="." />
    </xsl:variable>
    <li value="{$label}" class="toc tocentry-{$depth - 1}">
      <a href="#{$id}" class="toc toclink-{$depth - 1}">
        <!-- if need be, we could make another mode for copying header
             content into TOC entries, but currently, we copy them the
             same way as in the document proper, and use CSS to tweak
             the resulting styling when needed
        -->
        <xsl:apply-templates />
      </a>
      <xsl:if test="$depth &lt;= $maxdepth and key(concat('hkey-',$depth),generate-id(.))">
        <ol class="toc toc-{$depth}">
          <xsl:for-each select="key(concat('hkey-',$depth),generate-id(.))">
            <xsl:call-template name="toc-recurse">
              <xsl:with-param name="depth" select="$depth + 1" />
              <xsl:with-param name="maxdepth" select="$maxdepth" />
            </xsl:call-template>
          </xsl:for-each>
        </ol>
      </xsl:if>
    </li>
  </xsl:template>

  <!--
      In addition to disabling normal copying of text in toc mode,
      also shortcut some common elements that can't reasonably contain
      h* nodes.
  -->
  <xsl:template mode="toc" match="p|pre|ul|dl|ol|table|@*|text()" />

  <!--
      When copying h* nodes in normal mode, fill in the id tags.

      Also interpose an otherwise inert <span> between the h* and its
      content, to avoid problems with blockification when using flex
      tricks to decorate the header.
  -->
  <xsl:template match="h1|h2|h3|h4|h5">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:attribute name="id">
        <xsl:apply-templates mode="toc-id" select="." />
      </xsl:attribute>
      <span class="hspan-{name()}">
        <xsl:apply-templates />
      </span>
    </xsl:copy>
  </xsl:template>

  <!--
      Default actions for normal (null) mode.
  -->

  <!-- don't copy the body node, just process children. -->
  <xsl:template match="body">
    <xsl:apply-templates />
  </xsl:template>

  <!-- copy attributes on copied nodes by default too. -->
  <xsl:template match="*|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <!--
      For elements in the head, drop some attributes selectively
  -->
  <xsl:template mode="head" match="script">
    <xsl:copy>
      <!-- if type is exactly text/javascript, skip it -->
      <xsl:apply-templates select="@*[not(name()='type' and string()='text/javascript')]" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <!-- copy attributes on copied nodes by default too. -->
  <xsl:template mode="head" match="*|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <!--
      Actual skeleton of generated document.
  -->

  <xsl:template match="/">
    <html lang="en">
      <head>
        <title>PL/Lua Documentation</title>
        <link rel="canonical" href="{$canon_url}" />
        <meta name="viewport" content="width=device-width" />
        <xsl:apply-templates mode="head" select="//head/style[not(@id='logo.css')]" />
        <xsl:apply-templates mode="head" select="//head/script" />
        <xsl:apply-templates mode="head" select="//head/style[@id='logo.css']" />
        <xsl:apply-templates mode="head" select="//head/link[@rel='icon']" />
      </head>
      <body>
        <div id="topContainer" class="mainsection"></div>
        <div id="logoContainer" class="maincolumn">
          <div id="logo"><a href="{$canon_site}"></a></div>
        </div>
        <main id="mainContainer" class="mainsection">
          <div id="headcontent" class="maincolumn">
            <h1 id="S0">Contents</h1>
            <nav>
              <ol id="toc" class="toc toc-1">
                <xsl:apply-templates mode="toc" select="//body//h1">
                  <xsl:with-param name="maxdepth" select="3" />
                </xsl:apply-templates>
              </ol>
            </nav>
          </div>
          <div id="bodycontent" class="maincolumn bodycontent">
            <xsl:apply-templates select="//body" />
          </div>
        </main>
        <footer id="footerContainer" class="mainsection">
          <xsl:apply-templates select="//body/div[@id='footer']">
            <xsl:with-param name="footer" select="true()" />
          </xsl:apply-templates>
        </footer>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>