Skip to content

Commit

Permalink
2024.46.2
Browse files Browse the repository at this point in the history
- Added TSV XSLT
- Combined table separation into XSL
  • Loading branch information
simon-techkid committed May 25, 2024
1 parent 7291127 commit f6a7ba5
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
12 changes: 12 additions & 0 deletions SpotifyGPX/Xspf/csv.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:include href="extensions.xsl"/>
<xsl:include href="tableextensions.xsl"/>
<xsl:include href="separatedvaluestable.xsl"/>
<xsl:output method="html" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes" media-type="text/html"/>

<!-- TSV column delimiter -->
<xsl:variable name="delimiter" select="'&quot;&#44;&quot;'"/>

</xsl:stylesheet>
68 changes: 68 additions & 0 deletions SpotifyGPX/Xspf/extensions.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- New line character for description line breaks -->
<xsl:variable name="newLine" select="'&#xA;'"/>
<xsl:variable name="stylesheet" select="'styles.css'"/>

<!-- Template to create the doctype -->
<xsl:template name="doctype">
<xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html&gt;</xsl:text>
<xsl:value-of select="$newLine"/>
</xsl:template>

<!-- Template to create the head section of the HTML -->
<xsl:template name="html_head_template">
<xsl:param name="title" />
<head>
<title><xsl:value-of select="$title"/></title>
<link rel="stylesheet" href="{$stylesheet}" />
</head>
</xsl:template>

<!-- Template to recursively replace CRLF with <br> -->
<xsl:template name="replace-newline">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, $newLine)">
<xsl:value-of select="substring-before($text, $newLine)"/>
<br/>
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring-after($text, $newLine)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- Template to handle hyperlink generation -->
<xsl:template name="handleHyperlink">
<xsl:param name="string"/>
<xsl:param name="displayAs"/>
<a href="{$string}"><xsl:value-of select="$displayAs"/></a>
</xsl:template>

<!-- Template to calculate total duration -->
<xsl:template name="totalDuration">
<xsl:param name="durations" />
<xsl:variable name="totalMilliseconds" select="sum($durations)"/>
<xsl:call-template name="formatDuration">
<xsl:with-param name="milliseconds" select="$totalMilliseconds"/>
</xsl:call-template>
</xsl:template>

<!-- Template to format milliseconds into HH:MM:SS -->
<xsl:template name="formatDuration">
<xsl:param name="milliseconds" />
<xsl:variable name="hours" select="floor($milliseconds div 3600000)" />
<xsl:variable name="minutes" select="floor(($milliseconds mod 3600000) div 60000)" />
<xsl:variable name="seconds" select="floor(($milliseconds mod 60000) div 1000)" />
<xsl:variable name="formattedH" select="format-number($hours, '00')" />
<xsl:variable name="formattedM" select="format-number($minutes mod 60, '00')" />
<xsl:variable name="formattedS" select="format-number($seconds mod 60, '00')" />
<xsl:value-of select="concat($formattedH, ':', $formattedM, ':', $formattedS)"/>
</xsl:template>

</xsl:stylesheet>
106 changes: 106 additions & 0 deletions SpotifyGPX/Xspf/separatedvaluestable.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Template for the root element -->
<xsl:template match="/">
<xsl:variable name="type" select="'Pairs Table'"/>
<xsl:variable name="header" select="$type"/>

<xsl:call-template name="doctype"/>
<html>
<xsl:call-template name="html_head_template">
<xsl:with-param name="title" select="$header"/>
</xsl:call-template>
<xsl:call-template name="html_body_template">
<xsl:with-param name="header" select="$header"/>
<xsl:with-param name="lines" select="//Line"/>
</xsl:call-template>
</html>
</xsl:template>

<!-- Template for creating the HTML body -->
<xsl:template name="html_body_template">
<xsl:param name="header"/>
<xsl:param name="lines"/>
<body>
<h1><xsl:value-of select="$header"/></h1>
<hr/>
<xsl:call-template name="table">
<xsl:with-param name="lines" select="$lines"/>
</xsl:call-template>
<hr/>
</body>
</xsl:template>

<!-- Template for creating the table and underlying rows -->
<xsl:template name="table">
<xsl:param name="lines"/>
<table>
<!-- Apply template to process header line -->
<tr>
<xsl:call-template name="processLine">
<xsl:with-param name="line" select="$lines[1]"/>
<xsl:with-param name="isHeader" select="true()"/>
</xsl:call-template>
</tr>
<!-- Apply templates to process non-header lines -->
<xsl:apply-templates select="$lines[position() > 1]"/>
</table>
</xsl:template>

<!-- Template to process each line -->
<xsl:template match="Line">
<tr>
<!-- Call template to process the line -->
<xsl:call-template name="processLine">
<xsl:with-param name="line" select="."/>
<xsl:with-param name="isHeader" select="false()"/>
</xsl:call-template>
</tr>
</xsl:template>

<!-- Template to split the contents of a line -->
<xsl:template name="processLine">
<xsl:param name="line"/>
<xsl:param name="isHeader"/>
<!-- Split the line by the delimiter -->
<xsl:call-template name="split">
<xsl:with-param name="string" select="$line"/>
<xsl:with-param name="isHeader" select="$isHeader"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="split">
<xsl:param name="string" />
<xsl:param name="isHeader"/>
<xsl:choose>
<!-- If the string contains a delimiter character -->
<xsl:when test="contains($string, $delimiter)">
<!-- Extract the value before the delimiter character -->
<xsl:variable name="value" select="substring-before($string, $delimiter)" />

<!-- Process the extracted value -->
<xsl:call-template name="process-value">
<xsl:with-param name="isHeader" select="$isHeader"/>
<xsl:with-param name="value" select="$value" />
</xsl:call-template>

<!-- Recursively call split template with the remaining string -->
<xsl:variable name="remaining" select="substring-after($string, $delimiter)" />
<xsl:call-template name="split">
<xsl:with-param name="string" select="$remaining"/>
<xsl:with-param name="isHeader" select="$isHeader"/>
</xsl:call-template>
</xsl:when>
<!-- If no delimiter character is found -->
<xsl:otherwise>
<!-- Process the remaining part of the string -->
<xsl:call-template name="process-value">
<xsl:with-param name="isHeader" select="$isHeader"/>
<xsl:with-param name="value" select="$string" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
66 changes: 66 additions & 0 deletions SpotifyGPX/Xspf/tableextensions.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Should encapsulating quotes be removed from table cells? -->
<xsl:variable name="removeQuotes" select="true()"/>

<!-- What does an encapsulating quote look like? -->
<xsl:variable name="quotes" select="'&quot;'"/>

<!-- Template to create table headers or table rows and remove encapsulating quotes -->
<xsl:template name="process-value">
<xsl:param name="value"/>
<xsl:param name="isHeader"/>

<xsl:variable name="string">
<xsl:choose>
<xsl:when test="$removeQuotes = true()">
<xsl:call-template name="removeQuotes">
<xsl:with-param name="string" select="$value"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$removeQuotes = false()">
<xsl:value-of select="$value"/>
</xsl:when>
</xsl:choose>
</xsl:variable>

<xsl:choose>
<xsl:when test="$isHeader = true()">
<th><xsl:value-of select="$string"/></th>
</xsl:when>
<xsl:when test="$isHeader = false()">
<td><xsl:value-of select="$string"/></td>
</xsl:when>
</xsl:choose>
</xsl:template>

<!-- Template to remove encapsulating quotes from a given string -->
<xsl:template name="removeQuotes">
<xsl:param name="string"/>
<xsl:variable name="startQuote" select="substring($string, 1, 1) = $quotes"/>
<xsl:variable name="endQuote" select="substring($string, string-length($string), 1) = $quotes"/>
<xsl:variable name="startRemoved">
<xsl:choose>
<xsl:when test="$startQuote">
<xsl:value-of select="substring($string, 2)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="bothRemoved">
<xsl:choose>
<xsl:when test="$endQuote">
<xsl:value-of select="substring($startRemoved, 1, string-length($startRemoved) - 1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$startRemoved"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$bothRemoved"/>
</xsl:template>

</xsl:stylesheet>
12 changes: 12 additions & 0 deletions SpotifyGPX/Xspf/tsv.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:include href="extensions.xsl"/>
<xsl:include href="tableextensions.xsl"/>
<xsl:include href="separatedvaluestable.xsl"/>
<xsl:output method="html" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes" media-type="text/html"/>

<!-- TSV column delimiter -->
<xsl:variable name="delimiter" select="'&#9;'"/>

</xsl:stylesheet>

0 comments on commit f6a7ba5

Please sign in to comment.