formal.xsl 18 KB

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