summaryrefslogtreecommitdiff
path: root/xslt/mal-license.xsl
blob: 74efdce7ffd906a3d95bd67676e5e2c698d589cb (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mal="http://projectmallard.org/1.0/"
    xmlns:str="http://exslt.org/strings"
    xmlns:exsl="http://exslt.org/common"
    version="1.0">

<xsl:output method="text"/>

<xsl:param name="only" select="''"/>
<xsl:variable name="only_" select="concat(' ', translate($only, ',', ' '), ' ')"/>
<xsl:param name="except" select="''"/>
<xsl:variable name="except_" select="concat(' ', translate($except, ',', ' '), ' ')"/>
<xsl:param name="totals" select="''"/>

<xsl:param name="site.dir"/>

<xsl:template match="/">
  <xsl:variable name="idents">
    <xsl:if test="not(/mal:page/mal:info/mal:license)">
      <ident>none</ident>
    </xsl:if>
    <xsl:for-each select="/mal:page/mal:info/mal:license">
      <ident>
	<xsl:call-template name="license.identifier"/>
      </ident>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="display">
    <xsl:for-each select="exsl:node-set($idents)/*">
      <xsl:choose>
	<xsl:when test="$only != ''">
	  <xsl:if test="contains($only_, concat(' ', ., ' '))">
	    <xsl:text>1</xsl:text>
	  </xsl:if>
	</xsl:when>
	<xsl:when test="$except != ''">
	  <xsl:if test="not(contains($except_, concat(' ', ., ' ')))">
	    <xsl:text>1</xsl:text>
	  </xsl:if>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:text>1</xsl:text>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:variable>
  <xsl:if test="$display != ''">
    <xsl:choose>
      <xsl:when test="$totals = ''">
	<xsl:value-of select="$site.dir"/>
	<xsl:value-of select="concat(/mal:page/@id, ': ')"/>
	<xsl:for-each select="exsl:node-set($idents)/*">
	  <xsl:if test="position() != 1">
	    <xsl:text>, </xsl:text>
	  </xsl:if>
	  <xsl:value-of select="."/>
	</xsl:for-each>
	<xsl:text>&#x0A;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:for-each select="exsl:node-set($idents)/*">
	  <xsl:value-of select="."/>
	  <xsl:text>&#x0A;</xsl:text>
	</xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template>

<xsl:template name="license.identifier">
  <xsl:param name="href" select="@href"/>
  <xsl:choose>
    <xsl:when test="$href = ''">
      <xsl:text>unknown</xsl:text>
    </xsl:when>
    <xsl:when test="starts-with($href, 'http://creativecommons.org/licenses/')">
      <xsl:text>cc-</xsl:text>
      <xsl:for-each select="str:split(substring-after($href, 'http://creativecommons.org/licenses/'), '/')">
	<xsl:if test="position() != 1">
	  <xsl:text>-</xsl:text>
	</xsl:if>
	<xsl:value-of select="."/>
      </xsl:for-each>
    </xsl:when>
    <xsl:when test="starts-with($href, 'http://www.gnu.org/licenses/')">
      <xsl:variable name="basename">
	<xsl:value-of select="substring-after($href, 'http://www.gnu.org/licenses/')"/>
      </xsl:variable>
      <xsl:choose>
	<xsl:when test="substring($basename, string-length($basename) - 4) = '.html'">
	  <xsl:value-of select="substring($basename, 1, string-length($basename) - 5)"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="$basename"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>unknown</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>