source: archiver/pipelines/actions/mediatypes/css.xpl @ cf97a98

Revision cf97a98, 4.5 KB checked in by Eric van der Vlist <vdv@dyomedea.com>, 13 months ago (diff)

Fist version supporting CSS rewriting

  • Property mode set to 100644
Line 
1<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/">
2
3  <p:param name="archive" type="input"/>
4  <p:param name="rewritten" type="output"/>
5  <p:param name="links" type="output"/>
6
7
8  <!-- Store the document -->
9  <p:processor name="oxf:file-serializer">
10    <p:input name="config">
11      <config>
12        <scope>session</scope>
13      </config>
14    </p:input>
15    <p:input name="data" href="#archive#xpointer(/archive/response/document)"/>
16    <p:output name="data" id="url-written"/>
17  </p:processor>
18
19  <!-- And read it as CSS -->
20  <p:processor name="oxf:url-generator">
21    <p:input name="config" transform="oxf:xslt" href="#url-written">
22      <config xsl:version="2.0">
23        <url>
24          <xsl:value-of select="/*"/>
25        </url>
26        <mode>text</mode>
27      </config>
28    </p:input>
29    <p:output name="data" id="css" debug="css"/>
30  </p:processor>
31
32  <!-- Get a list of links to update -->
33  <!-- TODO: support links in inline CSS -->
34  <!-- TODO: support iframes and objects -->
35  <p:processor name="oxf:unsafe-xslt">
36    <p:input name="data" href="#css"/>
37    <p:input name="request" href="#archive#xpointer(/archive/request)"/>
38    <p:input name="config">
39      <xsl:stylesheet version="2.0">
40        <xsl:variable name="base" select="doc('input:request')/request/location"/>
41        <xsl:template match="/">
42          <links>
43            <xsl:variable name="links" as="node()*">
44              <xsl:analyze-string select="document" regex="url\([&quot;']?([^)'&quot;]+)[&quot;']?\)" flags="">
45                <xsl:matching-substring>
46                  <link href="{regex-group(1)}"/>
47                </xsl:matching-substring>
48              </xsl:analyze-string>
49            </xsl:variable>
50            <xsl:for-each-group select="$links" group-by="@href">
51              <xsl:variable name="abs-href" select="resolve-uri(@href, $base)"/>
52              <xsl:variable name="tokens" select="tokenize($abs-href, '/')"/>
53              <xsl:variable name="last-token" select="$tokens[last()]"/>
54              <xsl:variable name="tokens2" select="tokenize($last-token, '\.')"/>
55              <xsl:variable name="extension" select="$tokens2[last()]"/>
56              <link abs-href="{$abs-href}" new-href="{saxon:string-to-hexBinary(substring($abs-href, 1, string-length($abs-href) - string-length($extension) - 1), 'utf-8')}.{$extension}"
57                filename="{saxon:string-to-hexBinary($abs-href, 'utf-8')}.xml">
58                <xsl:copy-of select="@*"/>
59              </link>
60            </xsl:for-each-group>
61          </links>
62        </xsl:template>
63      </xsl:stylesheet>
64    </p:input>
65    <p:output name="data" id="links-local" debug="links"/>
66  </p:processor>
67
68  <p:processor name="oxf:identity">
69    <p:input name="data" href="#links-local"/>
70    <p:output name="data" ref="links"/>
71  </p:processor>
72
73  <!-- Update the links -->
74  <p:processor name="oxf:unsafe-xslt">
75    <p:input name="data" href="#css"/>
76    <p:input name="request" href="#archive#xpointer(/archive/request)"/>
77    <p:input name="links" href="#links-local"/>
78    <p:input name="config">
79      <xsl:stylesheet version="2.0">
80        <xsl:variable name="links" select="doc('input:links')/links"/>
81        <xsl:variable name="base" select="doc('input:request')/request/location"/>
82        <xsl:key name="link" match="link" use="@href"/>
83        <xsl:template match="/document">
84          <xsl:copy>
85            <xsl:copy-of select="@*"/>
86            <xsl:analyze-string select="." regex="url\([&quot;']?([^)'&quot;]+)[&quot;']?\)" flags="">
87              <xsl:matching-substring>
88                <xsl:text>url(</xsl:text>
89                <xsl:value-of select="$links/key('link', regex-group(1))/@new-href"/>
90                <xsl:text>)</xsl:text>
91              </xsl:matching-substring>
92              <xsl:non-matching-substring>
93                <xsl:copy-of select="."/>
94              </xsl:non-matching-substring>
95            </xsl:analyze-string>
96          </xsl:copy>
97        </xsl:template>
98      </xsl:stylesheet>
99    </p:input>
100    <p:output name="data" id="rewritten-local" debug="rewritten"/>
101  </p:processor>
102
103  <!-- It's a hack so that the document is not submitted as text through the xforms:submit processor... -->
104  <p:processor name="oxf:xslt">
105    <p:input name="config">
106      <document xsl:version="2.0">
107        <xsl:copy-of select="/"/>
108      </document>
109    </p:input>
110    <p:input name="data" href="#rewritten-local"/>
111    <p:output name="data" ref="rewritten"/>
112  </p:processor>
113
114</p:config>
Note: See TracBrowser for help on using the repository browser.