1<xsl:stylesheet version='1.0'
2  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
3  xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
4  xmlns:str='http://xsltsl.org/string'
5  extension-element-prefixes='str'
6  exclude-result-prefixes="doc">
7
8  <xsl:import href='xsltsl/stdlib.xsl'/>
9
10  <doc:book xmlns=''>
11    <title>Text Stylesheet</title>
12
13    <para>This stylesheet produces a text rendition of a DocBook document.</para>
14  </doc:book>
15
16  <xsl:output method='text'/>
17
18  <xsl:strip-space elements='*'/>
19  <xsl:preserve-space elements='programlisting literallayout command'/>
20
21  <xsl:template match='article'>
22    <xsl:choose>
23      <xsl:when test='title'>
24        <xsl:apply-templates select='title'/>
25      </xsl:when>
26      <xsl:when test='articleinfo/title'>
27        <xsl:apply-templates select='articleinfo/title'/>
28      </xsl:when>
29    </xsl:choose>
30
31    <xsl:apply-templates select='author|articleinfo/author'/>
32    <xsl:text>
33
34</xsl:text>
35
36    <xsl:apply-templates select='*[not(self::articleinfo)]'/>
37  </xsl:template>
38
39  <xsl:template match='article/title|articleinfo/title'>
40    <xsl:text>
41	</xsl:text>
42    <xsl:apply-templates/>
43    <xsl:text>
44
45</xsl:text>
46
47    <xsl:if test='following-sibling::subtitle'>
48      <xsl:text>	</xsl:text>
49      <xsl:apply-templates select='following-sibling::subtitle'/>
50      <xsl:if test='following-sibling::revhistory'>
51        <xsl:text> Version </xsl:text>
52        <xsl:apply-templates select='following-sibling::revhistory/revision[1]/revnumber'/>
53      </xsl:if>
54    </xsl:if>
55    <xsl:text>
56
57</xsl:text>
58  </xsl:template>
59
60  <xsl:template match='author'>
61    <xsl:apply-templates select='firstname'/>
62    <xsl:text> </xsl:text>
63    <xsl:apply-templates select='surname'/>
64    <xsl:if test='affiliation'>
65      <xsl:text>, </xsl:text>
66      <xsl:apply-templates select='affiliation/orgname'/>
67    </xsl:if>
68  </xsl:template>
69
70  <xsl:template match='para'>
71    <xsl:param name='indent' select='0'/>
72    <xsl:param name='linelen' select='80'/>
73
74    <xsl:call-template name='str:justify'>
75      <xsl:with-param name='text'>
76        <xsl:apply-templates/>
77      </xsl:with-param>
78      <xsl:with-param name='indent' select='$indent'/>
79      <xsl:with-param name='max' select='$linelen'/>
80    </xsl:call-template>
81    <xsl:text>
82
83</xsl:text>
84  </xsl:template>
85
86  <xsl:template match='note'>
87    <xsl:call-template name='str:justify'>
88      <xsl:with-param name='text'>
89        <xsl:apply-templates/>
90      </xsl:with-param>
91      <xsl:with-param name='indent' select='4'/>
92    </xsl:call-template>
93    <xsl:text>
94
95</xsl:text>
96  </xsl:template>
97
98  <xsl:template match='section'>
99    <xsl:text>
100
101</xsl:text>
102    <xsl:apply-templates select='title'/>
103    <xsl:if test='subtitle'>
104      <xsl:text> (</xsl:text>
105      <xsl:apply-templates select='subtitle'/>
106      <xsl:text>)</xsl:text>
107    </xsl:if>
108    <xsl:text>
109</xsl:text>
110    <xsl:variable name='titlelen'>
111      <xsl:choose>
112        <xsl:when test='subtitle'>
113          <xsl:value-of select='string-length(title) + 3 + string-length(subtitle)'/>
114        </xsl:when>
115        <xsl:otherwise>
116          <xsl:value-of select='string-length(title)'/>
117        </xsl:otherwise>
118      </xsl:choose>
119    </xsl:variable>
120    <xsl:call-template name='str:generate-string'>
121      <xsl:with-param name='count' select='$titlelen'/>
122      <xsl:with-param name='text'>
123        <xsl:choose>
124          <xsl:when test='parent::section'>-</xsl:when>
125          <xsl:otherwise>=</xsl:otherwise>
126        </xsl:choose>
127      </xsl:with-param>
128    </xsl:call-template>
129    <xsl:text>
130
131</xsl:text>
132
133    <xsl:apply-templates select='*[not(self::title|self::subtitle|self::sectioninfo)]'/>
134  </xsl:template>
135
136  <xsl:template match='itemizedlist'>
137    <xsl:apply-templates select='listitem'/>
138    <xsl:text>
139</xsl:text>
140  </xsl:template>
141  <xsl:template match='itemizedlist/listitem'>
142    <xsl:call-template name='str:generate-string'>
143      <xsl:with-param name='text' select='" "'/>
144      <xsl:with-param name='count' select='count(ancestor::itemizedlist|ancestor::variablelist) * 4'/>
145    </xsl:call-template>
146
147    <xsl:text>* </xsl:text>
148    <xsl:apply-templates select='*'>
149      <xsl:with-param name='indent' select='count(ancestor::itemizedlist|ancestor::variablelist) * 4'/>
150      <xsl:with-param name='linelen' select='80 - count(ancestor::itemizedlist|ancestor::variablelist) * 4'/>
151    </xsl:apply-templates>
152  </xsl:template>
153
154  <xsl:template match='variablelist'>
155    <xsl:apply-templates select='varlistentry'/>
156  </xsl:template>
157  <xsl:template match='varlistentry'>
158    <xsl:call-template name='str:generate-string'>
159      <xsl:with-param name='text' select='" "'/>
160      <xsl:with-param name='count' select='(count(ancestor::variablelist|ancestor::itemizedlist) - 1) * 4'/>
161    </xsl:call-template>
162
163    <xsl:apply-templates select='term'/>
164    <xsl:apply-templates select='listitem'/>
165    <xsl:text>
166
167</xsl:text>
168  </xsl:template>
169  <xsl:template match='varlistentry/term'>
170    <xsl:apply-templates/>
171    <xsl:text>
172</xsl:text>
173  </xsl:template>
174  <xsl:template match='varlistentry/listitem'>
175    <xsl:apply-templates select='*'>
176      <xsl:with-param name='indent' select='count(ancestor::variablelist|ancestor::itemizedlist) * 4'/>
177    </xsl:apply-templates>
178  </xsl:template>
179
180  <xsl:template match='programlisting|literallayout'>
181    <xsl:call-template name='indent'>
182      <xsl:with-param name='text' select='.'/>
183      <xsl:with-param name='indent' select='(count(ancestor::itemizedlist|ancestor::variablelist) + 1) * 4'/>
184    </xsl:call-template>
185    <xsl:text>
186
187</xsl:text>
188  </xsl:template>
189
190  <xsl:template name='indent'>
191    <xsl:param name='text'/>
192    <xsl:param name='indent' select='4'/>
193
194    <xsl:choose>
195      <xsl:when test='not($text)'/>
196      <xsl:when test='contains($text, "&#xa;")'>
197        <xsl:call-template name='str:generate-string'>
198          <xsl:with-param name='text' select='" "'/>
199          <xsl:with-param name='count' select='$indent'/>
200        </xsl:call-template>
201        <xsl:value-of select='substring-before($text, "&#xa;")'/>
202        <xsl:text>
203</xsl:text>
204        <xsl:call-template name='indent'>
205          <xsl:with-param name='text' select='substring-after($text, "&#xa;")'/>
206          <xsl:with-param name='indent' select='$indent'/>
207        </xsl:call-template>
208      </xsl:when>
209      <xsl:otherwise>
210        <xsl:call-template name='str:generate-string'>
211          <xsl:with-param name='text' select='" "'/>
212          <xsl:with-param name='count' select='$indent'/>
213        </xsl:call-template>
214        <xsl:value-of select='$text'/>
215      </xsl:otherwise>
216    </xsl:choose>
217  </xsl:template>
218
219  <xsl:template match='ulink'>
220    <xsl:apply-templates/>
221    <xsl:text> [</xsl:text>
222    <xsl:value-of select='@url'/>
223    <xsl:text>]</xsl:text>
224  </xsl:template>
225
226</xsl:stylesheet>
227