formal.xsl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:fo="http://www.w3.org/1999/XSL/Format"
  4. version='1.0'>
  5. <!-- ********************************************************************
  6. $Id$
  7. ********************************************************************
  8. This file is part of the XSL DocBook Stylesheet distribution.
  9. See ../README or http://nwalsh.com/docbook/xsl/ for copyright
  10. and other information.
  11. ******************************************************************** -->
  12. <!-- formal.object creates a basic block containing the
  13. result of processing the object, including its title
  14. and any keep-together properties.
  15. The template calling formal.object may wrap these results in a
  16. float or pgwide block. -->
  17. <xsl:template name="formal.object">
  18. <xsl:param name="placement" select="'before'"/>
  19. <xsl:variable name="id">
  20. <xsl:call-template name="object.id"/>
  21. </xsl:variable>
  22. <xsl:variable name="content">
  23. <xsl:if test="$placement = 'before'">
  24. <xsl:call-template name="formal.object.heading">
  25. <xsl:with-param name="placement" select="$placement"/>
  26. </xsl:call-template>
  27. </xsl:if>
  28. <xsl:apply-templates/>
  29. <xsl:if test="$placement != 'before'">
  30. <xsl:call-template name="formal.object.heading">
  31. <xsl:with-param name="placement" select="$placement"/>
  32. </xsl:call-template>
  33. </xsl:if>
  34. </xsl:variable>
  35. <xsl:variable name="keep.together">
  36. <xsl:call-template name="dbfo-attribute">
  37. <xsl:with-param name="pis"
  38. select="processing-instruction('dbfo')"/>
  39. <xsl:with-param name="attribute" select="'keep-together'"/>
  40. </xsl:call-template>
  41. </xsl:variable>
  42. <xsl:choose>
  43. <!-- tables have their own templates and
  44. are not handled by formal.object -->
  45. <xsl:when test="self::figure">
  46. <fo:block id="{$id}"
  47. xsl:use-attribute-sets="figure.properties">
  48. <xsl:if test="$keep.together != ''">
  49. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  50. select="$keep.together"/></xsl:attribute>
  51. </xsl:if>
  52. <xsl:copy-of select="$content"/>
  53. </fo:block>
  54. </xsl:when>
  55. <xsl:when test="self::example">
  56. <fo:block id="{$id}"
  57. xsl:use-attribute-sets="example.properties">
  58. <xsl:if test="$keep.together != ''">
  59. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  60. select="$keep.together"/></xsl:attribute>
  61. </xsl:if>
  62. <xsl:copy-of select="$content"/>
  63. </fo:block>
  64. </xsl:when>
  65. <xsl:when test="self::equation">
  66. <fo:block id="{$id}"
  67. xsl:use-attribute-sets="equation.properties">
  68. <xsl:if test="$keep.together != ''">
  69. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  70. select="$keep.together"/></xsl:attribute>
  71. </xsl:if>
  72. <xsl:copy-of select="$content"/>
  73. </fo:block>
  74. </xsl:when>
  75. <xsl:when test="self::procedure">
  76. <fo:block id="{$id}"
  77. xsl:use-attribute-sets="procedure.properties">
  78. <xsl:if test="$keep.together != ''">
  79. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  80. select="$keep.together"/></xsl:attribute>
  81. </xsl:if>
  82. <xsl:copy-of select="$content"/>
  83. </fo:block>
  84. </xsl:when>
  85. <xsl:otherwise>
  86. <fo:block id="{$id}"
  87. xsl:use-attribute-sets="formal.object.properties">
  88. <xsl:if test="$keep.together != ''">
  89. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  90. select="$keep.together"/></xsl:attribute>
  91. </xsl:if>
  92. <xsl:copy-of select="$content"/>
  93. </fo:block>
  94. </xsl:otherwise>
  95. </xsl:choose>
  96. </xsl:template>
  97. <xsl:template name="formal.object.heading">
  98. <xsl:param name="object" select="."/>
  99. <xsl:param name="placement" select="'before'"/>
  100. <fo:block xsl:use-attribute-sets="formal.title.properties">
  101. <xsl:choose>
  102. <xsl:when test="$placement = 'before'">
  103. <xsl:attribute
  104. name="keep-with-next.within-column">always</xsl:attribute>
  105. </xsl:when>
  106. <xsl:otherwise>
  107. <xsl:attribute
  108. name="keep-with-previous.within-column">always</xsl:attribute>
  109. </xsl:otherwise>
  110. </xsl:choose>
  111. <xsl:apply-templates select="$object" mode="object.title.markup">
  112. <xsl:with-param name="allow-anchors" select="1"/>
  113. </xsl:apply-templates>
  114. </fo:block>
  115. </xsl:template>
  116. <xsl:template name="informal.object">
  117. <xsl:variable name="id">
  118. <xsl:call-template name="object.id"/>
  119. </xsl:variable>
  120. <xsl:variable name="keep.together">
  121. <xsl:call-template name="dbfo-attribute">
  122. <xsl:with-param name="pis"
  123. select="processing-instruction('dbfo')"/>
  124. <xsl:with-param name="attribute" select="'keep-together'"/>
  125. </xsl:call-template>
  126. </xsl:variable>
  127. <xsl:choose>
  128. <!-- informaltables have their own templates and
  129. are not handled by formal.object -->
  130. <xsl:when test="local-name(.) = 'equation'">
  131. <fo:block id="{$id}"
  132. xsl:use-attribute-sets="equation.properties">
  133. <xsl:if test="$keep.together != ''">
  134. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  135. select="$keep.together"/></xsl:attribute>
  136. </xsl:if>
  137. <xsl:apply-templates/>
  138. </fo:block>
  139. </xsl:when>
  140. <xsl:when test="local-name(.) = 'procedure'">
  141. <fo:block id="{$id}"
  142. xsl:use-attribute-sets="procedure.properties">
  143. <xsl:if test="$keep.together != ''">
  144. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  145. select="$keep.together"/></xsl:attribute>
  146. </xsl:if>
  147. <xsl:apply-templates/>
  148. </fo:block>
  149. </xsl:when>
  150. <xsl:when test="local-name(.) = 'informalfigure'">
  151. <fo:block id="{$id}"
  152. xsl:use-attribute-sets="informalfigure.properties">
  153. <xsl:if test="$keep.together != ''">
  154. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  155. select="$keep.together"/></xsl:attribute>
  156. </xsl:if>
  157. <xsl:apply-templates/>
  158. </fo:block>
  159. </xsl:when>
  160. <xsl:when test="local-name(.) = 'informalexample'">
  161. <fo:block id="{$id}"
  162. xsl:use-attribute-sets="informalexample.properties">
  163. <xsl:if test="$keep.together != ''">
  164. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  165. select="$keep.together"/></xsl:attribute>
  166. </xsl:if>
  167. <xsl:apply-templates/>
  168. </fo:block>
  169. </xsl:when>
  170. <xsl:when test="local-name(.) = 'informalequation'">
  171. <fo:block id="{$id}"
  172. xsl:use-attribute-sets="informalequation.properties">
  173. <xsl:apply-templates/>
  174. </fo:block>
  175. </xsl:when>
  176. <xsl:otherwise>
  177. <fo:block id="{$id}"
  178. xsl:use-attribute-sets="informal.object.properties">
  179. <xsl:if test="$keep.together != ''">
  180. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  181. select="$keep.together"/></xsl:attribute>
  182. </xsl:if>
  183. <xsl:apply-templates/>
  184. </fo:block>
  185. </xsl:otherwise>
  186. </xsl:choose>
  187. </xsl:template>
  188. <xsl:template name="semiformal.object">
  189. <xsl:param name="placement" select="'before'"/>
  190. <xsl:choose>
  191. <xsl:when test="./title">
  192. <xsl:call-template name="formal.object">
  193. <xsl:with-param name="placement" select="$placement"/>
  194. </xsl:call-template>
  195. </xsl:when>
  196. <xsl:otherwise>
  197. <xsl:call-template name="informal.object"/>
  198. </xsl:otherwise>
  199. </xsl:choose>
  200. </xsl:template>
  201. <xsl:template match="figure">
  202. <xsl:variable name="param.placement"
  203. select="substring-after(normalize-space($formal.title.placement),
  204. concat(local-name(.), ' '))"/>
  205. <xsl:variable name="placement">
  206. <xsl:choose>
  207. <xsl:when test="contains($param.placement, ' ')">
  208. <xsl:value-of select="substring-before($param.placement, ' ')"/>
  209. </xsl:when>
  210. <xsl:when test="$param.placement = ''">before</xsl:when>
  211. <xsl:otherwise>
  212. <xsl:value-of select="$param.placement"/>
  213. </xsl:otherwise>
  214. </xsl:choose>
  215. </xsl:variable>
  216. <xsl:variable name="figure">
  217. <xsl:choose>
  218. <xsl:when test="@pgwide = '1'">
  219. <fo:block xsl:use-attribute-sets="pgwide.properties">
  220. <xsl:call-template name="formal.object">
  221. <xsl:with-param name="placement" select="$placement"/>
  222. </xsl:call-template>
  223. </fo:block>
  224. </xsl:when>
  225. <xsl:otherwise>
  226. <xsl:call-template name="formal.object">
  227. <xsl:with-param name="placement" select="$placement"/>
  228. </xsl:call-template>
  229. </xsl:otherwise>
  230. </xsl:choose>
  231. </xsl:variable>
  232. <xsl:variable name="floatstyle">
  233. <xsl:call-template name="floatstyle"/>
  234. </xsl:variable>
  235. <xsl:choose>
  236. <xsl:when test="$floatstyle != ''">
  237. <xsl:call-template name="floater">
  238. <xsl:with-param name="position" select="$floatstyle"/>
  239. <xsl:with-param name="content" select="$figure"/>
  240. </xsl:call-template>
  241. </xsl:when>
  242. <xsl:otherwise>
  243. <xsl:copy-of select="$figure"/>
  244. </xsl:otherwise>
  245. </xsl:choose>
  246. </xsl:template>
  247. <xsl:template match="example">
  248. <xsl:variable name="param.placement"
  249. select="substring-after(normalize-space($formal.title.placement),
  250. concat(local-name(.), ' '))"/>
  251. <xsl:variable name="placement">
  252. <xsl:choose>
  253. <xsl:when test="contains($param.placement, ' ')">
  254. <xsl:value-of select="substring-before($param.placement, ' ')"/>
  255. </xsl:when>
  256. <xsl:when test="$param.placement = ''">before</xsl:when>
  257. <xsl:otherwise>
  258. <xsl:value-of select="$param.placement"/>
  259. </xsl:otherwise>
  260. </xsl:choose>
  261. </xsl:variable>
  262. <!-- Example doesn't have a pgwide attribute, so may use a PI -->
  263. <xsl:variable name="pgwide.pi">
  264. <xsl:call-template name="dbfo-attribute">
  265. <xsl:with-param name="pis"
  266. select="processing-instruction('dbfo')"/>
  267. <xsl:with-param name="attribute" select="'pgwide'"/>
  268. </xsl:call-template>
  269. </xsl:variable>
  270. <xsl:variable name="pgwide">
  271. <xsl:choose>
  272. <xsl:when test="$pgwide.pi">
  273. <xsl:value-of select="$pgwide.pi"/>
  274. </xsl:when>
  275. <!-- child element may set pgwide -->
  276. <xsl:when test="*[@pgwide]">
  277. <xsl:value-of select="*[@pgwide][1]/@pgwide"/>
  278. </xsl:when>
  279. </xsl:choose>
  280. </xsl:variable>
  281. <!-- Get align value from internal mediaobject -->
  282. <xsl:variable name="align">
  283. <xsl:if test="mediaobject|mediaobjectco">
  284. <xsl:variable name="olist" select="mediaobject/imageobject
  285. |mediaobjectco/imageobjectco
  286. |mediaobject/videoobject
  287. |mediaobject/audioobject
  288. |mediaobject/textobject"/>
  289. <xsl:variable name="object.index">
  290. <xsl:call-template name="select.mediaobject.index">
  291. <xsl:with-param name="olist" select="$olist"/>
  292. <xsl:with-param name="count" select="1"/>
  293. </xsl:call-template>
  294. </xsl:variable>
  295. <xsl:variable name="object" select="$olist[position() = $object.index]"/>
  296. <xsl:value-of select="$object/descendant::imagedata[@align][1]/@align"/>
  297. </xsl:if>
  298. </xsl:variable>
  299. <xsl:variable name="example">
  300. <xsl:choose>
  301. <xsl:when test="$pgwide = '1'">
  302. <fo:block xsl:use-attribute-sets="pgwide.properties">
  303. <xsl:if test="$align != ''">
  304. <xsl:attribute name="text-align">
  305. <xsl:value-of select="$align"/>
  306. </xsl:attribute>
  307. </xsl:if>
  308. <xsl:call-template name="formal.object">
  309. <xsl:with-param name="placement" select="$placement"/>
  310. </xsl:call-template>
  311. </fo:block>
  312. </xsl:when>
  313. <xsl:otherwise>
  314. <fo:block>
  315. <xsl:if test="$align != ''">
  316. <xsl:attribute name="text-align">
  317. <xsl:value-of select="$align"/>
  318. </xsl:attribute>
  319. </xsl:if>
  320. <xsl:call-template name="formal.object">
  321. <xsl:with-param name="placement" select="$placement"/>
  322. </xsl:call-template>
  323. </fo:block>
  324. </xsl:otherwise>
  325. </xsl:choose>
  326. </xsl:variable>
  327. <xsl:variable name="floatstyle">
  328. <xsl:call-template name="floatstyle"/>
  329. </xsl:variable>
  330. <xsl:choose>
  331. <xsl:when test="$floatstyle != ''">
  332. <xsl:call-template name="floater">
  333. <xsl:with-param name="position" select="$floatstyle"/>
  334. <xsl:with-param name="content" select="$example"/>
  335. </xsl:call-template>
  336. </xsl:when>
  337. <xsl:otherwise>
  338. <xsl:copy-of select="$example"/>
  339. </xsl:otherwise>
  340. </xsl:choose>
  341. </xsl:template>
  342. <!-- Unified handling of CALS and HTML tables, formal and not -->
  343. <!-- Creates a hierarchy of nested containers:
  344. - Outer container does a float.
  345. - Nested container does block-container for rotation
  346. - Nested block contains title, layout table and footnotes
  347. - Nested layout table placeholder template supports extensions.
  348. - fo:table is innermost.
  349. Created from the innermost and working out.
  350. Not all layers apply to every table.
  351. -->
  352. <xsl:template match="table|informaltable">
  353. <xsl:if test="tgroup/tbody/tr
  354. |tgroup/thead/tr
  355. |tgroup/tfoot/tr">
  356. <xsl:message terminate="yes">
  357. <xsl:text>Broken table: tr descendent of CALS Table.</xsl:text>
  358. <xsl:text>The text in the first tr is:&#10;</xsl:text>
  359. <xsl:value-of
  360. select="(tgroup//tr)[1]"/>
  361. </xsl:message>
  362. </xsl:if>
  363. <xsl:if test="not(tgroup) and .//row">
  364. <xsl:message terminate="yes">
  365. <xsl:text>Broken table: row descendent of HTML table.</xsl:text>
  366. <xsl:text>The text in the first row is:&#10;</xsl:text>
  367. <xsl:value-of
  368. select=".//row[1]"/>
  369. </xsl:message>
  370. </xsl:if>
  371. <!-- Contains fo:table, not title or footnotes -->
  372. <xsl:variable name="table.content">
  373. <xsl:call-template name="make.table.content"/>
  374. </xsl:variable>
  375. <!-- Optional layout table template for extensions -->
  376. <xsl:variable name="table.layout">
  377. <xsl:call-template name="table.layout">
  378. <xsl:with-param name="table.content" select="$table.content"/>
  379. </xsl:call-template>
  380. </xsl:variable>
  381. <!-- fo:block contains title, layout table, and footnotes -->
  382. <xsl:variable name="table.block">
  383. <xsl:call-template name="table.block">
  384. <xsl:with-param name="table.layout" select="$table.layout"/>
  385. </xsl:call-template>
  386. </xsl:variable>
  387. <!-- pgwide or orient container -->
  388. <xsl:variable name="table.container">
  389. <xsl:call-template name="table.container">
  390. <xsl:with-param name="table.block" select="$table.block"/>
  391. </xsl:call-template>
  392. </xsl:variable>
  393. <!-- float or not -->
  394. <xsl:variable name="floatstyle">
  395. <xsl:call-template name="floatstyle"/>
  396. </xsl:variable>
  397. <xsl:choose>
  398. <xsl:when test="$floatstyle != ''">
  399. <xsl:call-template name="floater">
  400. <xsl:with-param name="position" select="$floatstyle"/>
  401. <xsl:with-param name="content" select="$table.container"/>
  402. </xsl:call-template>
  403. </xsl:when>
  404. <xsl:otherwise>
  405. <xsl:copy-of select="$table.container"/>
  406. </xsl:otherwise>
  407. </xsl:choose>
  408. </xsl:template>
  409. <xsl:template match="equation">
  410. <xsl:variable name="param.placement"
  411. select="substring-after(normalize-space($formal.title.placement),
  412. concat(local-name(.), ' '))"/>
  413. <xsl:variable name="placement">
  414. <xsl:choose>
  415. <xsl:when test="contains($param.placement, ' ')">
  416. <xsl:value-of select="substring-before($param.placement, ' ')"/>
  417. </xsl:when>
  418. <xsl:when test="$param.placement = ''">before</xsl:when>
  419. <xsl:otherwise>
  420. <xsl:value-of select="$param.placement"/>
  421. </xsl:otherwise>
  422. </xsl:choose>
  423. </xsl:variable>
  424. <!-- Equation doesn't have a pgwide attribute, so may use a PI -->
  425. <xsl:variable name="pgwide">
  426. <xsl:call-template name="dbfo-attribute">
  427. <xsl:with-param name="pis"
  428. select="processing-instruction('dbfo')"/>
  429. <xsl:with-param name="attribute" select="'pgwide'"/>
  430. </xsl:call-template>
  431. </xsl:variable>
  432. <xsl:variable name="equation">
  433. <xsl:choose>
  434. <xsl:when test="$pgwide = '1'">
  435. <fo:block xsl:use-attribute-sets="pgwide.properties">
  436. <xsl:call-template name="semiformal.object">
  437. <xsl:with-param name="placement" select="$placement"/>
  438. </xsl:call-template>
  439. </fo:block>
  440. </xsl:when>
  441. <xsl:otherwise>
  442. <xsl:call-template name="semiformal.object">
  443. <xsl:with-param name="placement" select="$placement"/>
  444. </xsl:call-template>
  445. </xsl:otherwise>
  446. </xsl:choose>
  447. </xsl:variable>
  448. <xsl:variable name="floatstyle">
  449. <xsl:call-template name="floatstyle"/>
  450. </xsl:variable>
  451. <xsl:choose>
  452. <xsl:when test="$floatstyle != ''">
  453. <xsl:call-template name="floater">
  454. <xsl:with-param name="position" select="$floatstyle"/>
  455. <xsl:with-param name="content" select="$equation"/>
  456. </xsl:call-template>
  457. </xsl:when>
  458. <xsl:otherwise>
  459. <xsl:copy-of select="$equation"/>
  460. </xsl:otherwise>
  461. </xsl:choose>
  462. </xsl:template>
  463. <xsl:template match="figure/title"></xsl:template>
  464. <xsl:template match="figure/titleabbrev"></xsl:template>
  465. <xsl:template match="table/title"></xsl:template>
  466. <xsl:template match="table/titleabbrev"></xsl:template>
  467. <xsl:template match="table/textobject"></xsl:template>
  468. <xsl:template match="example/title"></xsl:template>
  469. <xsl:template match="example/titleabbrev"></xsl:template>
  470. <xsl:template match="equation/title"></xsl:template>
  471. <xsl:template match="equation/titleabbrev"></xsl:template>
  472. <xsl:template match="informalfigure">
  473. <xsl:call-template name="informal.object"/>
  474. </xsl:template>
  475. <xsl:template match="informalexample">
  476. <xsl:call-template name="informal.object"/>
  477. </xsl:template>
  478. <xsl:template match="informaltable/textobject"></xsl:template>
  479. <xsl:template match="informalequation">
  480. <xsl:call-template name="informal.object"/>
  481. </xsl:template>
  482. <xsl:template name="floatstyle">
  483. <xsl:if test="(@float and @float != '0') or @floatstyle != ''">
  484. <xsl:choose>
  485. <xsl:when test="@floatstyle != ''">
  486. <xsl:value-of select="@floatstyle"/>
  487. </xsl:when>
  488. <xsl:when test="@float = '1'">
  489. <xsl:value-of select="$default.float.class"/>
  490. </xsl:when>
  491. <xsl:otherwise>
  492. <xsl:value-of select="@float"/>
  493. </xsl:otherwise>
  494. </xsl:choose>
  495. </xsl:if>
  496. </xsl:template>
  497. </xsl:stylesheet>