X-Git-Url: https://git.ladys.computer/Shushe/blobdiff_plain/2024d501192c7399c0791dec5ba0d3004613aefc..15648b660cd14e2834629ad52b65ceb313362c9b:/parsers/tsv.xslt

diff --git a/parsers/tsv.xslt b/parsers/tsv.xslt
index 2597060..90e156e 100644
--- a/parsers/tsv.xslt
+++ b/parsers/tsv.xslt
@@ -4,70 +4,92 @@ SPDX-FileCopyrightText: 2023, 2024 Lady <https://www.ladys.computer/about/#lady>
 SPDX-License-Identifier: MPL-2.0
 -->
 <!--
-⁌ ⛩️📰 书社 ∷ parsers/tsv.xslt
+⁌ ⛩📰 书社 ∷ parsers/tsv.xslt
 
-© 2023–2024 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/>.
 -->
+<!DOCTYPE transform [
+	<!ENTITY xhtml 'http://www.w3.org/1999/xhtml'>
+]>
 <transform
 	xmlns="http://www.w3.org/1999/XSL/Transform"
 	xmlns:exsl="http://exslt.org/common"
 	xmlns:exslstr="http://exslt.org/strings"
-	xmlns:html="http://www.w3.org/1999/xhtml"
+	xmlns:html="&xhtml;"
 	xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
-	exclude-result-prefixes="exsl exslstr"
+	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="$rows[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>
-		<variable name="body" select="$rows[not(position()=1)]"/>
-		<html:table>
-			<html:thead>
-				<html:tr>
+		<element name="table" namespace="&xhtml;">
+			<for-each select="$lines[starts-with(., '#') and following-sibling::*[generate-id(.)=generate-id($head)]]">
+				<comment>
+					<value-of select="substring-after(., '#')"/>
+				</comment>
+			</for-each>
+			<element name="thead" namespace="&xhtml;">
+				<element name="tr" namespace="&xhtml;">
 					<for-each select="exsl:node-set($headcols)/*">
-						<html:th scope="col">
+						<element name="th" namespace="&xhtml;">
+							<attribute name="scope">
+								<text>col</text>
+							</attribute>
 							<value-of select="."/>
-						</html:th>
+						</element>
 					</for-each>
-				</html:tr>
-			</html:thead>
-			<html:tbody>
-				<for-each select="$body">
-					<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>
-								<value-of select="."/>
-								<if test="position()=count(exsl:node-set($headcols)/*)">
-									<for-each select="following-sibling:*">
-										<text>&#x9;</text>
+				</element>
+			</element>
+			<element name="tbody" namespace="&xhtml;">
+				<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>
+							<element name="tr" namespace="&xhtml;">
+								<for-each select="exsl:node-set($cols)/*[count(exsl:node-set($headcols)/*)>=position()]">
+									<element name="td" namespace="&xhtml;">
+										<attribute name="data-tsv-header">
+											<value-of select="exsl:node-set($headcols)/*[count(current()/preceding-sibling::*)+1]"/>
+										</attribute>
 										<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>
+										<if test="position()=count(exsl:node-set($headcols)/*)">
+											<for-each select="following-sibling::*">
+												<text>&#x9;</text>
+												<value-of select="."/>
+											</for-each>
+										</if>
+									</element>
+								</for-each>
+								<for-each select="exsl:node-set($headcols)/*[position()>count(exsl:node-set($cols)/*)]">
+									<element name="td" namespace="&xhtml;"/>
+								</for-each>
+							</element>
+						</otherwise>
+					</choose>
 				</for-each>
-			</html:tbody>
-		</html:table>
+			</element>
+		</element>
 	</template>
 </transform>