itsb/xslt/itsb.xsl

210 lines
6.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://tilde.town/~lucidiot/itsb/"
xmlns:itsb="http://tilde.town/~lucidiot/itsb/">
<xsl:output method="xml" />
<!-- Preprocesses the itsb.xml file to autocomplete with converted feeds. -->
<!-- Caution: Final slash is required. -->
<xsl:variable name="itsbRoot" select="'https://tilde.town/~lucidiot/itsb/'" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="itsb:feed">
<xsl:copy-of select="." />
<!-- Store attributes in variables to make them accessible in conditionals later on -->
<xsl:variable name="lang" select="@lang" />
<xsl:variable name="type" select="@type" />
<!-- This is an Atom feed and there is no RSS feed: add a conversion -->
<xsl:choose>
<xsl:when test="@format = 'atom' and not(../itsb:feed[((not(@lang) and not($lang)) or @lang = $lang) and ((not(@type) and not($type)) or @type = $type) and (@format = 'rss' or @format = 'rdf')])">
<feed format="rss">
<xsl:if test="@lang">
<xsl:attribute name="lang">
<xsl:value-of select="@lang" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@type">
<xsl:attribute name="type">
<xsl:value-of select="@type" />
</xsl:attribute>
</xsl:if>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
<xsl:text>-rss</xsl:text>
</xsl:attribute>
<xsl:choose>
<xsl:when test="itsb:output">
<shell>
<xsl:text>cat $DIR/feeds/</xsl:text>
<xsl:value-of select="itsb:output/text()" />
</shell>
</xsl:when>
<xsl:otherwise>
<curl>
<url>
<xsl:value-of select="itsb:link/text()" />
</url>
<!-- Some official feeds do not work with curl's user agent -->
<header name="User-Agent">ITSB/1.0</header>
</curl>
</xsl:otherwise>
</xsl:choose>
<xml2json />
<jq path="convert/atom2rss.jq" />
<json2xml />
<output>
<xsl:text>_rss/</xsl:text>
<xsl:choose>
<xsl:when test="itsb:output">
<xsl:value-of select="itsb:output/text()" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@id" />
<xsl:text>.xml</xsl:text>
</xsl:otherwise>
</xsl:choose>
</output>
</feed>
<!-- Perform all the other conversions -->
<xsl:call-template name="convertall" />
</xsl:when>
<xsl:when test="@format = 'rss'">
<xsl:call-template name="convertall" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="convertall">
<xsl:call-template name="converter">
<xsl:with-param name="format" select="'json'" />
<xsl:with-param name="extension" select="'json'" />
</xsl:call-template>
<xsl:call-template name="converter">
<xsl:with-param name="format" select="'cdf'" />
<xsl:with-param name="extension" select="'cdf'" />
</xsl:call-template>
<xsl:call-template name="converter">
<xsl:with-param name="format" select="'echo'" />
<xsl:with-param name="extension" select="'xml'" />
</xsl:call-template>
<xsl:call-template name="converter">
<xsl:with-param name="format" select="'rss3'" />
<xsl:with-param name="extension" select="'txt'" />
</xsl:call-template>
</xsl:template>
<xsl:template name="converter">
<xsl:param name="format" />
<xsl:param name="extension" />
<feed format="{$format}">
<xsl:if test="@lang">
<xsl:attribute name="lang">
<xsl:value-of select="@lang" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@type">
<xsl:attribute name="type">
<xsl:value-of select="@type" />
</xsl:attribute>
</xsl:if>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
<xsl:text>-</xsl:text>
<xsl:value-of select="$format" />
</xsl:attribute>
<!--
For custom feeds, grab the previously generated feed locally.
For official RSS feeds, grab the feed online.
For official Atom feeds, grab the converted feed locally.
-->
<xsl:choose>
<xsl:when test="itsb:output or (@format = 'atom' and $format != 'json')">
<shell>
<xsl:text>cat $DIR/feeds/</xsl:text>
<xsl:choose>
<xsl:when test="@format = 'atom' and $format != 'json'">
<xsl:text>_rss/</xsl:text>
<xsl:value-of select="@id" />
<xsl:text>.xml</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="itsb:output/text()" />
</xsl:otherwise>
</xsl:choose>
</shell>
</xsl:when>
<xsl:otherwise>
<curl>
<url>
<xsl:value-of select="itsb:link/text()" />
</url>
<!-- Some official feeds do not work with curl's user agent -->
<header name="User-Agent">ITSB/1.0</header>
</curl>
</xsl:otherwise>
</xsl:choose>
<xml2json />
<jq>
<xsl:attribute name="path">
<xsl:text>convert/</xsl:text>
<xsl:choose>
<xsl:when test="$format = 'json'">
<xsl:value-of select="@format" />
<xsl:text>2jsonfeed.jq</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>rss2</xsl:text>
<xsl:value-of select="$format" />
<xsl:text>.jq</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="$format = 'json'">
<arg name="feed_url">
<xsl:value-of select="$itsbRoot" />
<xsl:text>feeds/_json/</xsl:text>
<xsl:value-of select="@id" />
<xsl:text>.json</xsl:text>
</arg>
</xsl:if>
<xsl:if test="$format = 'rss3'">
<xsl:attribute name="raw-output">true</xsl:attribute>
</xsl:if>
</jq>
<xsl:if test="$format != 'json' and $format != 'rss3'">
<json2xml short-empty="true" />
</xsl:if>
<output>
<xsl:text>_</xsl:text>
<xsl:value-of select="$format" />
<xsl:text>/</xsl:text>
<xsl:value-of select="@id" />
<xsl:text>.</xsl:text>
<xsl:value-of select="$extension" />
</output>
</feed>
</xsl:template>
</xsl:stylesheet>