]> git.stg.codes - stg.git/blob - doc/xslt/roundtrip/pages2normalise.xsl
Ticket. if (!data.tariffConf.changePolicy.empty()) construction added in
[stg.git] / doc / xslt / roundtrip / pages2normalise.xsl
1 <xsl:stylesheet version="1.0"
2   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
4   xmlns:sfa="http://developer.apple.com/namespaces/sfa"
5   xmlns:sf="http://developer.apple.com/namespaces/sf"
6   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7   xmlns:appsl="http://developer.apple.com/namespaces/sl"
8
9   xmlns:dbk='http://docbook.org/ns/docbook'
10   xmlns:rnd='http://docbook.org/ns/docbook/roundtrip'
11   exclude-result-prefixes='sfa sf xsi appsl'>
12
13   <xsl:output method="xml" indent='yes'/>
14
15   <!-- ********************************************************************
16        $Id: pages2normalise.xsl 7637 2008-01-09 20:48:30Z balls $
17        ********************************************************************
18
19        This file is part of the XSL DocBook Stylesheet distribution.
20        See ../README or http://nwalsh.com/docbook/xsl/ for copyright
21        and other information.
22
23        ******************************************************************** -->
24
25   <xsl:strip-space elements='*'/>
26   <xsl:preserve-space elements='sf:span'/>
27
28   <xsl:key name='styles'
29            match='sf:paragraphstyle[not(ancestor::appsl:section-prototypes)] |
30                   sf:characterstyle[not(ancestor::appsl:section-prototypes)] |
31                   sf:table-style'
32            use='@sf:ident|@sfa:ID'/>
33
34   <xsl:key name='ids'
35     match='*'
36     use='@sfa:ID'/>
37
38   <xsl:template match='appsl:document'>
39     <dbk:article>
40       <!-- TODO: headers and footers -->
41       <xsl:apply-templates select='sf:text-storage'/>
42     </dbk:article>
43   </xsl:template>
44
45   <xsl:template match='sf:p'>
46     <xsl:choose>
47       <xsl:when test='sf:attachment-ref and
48                       count(*) = count(sf:attachment-ref|sf:br|sf:selection-start|sf:selection-end)'>
49         <xsl:apply-templates/>
50       </xsl:when>
51       <xsl:otherwise>
52         <dbk:para>
53           <xsl:variable name='style-name'>
54             <xsl:call-template name='rnd:find-style'/>
55           </xsl:variable>
56           <xsl:if test='$style-name != "" and
57                         $style-name != "para"'>
58             <xsl:attribute name='rnd:style'>
59               <xsl:value-of select='$style-name'/>
60             </xsl:attribute>
61           </xsl:if>
62
63           <xsl:apply-templates/>
64         </dbk:para>
65       </xsl:otherwise>
66     </xsl:choose>
67   </xsl:template>
68
69   <xsl:template match='sf:span'>
70     <xsl:variable name='style-name'
71                   select='key("styles", @sf:style)/self::sf:characterstyle/@sf:name'/>
72
73     <xsl:variable name='char-style'>
74       <xsl:call-template name='rnd:find-style'>
75         <xsl:with-param name='char-style-name' select='$style-name'/>
76       </xsl:call-template>
77     </xsl:variable>
78
79     <xsl:choose>
80       <xsl:when test='$style-name = "attribute-name"'>
81         <xsl:if test='not(preceding-sibling::node()[not(self::text()) or (self::text() and normalize-space() != "")])'>
82           <xsl:attribute name='{.}'>
83             <xsl:apply-templates select='following-sibling::*[1][self::sf:span]'
84               mode='attribute'/>
85           </xsl:attribute>
86         </xsl:if>
87       </xsl:when>
88       <xsl:when test='$style-name = "attribute-value"'/>
89       <xsl:when test='$style-name = ""'>
90         <xsl:apply-templates/>
91       </xsl:when>
92       <xsl:when test='$char-style = "superscript" or
93                       $char-style = "subscript"'>
94         <xsl:element name='{$char-style}'
95           namespace='http://docbook.org/ns/docbook'>
96           <xsl:apply-templates/>
97         </xsl:element>
98       </xsl:when>
99       <xsl:otherwise>
100         <dbk:emphasis>
101           <xsl:choose>
102             <xsl:when test='$char-style = "emphasis-bold" or
103                             $char-style = "emphasis-strong"'>
104               <xsl:attribute name='role'>bold</xsl:attribute>
105             </xsl:when>
106             <xsl:when test='$char-style != "" and
107                             $char-style != "emphasis"'>
108               <xsl:attribute name='rnd:style'>
109                 <xsl:value-of select='$char-style'/>
110               </xsl:attribute>
111             </xsl:when>
112           </xsl:choose>
113
114           <xsl:apply-templates/>
115         </dbk:emphasis>
116       </xsl:otherwise>
117     </xsl:choose>
118   </xsl:template>
119
120   <xsl:template match='*' mode='attribute'>
121     <xsl:variable name='style-name'
122                   select='key("styles", @sf:style)/self::sf:characterstyle/@sf:name'/>
123
124     <xsl:if test='$style-name = "attribute-value"'>
125       <xsl:apply-templates/>
126     </xsl:if>
127   </xsl:template>
128
129   <xsl:template match='sf:br'/>
130   <xsl:template match='sf:lnbr|sf:crbr'>
131     <xsl:text>&#xa;</xsl:text>
132   </xsl:template>
133   <xsl:template match='sf:tab'>
134     <xsl:text>        </xsl:text>
135   </xsl:template>
136   <xsl:template match='sf:link'>
137     <dbk:ulink url='{@href}'>
138       <xsl:apply-templates/>
139     </dbk:ulink>
140   </xsl:template>
141
142   <xsl:template match='sf:attachment-ref'>
143     <xsl:if test='@sf:kind = "tabular-attachment"'>
144       <xsl:apply-templates select='key("ids", @sfa:IDREF)'/>
145     </xsl:if>
146   </xsl:template>
147
148   <xsl:template match='sf:attachment[@sf:kind = "tabular-attachment"]'>
149     <xsl:variable name='model'
150       select='key("ids", sf:tabular-info/sf:tabular-model-ref/@sfa:IDREF)'/>
151
152     <xsl:variable name='num-cols' select='$model/sf:grid/@sf:numcols'/>
153     <xsl:variable name='num-rows' select='$model/sf:grid/@sf:numrows'/>
154
155     <xsl:variable name='border.top'
156       select='count($model/sf:grid/sf:horizontal-gridline-styles/*) = 0 or
157               not($model/sf:grid/sf:horizontal-gridline-styles/sf:style-run[@sf:gridline-index = "0"])'/>
158     <xsl:variable name='border.bottom'
159       select='count($model/sf:grid/sf:horizontal-gridline-styles/*) = 0 or
160               not($model/sf:grid/sf:horizontal-gridline-styles/sf:style-run[@sf:gridline-index = $num-rows - 1])'/>
161     <xsl:variable name='border.left'
162       select='count($model/sf:grid/sf:vertical-gridline-styles/*) = 0 or
163               not($model/sf:grid/sf:vertical-gridline-styles/sf:style-run[@sf:gridline-index = "0"])'/>
164     <xsl:variable name='border.right'
165       select='count($model/sf:grid/sf:vertical-gridline-styles/*) = 0 or
166               not($model/sf:grid/sf:vertical-gridline-styles/sf:style-run[@sf:gridline-index = $num-cols])'/>
167
168     <xsl:choose>
169       <xsl:when test='not($num-rows) or $num-rows = ""'>
170         <xsl:message> cannot determine number of rows in table</xsl:message>
171         <xsl:comment> cannot determine number of rows in table </xsl:comment>
172       </xsl:when>
173       <xsl:when test='not($num-cols) or $num-cols = ""'>
174         <xsl:message> cannot determine number of columns in table</xsl:message>
175         <xsl:comment> cannot determine number of columns in table </xsl:comment>
176       </xsl:when>
177
178       <xsl:otherwise>
179         <dbk:informaltable>
180           <xsl:choose>
181             <xsl:when test='$border.top and $border.bottom and
182                             $border.left and $border.right'>
183               <xsl:attribute name='frame'>all</xsl:attribute>
184             </xsl:when>
185             <xsl:when test='$border.top and $border.bottom'>
186               <xsl:attribute name='frame'>topbot</xsl:attribute>
187             </xsl:when>
188             <xsl:when test='$border.left and $border.right'>
189               <xsl:attribute name='frame'>sides</xsl:attribute>
190             </xsl:when>
191             <xsl:when test='$border.top'>
192               <xsl:attribute name='frame'>top</xsl:attribute>
193             </xsl:when>
194             <xsl:when test='$border.bottom'>
195               <xsl:attribute name='frame'>bottom</xsl:attribute>
196             </xsl:when>
197           </xsl:choose>
198           <dbk:tgroup cols='{$num-cols}'>
199             <xsl:apply-templates select='$model/sf:grid/sf:columns/sf:grid-column'
200               mode='rnd:colspec'/>
201             <xsl:if test='$model/@sf:num-header-rows != 0'>
202               <dbk:thead>
203                 <xsl:call-template name='rnd:make-table-rows'>
204                   <xsl:with-param name='nodes'
205                     select='$model/sf:grid/sf:datasource/sf:text-cell[@sf:row &lt; $model/@sf:num-header-rows]'/>
206                   <xsl:with-param name='num-rows'
207                     select='$model/@sf:num-header-rows'/>
208                 </xsl:call-template>
209               </dbk:thead>
210             </xsl:if>
211             <dbk:tbody>
212               <xsl:call-template name='rnd:make-table-rows'>
213                 <xsl:with-param name='nodes'
214                   select='$model/sf:grid/sf:datasource/sf:text-cell[@sf:row >= $model/@sf:num-header-rows and
215                   @sf:row &lt; $num-rows - $model/@sf:num-footer-rows]'/>
216                 <xsl:with-param name='num-rows' select='$num-rows - $model/@sf:num-header-rows - $model/@sf:num-footer-rows'/>
217                 <xsl:with-param name='row' select='$model/@sf:num-header-rows'/>
218               </xsl:call-template>
219             </dbk:tbody>
220             <xsl:if test='$model/@sf:num-footer-rows != 0'>
221               <dbk:tfoot>
222                 <xsl:call-template name='rnd:make-table-rows'>
223                   <xsl:with-param name='nodes'
224                     select='$model/sf:grid/sf:datasource/sf:text-cell[@sf:row &gt;= $num-rows - $model/@sf:num-footer-rows]'/>
225                   <xsl:with-param name='num-rows'
226                     select='$model/@sf:num-footer-rows'/>
227                   <xsl:with-param name='row'
228                     select='$num-rows - $model/@sf:num-footer-rows'/>
229                 </xsl:call-template>
230               </dbk:tfoot>
231             </xsl:if>
232           </dbk:tgroup>
233         </dbk:informaltable>
234       </xsl:otherwise>
235     </xsl:choose>
236   </xsl:template>
237   <xsl:template match='sf:grid-column' mode='rnd:colspec'>
238     <dbk:colspec colwidth='{@sf:width}'
239       colname='column-{count(preceding-sibling::sf:grid-column) + 1}'/>
240   </xsl:template>
241   <xsl:template name='rnd:make-table-rows'>
242     <xsl:param name='num-rows' select='0'/>
243     <xsl:param name='nodes' select='/..'/>
244     <xsl:param name='row' select='0'/>
245
246     <xsl:choose>
247       <xsl:when test='not($nodes) and $num-rows != 0'>
248         <xsl:message>WARNING: insufficient table cells</xsl:message>
249         <xsl:comment> WARNING: insufficient table cells (num-rows <xsl:value-of select='$num-rows'/>, row <xsl:value-of select='$row'/>) </xsl:comment>
250       </xsl:when>
251       <xsl:when test='$nodes and $num-rows = 0'>
252         <xsl:message>WARNING: excess table cells</xsl:message>
253         <xsl:comment> WARNING: excess table cells (num-rows <xsl:value-of select='$num-rows'/>, row <xsl:value-of select='$row'/>) </xsl:comment>
254       </xsl:when>
255       <xsl:when test='not($nodes)'/>
256       <xsl:when test='$num-rows = 0'/>
257
258       <xsl:otherwise>
259         <dbk:row>
260           <xsl:apply-templates select='$nodes[@sf:row = $row]'/>
261         </dbk:row>
262         <xsl:call-template name='rnd:make-table-rows'>
263           <xsl:with-param name='num-rows'
264             select='$num-rows - 1'/>
265           <xsl:with-param name='row'
266             select='$row + 1'/>
267           <xsl:with-param name='nodes'
268             select='$nodes[@sf:row != $row]'/>
269         </xsl:call-template>
270       </xsl:otherwise>
271     </xsl:choose>
272   </xsl:template>
273   <xsl:template match='sf:text-cell'>
274     <dbk:entry>
275       <!-- Does this cell have no bottom border? -->
276       <xsl:variable name='horiz'
277         select='ancestor::sf:grid/sf:horizontal-gridline-styles'/>
278       <xsl:if test='not($horiz/*) or
279                     not($horiz/sf:style-run[@sf:gridline-index = current()/@sf:row + 1]/sf:vector-style-ref[@sf:start-index &lt;= current()/@sf:col and @sf:stop-index >= current()/@sf:col])'>
280         <xsl:attribute name='rowsep'>1</xsl:attribute>
281       </xsl:if>
282       <!-- Does this cell have no right border? -->
283       <xsl:variable name='vert'
284         select='ancestor::sf:grid/sf:vertical-gridline-styles'/>
285       <xsl:if test='not($vert/*) or
286                     not($vert/sf:style-run[@sf:gridline-index = current()/@sf:col + 1]/sf:vector-style-ref[@sf:start-index &lt;= current()/@sf:row and @sf:stop-index >= current()/@sf:row])'>
287         <xsl:attribute name='colsep'>1</xsl:attribute>
288       </xsl:if>
289
290       <xsl:choose>
291         <xsl:when test='sf:cell-text/@sfa:string'>
292           <dbk:para>
293             <xsl:apply-templates select='sf:cell-text/@sfa:string'/>
294           </dbk:para>
295         </xsl:when>
296         <xsl:otherwise>
297           <xsl:apply-templates select='sf:cell-text/sf:cell-storage/sf:text-body/*'/>
298         </xsl:otherwise>
299       </xsl:choose>
300     </dbk:entry>
301   </xsl:template>
302   <xsl:template match='sf:tableAttachmentTable |
303                        sf:tableModelCells'>
304     <xsl:apply-templates/>
305   </xsl:template>
306   <xsl:template match='sf:tableModelPartitionSource |
307                        sf:tableModelStyle-ref |
308                        sf:tableModelVectors |
309                        sf:tableCellArrayCellsByColumn |
310                        sf:tableModelTableID'/>
311
312   <xsl:template match='sf:text-storage |
313                        sf:text-body |
314                        sf:section |
315                        sf:layout'>
316     <xsl:apply-templates/>
317   </xsl:template>
318
319   <xsl:template match='sf:stylesheet|sf:stylesheet-ref |
320                        sf:container-hint |
321                        sf:page-start|sf:br |
322                        sf:selection-start|sf:selection-end |
323                        sf:insertion-point |
324                        sf:ghost-text |
325                        sf:attachments'/>
326
327   <xsl:template match='*'>
328     <xsl:message>element "<xsl:value-of select='name()'/>" not handled</xsl:message>
329   </xsl:template>
330
331   <xsl:template name='rnd:find-style'>
332     <xsl:param name='ident' select='@sf:style'/>
333     <xsl:param name='para-style-name'
334                select='key("styles", $ident)/self::sf:paragraphstyle/@sf:name'/>
335     <xsl:param name='char-style-name'
336                select='key("styles", $ident)/self::sf:characterstyle/@sf:name'/>
337
338     <xsl:choose>
339       <xsl:when test='$ident = "paragraph-style-default"'/>
340       <xsl:when test='$para-style-name != ""'>
341         <xsl:value-of select='$para-style-name'/>
342       </xsl:when>
343       <xsl:when test='key("styles", $ident)/self::sf:characterstyle/sf:property-map/sf:superscript/sf:number/@sfa:number = "1"'>superscript</xsl:when>
344       <xsl:when test='key("styles", $ident)/self::sf:characterstyle/sf:property-map/sf:subscript/sf:number/@sfa:number = "1"'>subscript</xsl:when>
345       <xsl:when test='$char-style-name != "" or
346                       key("styles", $ident)/self::sf:characterstyle/sf:property-map/*'>
347         <xsl:value-of select='$char-style-name'/>
348       </xsl:when>
349     </xsl:choose>
350   </xsl:template>
351 </xsl:stylesheet>