chunktoc.xsl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  2. xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
  3. version="1.0"
  4. exclude-result-prefixes="doc">
  5. <!-- ********************************************************************
  6. $Id: chunktoc.xsl 9286 2012-04-19 10:10:58Z bobstayton $
  7. ********************************************************************
  8. This file is part of the XSL DocBook Stylesheet distribution.
  9. See ../README or http://docbook.sf.net/release/xsl/current/ for
  10. copyright and other information.
  11. ******************************************************************** -->
  12. <!-- ==================================================================== -->
  13. <xsl:import href="docbook.xsl"/>
  14. <xsl:import href="chunk-common.xsl"/>
  15. <xsl:template name="chunk">
  16. <xsl:param name="node" select="."/>
  17. <!-- returns 1 if $node is a chunk -->
  18. <xsl:variable name="id">
  19. <xsl:call-template name="object.id">
  20. <xsl:with-param name="object" select="$node"/>
  21. </xsl:call-template>
  22. </xsl:variable>
  23. <xsl:variable name="chunks" select="document($chunk.toc,/)"/>
  24. <xsl:choose>
  25. <xsl:when test="$chunks//tocentry[@linkend=$id]">1</xsl:when>
  26. <xsl:otherwise>0</xsl:otherwise>
  27. </xsl:choose>
  28. </xsl:template>
  29. <!-- ==================================================================== -->
  30. <xsl:template match="*" mode="chunk-filename">
  31. <!-- returns the filename of a chunk -->
  32. <xsl:variable name="id">
  33. <xsl:call-template name="object.id"/>
  34. </xsl:variable>
  35. <xsl:variable name="chunks" select="document($chunk.toc,/)"/>
  36. <xsl:variable name="chunk" select="$chunks//tocentry[@linkend=$id]"/>
  37. <xsl:variable name="filename">
  38. <xsl:call-template name="pi.dbhtml_filename">
  39. <xsl:with-param name="node" select="$chunk"/>
  40. </xsl:call-template>
  41. </xsl:variable>
  42. <xsl:choose>
  43. <xsl:when test="$chunk">
  44. <xsl:value-of select="$filename"/>
  45. </xsl:when>
  46. <xsl:otherwise>
  47. <xsl:apply-templates select="parent::*" mode="chunk-filename"/>
  48. </xsl:otherwise>
  49. </xsl:choose>
  50. </xsl:template>
  51. <!-- ==================================================================== -->
  52. <xsl:template name="process-chunk">
  53. <xsl:variable name="id">
  54. <xsl:call-template name="object.id"/>
  55. </xsl:variable>
  56. <xsl:variable name="chunks" select="document($chunk.toc,/)"/>
  57. <xsl:variable name="chunk" select="$chunks//tocentry[@linkend=$id]"/>
  58. <xsl:variable name="prev-id"
  59. select="($chunk/preceding::tocentry
  60. |$chunk/ancestor::tocentry)[last()]/@linkend"/>
  61. <xsl:variable name="next-id"
  62. select="($chunk/following::tocentry
  63. |$chunk/child::tocentry)[1]/@linkend"/>
  64. <xsl:variable name="prev" select="key('id',$prev-id)"/>
  65. <xsl:variable name="next" select="key('id',$next-id)"/>
  66. <xsl:variable name="ischunk">
  67. <xsl:call-template name="chunk"/>
  68. </xsl:variable>
  69. <xsl:variable name="chunkfn">
  70. <xsl:if test="$ischunk='1'">
  71. <xsl:apply-templates mode="chunk-filename" select="."/>
  72. </xsl:if>
  73. </xsl:variable>
  74. <xsl:variable name="filename">
  75. <xsl:call-template name="make-relative-filename">
  76. <xsl:with-param name="base.dir" select="$chunk.base.dir"/>
  77. <xsl:with-param name="base.name" select="$chunkfn"/>
  78. </xsl:call-template>
  79. </xsl:variable>
  80. <xsl:choose>
  81. <xsl:when test="$ischunk = 0">
  82. <xsl:apply-imports/>
  83. </xsl:when>
  84. <xsl:otherwise>
  85. <xsl:call-template name="write.chunk">
  86. <xsl:with-param name="filename" select="$filename"/>
  87. <xsl:with-param name="content">
  88. <xsl:call-template name="chunk-element-content">
  89. <xsl:with-param name="prev" select="$prev"/>
  90. <xsl:with-param name="next" select="$next"/>
  91. </xsl:call-template>
  92. </xsl:with-param>
  93. <xsl:with-param name="quiet" select="$chunk.quietly"/>
  94. </xsl:call-template>
  95. </xsl:otherwise>
  96. </xsl:choose>
  97. </xsl:template>
  98. <!-- ==================================================================== -->
  99. <xsl:template match="set">
  100. <xsl:call-template name="process-chunk"/>
  101. </xsl:template>
  102. <xsl:template match="book">
  103. <xsl:call-template name="process-chunk"/>
  104. </xsl:template>
  105. <xsl:template match="book/appendix">
  106. <xsl:call-template name="process-chunk"/>
  107. </xsl:template>
  108. <xsl:template match="book/glossary">
  109. <xsl:call-template name="process-chunk"/>
  110. </xsl:template>
  111. <xsl:template match="book/bibliography">
  112. <xsl:call-template name="process-chunk"/>
  113. </xsl:template>
  114. <xsl:template match="dedication" mode="dedication">
  115. <xsl:call-template name="process-chunk"/>
  116. </xsl:template>
  117. <xsl:template match="preface|chapter">
  118. <xsl:call-template name="process-chunk"/>
  119. </xsl:template>
  120. <xsl:template match="part|reference">
  121. <xsl:call-template name="process-chunk"/>
  122. </xsl:template>
  123. <xsl:template match="refentry">
  124. <xsl:call-template name="process-chunk"/>
  125. </xsl:template>
  126. <xsl:template match="colophon">
  127. <xsl:call-template name="process-chunk"/>
  128. </xsl:template>
  129. <xsl:template match="article">
  130. <xsl:call-template name="process-chunk"/>
  131. </xsl:template>
  132. <xsl:template match="topic">
  133. <xsl:call-template name="process-chunk"/>
  134. </xsl:template>
  135. <xsl:template match="article/appendix">
  136. <xsl:call-template name="process-chunk"/>
  137. </xsl:template>
  138. <xsl:template match="article/glossary">
  139. <xsl:call-template name="process-chunk"/>
  140. </xsl:template>
  141. <xsl:template match="article/bibliography">
  142. <xsl:call-template name="process-chunk"/>
  143. </xsl:template>
  144. <xsl:template match="sect1|sect2|sect3|sect4|sect5|section">
  145. <xsl:variable name="ischunk">
  146. <xsl:call-template name="chunk"/>
  147. </xsl:variable>
  148. <xsl:choose>
  149. <xsl:when test="$ischunk != 0">
  150. <xsl:call-template name="process-chunk"/>
  151. </xsl:when>
  152. <xsl:otherwise>
  153. <xsl:apply-imports/>
  154. </xsl:otherwise>
  155. </xsl:choose>
  156. </xsl:template>
  157. <xsl:template match="setindex
  158. |book/index
  159. |article/index">
  160. <!-- some implementations use completely empty index tags to indicate -->
  161. <!-- where an automatically generated index should be inserted. so -->
  162. <!-- if the index is completely empty, skip it. -->
  163. <xsl:if test="count(*)>0 or $generate.index != '0'">
  164. <xsl:call-template name="process-chunk"/>
  165. </xsl:if>
  166. </xsl:template>
  167. <!-- ==================================================================== -->
  168. <xsl:template match="/">
  169. <!-- * Get a title for current doc so that we let the user -->
  170. <!-- * know what document we are processing at this point. -->
  171. <xsl:variable name="doc.title">
  172. <xsl:call-template name="get.doc.title"/>
  173. </xsl:variable>
  174. <xsl:choose>
  175. <xsl:when test="$chunk.toc = ''">
  176. <xsl:message terminate="yes">
  177. <xsl:text>The chunk.toc file is not set.</xsl:text>
  178. </xsl:message>
  179. </xsl:when>
  180. <!-- Hack! If someone hands us a DocBook V5.x or DocBook NG document,
  181. toss the namespace and continue. Use the docbook5 namespaced
  182. stylesheets for DocBook5 if you don't want to use this feature.-->
  183. <!-- include extra test for Xalan quirk -->
  184. <xsl:when test="$exsl.node.set.available != 0
  185. and (*/self::ng:* or */self::db:*)">
  186. <xsl:call-template name="log.message">
  187. <xsl:with-param name="level">Note</xsl:with-param>
  188. <xsl:with-param name="source" select="$doc.title"/>
  189. <xsl:with-param name="context-desc">
  190. <xsl:text>namesp. cut</xsl:text>
  191. </xsl:with-param>
  192. <xsl:with-param name="message">
  193. <xsl:text>stripped namespace before processing</xsl:text>
  194. </xsl:with-param>
  195. </xsl:call-template>
  196. <xsl:variable name="nons">
  197. <xsl:apply-templates mode="stripNS"/>
  198. </xsl:variable>
  199. <xsl:call-template name="log.message">
  200. <xsl:with-param name="level">Note</xsl:with-param>
  201. <xsl:with-param name="source" select="$doc.title"/>
  202. <xsl:with-param name="context-desc">
  203. <xsl:text>namesp. cut</xsl:text>
  204. </xsl:with-param>
  205. <xsl:with-param name="message">
  206. <xsl:text>processing stripped document</xsl:text>
  207. </xsl:with-param>
  208. </xsl:call-template>
  209. <xsl:apply-templates select="exsl:node-set($nons)"/>
  210. </xsl:when>
  211. <!-- Can't process unless namespace removed -->
  212. <xsl:when test="*/self::ng:* or */self::db:*">
  213. <xsl:message terminate="yes">
  214. <xsl:text>Unable to strip the namespace from DB5 document,</xsl:text>
  215. <xsl:text> cannot proceed.</xsl:text>
  216. </xsl:message>
  217. </xsl:when>
  218. <xsl:otherwise>
  219. <xsl:choose>
  220. <xsl:when test="$rootid != ''">
  221. <xsl:choose>
  222. <xsl:when test="count(key('id',$rootid)) = 0">
  223. <xsl:message terminate="yes">
  224. <xsl:text>ID '</xsl:text>
  225. <xsl:value-of select="$rootid"/>
  226. <xsl:text>' not found in document.</xsl:text>
  227. </xsl:message>
  228. </xsl:when>
  229. <xsl:otherwise>
  230. <xsl:if test="$collect.xref.targets = 'yes' or
  231. $collect.xref.targets = 'only'">
  232. <xsl:apply-templates select="key('id', $rootid)"
  233. mode="collect.targets"/>
  234. </xsl:if>
  235. <xsl:if test="$collect.xref.targets != 'only'">
  236. <xsl:apply-templates select="key('id',$rootid)"
  237. mode="process.root"/>
  238. <xsl:if test="$tex.math.in.alt != ''">
  239. <xsl:apply-templates select="key('id',$rootid)"
  240. mode="collect.tex.math"/>
  241. </xsl:if>
  242. <xsl:if test="$generate.manifest != 0">
  243. <xsl:call-template name="generate.manifest">
  244. <xsl:with-param name="node" select="key('id',$rootid)"/>
  245. </xsl:call-template>
  246. </xsl:if>
  247. </xsl:if>
  248. </xsl:otherwise>
  249. </xsl:choose>
  250. </xsl:when>
  251. <xsl:otherwise>
  252. <xsl:if test="$collect.xref.targets = 'yes' or
  253. $collect.xref.targets = 'only'">
  254. <xsl:apply-templates select="/" mode="collect.targets"/>
  255. </xsl:if>
  256. <xsl:if test="$collect.xref.targets != 'only'">
  257. <xsl:apply-templates select="/" mode="process.root"/>
  258. <xsl:if test="$tex.math.in.alt != ''">
  259. <xsl:apply-templates select="/" mode="collect.tex.math"/>
  260. </xsl:if>
  261. <xsl:if test="$generate.manifest != 0">
  262. <xsl:call-template name="generate.manifest">
  263. <xsl:with-param name="node" select="/"/>
  264. </xsl:call-template>
  265. </xsl:if>
  266. </xsl:if>
  267. </xsl:otherwise>
  268. </xsl:choose>
  269. </xsl:otherwise>
  270. </xsl:choose>
  271. </xsl:template>
  272. <xsl:template match="*" mode="process.root">
  273. <xsl:apply-templates select="."/>
  274. <xsl:call-template name="generate.css"/>
  275. </xsl:template>
  276. <xsl:template name="make.lots">
  277. <xsl:param name="toc.params" select="''"/>
  278. <xsl:param name="toc"/>
  279. <xsl:variable name="lots">
  280. <xsl:if test="contains($toc.params, 'toc')">
  281. <xsl:copy-of select="$toc"/>
  282. </xsl:if>
  283. <xsl:if test="contains($toc.params, 'figure')">
  284. <xsl:choose>
  285. <xsl:when test="$chunk.separate.lots != '0'">
  286. <xsl:call-template name="make.lot.chunk">
  287. <xsl:with-param name="type" select="'figure'"/>
  288. <xsl:with-param name="lot">
  289. <xsl:call-template name="list.of.titles">
  290. <xsl:with-param name="titles" select="'figure'"/>
  291. <xsl:with-param name="nodes" select=".//figure"/>
  292. </xsl:call-template>
  293. </xsl:with-param>
  294. </xsl:call-template>
  295. </xsl:when>
  296. <xsl:otherwise>
  297. <xsl:call-template name="list.of.titles">
  298. <xsl:with-param name="titles" select="'figure'"/>
  299. <xsl:with-param name="nodes" select=".//figure"/>
  300. </xsl:call-template>
  301. </xsl:otherwise>
  302. </xsl:choose>
  303. </xsl:if>
  304. <xsl:if test="contains($toc.params, 'table')">
  305. <xsl:choose>
  306. <xsl:when test="$chunk.separate.lots != '0'">
  307. <xsl:call-template name="make.lot.chunk">
  308. <xsl:with-param name="type" select="'table'"/>
  309. <xsl:with-param name="lot">
  310. <xsl:call-template name="list.of.titles">
  311. <xsl:with-param name="titles" select="'table'"/>
  312. <xsl:with-param name="nodes" select=".//table"/>
  313. </xsl:call-template>
  314. </xsl:with-param>
  315. </xsl:call-template>
  316. </xsl:when>
  317. <xsl:otherwise>
  318. <xsl:call-template name="list.of.titles">
  319. <xsl:with-param name="titles" select="'table'"/>
  320. <xsl:with-param name="nodes" select=".//table"/>
  321. </xsl:call-template>
  322. </xsl:otherwise>
  323. </xsl:choose>
  324. </xsl:if>
  325. <xsl:if test="contains($toc.params, 'example')">
  326. <xsl:choose>
  327. <xsl:when test="$chunk.separate.lots != '0'">
  328. <xsl:call-template name="make.lot.chunk">
  329. <xsl:with-param name="type" select="'example'"/>
  330. <xsl:with-param name="lot">
  331. <xsl:call-template name="list.of.titles">
  332. <xsl:with-param name="titles" select="'example'"/>
  333. <xsl:with-param name="nodes" select=".//example"/>
  334. </xsl:call-template>
  335. </xsl:with-param>
  336. </xsl:call-template>
  337. </xsl:when>
  338. <xsl:otherwise>
  339. <xsl:call-template name="list.of.titles">
  340. <xsl:with-param name="titles" select="'example'"/>
  341. <xsl:with-param name="nodes" select=".//example"/>
  342. </xsl:call-template>
  343. </xsl:otherwise>
  344. </xsl:choose>
  345. </xsl:if>
  346. <xsl:if test="contains($toc.params, 'equation')">
  347. <xsl:choose>
  348. <xsl:when test="$chunk.separate.lots != '0'">
  349. <xsl:call-template name="make.lot.chunk">
  350. <xsl:with-param name="type" select="'equation'"/>
  351. <xsl:with-param name="lot">
  352. <xsl:call-template name="list.of.titles">
  353. <xsl:with-param name="titles" select="'equation'"/>
  354. <xsl:with-param name="nodes" select=".//equation"/>
  355. </xsl:call-template>
  356. </xsl:with-param>
  357. </xsl:call-template>
  358. </xsl:when>
  359. <xsl:otherwise>
  360. <xsl:call-template name="list.of.titles">
  361. <xsl:with-param name="titles" select="'equation'"/>
  362. <xsl:with-param name="nodes" select=".//equation"/>
  363. </xsl:call-template>
  364. </xsl:otherwise>
  365. </xsl:choose>
  366. </xsl:if>
  367. <xsl:if test="contains($toc.params, 'procedure')">
  368. <xsl:choose>
  369. <xsl:when test="$chunk.separate.lots != '0'">
  370. <xsl:call-template name="make.lot.chunk">
  371. <xsl:with-param name="type" select="'procedure'"/>
  372. <xsl:with-param name="lot">
  373. <xsl:call-template name="list.of.titles">
  374. <xsl:with-param name="titles" select="'procedure'"/>
  375. <xsl:with-param name="nodes" select=".//procedure[title]"/>
  376. </xsl:call-template>
  377. </xsl:with-param>
  378. </xsl:call-template>
  379. </xsl:when>
  380. <xsl:otherwise>
  381. <xsl:call-template name="list.of.titles">
  382. <xsl:with-param name="titles" select="'procedure'"/>
  383. <xsl:with-param name="nodes" select=".//procedure[title]"/>
  384. </xsl:call-template>
  385. </xsl:otherwise>
  386. </xsl:choose>
  387. </xsl:if>
  388. </xsl:variable>
  389. <xsl:if test="string($lots) != ''">
  390. <xsl:choose>
  391. <xsl:when test="$chunk.tocs.and.lots != 0 and not(parent::*)">
  392. <xsl:call-template name="write.chunk">
  393. <xsl:with-param name="filename">
  394. <xsl:call-template name="make-relative-filename">
  395. <xsl:with-param name="base.dir" select="$chunk.base.dir"/>
  396. <xsl:with-param name="base.name">
  397. <xsl:call-template name="dbhtml-dir"/>
  398. <xsl:apply-templates select="." mode="recursive-chunk-filename">
  399. <xsl:with-param name="recursive" select="true()"/>
  400. </xsl:apply-templates>
  401. <xsl:text>-toc</xsl:text>
  402. <xsl:value-of select="$html.ext"/>
  403. </xsl:with-param>
  404. </xsl:call-template>
  405. </xsl:with-param>
  406. <xsl:with-param name="content">
  407. <xsl:call-template name="chunk-element-content">
  408. <xsl:with-param name="prev" select="/foo"/>
  409. <xsl:with-param name="next" select="/foo"/>
  410. <xsl:with-param name="nav.context" select="'toc'"/>
  411. <xsl:with-param name="content">
  412. <h1>
  413. <xsl:apply-templates select="." mode="object.title.markup"/>
  414. </h1>
  415. <xsl:copy-of select="$lots"/>
  416. </xsl:with-param>
  417. </xsl:call-template>
  418. </xsl:with-param>
  419. <xsl:with-param name="quiet" select="$chunk.quietly"/>
  420. </xsl:call-template>
  421. </xsl:when>
  422. <xsl:otherwise>
  423. <xsl:copy-of select="$lots"/>
  424. </xsl:otherwise>
  425. </xsl:choose>
  426. </xsl:if>
  427. </xsl:template>
  428. <xsl:template name="make.lot.chunk">
  429. <xsl:param name="type" select="''"/>
  430. <xsl:param name="lot"/>
  431. <xsl:if test="string($lot) != ''">
  432. <xsl:variable name="filename">
  433. <xsl:call-template name="make-relative-filename">
  434. <xsl:with-param name="base.dir" select="$chunk.base.dir"/>
  435. <xsl:with-param name="base.name">
  436. <xsl:call-template name="dbhtml-dir"/>
  437. <xsl:value-of select="$type"/>
  438. <xsl:text>-toc</xsl:text>
  439. <xsl:value-of select="$html.ext"/>
  440. </xsl:with-param>
  441. </xsl:call-template>
  442. </xsl:variable>
  443. <xsl:variable name="href">
  444. <xsl:call-template name="make-relative-filename">
  445. <xsl:with-param name="base.dir" select="''"/>
  446. <xsl:with-param name="base.name">
  447. <xsl:call-template name="dbhtml-dir"/>
  448. <xsl:value-of select="$type"/>
  449. <xsl:text>-toc</xsl:text>
  450. <xsl:value-of select="$html.ext"/>
  451. </xsl:with-param>
  452. </xsl:call-template>
  453. </xsl:variable>
  454. <xsl:call-template name="write.chunk">
  455. <xsl:with-param name="filename" select="$filename"/>
  456. <xsl:with-param name="content">
  457. <xsl:call-template name="chunk-element-content">
  458. <xsl:with-param name="prev" select="/foo"/>
  459. <xsl:with-param name="next" select="/foo"/>
  460. <xsl:with-param name="nav.context" select="'toc'"/>
  461. <xsl:with-param name="content">
  462. <xsl:copy-of select="$lot"/>
  463. </xsl:with-param>
  464. </xsl:call-template>
  465. </xsl:with-param>
  466. <xsl:with-param name="quiet" select="$chunk.quietly"/>
  467. </xsl:call-template>
  468. <!-- And output a link to this file -->
  469. <div>
  470. <xsl:attribute name="class">
  471. <xsl:text>ListofTitles</xsl:text>
  472. </xsl:attribute>
  473. <a href="{$href}">
  474. <xsl:call-template name="gentext">
  475. <xsl:with-param name="key">
  476. <xsl:choose>
  477. <xsl:when test="$type='table'">ListofTables</xsl:when>
  478. <xsl:when test="$type='figure'">ListofFigures</xsl:when>
  479. <xsl:when test="$type='equation'">ListofEquations</xsl:when>
  480. <xsl:when test="$type='example'">ListofExamples</xsl:when>
  481. <xsl:when test="$type='procedure'">ListofProcedures</xsl:when>
  482. <xsl:otherwise>ListofUnknown</xsl:otherwise>
  483. </xsl:choose>
  484. </xsl:with-param>
  485. </xsl:call-template>
  486. </a>
  487. </div>
  488. </xsl:if>
  489. </xsl:template>
  490. </xsl:stylesheet>