Echo support, closes #61
This commit is contained in:
parent
0a401bd6c8
commit
8bff8f3ca6
Binary file not shown.
After Width: | Height: | Size: 932 B |
|
@ -0,0 +1,71 @@
|
|||
# RSS->Echo converter
|
||||
# Expects xmltodict JSON output as input, outputs an Echo feed
|
||||
|
||||
def ensure_string: if type == "array" then .[0] else . end | if type == "object" then .["#text"] else . end;
|
||||
def ensure_array: (. // []) | if type == "array" then . else [.] end;
|
||||
def parse_date: ensure_string | strptime("%a, %d %b %Y %T %Z") | mktime | todateiso8601;
|
||||
|
||||
{"feed": (
|
||||
.rss.channel | . as $channel | {
|
||||
"@xmlns": "http://purl.org/echo/",
|
||||
"@version": "1.0",
|
||||
"title": .title,
|
||||
"summary": .description,
|
||||
"link": [
|
||||
{
|
||||
"@rel": "alternate",
|
||||
"@type": "text/html",
|
||||
"@href": (.link | ensure_string),
|
||||
}
|
||||
],
|
||||
"entry": [
|
||||
.item | ensure_array[] | . as $item | {
|
||||
"@id": (.guid // .link // .title | ensure_string),
|
||||
"title": (.title | ensure_string),
|
||||
"link": []
|
||||
}
|
||||
| if $item.author then .author = {"name": ($item.author | ensure_string)} else . end
|
||||
| if $item.pubDate then
|
||||
.issued = ($item.pubDate | parse_date)
|
||||
| .created = .issued
|
||||
| .modified = .issued
|
||||
else . end
|
||||
| if $item.link then .link += [{
|
||||
"@rel": "alternate",
|
||||
"@type": "text/html",
|
||||
"@href": ($item.link | ensure_string)
|
||||
}] else . end
|
||||
| if $item.source then .link += [{
|
||||
"@rel": "source",
|
||||
"@type": "text/html",
|
||||
"@href": ($item.source | ensure_string)
|
||||
}] else . end
|
||||
| if $item.description then
|
||||
.content = {
|
||||
"@type": "text/html",
|
||||
"#text": ($item.description | ensure_string)
|
||||
}
|
||||
else . end
|
||||
| if $item.enclosure then
|
||||
. as $entry | .content = (
|
||||
{
|
||||
"@type": $item.enclosure["@type"],
|
||||
"@href": $item.enclosure["@url"]
|
||||
}
|
||||
# Wrap in a multipart if the entry already has a content
|
||||
| if $entry.content then {
|
||||
"@type": "multipart/related",
|
||||
"content": [$entry.content, .]
|
||||
} else . end
|
||||
)
|
||||
else . end
|
||||
]
|
||||
}
|
||||
| if $channel.language then .["@xml:lang"] = ($channel.language | ensure_string) else . end
|
||||
| if $channel.image then .link += [{
|
||||
"@rel": "logo",
|
||||
# RSS does not provide an image's mime type, so we try to guess
|
||||
"@type": ("image/" + ($channel.image.url | capture("\\.(?<ext>[a-zA-Z0-9]+)$").ext // "x-unknown")),
|
||||
"@href": $channel.image.url
|
||||
}] else . end
|
||||
)}
|
123
xslt/itsb.xsl
123
xslt/itsb.xsl
|
@ -77,74 +77,36 @@
|
|||
</output>
|
||||
</feed>
|
||||
|
||||
<xsl:call-template name="jsonfeed" />
|
||||
<xsl:call-template name="cdf" />
|
||||
<!-- Perform all the other conversions -->
|
||||
<xsl:call-template name="convertall" />
|
||||
</xsl:when>
|
||||
|
||||
<xsl:when test="@format = 'rss'">
|
||||
<xsl:call-template name="jsonfeed" />
|
||||
<xsl:call-template name="cdf" />
|
||||
<xsl:call-template name="convertall" />
|
||||
</xsl:when>
|
||||
</xsl:choose>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template name="jsonfeed">
|
||||
<feed format="json">
|
||||
<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>-json</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>
|
||||
</curl>
|
||||
</xsl:otherwise>
|
||||
</xsl:choose>
|
||||
|
||||
<xml2json />
|
||||
<jq>
|
||||
<xsl:attribute name="path">
|
||||
<xsl:text>convert/</xsl:text>
|
||||
<xsl:value-of select="@format" />
|
||||
<xsl:text>2jsonfeed.jq</xsl:text>
|
||||
</xsl:attribute>
|
||||
<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>
|
||||
</jq>
|
||||
<output>
|
||||
<xsl:text>_json/</xsl:text>
|
||||
<xsl:value-of select="@id" />
|
||||
<xsl:text>.json</xsl:text>
|
||||
</output>
|
||||
</feed>
|
||||
<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:template>
|
||||
|
||||
<xsl:template name="converter">
|
||||
<xsl:param name="format" />
|
||||
<xsl:param name="extension" />
|
||||
|
||||
<xsl:template name="cdf">
|
||||
<feed format="cdf">
|
||||
<feed format="{$format}">
|
||||
<xsl:if test="@lang">
|
||||
<xsl:attribute name="lang">
|
||||
<xsl:value-of select="@lang" />
|
||||
|
@ -157,7 +119,8 @@
|
|||
</xsl:if>
|
||||
<xsl:attribute name="id">
|
||||
<xsl:value-of select="@id" />
|
||||
<xsl:text>-cdf</xsl:text>
|
||||
<xsl:text>-</xsl:text>
|
||||
<xsl:value-of select="$format" />
|
||||
</xsl:attribute>
|
||||
|
||||
<xsl:choose>
|
||||
|
@ -165,7 +128,7 @@
|
|||
<shell>
|
||||
<xsl:text>cat $DIR/feeds/</xsl:text>
|
||||
<xsl:choose>
|
||||
<xsl:when test="@format = 'atom'">
|
||||
<xsl:when test="@format = 'atom' and $format != 'json'">
|
||||
<xsl:text>_rss/</xsl:text>
|
||||
<xsl:value-of select="@id" />
|
||||
<xsl:text>.xml</xsl:text>
|
||||
|
@ -186,11 +149,43 @@
|
|||
</xsl:choose>
|
||||
|
||||
<xml2json />
|
||||
<jq path="convert/rss2cdf.jq" />
|
||||
<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>
|
||||
</jq>
|
||||
|
||||
<xsl:if test="$format != 'json'">
|
||||
<json2xml short-empty="true" />
|
||||
</xsl:if>
|
||||
|
||||
<output>
|
||||
<xsl:text>_cdf/</xsl:text>
|
||||
<xsl:text>_</xsl:text>
|
||||
<xsl:value-of select="$format" />
|
||||
<xsl:text>/</xsl:text>
|
||||
<xsl:value-of select="@id" />
|
||||
<xsl:text>.cdf</xsl:text>
|
||||
<xsl:text>.</xsl:text>
|
||||
<xsl:value-of select="$extension" />
|
||||
</output>
|
||||
</feed>
|
||||
</xsl:template>
|
||||
|
|
Loading…
Reference in New Issue