about summary refs log tree commit diff homepage
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
parent2189f8ae2f9849b7e473cfd76952908a01d359a7 (diff)
downloadsite-4e036ed653e4f5ae35b172afc23860c425ea1a0d.tar.gz
Use XSLT for gemtext
-rw-r--r--Makefile10
-rw-r--r--README.md2
-rw-r--r--gmi.xslt88
-rw-r--r--shell.nix2
4 files changed, 95 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index d865ae7..ae906eb 100644
--- a/Makefile
+++ b/Makefile
@@ -9,15 +9,15 @@ all: gemini www
 %/:
 	mkdir $@
 
-gemini/%.gmi: %.md
-	md2gemini --links=paragraph < $< > $@
+gemini/%.gmi: www/%.xhtml gemini/
+	xsltproc gmi.xslt $< > $@
 
-gemini: gemini/ $(GMI)
+gemini: $(GMI)
 
-www/%.xhtml: %.md
+www/%.xhtml: %.md www/
 	makepage < $< | xsltproc xhtml.xslt - > $@
 
-www: www/ $(XHTML)
+www: $(XHTML)
 
 clean:
 	rm -f $(GMI) $(XHTML)
diff --git a/README.md b/README.md
index a7faf30..19a4288 100644
--- a/README.md
+++ b/README.md
@@ -68,7 +68,7 @@ data input by users are stored in as-is;
 is strongly recommended
 
 admins neither parse nor read user data,
-but they have no power regarding physical access
+but they are not the one with physical access
 
 ## joining
 
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>
diff --git a/shell.nix b/shell.nix
index bbadc58..715cd8c 100644
--- a/shell.nix
+++ b/shell.nix
@@ -1,4 +1,4 @@
 with import <nixpkgs> {};
 mkShell {
-  nativeBuildInputs = [ discount libxslt md2gemini ];
+  nativeBuildInputs = [ discount libxslt ];
 }