]> git.draconx.ca Git - homepage.git/blob - layouts/embed-svg.xsl
Add separator rows to the sorted part of clicky tables.
[homepage.git] / layouts / embed-svg.xsl
1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!--
3   Nick's web site: SVG embedding.
4
5   Copyright © 2021 Nick Bowler
6
7   This program is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <https://www.gnu.org/licenses/>
19 -->
20 <xsl:stylesheet version='1.0'
21   xmlns='http://www.w3.org/1999/xhtml'
22   xmlns:cc='http://creativecommons.org/ns#'
23   xmlns:dc='http://purl.org/dc/elements/1.1/'
24   xmlns:f='http://draconx.ca/my-functions'
25   xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
26   xmlns:svg="http://www.w3.org/2000/svg"
27   xmlns:xhtml='http://www.w3.org/1999/xhtml'
28   xmlns:xlink='http://www.w3.org/1999/xlink'
29   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
30   extension-element-prefixes="f"
31   exclude-result-prefixes="cc dc rdf xhtml">
32
33 <xsl:import href='layouts/functions.xsl' />
34 <xsl:output cdata-section-elements='style script' />
35 <xsl:strip-space elements='xhtml:html xhtml:body' />
36
37 <xsl:key name='embed-svg' match='//xhtml:img[f:ends-with(@src, ".svg")]'
38   use='@src' />
39
40 <xsl:key name='id' match='//*[@id]' use='@id' />
41
42 <xsl:template match='node()|@*'>
43   <xsl:copy>
44     <xsl:apply-templates select='node()|@*' />
45   </xsl:copy>
46 </xsl:template>
47
48 <!-- force a literal result to ensure html gets all namespace nodes -->
49 <xsl:template name='rewrite-html'>
50   <html>
51     <xsl:apply-templates select='node()|@*' />
52   </html>
53 </xsl:template>
54
55 <xsl:template match='/xhtml:html'>
56   <xsl:choose>
57     <xsl:when test='//xhtml:img[key("embed-svg", @src)]'>
58       <xsl:call-template name='rewrite-html' />
59     </xsl:when>
60     <xsl:otherwise>
61       <xsl:element name='{name()}' namespace='{namespace-uri()}'>
62         <xsl:apply-templates select='node()|@*' />
63       </xsl:element>
64     </xsl:otherwise>
65   </xsl:choose>
66 </xsl:template>
67
68 <xsl:template match='xhtml:img[key("embed-svg", @src)]'>
69   <span class='svg'>
70     <xsl:apply-templates select='@style' />
71     <svg:svg>
72       <xsl:apply-templates select='@width|@height' />
73       <svg:switch>
74         <svg:use xlink:href='#es-{generate-id(key("embed-svg", @src))}' />
75         <svg:foreignObject width='0' height='0'>
76           <!-- TODO: this hardcoded fallback method needs to be more flexible -->
77           <xsl:copy>
78             <xsl:attribute name='src'>
79               <xsl:value-of select='substring-before(@src, ".svg")' />
80               <xsl:text>-32.png</xsl:text>
81             </xsl:attribute>
82             <xsl:attribute name='class'>svgfallback</xsl:attribute>
83             <xsl:apply-templates select='node()|
84                   @*[not(contains("src style class", local-name()))]' />
85           </xsl:copy>
86         </svg:foreignObject>
87       </svg:switch>
88     </svg:svg>
89   </span>
90 </xsl:template>
91
92 <!-- SVG embedding -->
93 <xsl:template mode='embed-svg' match='node()|@*'>
94   <xsl:copy>
95     <xsl:apply-templates select='node()|@*' />
96   </xsl:copy>
97 </xsl:template>
98
99 <!-- Remove all whitespace-only text nodes from embedded SVG -->
100 <xsl:template mode='embed-svg' match='text()[normalize-space(.) = ""]' />
101
102 <xsl:template name='svgnode' mode='embed-svg' match='svg:*'>
103   <xsl:param name='idnode' select='.' />
104
105   <!-- doctype demands svg: prefix -->
106   <xsl:element name='svg:{local-name()}' namespace='{namespace-uri()}'>
107     <!-- rewrite all id nodes to avoid conflicts with other embeddings -->
108     <xsl:if test='@id or not(parent::*)'>
109       <xsl:attribute name='id'>
110         <xsl:value-of select='concat("es-", generate-id($idnode))' />
111       </xsl:attribute>
112     </xsl:if>
113     <xsl:apply-templates mode='embed-svg' select='@*[local-name()!="id"]' />
114     <xsl:apply-templates mode='embed-svg' select='node()'>
115       <xsl:sort select='-count(self::svg:metadata)' data-type='number' />
116     </xsl:apply-templates>
117   </xsl:element>
118 </xsl:template>
119
120 <xsl:template name='depx'>
121   <xsl:param name='node' select='.' />
122   <xsl:choose>
123     <xsl:when test='contains($node, "px")'>
124       <xsl:value-of select='substring-before($node, "px")' />
125     </xsl:when>
126     <xsl:otherwise>
127       <xsl:value-of select='node' />
128     </xsl:otherwise>
129   </xsl:choose>
130 </xsl:template>
131
132 <xsl:template mode='embed-svg' match='/svg:svg/@width[../@height]'>
133   <xsl:attribute name='viewBox'>
134     <xsl:text>0 0 </xsl:text>
135     <xsl:call-template name='depx' />
136     <xsl:text> </xsl:text>
137     <xsl:call-template name='depx'>
138       <xsl:with-param name='node' select='../@height' />
139     </xsl:call-template>
140   </xsl:attribute>
141 </xsl:template>
142 <xsl:template mode='embed-svg' match='/svg:svg/@height[../@width]' />
143
144 <!--
145   the RDF stuff is disallowed by doctype, try and transform to an
146   attribution comment.
147 -->
148 <xsl:template mode='embed-svg' match='svg:metadata'>
149   <xsl:if test='descendant::cc:Work'>
150     <xsl:comment>
151       <xsl:apply-templates select='descendant::cc:Work' />
152     </xsl:comment>
153   </xsl:if>
154 </xsl:template>
155
156 <!-- hackjob to stringify the work info -->
157 <xsl:template match='cc:Work'>
158   <xsl:text>
159   </xsl:text>
160   <xsl:if test='dc:title'>
161     <xsl:value-of select='concat("“", dc:title, "”")' />
162   </xsl:if>
163   <xsl:if test='dc:creator/cc:Agent'>
164     <xsl:text> by </xsl:text>
165     <xsl:for-each select='dc:creator/cc:Agent'>
166       <xsl:value-of select='dc:title' />
167       <xsl:choose>
168         <xsl:when test='position()=last()'>
169           <xsl:text>.</xsl:text>
170         </xsl:when>
171         <xsl:otherwise>
172           <xsl:text>, </xsl:text>
173         </xsl:otherwise>
174       </xsl:choose>
175     </xsl:for-each>
176   </xsl:if>
177   <xsl:if test='dc:source'>
178     <xsl:text>
179   </xsl:text>
180     <xsl:value-of select='dc:source' />
181   </xsl:if>
182   <xsl:if test='cc:license/@rdf:resource'>
183     <xsl:text>
184   </xsl:text>
185     <xsl:value-of select='cc:license/@rdf:resource' />
186   </xsl:if>
187   <xsl:text>
188 </xsl:text>
189 </xsl:template>
190
191 <!-- match all xlink:href attributes to generated IDs for this document -->
192 <xsl:template mode='embed-svg' match='@xlink:href[starts-with(.,"#")]'>
193   <xsl:variable name='ref' select='substring-after(., "#")' />
194
195   <xsl:attribute name='xlink:href'>
196     <xsl:value-of select='concat("#es-", generate-id(key("id", $ref)))' />
197   </xsl:attribute>
198 </xsl:template>
199
200 <!-- rewrite all attributes containing url(#id) to generated IDs -->
201 <xsl:template name='rewrite-urls'>
202   <xsl:param name='val' select='.' />
203
204   <xsl:choose>
205     <xsl:when test='contains($val, "url(#")'>
206       <xsl:variable name='tail' select='substring-after($val, "url(#")' />
207       <xsl:variable name='ref' select='substring-before($tail, ")")' />
208
209       <xsl:value-of select='substring-before($val, "url(#")' />
210       <xsl:text>url(</xsl:text>
211       <xsl:value-of select='concat("#es-", generate-id(key("id", $ref)))' />
212       <xsl:text>)</xsl:text>
213       <xsl:call-template name='rewrite-urls'>
214         <xsl:with-param name='val' select='substring-after($tail, ")")' />
215       </xsl:call-template>
216     </xsl:when>
217     <xsl:otherwise>
218       <xsl:value-of select='$val' />
219     </xsl:otherwise>
220   </xsl:choose>
221 </xsl:template>
222
223 <xsl:template mode='embed-svg' match='@*[contains(., "url(#")]'>
224   <xsl:attribute name='{name()}'>
225     <xsl:call-template name='rewrite-urls' />
226   </xsl:attribute>
227 </xsl:template>
228
229 <xsl:template match='xhtml:body'>
230   <xsl:copy>
231     <xsl:apply-templates select='node()|@*' />
232     <xsl:if test='key("embed-svg", //xhtml:img/@src)'>
233       <script type='x'><![CDATA[]]x><!--]]></script>
234       <svg:svg width='0' height='0'>
235         <svg:defs>
236           <xsl:for-each select='//xhtml:img'>
237             <xsl:if test='generate-id(.)=generate-id(key("embed-svg", @src))'>
238               <xsl:apply-templates mode='embed-svg'
239                 select='document(concat("output", @src))'>
240                 <xsl:with-param name='idnode' select='.' />
241               </xsl:apply-templates>
242             </xsl:if>
243           </xsl:for-each>
244         </svg:defs>
245       </svg:svg>
246       <script type='x'>--></script>
247     </xsl:if>
248   </xsl:copy>
249 </xsl:template>
250
251 </xsl:stylesheet>