[r17023]: branches / dmccreary / docs / webapp / docs / docbook5 / docbook2xhtml-v2.xqm Maximize Restore History

Download this file

docbook2xhtml-v2.xqm    289 lines (248 with data), 11.3 kB

  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
281
282
283
284
285
286
287
288
module namespace docbook2xhtml="http://xmlopenfoundation.org/docbook2xhtml";
declare default element namespace "http://docbook.org/ns/docbook";
declare namespace db="http://docbook.org/ns/docbook";
declare namespace html="http://www.w3.org/1999/xhtml";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
declare namespace xf="http://www.w3.org/2002/xforms";
(: designed to store resources shared by two or more documents :)
declare variable $docbook2xhtml:site-resources := '/db/doc/resources/';
declare variable $docbook2xhtml:site-images := concat($docbook2xhtml:site-resources, 'images/')
declare variable $docbook2xhtml:site-css := concat($docbook2xhtml:site-resources, 'css/')
declare variable $docbook2xhtml:site-scripts := concat($docbook2xhtml:site-resources, 'scripts/')
(: document-specific resources are stored relative to the rendering output location :)
declare variable $docbook2xhtml:loc-images := 'images/';
declare variable $docbook2xhtml:loc-scripts := 'scripts/';
declare variable $docbook2xhtml:loc-css := 'css/';
declare variable $docbook2xhtml:syntax-highlight := 'yes';
declare variable $docbook2xhtml:include-table-of-contents := 'yes';
declare function docbook2xhtml:main($content as node()*) as item()* {
for $node in $content/node()
return
typeswitch($node)
case text() return $node
case element(article) return docbook2xhtml:article($node)
(: case element(book) return docbook2xhtml:book($node) :)
(: case element(topic) return docbook2xhtml:topic($node) :)
case element(chapter) return docbook2xhtml:chapter($node)
case element(section) return docbook2xhtml:section($node)
(: front matter :)
case element(info) return docbook2xhtml:info($node)
case element(author) return docbook2xhtml:author($node)
case element(revhistory) return docbook2xhtml:revhistory($node)
case element(para) return docbook2xhtml:para($node)
case element(title) return docbook2xhtml:title($node)
case element(listitem) return docbook2xhtml:listitem($node)
case element(emphasis) return docbook2xhtml:emphasis($node)
case element(programlisting) return docbook2xhtml:programlisting($node)
case element(imagedata) return docbook2xhtml:imagedata($node)
default return docbook2xhtml:recurse($node)
};
declare function docbook2xhtml:recurse($node as node()) as item()* {
docbook2xhtml:main($node)
};
declare function docbook2xhtml:article($node as element(article)) as element() {
let $title := $node/info/title/text()
return
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{$title}</title>
<link rel="shortcut icon" href="{$docbook2xhtml:site-images}exist_icon_16x16.ico"/>
<link rel="icon" href="{$docbook2xhtml:site-images}exist_icon_16x16.png"
type="image/png"/>
<meta http-equiv="Content-Type" content="charset=utf-8"/>
<script language="Javascript" type="text/javascript" src="{$docbook2xhtml:site-scripts}curvycorners.js"/>
<link rel="stylesheet" type="text/css" href="{$docbook2xhtml:site-css}docbook.css"/>
<link rel="stylesheet" type="text/css" href="{$docbook2xhtml:site-css}/style.css"/>
{if ($docbook2xhtml:syntax-highlight = 'yes')
then (
<script type="text/javascript" src="{$docbook2xhtml:site-scripts}syntax/shCore.js"/>,
<script type="text/javascript" src="{$docbook2xhtml:site-scripts}syntax/shBrushCss.js"/>,
<script type="text/javascript" src="{$docbook2xhtml:site-scripts}syntax/shBrushJScript.js"/>,
<script type="text/javascript" src="{$docbook2xhtml:site-scripts}syntax/shBrushPlain.js"/>,
<script type="text/javascript" src="{$docbook2xhtml:site-scripts}syntax/shBrushXml.js"/>,
<script type="text/javascript" src="{$docbook2xhtml:site-scripts}syntax/shBrushXQuery.js"/>,
<link type="text/css" rel="stylesheet" href="{$docbook2xhtml:site-css}styles/syntax/shCore.css"/>,
<link type="text/css" rel="Stylesheet" href="{$docbook2xhtml:site-css}styles/syntax/shThemeDefault.css" id="theme"/>,
<script type="text/javascript">
SyntaxHighlighter.config.stripBrs = true;
SyntaxHighlighter.defaults[ 'auto-links'] = false;
SyntaxHighlighter.defaults[ 'wrap-lines'] = false;
SyntaxHighlighter.all();
</script>
) else ()}
</head>
<body>
<div class="content">
{docbook2xhtml:front-matter($node)}
{docbook2xhtml:recurse($node)}
{docbook2xhtml:analytics()}
</div>
</body>
</html>
};
declare function docbook2xhtml:chapter($node as element(chapter)) as element() {
<div class="chapter">
{docbook2xhtml:recurse($node)}
</div>
};
declare function docbook2xhtml:toc($node as element(article)) as element() {
<div class="toc">
<div class="toc-label">Table of Contents</div>
{for $section at $sec-count in $node/section
return
<div class="toc-line">
{$sec-count} {' '} <a href="#s-{$sec-count}">{$section/title/text()}</a>
{for $sub-section at $subsec-count in $section/section
return
<div class="toc-line">
{$sec-count}.{$subsec-count} {' '} <a href="#s-{$sec-count}-{$subsec-count}" >{$sub-section/title/text()} </a>
</div>
}
</div>
}
</div>
};
declare function docbook2xhtml:sections($node as element(section)) as element() {
let $section-number := count($node/../preceding::section)
let $sub-section-number := count($node/preceding::section)
return
<div class="sections">
{for $section at $sec-count in $node/section
return
<div class="section1">
{$sec-count} {' '} <a name="s-{$sec-count}">{$section/title/text()}</a>
{for $sub-section at $subsec-count in $section/section
return
<div class="section2">
{$sec-count}.{$subsec-count} {' '} <a name="ss-{$sec-count}-{$subsec-count}" >{$sub-section/title/text()} </a>
</div>
}
</div>
}
</div>
};
declare function docbook2xhtml:section($node as element(section)) as element() {
if ( name($node/..) = 'article')
then
let $section-number := count($node/preceding-sibling::section) + 1
return
<div class="section1">
<a name="s-{$section-number}"/>
<h1 class="section1">{$node/title/text()}</h1>
{docbook2xhtml:recurse($node)}
</div>
else
let $section-number := count($node/../preceding-sibling::section) + 1
let $sub-section-number := count($node/preceding-sibling::section) + 1
return
<div class="section2">
<a name="s-{$section-number}-{$sub-section-number}"/>
<h2 class="section2">{$node/title/text()}</h2>
{docbook2xhtml:recurse($node)}
</div>
};
(: we are explicitly putting in titles for each section :)
declare function docbook2xhtml:title($node as element(title)) {
''
};
declare function docbook2xhtml:info($node as element(info)) {
''
};
declare function docbook2xhtml:para($node as element(para)) as element() {
<p class="para">
{docbook2xhtml:recurse($node)}
</p>
};
declare function docbook2xhtml:emphasis($node as element(emphasis)) as element() {
if ($node/@role = 'bold')
then <b>{docbook2xhtml:recurse($node)}</b>
else <i>{docbook2xhtml:recurse($node)}</i>
};
declare function docbook2xhtml:programlisting($node as element(programlisting)) as element() {
<pre class="programlisting">
{$node/text()}
{docbook2xhtml:recurse($node)}
</pre>
};
declare function docbook2xhtml:imagedata($node as element(imagedata)) as element() {
<img src="{$docbook2xhtml:loc-images}{$node/@fileref}" width="{$node/contentwidth}" alt-text="{$node/@alt-text}" title="{$node/@annotations}"/>
};
declare function docbook2xhtml:analytics() as element() {
<div class="google-analytics">
</div>
};
declare function docbook2xhtml:front-matter($node) as element() {
<div class="front-matter">
<div class="article-title">{$node/info/title/text()}</div>
<div class="article-subtitle">{$node/info/subtitle/text()}</div>
<div class="version">
Version: {$node/info/revhistory/revision[last()]/revnumber/text()}
Date: {$node/info/revhistory/revision[last()]/date/text()}
</div>
{docbook2xhtml:authors($node)}
{docbook2xhtml:revhistory($node/info/revhistory)}
{docbook2xhtml:toc($node)}
</div>
};
declare function docbook2xhtml:authors($node) as element() {
<div class="authors">
<div class="authors-label">Authors</div>
{for $author in $node/info/author
return docbook2xhtml:author($author)
}
</div>
};
declare function docbook2xhtml:author($person) as element() {
<div class="author">
<span class="personanme">
{$person/personname/honorific/text()} {' '}
{$person/personname/firstname/text()} {' '}
{$person/personname/surname/text()}</span> {', '}
{ if ($person/affiliation/orgname)
then <span class="orgname"> {$person/affiliation/orgname/text()}, </span>
else ()
}
<span class="email">{$person/email/text()} {' '}</span>
</div>
};
declare function docbook2xhtml:revhistory($revhistory) as element() {
<div class="revhistory">
<div class="rev-history-label">Revision History</div>
<table class="rev-history-table">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Who</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{for $rev in $revhistory/revision
return
<tr>
<td>{$rev/revnumber/text()}</td>
<td>{$rev/date/text()}</td>
<td>
{$rev/authorinitials/text()}
{$rev//firstname/text()}
{ if ($rev//surname) then ' ' else '' }
{$rev//surname/text()}
</td>
<td>{$rev/revdescription/para/text()}</td>
</tr>
}
</tbody>
</table>
</div>
};
declare function docbook2xhtml:back-matter($node) as element() {
<div class="back-matter">
{docbook2xhtml:recurse($node)}
</div>
};
declare function docbook2xhtml:listitem($node) as element() {
<div class="list">
{docbook2xhtml:recurse($node)}
</div>
};