Skip to content

Commit

Permalink
Support local asset copying, rooted build trees, asset deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
David Sheets committed Aug 31, 2012
1 parent a254051 commit 921e790
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 51 deletions.
67 changes: 40 additions & 27 deletions demo-page.xsl
Expand Up @@ -55,16 +55,39 @@
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="link" mode="manifest">
<xsl:if test="@rel!='start'">
<link>
<xsl:copy-of select="@*[local-name(.)!='data-src']" />
</link>
</xsl:if>

<xsl:template name="prefix-href">
<xsl:param name="root" />
<xsl:attribute name="{local-name()}">
<xsl:choose>
<xsl:when test="substring(.,1,4)='http'">
<xsl:copy />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($root,.)" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
<xsl:template match="script" mode="manifest">
<xsl:copy-of select="." />

<xsl:template match="@data-local" mode="manifest" />
<xsl:template match="@data-src" mode="manifest" />
<xsl:template match="@src | @href" mode="manifest">
<xsl:param name="root" />
<xsl:call-template name="prefix-href">
<xsl:with-param name="root" select="$root" />
</xsl:call-template>
</xsl:template>
<xsl:template match="@*" mode="manifest"><xsl:copy /></xsl:template>

<xsl:template match="link[@rel='start']" mode="manifest" />
<xsl:template match="link|script" mode="manifest">
<xsl:param name="root" />
<xsl:copy>
<xsl:apply-templates select="@*" mode="manifest">
<xsl:with-param name="root" select="$root" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="@*|node()" mode="article">
Expand All @@ -78,27 +101,15 @@
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="script/@src" mode="article">
<xsl:template match="img/@src | script/@src" mode="article">
<xsl:param name="root" />
<xsl:attribute name="src">
<xsl:value-of select="concat($root,.)" />
</xsl:attribute>
</xsl:template>
<xsl:template match="img/@src" mode="article">
<xsl:param name="root" />
<xsl:attribute name="src">
<xsl:choose>
<xsl:when test="substring(.,1,4)='http'">
<xsl:copy />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($root,.)" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:call-template name="prefix-href">
<xsl:with-param name="root" select="$root" />
</xsl:call-template>
</xsl:template>

<xsl:template match="manifest">
<xsl:param name="reldir" select="$reldir" />
<xsl:variable name="root">
<xsl:call-template name="dirname">
<xsl:with-param name="reldir" select="$reldir" />
Expand All @@ -113,7 +124,9 @@
<xsl:copy-of select="." />
</xsl:for-each>

<xsl:apply-templates select="link | script" mode="manifest" />
<xsl:apply-templates select="link | script" mode="manifest">
<xsl:with-param name="root" select="$root" />
</xsl:apply-templates>

<xsl:copy-of select="$exprs/head/*" />
</head>
Expand Down
81 changes: 64 additions & 17 deletions local-assets.xsl
Expand Up @@ -4,30 +4,77 @@
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="path-utils.xsl" />
<xsl:output method="text" indent="no" />

<xsl:param name="action" select="'create'" />

<xsl:template match="/">
<xsl:text>#!/bin/sh&#xA;&#xA;</xsl:text>
<xsl:apply-templates mode="http" />
<xsl:apply-templates mode="remote" />
<xsl:apply-templates mode="local" />
</xsl:template>

<xsl:template match="text()" mode="http">
<xsl:template match="text()" mode="remote">
<xsl:text>&#xA;</xsl:text>
</xsl:template>
<xsl:template match="text()" mode="local">
<xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template match="link[@data-src]" mode="http">
<xsl:variable name="prefix">
<xsl:if test="//head/@data-src-prefix">
<xsl:value-of select="//head/@data-src-prefix" />
</xsl:if>
<xsl:if test="/manifest/@data-src-prefix">
<xsl:value-of select="/manifest/@data-src-prefix" />
</xsl:if>
</xsl:variable>
<xsl:text>curl </xsl:text>
<xsl:value-of select="concat($prefix,@data-src)" />
<xsl:text> --create-dirs -o </xsl:text>
<xsl:value-of select="@href" />
<xsl:text> -z </xsl:text>
<xsl:value-of select="@href" />
<xsl:template match="link[@data-src]" mode="remote">
<xsl:if test="$action='create'">
<xsl:variable name="prefix">
<xsl:if test="//head/@data-src-prefix">
<xsl:value-of select="//head/@data-src-prefix" />
</xsl:if>
<xsl:if test="/manifest/@data-src-prefix">
<xsl:value-of select="/manifest/@data-src-prefix" />
</xsl:if>
</xsl:variable>
<xsl:text>curl </xsl:text>
<xsl:value-of select="concat($prefix,@data-src)" />
<xsl:text> --create-dirs -o </xsl:text>
<xsl:value-of select="@href" />
<xsl:text> -z </xsl:text>
<xsl:value-of select="@href" />
<xsl:text>&#xA;</xsl:text>
</xsl:if>
</xsl:template>

<xsl:template name="local-fs">
<xsl:param name="path" />
<xsl:if test="$action='create'">
<xsl:text>mkdir -p `dirname </xsl:text>
<xsl:value-of select="$path" />
<xsl:text>`&#xA;</xsl:text>
<xsl:text>cp -f </xsl:text>
<xsl:value-of select="@data-local" />
<xsl:text> </xsl:text>
<xsl:value-of select="$path" />
<xsl:text>&#xA;</xsl:text>
</xsl:if>
<xsl:if test="$action='delete'">
<xsl:text>rm -rfv </xsl:text>
<xsl:choose>
<xsl:when test="contains($path,'/')">
<xsl:value-of select="substring-before($path,'/')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$path" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>

<xsl:template match="link[@data-local]" mode="local">
<xsl:call-template name="local-fs">
<xsl:with-param name="path" select="@href" />
</xsl:call-template>
</xsl:template>

<xsl:template match="script[@data-local]" mode="local">
<xsl:call-template name="local-fs">
<xsl:with-param name="path" select="@src" />
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
52 changes: 45 additions & 7 deletions static-manifest.xsl
Expand Up @@ -4,6 +4,8 @@
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="path-utils.xsl" />

<xsl:param name="rooted" select="false()" />

<xsl:template match="/">
<manifest>
<xsl:apply-templates />
Expand Down Expand Up @@ -31,32 +33,68 @@
<xsl:choose>
<xsl:when test="@rel='start' and $root != ''" />
<xsl:otherwise>
<link href="{concat($root,@href)}">
<xsl:apply-templates select="@*" />
<link>
<xsl:apply-templates select="@*">
<xsl:with-param name="root" select="$root" />
</xsl:apply-templates>
</link>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="head//script[@src] | script[@src]">
<xsl:param name="root" />
<script src="{concat($root,@src)}">
<xsl:apply-templates select="@*" />
<script>
<xsl:apply-templates select="@*">
<xsl:with-param name="root" select="$root" />
</xsl:apply-templates>
</script>
</xsl:template>

<xsl:template match="body//script[@src]">
<xsl:param name="root" />
<link rel="tag" href="{concat($root,@src)}">
<xsl:apply-templates select="@type" />
<link rel="tag">
<xsl:apply-templates select="@src | @type">
<xsl:with-param name="root" select="$root" />
<xsl:with-param name="attr" select="'href'" />
</xsl:apply-templates>
</link>
</xsl:template>

<xsl:template match="@*">
<xsl:copy />
</xsl:template>

<xsl:template match="@href | @src" />
<xsl:template name="make-data-local">
<xsl:param name="path" />
<xsl:for-each select="..">
<xsl:attribute name="data-local">
<xsl:value-of select="$path" />
</xsl:attribute>
</xsl:for-each>
</xsl:template>

<xsl:template match="@href | @src">
<xsl:param name="root" />
<xsl:param name="attr" select="local-name()" />
<xsl:choose>
<xsl:when test="$rooted">
<xsl:attribute name="{$attr}">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:if test="$root!='' and not(../@data-src)">
<xsl:call-template name="make-data-local">
<xsl:with-param name="path" select="concat($root,.)" />
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="{$attr}">
<xsl:value-of select="concat($root,.)" />
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="@data-src">
<xsl:attribute name="data-src">
Expand Down

0 comments on commit 921e790

Please sign in to comment.