summaryrefslogtreecommitdiff
path: root/xsl/img.xsl
blob: 0c10fd23abc2560b0f1c819cd7f1d17e1473d958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  exclude-result-prefixes="s"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:s="https://beratung-franzke.de/web-tpl"
  xmlns:html="http://www.w3.org/1999/xhtml">

  <xsl:template match="html:img" mode="html">
    <xsl:copy>
      <!-- Copy all attributes and nested nodes -->
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates mode="html"/>

      <!-- ..but modify the src attribute to have a ?git-hash suffix -->
      <xsl:attribute name="src">

        <xsl:variable name="replace_jpg">
          <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="@src"/>
            <xsl:with-param name="replace" select="'.jpg'" />
            <xsl:with-param name="with" select="''"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:variable name="replace_jpg_png">
          <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="$replace_jpg"/>
            <xsl:with-param name="replace" select="'.png'" />
            <xsl:with-param name="with" select="''"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:value-of select="$replace_jpg_png"/>

        <xsl:choose>
          <xsl:when test="contains(@src, '.jpg')">
            <xsl:value-of select="$versioned_jpg"/>
          </xsl:when>
          <xsl:when test="contains(@src, '.png')">
            <xsl:value-of select="$versioned_png"/>
          </xsl:when>
        </xsl:choose>
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$with"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text"
select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>