]> git.stg.codes - stg.git/blob - doc/xslt/roundtrip/dbk2pages.xsl
[NY] Code deduplication.
[stg.git] / doc / xslt / roundtrip / dbk2pages.xsl
1 <xsl:stylesheet version="1.0"
2   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3   xmlns:sfa="http://developer.apple.com/namespaces/sfa"
4   xmlns:sf="http://developer.apple.com/namespaces/sf"
5   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6   xmlns:sl="http://developer.apple.com/namespaces/sl"
7   xmlns:xi="http://www.w3.org/2001/XInclude"
8   xmlns:doc='http://docbook.org/ns/docbook'
9   xmlns:rnd='http://docbook.org/ns/docbook/roundtrip'
10   exclude-result-prefixes='doc rnd xi'>
11
12   <xsl:import href='dbk2wp.xsl'/>
13
14   <xsl:output method="xml" indent='yes' encoding='ascii'/>
15
16   <!-- ********************************************************************
17        $Id: dbk2pages.xsl 7701 2008-02-22 06:07:31Z balls $
18        ********************************************************************
19
20        This file is part of the XSL DocBook Stylesheet distribution.
21        See ../README or http://docbook.sf.net/release/xsl/current/ for
22        copyright and other information.
23
24        ******************************************************************** -->
25
26   <xsl:include href='../VERSION'/>
27   <xsl:include href='param.xsl'/>
28
29   <xsl:variable name='templatedoc' select='document($pages.template)'/>
30
31   <!-- Find all tables in the document once,
32           as we will be iterating over them in more than one place.
33       -->
34   <xsl:variable name='rnd:pages-tables'
35     select='//doc:table|//doc:informaltable'/>
36
37   <!-- Lookup style identifiers from their user-visible name -->
38   <xsl:variable name='paragraph-styles'
39                 select='$templatedoc//sf:paragraphstyle'/>
40   <xsl:variable name='character-styles'
41                 select='$templatedoc//sf:characterstyle'/>
42
43   <xsl:template match="/" name='pages.top'>
44     <xsl:param name='doc' select='/'/>
45
46     <xsl:if test='not($pages.template)'>
47       <xsl:message terminate='yes'>Please specify the template document with the "pages.template" parameter</xsl:message>
48     </xsl:if>
49     <xsl:if test='not($templatedoc)'>
50       <xsl:message terminate='yes'>Unable to open template document "<xsl:value-of select='$pages.template'/>"</xsl:message>
51     </xsl:if>
52
53     <sl:document
54       sfa:ID="SLPublicationModel-0"
55       sl:version="72007061400"
56       sl:generator="slingshot"
57       sl:app_build_date="Sep 26 2007, 14:46:11">
58
59       <xsl:apply-templates select='$templatedoc/sl:document/*[not(self::sf:text-storage)]'
60         mode='doc:copy'/>
61
62       <xsl:apply-templates select='$doc/*'
63         mode='doc:toplevel'/>
64
65       <xsl:apply-templates select='$templatedoc/sl:document/sf:text-storage/following-sibling::*'
66         mode='doc:copy'/>
67     </sl:document>
68   </xsl:template>
69
70   <xsl:template match='sf:calc-engine' mode='doc:copy'>
71     <xsl:copy>
72       <xsl:apply-templates select='@*' mode='doc:copy'/>
73
74       <xsl:choose>
75         <xsl:when test='not(sf:calc-engine-entities)'>
76           <xsl:call-template name='rnd:pages-make-calc-engine-entities'/>
77           <xsl:apply-templates mode='doc:copy'/>
78         </xsl:when>
79         <xsl:otherwise>
80           <xsl:apply-templates mode='doc:copy'/>
81         </xsl:otherwise>
82       </xsl:choose>
83     </xsl:copy>
84   </xsl:template>
85   <xsl:template name='rnd:pages-make-calc-engine-entities'
86     match='sf:calc-engine-entities' mode='doc:copy'>
87     <sf:calc-engine-entities>
88       <xsl:choose>
89         <xsl:when test='self::calc-engine-entities'>
90           <xsl:apply-templates select='@*' mode='doc:copy'/>
91           <xsl:apply-templates mode='doc:copy'/>
92         </xsl:when>
93         <xsl:otherwise>
94           <xsl:attribute name='sfa:ID'>
95             <xsl:text>DocBookRoundtrip-1</xsl:text>
96           </xsl:attribute>
97         </xsl:otherwise>
98       </xsl:choose>
99
100       <xsl:for-each select='$rnd:pages-tables'>
101         <xsl:variable name='num' select='position()'/>
102
103         <xsl:variable name='num-cells'
104           select='count(doc:tgroup/*/doc:row/doc:entry)'/>
105         <xsl:variable name='num-rows' select='count(doc:tgroup/*/doc:row)'/>
106         <xsl:variable name='num-cols' select='$num-cells div $num-rows'/>
107
108         <sf:tabular-model sfa:ID='SFTTableModel-{$num}' sf:name='Unnamed Table'
109           sfa:id=''
110           sf:num-header-rows='{count(doc:tgroup/doc:thead/doc:row)}' sf:num-footer-rows='{count(doc:tgroup/doc:tfoot/doc:row)}'
111           sf:num-header-columns='0' sf:name-is-visible='false' sf:grouping-enabled='false'>
112           <sf:grid sfa:ID='SFTTableGrid-{$num}'
113             sf:ocnt='{$num-cells}'
114             sf:numcols='{$num-cols}'
115             sf:numrows='{$num-rows}'
116             sf:hiddennumcols='0' sf:hiddennumrows='0'>
117             <sf:columns sf:count='{$num-cols}'>
118               <xsl:apply-templates select='doc:tgroup/doc:colspec'
119                 mode='rnd:pages-table-colspec'/>
120             </sf:columns>
121             <sf:vertical-gridline-styles sf:array-size='0'/> <!-- TODO: borders -->
122             <sf:rows sf:count='{$num-rows}'>
123               <xsl:call-template name='rnd:pages-make-table-rowspecs'>
124                 <xsl:with-param name='number' select='$num-rows'/>
125               </xsl:call-template>
126             </sf:rows>
127             <sf:horizontal-gridline-styles sf:array-size='0'/> <!-- TODO: borders -->
128             <sf:datasource sfa:ID='SFTConcreteTableDataSource-{$num}'>
129               <xsl:apply-templates select='doc:tgroup/doc:thead/doc:row'
130                 mode='rnd:pages-table-data'>
131                 <xsl:with-param name='start-row' select='0'/>
132               </xsl:apply-templates>
133               <xsl:apply-templates select='doc:tgroup/doc:tbody/doc:row'
134                 mode='rnd:pages-table-data'>
135                 <xsl:with-param name='start-row' select='count(doc:tgroup/doc:thead/doc:row)'/>
136               </xsl:apply-templates>
137               <xsl:apply-templates select='doc:tgroup/doc:tfoot/doc:row'
138                 mode='rnd:pages-table-data'>
139                 <xsl:with-param name='start-row' select='count(doc:tgroup/doc:thead/doc:row) + count(doc:tgroup/doc:tbody/doc:row)'/>
140               </xsl:apply-templates>
141             </sf:datasource>
142             <sf:sort sfa:ID='DocBookPagesTableNSArray-{$num}'>
143               <sf:sort-spec sfa:ID='SFTTableSortSpec-{$num}'
144                 sf:sort-col='0' sf:sort-order='true'/>
145             </sf:sort>
146             <sf:filterset sfa:ID='SFTTableFilterSet-{$num}'
147               sf:type='0' sf:enabled='false' sf:spec-count='1'>
148               <sf:filterspec sfa:ID='SFTTableFilterSpec-{$num}'
149                 sf:filtercol='0' sf:predicate='1' sf:keyscale='0' sf:key1=''/>
150             </sf:filterset>
151             <sf:cell-comment-mapping sfa:ID='DocBookPagesTableCellCommentNSMutableDictionary-{$num}'/>
152             <sf:error_warning_mapping sfa:ID='DocBookPagesTableErrorWarningNSMutableDictionary-{$num}'/>
153           </sf:grid>
154         </sf:tabular-model>
155       </xsl:for-each>
156     </sf:calc-engine-entities>
157   </xsl:template>
158
159   <xsl:template match='doc:row' mode='rnd:pages-table-data'>
160     <xsl:param name='start-row' select='0'/>
161     <xsl:apply-templates mode='rnd:pages-table-data'>
162       <xsl:with-param name='row'
163         select='$start-row + count(preceding-sibling::doc:row)'/>
164     </xsl:apply-templates>
165   </xsl:template>
166   <xsl:template match='doc:entry' mode='rnd:pages-table-data'>
167     <xsl:param name='row' select='0'/>
168     <sf:text-cell sf:flags='4' sf:col='{count(preceding-sibling::doc:entry)}' sf:row='{$row}'>
169       <sf:cell-style-ref sfa:IDREF='SFTCellStyle-3'/>
170       <sf:content-size sfa:w='60' sfa:h='25'/>
171       <sf:cell-text>
172         <xsl:choose>
173           <xsl:when test='count(.//*) > 2'>
174             <sf:cell-storage>
175               <sf:stylesheet-ref sfa:IDREF="SFSStylesheet-1"/>
176               <sf:text-body>
177                 <xsl:apply-templates/>
178               </sf:text-body>
179             </sf:cell-storage>
180           </xsl:when>
181           <xsl:otherwise>
182             <xsl:attribute name='sfa:string'>
183               <xsl:apply-templates mode='rnd:pages-table-text'/>
184             </xsl:attribute>
185           </xsl:otherwise>
186         </xsl:choose>
187       </sf:cell-text>
188     </sf:text-cell>
189   </xsl:template>
190
191   <xsl:template name='rnd:pages-make-table-rowspecs'>
192     <xsl:param name='number' select='0'/>
193
194     <xsl:choose>
195       <xsl:when test='$number &lt;= 0'/>
196       <xsl:otherwise>
197         <sf:grid-row sf:height='25' sf:fitting-height='25'/>
198         <xsl:call-template name='rnd:pages-make-table-rowspecs'>
199           <xsl:with-param name='number' select='$number - 1'/>
200         </xsl:call-template>
201       </xsl:otherwise>
202     </xsl:choose>
203   </xsl:template>
204
205   <xsl:template match='doc:colspec' mode='rnd:pages-table-colspec'>
206     <sf:grid-column sf:width='{@colwidth}' sf:preferred-width='{@colwidth}'
207       sf:fitting-width='{@colwidth}'/>
208   </xsl:template>
209
210   <xsl:template name='doc:make-body'>
211     <xsl:param name='content'>
212       <xsl:apply-templates mode='doc:body'/>
213     </xsl:param>
214
215     <sf:text-storage sf:kind='body' sfa:ID='SFWPStorage-7'>
216       <sf:stylesheet-ref sfa:IDREF='SFSStylesheet-1'/>
217
218       <xsl:if test='$rnd:pages-tables'>
219         <sf:attachments>
220           <xsl:for-each select='$rnd:pages-tables'>
221             <xsl:variable name='num' select='position()'/>
222
223             <sf:attachment sfa:ID='SFTTableAttachment-{$num}'
224               sfa:sfclass='' sf:kind='tabular-attachment'>
225               <sf:tabular-info sfa:ID='SFTTableInfo-{$num}'>
226                 <sf:geometry sfa:ID='DocBookAffineGeometry-{$num}'
227                   sf:sizesLocked='true'>
228                   <sf:naturalSize sfa:w='480' sfa:h='126'/>
229                   <sf:size sfa:w='480' sfa:h='126'/>
230                   <sf:position sfa:x='0' sfa:y='0'/>
231                 </sf:geometry>
232                 <sf:style>
233                   <sf:tabular-style-ref sfa:IDREF='SFTTableStyle-0'/>
234                 </sf:style>
235                 <sf:tabular-model-ref sfa:IDREF='SFTTableModel-{$num}'/>
236               </sf:tabular-info>
237               <sf:position sfa:x='0' sfa:y='0'/>
238             </sf:attachment>
239
240           </xsl:for-each>
241         </sf:attachments>
242       </xsl:if>
243
244       <sf:text-body>
245         <sf:page-start sf:page-index='0'/>
246         <sf:container-hint sf:page-index="0" sf:cindex="0" sf:sindex="0" sf:lindex="0" sf:frame-x="56.692913055419922" sf:frame-y="56.692913055419922" sf:frame-w="481.61416625976562" sf:frame-h="714" sf:anchor-loc="0"/>
247
248         <sf:section sf:name="Chapter 1" sf:style="section-style-0">
249           <sf:layout sf:style="layout-style-20">
250             <xsl:copy-of select='$content'/>
251           </sf:layout>
252         </sf:section>
253       </sf:text-body>
254     </sf:text-storage>
255   </xsl:template>
256
257   <xsl:template name='doc:make-subsection'>
258     <xsl:param name='content'>
259       <xsl:apply-templates mode='doc:body'/>
260     </xsl:param>
261
262     <xsl:copy-of select='$content'/>
263   </xsl:template>
264
265   <xsl:template name='doc:make-paragraph'>
266     <xsl:param name='style' select='"unknown"'/>
267     <xsl:param name='content'>
268       <xsl:apply-templates mode='doc:body'/>
269     </xsl:param>
270     <xsl:param name='outline.level' select='0'/>
271     <xsl:param name='attributes.node' select='.'/>
272
273     <sf:p>
274       <xsl:if test='$style != ""'>
275         <xsl:attribute name='sf:style'>
276           <xsl:call-template name='doc:lookup-paragraph-style'>
277             <xsl:with-param name='style' select='$style'/>
278           </xsl:call-template>
279         </xsl:attribute>
280       </xsl:if>
281
282       <xsl:call-template name='attributes'>
283         <xsl:with-param name='node' select='$attributes.node'/>
284       </xsl:call-template>
285
286       <xsl:copy-of select='$content'/>
287       <sf:br/>
288     </sf:p>
289   </xsl:template>
290
291   <xsl:template name='doc:make-phrase'>
292     <xsl:param name='style' select='""'/>
293     <xsl:param name='italic' select='0'/>
294     <xsl:param name='bold' select='0'/>
295     <xsl:param name='content'>
296       <xsl:apply-templates mode='doc:phrase'/>
297     </xsl:param>
298
299     <!-- TODO: handle italic and bold parameters -->
300
301     <xsl:choose>
302       <xsl:when test='$style != ""'>
303         <sf:span>
304           <xsl:attribute name='sf:style'>
305             <xsl:call-template name='doc:lookup-character-style'>
306               <xsl:with-param name='style' select='$style'/>
307             </xsl:call-template>
308           </xsl:attribute>
309
310           <xsl:copy-of select='$content'/>
311         </sf:span>
312       </xsl:when>
313       <xsl:otherwise>
314         <xsl:copy-of select='$content'/>
315       </xsl:otherwise>
316     </xsl:choose>
317   </xsl:template>
318
319   <xsl:template name='doc:make-hyperlink'>
320     <xsl:param name='target'/>
321     <xsl:param name='content'>
322       <xsl:apply-templates mode='doc:body'/>
323     </xsl:param>
324
325     <sf:link href='{$target}'>
326       <sf:span>
327         <xsl:attribute name='sf:style'>
328           <xsl:call-template name='doc:lookup-character-style'>
329             <xsl:with-param name='style'>email</xsl:with-param>
330           </xsl:call-template>
331         </xsl:attribute>
332         <xsl:copy-of select='$content'/>
333       </sf:span>
334     </sf:link>
335   </xsl:template>
336
337   <xsl:template name='doc:make-table'>
338     <xsl:param name='columns'/>
339     <xsl:param name='content'>
340       <xsl:apply-templates mode='doc:body'/>
341     </xsl:param>
342
343     <xsl:variable name='this' select='.'/>
344
345     <sf:p>
346       <xsl:attribute name='sf:style'>
347         <xsl:call-template name='doc:lookup-paragraph-style'>
348           <xsl:with-param name='style' select='"para"'/>
349         </xsl:call-template>
350       </xsl:attribute>
351
352       <sf:attachment-ref sf:kind='tabular-attachment'>
353         <xsl:attribute name='sfa:IDREF'>
354           <xsl:text>SFTTableAttachment-</xsl:text>
355           <xsl:for-each select='$rnd:pages-tables'>
356             <xsl:if test='generate-id() = generate-id($this)'>
357               <xsl:value-of select='position()'/>
358             </xsl:if>
359           </xsl:for-each>
360         </xsl:attribute>
361       </sf:attachment-ref>
362       <sf:br/>
363     </sf:p>
364   </xsl:template>
365
366   <xsl:template name='doc:make-column'>
367     <xsl:param name='width' select='0'/>
368   </xsl:template>
369
370   <xsl:template name='doc:make-table-row'>
371     <xsl:param name='content'>
372       <xsl:apply-templates mode='doc:body'/>
373     </xsl:param>
374     <xsl:param name='is-header' select='false()'/>
375   </xsl:template>
376
377   <xsl:template name='doc:make-table-cell'>
378     <xsl:param name='width' select='0'/>
379     <xsl:param name='hidden' select='false()'/>
380     <xsl:param name='rowspan' select='1'/>
381     <xsl:param name='colspan' select='1'/>
382     <xsl:param name='content'>
383       <xsl:apply-templates mode='doc:body'/>
384     </xsl:param>
385   </xsl:template>
386
387   <xsl:template name='doc:make-soft-break'>
388     <sf:br/>
389   </xsl:template>
390
391   <xsl:template name='attributes'>
392     <xsl:param name='node' select='.'/>
393
394     <xsl:for-each select='$node/@*'>
395       <sf:span>
396         <xsl:attribute name='sf:style'>
397           <xsl:call-template name='doc:lookup-character-style'>
398             <xsl:with-param name='style'>attribute-name</xsl:with-param>
399           </xsl:call-template>
400         </xsl:attribute>
401         <xsl:value-of select='name()'/>
402       </sf:span>
403       <sf:span>
404         <xsl:attribute name='sf:style'>
405           <xsl:call-template name='doc:lookup-character-style'>
406             <xsl:with-param name='style'>attribute-value</xsl:with-param>
407           </xsl:call-template>
408         </xsl:attribute>
409         <xsl:value-of select='.'/>
410       </sf:span>
411     </xsl:for-each>
412   </xsl:template>
413
414   <xsl:template name='doc:lookup-paragraph-style'>
415     <xsl:param name='style'/>
416
417     <xsl:variable name='style.cooked'>
418       <xsl:choose>
419         <xsl:when test='$style = "Normal"'>para</xsl:when>
420         <xsl:otherwise>
421           <xsl:value-of select='$style'/>
422         </xsl:otherwise>
423       </xsl:choose>
424     </xsl:variable>
425
426     <xsl:if test='not($paragraph-styles[@sf:name = $style.cooked])'>
427       <xsl:message>unable to find paragraph style "<xsl:value-of select='$style.cooked'/>"</xsl:message>
428     </xsl:if>
429
430     <xsl:value-of select='$paragraph-styles[@sf:name = $style.cooked]/@sf:ident'/>
431   </xsl:template>
432   <xsl:template name='doc:lookup-character-style'>
433     <xsl:param name='style'/>
434
435     <xsl:if test='not($character-styles[@sf:name = $style])'>
436       <xsl:message>unable to find character style "<xsl:value-of select='$style'/>"</xsl:message>
437     </xsl:if>
438
439     <xsl:value-of select='$character-styles[@sf:name = $style]/@sf:ident'/>
440   </xsl:template>
441 </xsl:stylesheet>