~psmay/ixan/publish-pods

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<xsl:transform
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:qc="urn:uuid:19bb423c-3450-4626-9da2-31854c894b83"
	xmlns:exsl="http://exslt.org/common"
	xmlns:func="http://exslt.org/functions"
	xmlns:lo="urn:uuid:234a6687-4fde-4130-98fd-c98b5e742c76"
	extension-element-prefixes="func qc lo exsl"
	exclude-result-prefixes="func qc lo exsl"
	version="1.0">
	
	<!--
		qc:split(text, delim)
		Returns pieces as series of <token/> nodes.
	-->
	<func:function name="qc:split">
		<xsl:param name="text"/>
		<xsl:param name="delim"/>
		<func:result select="exsl:node-set(
			lo:presplit($text,$delim))/token"/>
	</func:function>
	
	<!--
		qc:split-all(tokens, delim)
		$tokens is expected to be a node set (of element nodes).
		Returns pieces of qc:split over all nodes as new series of <token/>.
	-->
	<func:function name="qc:split-all">
		<xsl:param name="tokens"/>
		<xsl:param name="delim"/>
		<func:result select="exsl:node-set(
			lo:presplit-all($tokens,$delim))/token"/>
	</func:function>
	
	<!--
		qc:split-lines(text)
		Splits text over \r\n, then \n, then finally \r.
		Returns lines as series of <token/> nodes.
	-->
	<func:function name="qc:split-lines">
		<xsl:param name="text"/>
		<func:result select="
			qc:split-all(
				qc:split-all(
					qc:split($text,'&#xD;&#xA;'), '&#xA;'
				), '&#xD;'
			)
		"/>
	</func:function>
	
	<!--
		qc:replace(text, find, replace)
		Replaces all instances of $find in $text with $replace.
	-->
	<func:function name="qc:replace">
		<xsl:param name="text"/>
		<xsl:param name="find"/>
		<xsl:param name="replace"/>
		<xsl:choose>
			<xsl:when test="contains($text, $find)">
				<func:result select="concat(
					qc:replace(substring-before($text, $find), $find, $replace),
					$replace,
					qc:replace(substring-after($text, $find), $find, $replace)
					)"
				/>
			</xsl:when>
			<xsl:otherwise>
				<func:result select="$text"/>
			</xsl:otherwise>
		</xsl:choose>
	</func:function>

	<!--
		qc:repeat(text, count)
		Repeats a string.
	-->
	<func:function name="qc:repeat">
		<xsl:param name="text"/>
		<xsl:param name="count"/>
		<xsl:choose>
			<xsl:when test="number($count) &lt; 1">
				<func:result select="''"/>
			</xsl:when>
			<xsl:otherwise>
				<func:result select="concat($text, qc:repeat($text, number($count) - 1))"/>
			</xsl:otherwise>
		</xsl:choose>
	</func:function>

	<!--
		qc:repeat-join(text, delim, count)
		Repeats a string with a delimiter between repetitions.
		0 reps always returns ''.
		1 rep returns text.
	-->
	<func:function name="qc:repeat-join">
		<xsl:param name="text"/>
		<xsl:param name="delim"/>
		<xsl:param name="count"/>
		<xsl:choose>
			<xsl:when test="$count &lt; 1">
				<func:result select="''"/>
			</xsl:when>
			<xsl:otherwise>
				<func:result select="concat($text, qc:repeat(concat($delim, $text), $count - 1))"/>
			</xsl:otherwise>
		</xsl:choose>
	</func:function>

	<!--
		(internal)
		This function does the grunt work of splitting a string on a delimiter.
	-->
	<func:function name="lo:presplit">
		<xsl:param name="text"/>
		<xsl:param name="delim"/>
		<xsl:choose>
			<xsl:when test="contains($text, $delim)">
				<func:result>
					<xsl:copy-of select="lo:presplit(substring-before($text, $delim), $delim)"/>
					<xsl:copy-of select="lo:presplit(substring-after($text, $delim), $delim)"/>
				</func:result>
			</xsl:when>
			<xsl:otherwise>
				<func:result>
					<token>
						<xsl:value-of select="$text"/>
					</token>
				</func:result>
			</xsl:otherwise>
		</xsl:choose>
	</func:function>
	
	<!--
		(internal)
		This function runs presplit iteratively on the text contents of the
		provided nodes.
	-->
	<func:function name="lo:presplit-all">
		<xsl:param name="tokens"/>
		<xsl:param name="delim"/>
		<func:result>
			<xsl:for-each select="$tokens">
				<xsl:copy-of select="lo:presplit(string(.), $delim)"/>
			</xsl:for-each>
		</func:result>
	</func:function>

	<!--
		qc:parse-indent(text)
		Alias for qc:parse-indent-expand($text, 8).
	-->
	<func:function name="qc:parse-indent">
		<xsl:param name="text"/>
		<func:result select="qc:parse-indent-expand($text, 8)"/>
	</func:function>

	<!--
		qc:parse-indent-expand(text, tab-width)
		With provided text, returns
			
			<token indent-depth="(number)" indent="(string)">text without leading ' ' or tab</token>
		
		@indent is the spaces stripped from the left of the string.
		@indent-depth is the width in spaces of the indent parsed. Often it
		will be S + WT, where S and T are the numbers of spaces and tabs,
		respectively, and W is the provided tab width. It may be fewer,
		however, if spaces occur that are obscured by tabs (which technically
		have a non-deterministic width).
	-->
	<func:function name="qc:parse-indent-expand">
		<xsl:param name="text"/>
		<xsl:param name="tab-width"/>
		<func:result select="exsl:node-set( lo:parse-indent-expand('', 0, string($text), number($tab-width)) )/token"/>
	</func:function>

	<!--
		(internal)
		Implements the guts of qc:parse-indent-expand.
	-->
	<func:function name="lo:parse-indent-expand">
		<xsl:param name="parsed-indent"/>
		<xsl:param name="parsed-indent-depth"/>
		<xsl:param name="text"/>
		<xsl:param name="tab-width"/>
		<xsl:choose>
			<!-- Accept the space and advance 1 -->
			<xsl:when test="starts-with($text, '&#x20;')">
				<func:result select="lo:parse-indent-expand(concat($parsed-indent,'&#x20;'),
					$parsed-indent-depth + 1,
					substring($text,2), $tab-width)"/>
			</xsl:when>
			<!-- Accept the tab and advance to the next tab stop -->
					<!-- floor(($parsed-indent-depth + $tab-width) div $tab-width) * $tab-width -->
			<xsl:when test="starts-with($text, '&#x09;')">
				<func:result select="lo:parse-indent-expand(concat($parsed-indent,'&#x09;'),
					floor(($parsed-indent-depth + $tab-width) div $tab-width) * $tab-width,
					substring($text,2), $tab-width)"/>
			</xsl:when>
			<xsl:otherwise>
				<func:result>
					<token indent-depth="{$parsed-indent-depth}" indent="{$parsed-indent}"><xsl:value-of select="$text"/></token>
				</func:result>
			</xsl:otherwise>
		</xsl:choose>
	</func:function>
	
	<!--
		qc:as-anchor-id(text)
		Returns a string with some possibility of equivalence to the same anchor on search.cpan.org.
		Various sources suggest that the site uses Pod::Simple::HTML, so that should be the frame of reference.
		See also http://search.cpan.org/dist/App-Pod2CpanHtml/bin/pod2cpanhtml
	-->
	<func:function name="qc:as-anchor-id">
		<xsl:param name="text"/>
		<!-- TODO -->
		<func:result select="qc:replace($text, ' ', '_')"/>
	</func:function>
	
	<!--
		template qc:repeat-nodes-here(content, times)
	-->
	<xsl:template name="qc:repeat-nodes-here">
		<xsl:param name="content"/>
		<xsl:param name="times"/>
		<xsl:variable name="clean-times" select="floor(number($times))"/>
		<xsl:if test="$clean-times &gt; 0">
			<xsl:call-template name="lo:repeat-nodes-by-doubling">
				<xsl:with-param name="content" select="$content"/>
				<xsl:with-param name="times" select="$clean-times"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
	<!--
		qc:repeat-nodes(content, times)
		Returns the specified number $times of copies of $content as a
		node-set.
	-->
	<func:function name="qc:repeat-nodes">
		<xsl:param name="content"/>
		<xsl:param name="times"/>
		<xsl:variable name="result-tree">
			<xsl:call-template name="qc:repeat-nodes-here">
				<xsl:with-param name="content" select="$content"/>
				<xsl:with-param name="times" select="$times"/>
			</xsl:call-template>
		</xsl:variable>
		<func:result select="exsl:node-set($result-tree)"/>
	</func:function>
	
	<!--
		(internal)
		Repeats some content a given number of times by repeatedly doubling it
		and providing output where a 1 occurs in the binary. This internal
		version is a template and expects times to be a nonnegative integer.
	-->
	<xsl:template name="lo:repeat-nodes-by-doubling">
		<xsl:param name="content"/>
		<xsl:param name="times"/>
		<xsl:variable name="right-bit" select="$times mod 2"/>
		<xsl:variable name="left-bits" select="($times - $right-bit) div 2"/>
		<!-- An odd number of times -->
		<xsl:if test="$right-bit = 1">
			<xsl:copy-of select="$content"/>
		</xsl:if>
		<!-- Any number of times -->
		<xsl:if test="$left-bits &gt; 0">
			<xsl:call-template name="lo:repeat-nodes-by-doubling">
				<xsl:with-param name="content">
					<!-- The number of repetitions is halved while the content itself is doubled -->
					<xsl:copy-of select="$content"/>
					<xsl:copy-of select="$content"/>
				</xsl:with-param>
				<xsl:with-param name="times" select="$left-bits"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

</xsl:transform>