ctucx.git: webmusic-nginx

nginx xslt-based index files optimized for music, inspired by https://git.ztn.sh/zotan/webmusic

commit ec45c22794519368755856cd7da249842bfe4633
parent 353f02e187d7539b0cd4811d0d59562d20de6c64
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 9 Mar 2021 14:55:11 +0100

improve xsl templates, use internal url-encode function
2 files changed, 20 insertions(+), 207 deletions(-)
M
webmusic-playlist.xslt
|
77
+++--------------------------------------------------------------------------
M
webmusic.xslt
|
150
+++++++++----------------------------------------------------------------------
diff --git a/webmusic-playlist.xslt b/webmusic-playlist.xslt
@@ -1,87 +1,16 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 
-<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:func="http://exslt.org/functions" xmlns:str="http://exslt.org/strings" version="1.0" extension-element-prefixes="func str">
 	<xsl:output method="text" indent="no" encoding="utf-8"/>
 
-	<xsl:variable name="hex" select="'0123456789ABCDEF'"/>
-	<xsl:variable name="ascii"> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>
-	<xsl:variable name="safe">/!()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~</xsl:variable>
-	<xsl:variable name="latin1">&#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;</xsl:variable>
-
-	<xsl:template name="decode">
-		<xsl:param name="str"/>
-
-		<xsl:choose>
-			<xsl:when test="contains($str,'%')">
-				<xsl:value-of select="substring-before($str,'%')"/>
-				<xsl:variable name="hexpair" select="translate(substring(substring-after($str,'%'),1,2),'abcdef','ABCDEF')"/>
-				<xsl:variable name="decimal" select="(string-length(substring-before($hex,substring($hexpair,1,1))))*16 + string-length(substring-before($hex,substring($hexpair,2,1)))"/>
-				<xsl:choose>
-					<xsl:when test="$decimal &lt; 127 and $decimal &gt; 31">
-						<xsl:value-of select="substring($ascii,$decimal - 31,1)"/>
-					</xsl:when>
-					<xsl:when test="$decimal &gt; 159">
-						<xsl:value-of select="substring($latin1,$decimal - 159,1)"/>
-					</xsl:when>
-					<xsl:otherwise>?</xsl:otherwise>
-				</xsl:choose>
-				<xsl:call-template name="decode">
-					<xsl:with-param name="str" select="substring(substring-after($str,'%'),3)"/>
-				</xsl:call-template>
-			</xsl:when>
-			<xsl:otherwise>
-				<xsl:value-of select="$str"/>
-			</xsl:otherwise>
-		</xsl:choose>
-	</xsl:template>
-
-
-	<xsl:template name="url-encode">
-		<xsl:param name="str"/>   
-		<xsl:if test="$str">
-			<xsl:variable name="first-char" select="substring($str,1,1)"/>
-			<xsl:choose>
-				<xsl:when test="contains($safe,$first-char)">
-					<xsl:value-of select="$first-char"/>
-				</xsl:when>
-				<xsl:otherwise>
-					<xsl:variable name="codepoint">
-						<xsl:choose>
-							<xsl:when test="contains($ascii,$first-char)">
-								<xsl:value-of select="string-length(substring-before($ascii,$first-char)) + 32"/>
-							</xsl:when>
-							<xsl:when test="contains($latin1,$first-char)">
-								<xsl:value-of select="string-length(substring-before($latin1,$first-char)) + 160"/>
-							</xsl:when>
-							<xsl:otherwise>
-								<xsl:message terminate="no">Warning: string contains a character that is out of range! Substituting "?".</xsl:message>
-								<xsl:text>63</xsl:text>
-							</xsl:otherwise>
-						</xsl:choose>
-					</xsl:variable>
-					<xsl:variable name="hex-digit1" select="substring($hex,floor($codepoint div 16) + 1,1)"/>
-					<xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
-					<xsl:value-of select="concat('%',$hex-digit1,$hex-digit2)"/>
-				</xsl:otherwise>
-			</xsl:choose>
-			<xsl:if test="string-length($str) &gt; 1">
-				<xsl:call-template name="url-encode">
-					<xsl:with-param name="str" select="substring($str,2)"/>
-				</xsl:call-template>
-			</xsl:if>
-		</xsl:if>
-	</xsl:template>
-
-
 	<xsl:template match="directory">
 	</xsl:template>
 
 	<xsl:template match="file">
 	<m3u>
 	<xsl:value-of select="$domain" />
-	<xsl:call-template name="url-encode">
-		<xsl:with-param name="str" select="concat($path, current())" />
-	</xsl:call-template>
+	<xsl:value-of select="$path" />
+	{str:encode-uri(current(),true())}
 	<xsl:text>&#xa;</xsl:text>
 	</m3u>
 	</xsl:template>
diff --git a/webmusic.xslt b/webmusic.xslt
@@ -1,127 +1,19 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 
-<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:func="http://exslt.org/functions" xmlns:str="http://exslt.org/strings" version="1.0" exclude-result-prefixes="xhtml" extension-element-prefixes="func str">
 	<xsl:output method="html" encoding="UTF-8" />
-
-	<xsl:variable name="hex" select="'0123456789ABCDEF'"/>
-	<xsl:variable name="ascii"> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>
-	<xsl:variable name="safe">/!()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~</xsl:variable>
-	<xsl:variable name="latin1">&#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;</xsl:variable>
-
-	<xsl:template name="decode">
-		<xsl:param name="str"/>
-
-		<xsl:choose>
-			<xsl:when test="contains($str,'%')">
-				<xsl:value-of select="substring-before($str,'%')"/>
-				<xsl:variable name="hexpair" select="translate(substring(substring-after($str,'%'),1,2),'abcdef','ABCDEF')"/>
-				<xsl:variable name="decimal" select="(string-length(substring-before($hex,substring($hexpair,1,1))))*16 + string-length(substring-before($hex,substring($hexpair,2,1)))"/>
-				<xsl:choose>
-					<xsl:when test="$decimal &lt; 127 and $decimal &gt; 31">
-						<xsl:value-of select="substring($ascii,$decimal - 31,1)"/>
-					</xsl:when>
-					<xsl:when test="$decimal &gt; 159">
-						<xsl:value-of select="substring($latin1,$decimal - 159,1)"/>
-					</xsl:when>
-					<xsl:otherwise>?</xsl:otherwise>
-				</xsl:choose>
-				<xsl:call-template name="decode">
-					<xsl:with-param name="str" select="substring(substring-after($str,'%'),3)"/>
-				</xsl:call-template>
-			</xsl:when>
-			<xsl:otherwise>
-				<xsl:value-of select="$str"/>
-			</xsl:otherwise>
-		</xsl:choose>
-	</xsl:template>
-
-
-	<xsl:template name="url-encode">
-		<xsl:param name="str"/>   
-		<xsl:if test="$str">
-			<xsl:variable name="first-char" select="substring($str,1,1)"/>
-			<xsl:choose>
-				<xsl:when test="contains($safe,$first-char)">
-					<xsl:value-of select="$first-char"/>
-				</xsl:when>
-				<xsl:otherwise>
-					<xsl:variable name="codepoint">
-						<xsl:choose>
-							<xsl:when test="contains($ascii,$first-char)">
-								<xsl:value-of select="string-length(substring-before($ascii,$first-char)) + 32"/>
-							</xsl:when>
-							<xsl:when test="contains($latin1,$first-char)">
-								<xsl:value-of select="string-length(substring-before($latin1,$first-char)) + 160"/>
-							</xsl:when>
-							<xsl:otherwise>
-								<xsl:message terminate="no">Warning: string contains a character that is out of range! Substituting "?".</xsl:message>
-								<xsl:text>63</xsl:text>
-							</xsl:otherwise>
-						</xsl:choose>
-					</xsl:variable>
-					<xsl:variable name="hex-digit1" select="substring($hex,floor($codepoint div 16) + 1,1)"/>
-					<xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
-					<xsl:value-of select="concat('%',$hex-digit1,$hex-digit2)"/>
-				</xsl:otherwise>
-			</xsl:choose>
-			<xsl:if test="string-length($str) &gt; 1">
-				<xsl:call-template name="url-encode">
-					<xsl:with-param name="str" select="substring($str,2)"/>
-				</xsl:call-template>
-			</xsl:if>
-		</xsl:if>
-	</xsl:template>
-
-
 	<xsl:template match="directory">
-	<xsl:element name="li">
-		<xsl:element name="a">
-			<xsl:attribute name="class">
-				<xsl:text>action</xsl:text>
-			</xsl:attribute>
-			<xsl:text>[--] </xsl:text>
-		</xsl:element>
-		<xsl:element name="a">
-			<xsl:attribute name="class">
-				<xsl:text>dir</xsl:text>
-			</xsl:attribute>
-			<xsl:attribute name="href">
-				<xsl:call-template name="url-encode">
-					<xsl:with-param name="str" select="concat($path, current(), '/')" />
-				</xsl:call-template>
-			</xsl:attribute>
-			<xsl:value-of select="." />
-		</xsl:element>
-	</xsl:element>
+	<li>
+		<a class="action"><xsl:text>[--] </xsl:text></a>
+		<a class="dir" href="{str:encode-uri(current(),true())}/"><xsl:value-of select="." /></a>
+	</li>
 	</xsl:template>
 
 	<xsl:template match="file">
-	<xsl:element name="li">
-		<xsl:element name="a">
-			<xsl:attribute name="class">
-				<xsl:text>action</xsl:text>
-			</xsl:attribute>
-			<xsl:attribute name="href">
-				<xsl:call-template name="url-encode">
-					<xsl:with-param name="str" select="concat($path, current())" />
-				</xsl:call-template>
-			</xsl:attribute>
-			<xsl:attribute name="download">
-			</xsl:attribute>
-			<xsl:text>[DL] </xsl:text>
-		</xsl:element>
-		<xsl:element name="a">
-			<xsl:attribute name="class">
-				<xsl:text>file</xsl:text>
-			</xsl:attribute>
-			<xsl:attribute name="href">
-				<xsl:call-template name="url-encode">
-					<xsl:with-param name="str" select="concat($path, current())" />
-				</xsl:call-template>
-			</xsl:attribute>
-			<xsl:value-of select="." />
-		</xsl:element>
-	</xsl:element>
+	<li>
+		<a class="action" href="{str:encode-uri(current(),true())}" download=""><xsl:text>[DL] </xsl:text></a>
+		<a class="file" href="{str:encode-uri(current(),true())}" download=""><xsl:value-of select="." /></a>
+	</li>
 	</xsl:template>
 
 	<xsl:template match="/">

@@ -159,22 +51,14 @@
 			<div id="container">
 				<h2><xsl:value-of select="$path"/><xsl:text> </xsl:text>
 				<span id="state"></span><xsl:text> </xsl:text><span id="flags">[<span id="repeatButton">R</span><span id="continuousButton">C</span>]</span></h2>
-				<a class="action-muted">[..]</a>
-				<a href=".." id="back" class="entry-muted cfont"> Go back</a>
-				<br/>
-				<a class="action-muted">[--]</a>
-				<xsl:element name="a">
-					<xsl:attribute name="class">
-						<xsl:text>entry-muted cfont</xsl:text>
-					</xsl:attribute>
-					<xsl:attribute name="href">
-						<xsl:call-template name="url-encode">
-							<xsl:with-param name="str" select="concat($path, 'playlist.m3u')" />
-						</xsl:call-template>
-					</xsl:attribute>
-					<xsl:text> Download playlist</xsl:text>
-				</xsl:element>
-				<br/>
+				<li>
+					<a class="action-muted">[..]</a>
+					<a href=".." id="back" class="entry-muted cfont"> Go back</a>
+				</li>
+				<li>
+					<a class="action-muted">[--]</a>
+					<a class="entry-muted cfont" href="playlist.m3u"><xsl:text> Download playlist</xsl:text></a>
+				</li>
 				<br/>
 				<xsl:apply-templates />
 			</div>