about summary refs log tree commit diff homepage
path: root/gmi.xslt
diff options
context:
space:
mode:
authorNguyễn Gia Phong <cnx@loang.net>2023-10-13 18:03:08 +0900
committerNguyễn Gia Phong <cnx@loang.net>2023-10-13 18:03:08 +0900
commit4e036ed653e4f5ae35b172afc23860c425ea1a0d (patch)
tree6a8eec3e64c8e4185ae46422bfb94a91da02d282 /gmi.xslt
parent2189f8ae2f9849b7e473cfd76952908a01d359a7 (diff)
downloadsite-4e036ed653e4f5ae35b172afc23860c425ea1a0d.tar.gz
Use XSLT for gemtext
Diffstat (limited to 'gmi.xslt')
-rw-r--r--gmi.xslt88
1 files changed, 88 insertions, 0 deletions
diff --git a/gmi.xslt b/gmi.xslt
new file mode 100644
index 0000000..d52fab1
--- /dev/null
+++ b/gmi.xslt
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                xmlns:xhtml="http://www.w3.org/1999/xhtml">
+  <xsl:output method="text" encoding="UTF-8"/>
+  <xsl:variable name="root">https://loang.net</xsl:variable>
+  <xsl:template match="text()">
+    <xsl:value-of select="normalize-space()"/>
+  </xsl:template>
+
+  <xsl:template name="link">
+    <xsl:for-each select=".//xhtml:a">
+      <xsl:text>=> </xsl:text>
+      <xsl:if test="substring(@href, 1, 1) = '/'">
+        <xsl:value-of select="$root"/>
+      </xsl:if>
+      <xsl:value-of select="@href"/>
+      <xsl:text> </xsl:text>
+      <xsl:value-of select="."/>
+      <xsl:text>&#10;</xsl:text>
+    </xsl:for-each>
+    <xsl:text>&#10;</xsl:text>
+  </xsl:template>
+
+  <xsl:template match="xhtml:a">
+    <xsl:variable name="pre">
+      <xsl:value-of select="preceding::text()[1]"/>
+    </xsl:variable>
+    <xsl:choose>
+      <!-- consecutive anchors -->
+      <xsl:when test="$pre = ' ' or $pre = '&#10;'">
+        <xsl:text> </xsl:text>
+      </xsl:when>
+      <!-- whitespace noise -->
+      <xsl:when test="normalize-space($pre) = ''"/>
+      <!-- text ending with a whitespace -->
+      <xsl:when test="substring($pre, string-length($pre), 1) = ' '">
+        <xsl:text> </xsl:text>
+      </xsl:when>
+      <xsl:when test="substring($pre, string-length($pre), 1) = '&#10;'">
+        <xsl:text> </xsl:text>
+      </xsl:when>
+    </xsl:choose>
+    <xsl:value-of select="."/>
+    <xsl:variable name="suf">
+      <xsl:value-of select="substring(following::text()[1], 1, 1)"/>
+    </xsl:variable>
+    <xsl:if test="$suf = ' ' or $suf = '&#10;'">
+      <xsl:text> </xsl:text>
+    </xsl:if>
+  </xsl:template>
+
+  <xsl:template match="xhtml:h1">
+    <xsl:text># </xsl:text>
+    <xsl:value-of select="."/>
+    <xsl:text>&#10;&#10;</xsl:text>
+  </xsl:template>
+  <xsl:template match="xhtml:h2">
+    <xsl:text>## </xsl:text>
+    <xsl:value-of select="."/>
+    <xsl:text>&#10;&#10;</xsl:text>
+  </xsl:template>
+  <xsl:template match="xhtml:h3">
+    <xsl:text>### </xsl:text>
+    <xsl:value-of select="."/>
+    <xsl:text>&#10;&#10;</xsl:text>
+  </xsl:template>
+
+  <xsl:template match="xhtml:p">
+    <xsl:apply-templates select="text()|*"/>
+    <xsl:text>&#10;</xsl:text>
+    <xsl:call-template name="link"/>
+  </xsl:template>
+
+  <xsl:template match="xhtml:li">
+    <xsl:text>* </xsl:text>
+    <xsl:apply-templates select="text()|*"/>
+    <xsl:text>&#10;</xsl:text>
+  </xsl:template>
+  <xsl:template match="xhtml:ul">
+    <xsl:apply-templates select="*"/>
+    <xsl:call-template name="link"/>
+  </xsl:template>
+
+  <xsl:template match="xhtml:head"/>
+  <xsl:template match="xhtml:body">
+    <xsl:apply-templates select="*"/>
+  </xsl:template>
+</xsl:stylesheet>