1<?xml version="1.0" encoding="ISO-8859-1"?> <!-- -*- sgml -*- -->
2<!DOCTYPE xsl:stylesheet [
3  <!ENTITY nl "&#xa;">
4  <!ENTITY line-len "72">
5  <!ENTITY indent "    ">
6  <!-- we always output the same-length rule, so just define it as an entity -->
7  <!ENTITY rule "------------------------------------------------------------------------">
8]>
9<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
10<xsl:import href="http://docbook.sourceforge.net/release/xsl/current/fo/docbook.xsl"/>
11
12
13<!-- line-wrapping stuff -->
14<xsl:import href="line-wrap.xsl"/>
15
16<!-- dump all white-space only nodes -->
17<xsl:strip-space elements="*"/>
18
19
20<xsl:output method="text" encoding="ISO-8859-1"
21            indent="yes" omit-xml-declaration="yes"/>
22
23<xsl:param name="generate.toc">
24book      nop
25article   nop
26qandaset  toc
27qandadiv  nop
28</xsl:param>
29
30
31<!-- top-level place to start -->
32<xsl:template match="*" mode="process.root">
33  <xsl:text>&nl;</xsl:text>
34  <xsl:apply-templates select="."/>
35</xsl:template>
36
37
38<!-- book templates -->
39<xsl:template match="book">
40  <xsl:call-template name="book.titlepage"/>
41  <xsl:apply-templates/>
42</xsl:template>
43
44<xsl:template name="book.titlepage">
45  <xsl:text>&nl;</xsl:text>
46  <xsl:variable name="recto.content">
47    <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
48                         select="bookinfo/title"/>
49    <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
50                         select="bookinfo/releaseinfo"/>
51    <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
52                         select="bookinfo/copyright"/>
53    <xsl:apply-templates mode="book.titlepage.recto.auto.mode"
54                         select="bookinfo/author"/>
55  </xsl:variable>
56    <xsl:copy-of select="$recto.content"/>
57
58</xsl:template>
59
60<xsl:template match="title" mode="book.titlepage.recto.auto.mode">
61  <xsl:value-of select="."/>
62  <xsl:text>&nl;</xsl:text>
63</xsl:template>
64
65<xsl:template match="releaseinfo" mode="book.titlepage.recto.auto.mode">
66  <xsl:value-of select="."/>
67  <xsl:text>&nl;</xsl:text>
68
69  <xsl:call-template name="ruler">
70    <xsl:with-param name="char" select="'~'"/>
71    <xsl:with-param name="len" select="string-length(.)"/>
72  </xsl:call-template>
73
74  <xsl:text>&nl;</xsl:text>
75</xsl:template>
76
77<!-- noop -->
78<xsl:template match="copyright" mode="book.titlepage.recto.auto.mode"/>
79<!-- noop -->
80<xsl:template match="author" mode="book.titlepage.recto.auto.mode"/>
81
82
83<!-- article templates -->
84<xsl:template match="article">
85  <xsl:call-template name="article.titlepage"/>
86  <xsl:apply-templates/>
87</xsl:template>
88
89<!-- noop -->
90<xsl:template name="article.titlepage"/>
91<!-- noop -->
92<xsl:template match="article/title" mode="titlepage.mode"/>
93
94
95
96<!-- qandaset / qandadiv / qandaentry templates -->
97<xsl:template match="qandaset">
98  <xsl:variable name="title" select="(title)[1]"/>
99    <xsl:text>Table of Contents</xsl:text>
100    <xsl:apply-templates select="$title"/>
101    <xsl:call-template name="process.qanda.toc"/>
102    <xsl:text>&nl;</xsl:text>
103    <!-- do the thang -->
104    <xsl:apply-templates select="qandaentry|qandadiv"/>
105</xsl:template>
106
107<xsl:template match="qandadiv/title">
108  <xsl:variable name="qalevel">
109    <xsl:call-template name="qandadiv.section.level"/>
110  </xsl:variable>
111
112  <xsl:text>&nl;&rule;&nl;</xsl:text>
113  <xsl:apply-templates select="parent::qandadiv" mode="label.markup"/>
114  <xsl:text>. </xsl:text>
115  <xsl:apply-templates/>
116  <xsl:text>&nl;&rule;&nl;&nl;</xsl:text>
117</xsl:template>
118
119<xsl:template name="process.qanda.toc">
120  <xsl:apply-templates select="qandadiv" mode="qandatoc.mode"/>
121</xsl:template>
122
123<xsl:template match="qandadiv" mode="qandatoc.mode">
124  <xsl:apply-templates select="title" mode="qandatoc.mode"/>
125  <xsl:call-template name="process.qanda.toc"/>
126</xsl:template>
127
128<xsl:template match="qandadiv/title" mode="qandatoc.mode">
129  <xsl:variable name="qalevel">
130    <xsl:call-template name="qandadiv.section.level"/>
131  </xsl:variable>
132
133  <xsl:text>&nl;</xsl:text>
134  <xsl:apply-templates select="parent::qandadiv" mode="label.markup"/>
135  <xsl:text>. </xsl:text>
136
137  <xsl:apply-templates/>
138</xsl:template>
139
140<xsl:template match="qandadiv">
141  <xsl:apply-templates select="(title)[1]"/>
142  <xsl:apply-templates select="qandadiv|qandaentry"/>
143</xsl:template>
144
145<!-- if this qandaentry is not the first child in the parent qandadiv, -->
146<!-- then output a separator line -->
147<xsl:template match="qandaentry">
148  <xsl:if test="position() > 1">
149    <xsl:text>&rule;&nl;&nl;</xsl:text>
150  </xsl:if>
151  <xsl:apply-templates/>
152</xsl:template>
153
154
155
156<!-- TODO: implement indenting of subsequent lines in question para -->
157<!-- maybe pass a var 'indent' along to foldl ... ... -->
158<xsl:template match="question">
159  <xsl:variable name="qnum">
160    <xsl:apply-templates select="." mode="label.markup"/>
161  </xsl:variable>
162
163  <xsl:for-each select="para|screen|programlisting|itemizedlist|orderedlist">
164    <xsl:choose>
165      <!-- glue $qnum onto the front of the first question para -->
166      <xsl:when test="local-name() = 'para' and position() = 1">
167        <xsl:apply-templates select=".">
168          <xsl:with-param name="prefix" select="concat($qnum, '. ')"/>
169        </xsl:apply-templates>
170      </xsl:when>
171      <xsl:otherwise>
172        <xsl:apply-templates select=".">
173          <xsl:with-param name="prefix" select="zzzz"/>
174        </xsl:apply-templates>
175      </xsl:otherwise>
176    </xsl:choose>
177  </xsl:for-each>
178
179</xsl:template>
180
181
182<xsl:template match="answer">
183  <xsl:apply-templates select="*[name(.) != 'label']"/>
184</xsl:template>
185
186
187<xsl:template match="para">
188  <xsl:param name="prefix" select="''"/>
189
190  <!-- reformat any ulinks found -->
191  <xsl:variable name="text">
192    <xsl:choose>
193      <xsl:when test="descendant-or-self::ulink">
194        <xsl:apply-templates name="ulink"/>
195      </xsl:when>
196      <xsl:otherwise>
197        <xsl:value-of select="."/>
198      </xsl:otherwise>
199    </xsl:choose>
200  </xsl:variable>
201
202  <!-- the input to str-split-to-lines must be delim'd by either   -->
203  <!-- a space or a newline. since we need a newline at the end of -->
204  <!-- every para anyway, stick it on now -->
205  <xsl:variable name="pLine"
206                select="concat($prefix, normalize-space($text), '&nl;')"/>
207
208  <xsl:choose>
209    <xsl:when test="string-length($pLine) &lt; &line-len;">
210      <xsl:value-of select="$pLine"/>
211    </xsl:when>
212    <xsl:otherwise>
213      <!-- TODO: this is where an 'indent' param should be sent -->
214      <xsl:call-template name="str-split-to-lines">
215        <xsl:with-param name="pStr" select="$pLine"/>
216        <xsl:with-param name="pLineLength" select="&line-len;"/>
217        <xsl:with-param name="pDelimiters" select="' &nl;'"/>
218      </xsl:call-template>
219    </xsl:otherwise>
220  </xsl:choose>
221  <xsl:text>&nl;</xsl:text>
222</xsl:template>
223
224
225<!-- always indent the contents of screen / programlisting -->
226<xsl:template match="screen|programlisting">
227  <xsl:call-template name="indent.me">
228    <xsl:with-param name="first" select="'1'"/>
229  </xsl:call-template>
230  <xsl:text>&nl;</xsl:text>
231</xsl:template>
232
233
234<xsl:template match="itemizedlist">
235  <xsl:apply-templates select="listitem"/>
236</xsl:template>
237
238<xsl:template match="itemizedlist/listitem">
239  <xsl:apply-templates>
240    <xsl:with-param name="prefix" select="'* '"/>
241  </xsl:apply-templates>
242</xsl:template>
243
244
245<xsl:template match="orderedlist">
246  <xsl:apply-templates select="listitem"/>
247</xsl:template>
248
249<xsl:template match="orderedlist/listitem">
250  <xsl:apply-templates>
251    <xsl:with-param name="prefix" select="concat(position(), '. ' )"/>
252  </xsl:apply-templates>
253</xsl:template>
254
255
256<xsl:template match="ulink" name="ulink">
257  <xsl:variable name="url"  select="normalize-space(@url)"/>
258
259  <xsl:value-of select="concat(text(), ': ')"/>
260
261  <xsl:text>&lt;</xsl:text>
262  <xsl:choose>
263    <xsl:when test="starts-with($url, 'mailto:')">
264      <xsl:value-of select="substring-after($url, ':')"/>
265    </xsl:when>
266    <xsl:otherwise>
267      <xsl:value-of select="$url"/>
268    </xsl:otherwise>
269  </xsl:choose>
270  <xsl:text>&gt;</xsl:text>
271
272</xsl:template>
273
274
275<!-- indent a string -->
276<xsl:template name="indent.me">
277  <xsl:param name="text" select="."/>
278  <xsl:param name="first" select="'0'"/>
279  <xsl:choose>
280    <xsl:when test="contains($text, '&#xa;')">
281      <xsl:value-of select="substring-before($text, '&#xa;')"/>
282      <xsl:if test="$first = 0">
283        <xsl:text>&nl;</xsl:text>
284      </xsl:if>
285      <xsl:text>&indent;</xsl:text>
286      <xsl:call-template name="indent.me">
287        <xsl:with-param name="text" select="substring-after($text, '&#xa;')"/>
288      </xsl:call-template>
289    </xsl:when>
290    <xsl:otherwise>
291      <xsl:if test="not(string-length($text) = 0)">
292        <xsl:text>&indent;</xsl:text>
293	      <xsl:value-of select="$text"/>
294        <xsl:text>&nl;</xsl:text>
295      </xsl:if>
296    </xsl:otherwise>
297  </xsl:choose>
298</xsl:template>
299
300
301<!-- Repeat the character 'char' 'len' times -->
302<xsl:template name="ruler">
303  <xsl:param name="char"/>
304  <xsl:param name="len"/>
305  <xsl:param name="line"/>
306  <xsl:choose>
307    <xsl:when test="$len = 0">
308      <xsl:value-of select="$line"/>
309      <xsl:text>&nl;</xsl:text>
310    </xsl:when>
311    <xsl:when test="$len mod 2 = 1">
312      <xsl:call-template name="ruler">
313	      <xsl:with-param name="char" select="concat($char,$char)"/>
314	      <xsl:with-param name="len" select="($len - 1) div 2"/>
315        <xsl:with-param name="line" select="concat($line,$char)"/>
316	    </xsl:call-template>
317    </xsl:when>
318    <xsl:otherwise>
319	    <xsl:call-template name="ruler">
320	      <xsl:with-param name="char" select="concat($char,$char)"/>
321	      <xsl:with-param name="len" select="$len div 2"/>
322        <xsl:with-param name="line" select="$line"/>
323	    </xsl:call-template>
324    </xsl:otherwise>
325  </xsl:choose>
326</xsl:template>
327
328
329</xsl:stylesheet>
330
331