]> Lady’s Gitweb - Shushe/blobdiff - parsers/tsv.xslt
Simplify two‐step build
[Shushe] / parsers / tsv.xslt
index 4de3b9853401f6b838ac84afb2bdf2fe121ac0c5..5b3cc684f6821c0e3a85f09e2637d7027c391c00 100644 (file)
@@ -1,8 +1,12 @@
 <?xml version="1.0"?>
 <!--
-⁌ ⛩️📰 书社 ∷ parsers/tsv.xslt
+SPDX-FileCopyrightText: 2023, 2024 Lady <https://www.ladys.computer/about/#lady>
+SPDX-License-Identifier: MPL-2.0
+-->
+<!--
+⁌ ⛩📰 书社 ∷ parsers/tsv.xslt
 
-© 2023 Lady [@ Lady’s Computer]
+© 2023–2024 Lady [@ Ladys Computer].
 
 This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
 If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
@@ -12,32 +16,72 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
        xmlns:exsl="http://exslt.org/common"
        xmlns:exslstr="http://exslt.org/strings"
        xmlns:html="http://www.w3.org/1999/xhtml"
-       exclude-result-prefixes="exsl exslstr"
+       xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
+       extension-element-prefixes="exsl exslstr"
        version="1.0"
 >
+       <import href="../lib/split.xslt"/>
+       <书社:id>urn:fdc:ladys.computer:20231231:Shu1She4:tsv.xslt</书社:id>
        <template match="html:script[@type='text/tab-separated-values']">
-               <variable name="rows" select="exslstr:tokenize(., '&#xA;')[normalize-space(.) and not(starts-with(., '#'))]"/>
-               <variable name="head" select="exsl:node-set($rows)[1]"/>
-               <variable name="body" select="exsl:node-set($rows)[not(position()=1)]"/>
+               <variable name="lines" select="exslstr:tokenize(., '&#xA;')"/>
+               <variable name="head" select="$lines[normalize-space(.)!='' and not(starts-with(., '#'))][1]"/>
+               <variable name="headcols">
+                       <call-template name="书社:split">
+                               <with-param name="source" select="string($head)"/>
+                               <with-param name="separator" select="'&#x9;'"/>
+                       </call-template>
+               </variable>
                <html:table>
+                       <for-each select="$lines[starts-with(., '#') and following-sibling::*[generate-id(.)=generate-id($head)]]">
+                               <comment>
+                                       <value-of select="substring-after(., '#')"/>
+                               </comment>
+                       </for-each>
                        <html:thead>
                                <html:tr>
-                                       <for-each select="exslstr:tokenize($head, '&#x9;')">
-                                               <html:th scope="row">
+                                       <for-each select="exsl:node-set($headcols)/*">
+                                               <html:th scope="col">
                                                        <value-of select="."/>
                                                </html:th>
                                        </for-each>
                                </html:tr>
                        </html:thead>
                        <html:tbody>
-                               <for-each select="exsl:node-set($body)">
-                                       <html:tr>
-                                               <for-each select="exslstr:tokenize(., '&#x9;')">
-                                                       <html:td>
-                                                               <value-of select="."/>
-                                                       </html:td>
-                                               </for-each>
-                                       </html:tr>
+                               <for-each select="$head/following-sibling::*[normalize-space(.)!='']">
+                                       <choose>
+                                               <when test="starts-with(., '#')">
+                                                       <comment>
+                                                               <value-of select="substring-after(., '#')"/>
+                                                       </comment>
+                                               </when>
+                                               <otherwise>
+                                                       <variable name="cols">
+                                                               <call-template name="书社:split">
+                                                                       <with-param name="source" select="string(.)"/>
+                                                                       <with-param name="separator" select="'&#x9;'"/>
+                                                               </call-template>
+                                                       </variable>
+                                                       <html:tr>
+                                                               <for-each select="exsl:node-set($cols)/*[count(exsl:node-set($headcols)/*)>=position()]">
+                                                                       <html:td>
+                                                                               <attribute name="data-tsv-header">
+                                                                                       <value-of select="exsl:node-set($headcols)/*[count(current()/preceding-sibling::*)+1]"/>
+                                                                               </attribute>
+                                                                               <value-of select="."/>
+                                                                               <if test="position()=count(exsl:node-set($headcols)/*)">
+                                                                                       <for-each select="following-sibling::*">
+                                                                                               <text>&#x9;</text>
+                                                                                               <value-of select="."/>
+                                                                                       </for-each>
+                                                                               </if>
+                                                                       </html:td>
+                                                               </for-each>
+                                                               <for-each select="exsl:node-set($headcols)/*[position()>count(exsl:node-set($cols)/*)]">
+                                                                       <html:td/>
+                                                               </for-each>
+                                                       </html:tr>
+                                               </otherwise>
+                                       </choose>
                                </for-each>
                        </html:tbody>
                </html:table>
This page took 0.053922 seconds and 4 git commands to generate.