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
106
107
108
109
110
111
|
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mped="https://labo.planete-kraus.eu/mped.git" xmlns:docbook="http://docbook.org/ns/docbook" version="1.0">
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="docbook:programlisting[@mped:tangle-to]">
<xsl:text>mkdir -p $(dirname "</xsl:text>
<xsl:value-of select="@mped:tangle-to"/>
<xsl:text>")
</xsl:text>
<xsl:text>cat >> </xsl:text>
<xsl:value-of select="@mped:tangle-to"/>
<xsl:text> << "_MPED_EOF"
</xsl:text>
<xsl:apply-templates mode="copy-source-code"/>
<xsl:text>
_MPED_EOF
</xsl:text>
</xsl:template>
<xsl:template name="mped-private-leading-empty-lines">
<xsl:param name="indentation" select="''"/>
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="substring($text, 1, 1) = ' '">
<xsl:call-template name="mped-private-leading-empty-lines">
<xsl:with-param name="indentation" select="''"/>
<xsl:with-param name="text" select="substring($text, 2)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="( substring($text, 1, 1) = ' ' or substring($text, 1, 1) = '	' or substring($text, 1, 1) = ' ')">
<xsl:call-template name="mped-private-leading-empty-lines">
<xsl:with-param name="indentation" select="concat($indentation, substring($text, 1, 1))"/>
<xsl:with-param name="text" select="substring($text, 2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$indentation"/>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="remove-leading-empty-lines">
<xsl:param name="text" select="."/>
<xsl:call-template name="mped-private-leading-empty-lines">
<xsl:with-param name="indentation" select="''"/>
<xsl:with-param name="text" select="$text"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="remove-trailing-whitespace">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="$text = ''">
<xsl:value-of select="$text"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="last" select="substring($text, string-length ($text), 1)"/>
<xsl:variable name="before" select="substring($text, 1, string-length ($text) - 1)"/>
<xsl:choose>
<xsl:when test="$last = ' ' or $last = '	' or $last = ' ' or $last = ' '">
<xsl:call-template name="remove-trailing-whitespace">
<xsl:with-param name="text" select="$before"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()[position() = 1 and position() = last()]" mode="copy-source-code">
<xsl:call-template name="remove-trailing-whitespace">
<xsl:with-param name="text">
<xsl:call-template name="remove-leading-empty-lines">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()[position() = 1 and position() != last()]" mode="copy-source-code">
<xsl:call-template name="remove-leading-empty-lines">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()[position() > 1 and position() = last()]" mode="copy-source-code">
<xsl:call-template name="remove-trailing-whitespace">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="mped:copy" mode="copy-source-code">
<xsl:variable name="ref" select="@linkend"/>
<xsl:variable name="candidates" select="count(//docbook:programlisting[@xml:id = $ref])"/>
<xsl:if test="$candidates = 0">
<xsl:message terminate="yes">
<xsl:text>There are no listing with ID '</xsl:text>
<xsl:value-of select="$ref"/>
<xsl:text>'.
</xsl:text>
</xsl:message>
</xsl:if>
<xsl:if test="$candidates > 1">
<xsl:message terminate="yes">
<xsl:text>There are multiple listings with ID '</xsl:text>
<xsl:value-of select="$ref"/>
<xsl:text>'.
</xsl:text>
</xsl:message>
</xsl:if>
<xsl:apply-templates select="//docbook:programlisting[@xml:id = $ref]" mode="copy-source"/>
</xsl:template>
</xsl:stylesheet>
|