step2-base.xsl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!-- This file is part of the DITA Open Toolkit project.
  3. See the accompanying license.txt file for applicable licenses.-->
  4. <!-- (c) Copyright IBM Corp. 2004, 2006 All Rights Reserved. -->
  5. <!-- Second step in the DITA to text transform. This takes an intermediate
  6. format, and converts it to text output. The text style is determined by
  7. the OUTFORMAT parameter. Currently supported values are plaintext, troff,
  8. and nroff (troff and nroff match at the moment).
  9. The first step creates an intermediate format that uses only a few elements.
  10. It has a root <dita> element, and everything else fits in to these elements:
  11. <section> : used for <section> and <example>. This can nest any of the following elements.
  12. <sectiontitle> : used for the titles of <section> and <example>. This will nest the <text> element.
  13. <block> : all other block-like elements. The reason section does not use <block>
  14. is that it maps well to troff-style sections that use the .SH macro
  15. for highlighting and indenting. This can nest any number of <block>
  16. or <text> elements. Attributes set lead-in text (such as list item numbers
  17. that must appear before the list item text), as well as indent values.
  18. Other attributes are described below.
  19. <text> : all text nodes and phrases. This can include text or additional <text> elements.
  20. Text will be wrapped, with the width determined by the LINELENGTH parameter.
  21. Formatters such as troff may reflow the text as needed. Line breaks should only
  22. be forced in pre-formatted text, or between blocks.
  23. -->
  24. <xsl:stylesheet version="2.0"
  25. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  26. >
  27. <!--
  28. ALL ELEMENTS CAN TAKE @xtrf and @xtrc
  29. Attributes on dita:
  30. Attributes on section:
  31. Attributes on sectiontitle:
  32. Attributes on block:
  33. @xml:space="preserve"
  34. @position="center"
  35. @indent="digit" - additional indent new for this element
  36. @compact="yes|no"
  37. @leadin="" - text that appears once at the start of the element. It does not get the
  38. extra indenting specified by @indent.
  39. Attributes on text:
  40. @style="bold|italics|underlined|tt|sup|sub"
  41. @href="" [target, if this is a link]
  42. @format="" copy through @format for a link
  43. @scope="" copy through @scope for a link
  44. -->
  45. <xsl:import href="plugin:org.dita.base:xsl/common/output-message.xsl"/>
  46. <xsl:output method="text"
  47. encoding="UTF-8"
  48. indent="no"
  49. omit-xml-declaration = "yes"
  50. />
  51. <xsl:param name="LINELENGTH">65</xsl:param>
  52. <xsl:param name="FILENAME"></xsl:param>
  53. <!-- Deprecated since 2.3 -->
  54. <xsl:variable name="msgprefix">DOTX</xsl:variable>
  55. <xsl:variable name="OUTEXT">txt</xsl:variable>
  56. <!-- Single newline character. This is used to search for newlines in pre-formatted text, and
  57. is used for wrapping text (processors may choose to reflow wrapped text). -->
  58. <xsl:variable name="newline"><xsl:text>
  59. </xsl:text></xsl:variable>
  60. <xsl:template name="force-newline">
  61. <xsl:value-of select="$newline"/>
  62. </xsl:template>
  63. <xsl:template name="force-two-newlines">
  64. <xsl:value-of select="$newline"/><xsl:value-of select="$newline"/>
  65. </xsl:template>
  66. <!-- Turn on centering -->
  67. <xsl:template name="start-centering">
  68. <xsl:value-of select="$newline"/>
  69. </xsl:template>
  70. <!-- Turn on centering -->
  71. <xsl:template name="stop-centering">
  72. <xsl:value-of select="$newline"/>
  73. </xsl:template>
  74. <!-- root rule -->
  75. <xsl:template match="/">
  76. <xsl:apply-templates select="*[1]"/>
  77. </xsl:template>
  78. <xsl:template match="*">
  79. <xsl:apply-templates select="*[1]"/>
  80. </xsl:template>
  81. <!-- Find the current indent length. If formatters (such as troff?) do indenting on
  82. their own, they can always return '' from this function. -->
  83. <xsl:template match="*" mode="find-indent">
  84. <xsl:choose>
  85. <xsl:when test="not(@indent) or @expanse='page'"/>
  86. <xsl:when test="@indent='1'"><xsl:text> </xsl:text></xsl:when>
  87. <xsl:when test="@indent='2'"><xsl:text> </xsl:text></xsl:when>
  88. <xsl:when test="@indent='3'"><xsl:text> </xsl:text></xsl:when>
  89. <xsl:when test="@indent='4'"><xsl:text> </xsl:text></xsl:when>
  90. <xsl:when test="@indent='5'"><xsl:text> </xsl:text></xsl:when>
  91. <xsl:when test="@indent='6'"><xsl:text> </xsl:text></xsl:when>
  92. <xsl:when test="@indent='7'"><xsl:text> </xsl:text></xsl:when>
  93. <xsl:when test="@indent='8'"><xsl:text> </xsl:text></xsl:when>
  94. <xsl:when test="@indent='9'"><xsl:text> </xsl:text></xsl:when>
  95. <xsl:when test="@indent='10'"><xsl:text> </xsl:text></xsl:when>
  96. <xsl:when test="@indent='11'"><xsl:text> </xsl:text></xsl:when>
  97. <xsl:when test="@indent='12'"><xsl:text> </xsl:text></xsl:when>
  98. <xsl:when test="@indent='13'"><xsl:text> </xsl:text></xsl:when>
  99. <xsl:when test="@indent='14'"><xsl:text> </xsl:text></xsl:when>
  100. <xsl:when test="@indent='15'"><xsl:text> </xsl:text></xsl:when>
  101. </xsl:choose>
  102. </xsl:template>
  103. <xsl:template name="getFirstWord">
  104. <xsl:param name="string"/>
  105. <xsl:choose>
  106. <!-- For DBCS text, only take one character, unless English, or followed by punctuation -->
  107. <xsl:when test="contains($string,' ')"><xsl:value-of select="substring-before($string,' ')"/></xsl:when>
  108. <xsl:otherwise><xsl:value-of select="$string"/></xsl:otherwise>
  109. </xsl:choose>
  110. </xsl:template>
  111. <!-- This drops leading spaces... but that's already done if calling with normalize-space.
  112. The function recursivly processes "string" while counting the line length. It adds a
  113. newline when needed, and resets the current length.
  114. If a formatter wants to control indenting, simply start with the indent command. May want
  115. to add a parameter that makes it easy to tell if this is the first time "wrap" was called
  116. in order to enable this. Then update the find-indent template to return '' for that output
  117. format. -->
  118. <xsl:template name="wrap">
  119. <xsl:param name="string"/>
  120. <xsl:param name="curLength" select="0"/>
  121. <xsl:param name="leadin"/> <!-- Text to use once, before indent -->
  122. <xsl:param name="addIndent">
  123. <xsl:choose>
  124. <xsl:when test="@expanse='page'"/> <!-- Ignore any active indent -->
  125. <!-- If there is lead-in text that does not indent, only get the indent from ancestor blocks -->
  126. <xsl:when test="string-length(normalize-space($leadin))>0">
  127. <xsl:apply-templates select="ancestor-or-self::block/ancestor::*[@indent]" mode="find-indent"/>
  128. </xsl:when>
  129. <!-- Otherwise, start with the indent on the current element -->
  130. <xsl:otherwise>
  131. <xsl:apply-templates select="ancestor-or-self::*[@indent]" mode="find-indent"/>
  132. </xsl:otherwise>
  133. </xsl:choose>
  134. </xsl:param>
  135. <xsl:variable name="firstword">
  136. <xsl:call-template name="getFirstWord">
  137. <xsl:with-param name="string" select="$string"/>
  138. </xsl:call-template>
  139. </xsl:variable>
  140. <xsl:variable name="remainder" select="substring-after($string,' ')"/>
  141. <xsl:choose>
  142. <!-- If there is leadin text, issue it with the current indent, before
  143. adding any indent for this block -->
  144. <xsl:when test="string-length(normalize-space($leadin))>0">
  145. <xsl:value-of select="$addIndent"/>
  146. <xsl:value-of select="$leadin"/>
  147. <xsl:value-of select="$firstword"/>
  148. <xsl:call-template name="wrap">
  149. <xsl:with-param name="string" select="$remainder"/>
  150. <xsl:with-param name="curLength" select="string-length($leadin) + string-length($firstword) + string-length($addIndent)"/>
  151. </xsl:call-template>
  152. </xsl:when>
  153. <!-- End of the string; nothing left to evaluate, so quit -->
  154. <xsl:when test="string-length($string)=0"/>
  155. <!-- At the start of the line; add the word, whatever the length -->
  156. <xsl:when test="$curLength = 0">
  157. <xsl:value-of select="$addIndent"/>
  158. <xsl:value-of select="$firstword"/>
  159. <xsl:call-template name="wrap">
  160. <xsl:with-param name="string" select="$remainder"/>
  161. <xsl:with-param name="curLength" select="string-length($firstword) + string-length($addIndent)"/>
  162. <xsl:with-param name="addIndent" select="$addIndent"/>
  163. </xsl:call-template>
  164. </xsl:when>
  165. <!-- Normal text. This word does not fit on the line. End this line, start the next. -->
  166. <xsl:when test="string-length($firstword) + 1 + number($curLength) > $LINELENGTH">
  167. <xsl:value-of select="$newline"/>
  168. <xsl:value-of select="$addIndent"/>
  169. <xsl:value-of select="$firstword"/>
  170. <xsl:call-template name="wrap">
  171. <xsl:with-param name="string" select="$remainder"/>
  172. <xsl:with-param name="curLength" select="string-length($firstword) + string-length($addIndent)"/>
  173. <xsl:with-param name="addIndent" select="$addIndent"/>
  174. </xsl:call-template>
  175. </xsl:when>
  176. <!-- Normal text; this word fits on the line. Add it and continue. -->
  177. <xsl:otherwise>
  178. <xsl:if test="$curLength>0"><xsl:text> </xsl:text></xsl:if>
  179. <xsl:value-of select="$firstword"/>
  180. <xsl:call-template name="wrap">
  181. <xsl:with-param name="string" select="$remainder"/>
  182. <xsl:with-param name="curLength" select="number($curLength) + 1 + string-length($firstword)"/>
  183. <xsl:with-param name="addIndent" select="$addIndent"/>
  184. </xsl:call-template>
  185. </xsl:otherwise>
  186. </xsl:choose>
  187. </xsl:template>
  188. <!-- Process pre-formatted text. Newlines should be preserved, none should be added. -->
  189. <xsl:template name="preserve-space">
  190. <xsl:param name="string" select="."/>
  191. <xsl:param name="leadin"/> <!-- Text to use once, before indent -->
  192. <xsl:param name="addIndent">
  193. <xsl:choose>
  194. <xsl:when test="@expanse='page'"/> <!-- Ignore any active indent -->
  195. <!-- If there is lead-in text that does not indent, only get the indent from ancestor blocks -->
  196. <xsl:when test="string-length(normalize-space($leadin))>0">
  197. <xsl:apply-templates select="ancestor-or-self::block/ancestor::*[@indent]" mode="find-indent"/>
  198. </xsl:when>
  199. <!-- Otherwise, start with the indent on the current element -->
  200. <xsl:otherwise>
  201. <xsl:apply-templates select="ancestor-or-self::*[@indent]" mode="find-indent"/>
  202. </xsl:otherwise>
  203. </xsl:choose>
  204. </xsl:param>
  205. <xsl:choose>
  206. <xsl:when test="string-length($string)=0"/>
  207. <xsl:when test="contains($string,$newline)">
  208. <!-- Warn if the line exceeds the limit? -->
  209. <xsl:value-of select="$addIndent"/>
  210. <xsl:value-of select="substring-before($string,$newline)"/>
  211. <xsl:call-template name="force-newline"/>
  212. <xsl:call-template name="preserve-space">
  213. <xsl:with-param name="string" select="substring-after($string,$newline)"/>
  214. </xsl:call-template>
  215. </xsl:when>
  216. <xsl:otherwise>
  217. <xsl:value-of select="$addIndent"/>
  218. <xsl:value-of select="$string"/>
  219. </xsl:otherwise>
  220. </xsl:choose>
  221. </xsl:template>
  222. <xsl:template name="center-this-block">
  223. <xsl:if test="@position='center' and not(ancestor::*[@position='center'])">
  224. <xsl:call-template name="start-centering"/>
  225. </xsl:if>
  226. </xsl:template>
  227. <xsl:template name="UN-center-this-block">
  228. <xsl:if test="@position='center' and not(ancestor::*[@position='center'])">
  229. <xsl:call-template name="stop-centering"/>
  230. </xsl:if>
  231. </xsl:template>
  232. <!-- Process a block. If there was a block or text immediately before, we need to jump down
  233. a new line. -->
  234. <!-- If a block is in <text> it probably means this was a breaking image in a phrase.
  235. Otherwise, blocks should not be able to appear in text. In that case, treat it as inline. -->
  236. <xsl:template match="block">
  237. <xsl:variable name="thisLeadin">
  238. <!-- If there is no text inside here, and it should have lead-in (such as a list
  239. number), ensure the lead-in still shows up. -->
  240. <xsl:if test="@leadin and (not(*) or *[1][self::block|section])"><xsl:value-of select="@leadin"/></xsl:if>
  241. </xsl:variable>
  242. <xsl:variable name="leadinWithIndent">
  243. <xsl:if test="normalize-space($thisLeadin)">
  244. <xsl:apply-templates select="ancestor::*[@indent]" mode="find-indent"/>
  245. <xsl:value-of select="$thisLeadin"/>
  246. <xsl:value-of select="$newline"/>
  247. </xsl:if>
  248. </xsl:variable>
  249. <xsl:choose>
  250. <xsl:when test="ancestor::text">
  251. <xsl:value-of select="$thisLeadin"/> <!-- If doing it inline, do not use indent -->
  252. <xsl:apply-templates select="*[1]"/>
  253. </xsl:when>
  254. <xsl:otherwise>
  255. <xsl:if test="preceding-sibling::*">
  256. <xsl:choose>
  257. <xsl:when test="@compact='yes'"><xsl:call-template name="force-newline"/></xsl:when>
  258. <xsl:otherwise><xsl:call-template name="force-two-newlines"/></xsl:otherwise>
  259. </xsl:choose>
  260. </xsl:if>
  261. <xsl:call-template name="center-this-block"/>
  262. <xsl:value-of select="$leadinWithIndent"/>
  263. <xsl:apply-templates select="*[1]"/>
  264. <xsl:call-template name="UN-center-this-block"/>
  265. </xsl:otherwise>
  266. </xsl:choose>
  267. <xsl:apply-templates select="following-sibling::*[1]"/>
  268. </xsl:template>
  269. <!-- If the section has a title, TROFF can use the .SH macro to get the title formatting. -->
  270. <xsl:template match="section">
  271. <xsl:choose>
  272. <xsl:when test="sectiontitle">
  273. <xsl:apply-templates select="sectiontitle[1]"/>
  274. <xsl:apply-templates select="(text|block)[1]"/>
  275. </xsl:when>
  276. <xsl:otherwise>
  277. <xsl:if test="preceding-sibling::*">
  278. <xsl:call-template name="force-two-newlines"/>
  279. </xsl:if>
  280. <xsl:apply-templates select="*[1]"/>
  281. </xsl:otherwise>
  282. </xsl:choose>
  283. <xsl:apply-templates select="following-sibling::*[1]"/>
  284. </xsl:template>
  285. <!-- Based on step1, section titles should come first in the section. If this is
  286. a *ROFF format, use the .SH macro to get roff's section-like formatting. -->
  287. <xsl:template match="sectiontitle">
  288. <xsl:if test="preceding-sibling::*">
  289. <xsl:call-template name="force-two-newlines"/>
  290. </xsl:if>
  291. <xsl:call-template name="force-two-newlines"/>
  292. <xsl:apply-templates select="*[1]"/>
  293. <!-- Do not process following siblings: those come through from section -->
  294. </xsl:template>
  295. <!-- Use <block @position="center"> for centering - not yet implemented.
  296. This template matches pre-formatted blocks like <pre> and <lines>.
  297. May be able to update this to use TROFF commands that create an entire
  298. preformatted section; would need to make sure that nested elements do
  299. not cause problems with that. -->
  300. <xsl:template match="block[@xml:space='preserve']">
  301. <xsl:variable name="thisLeadin">
  302. <!-- If there is no text inside here, and it should have lead-in (such as a list
  303. number), ensure the lead-in still shows up. -->
  304. <xsl:if test="@leadin and (not(*) or *[1][self::block|section])"><xsl:value-of select="@leadin"/></xsl:if>
  305. </xsl:variable>
  306. <xsl:variable name="leadinWithIndent">
  307. <xsl:if test="normalize-space($thisLeadin)">
  308. <xsl:apply-templates select="ancestor::*[@indent]" mode="find-indent"/>
  309. <xsl:value-of select="$thisLeadin"/>
  310. <xsl:value-of select="$newline"/>
  311. </xsl:if>
  312. </xsl:variable>
  313. <xsl:choose>
  314. <xsl:when test="ancestor::text"> <!-- Should not ever be active, but just in case -->
  315. <xsl:value-of select="$thisLeadin"/>
  316. <xsl:call-template name="preserve-space"/>
  317. </xsl:when>
  318. <xsl:otherwise>
  319. <xsl:if test="preceding-sibling::*">
  320. <xsl:call-template name="force-two-newlines"/>
  321. </xsl:if>
  322. <xsl:call-template name="center-this-block"/>
  323. <xsl:value-of select="$leadinWithIndent"/>
  324. <xsl:call-template name="preserve-space"/>
  325. <xsl:call-template name="UN-center-this-block"/>
  326. </xsl:otherwise>
  327. </xsl:choose>
  328. <xsl:apply-templates select="(following-sibling::*)[1]"/>
  329. </xsl:template>
  330. <!-- This is called to process the contents of <text> elements. It will set
  331. the correct style if needed, and process children, and then return the
  332. style to normal. -->
  333. <xsl:template name="format-text">
  334. <xsl:param name="current-style" select="'normal'"/>
  335. <xsl:apply-templates select="." mode="format-text">
  336. <xsl:with-param name="current-style" select="@style"/>
  337. </xsl:apply-templates>
  338. </xsl:template>
  339. <xsl:template match="*" mode="format-text">
  340. <xsl:param name="current-style" select="'normal'"/>
  341. <xsl:apply-templates>
  342. <xsl:with-param name="current-style" select="@style"/>
  343. </xsl:apply-templates>
  344. </xsl:template>
  345. <!-- For text within text, do not worry about newlines. Just process the contents. -->
  346. <xsl:template match="text/text">
  347. <xsl:param name="current-style"/>
  348. <xsl:call-template name="format-text">
  349. <xsl:with-param name="current-style" select="$current-style"/>
  350. </xsl:call-template>
  351. </xsl:template>
  352. <!-- For text that is directly inside a block or section, newlines may be needed. Process
  353. all consecutive text elements at once. -->
  354. <xsl:template match="text">
  355. <!-- There should not be a style active, because the parent is a block. However, in the future,
  356. there could be a reason to set an entire block to bold, italics, etc, so go ahead
  357. and allow for it. -->
  358. <xsl:param name="current-style"/>
  359. <!-- Get all of the text up to the next block. This allows for easy wrapping, and prevents
  360. us from putting out newlines between text elements. First get the current element,
  361. then progress through following elements. -->
  362. <xsl:variable name="upToBlock">
  363. <xsl:call-template name="format-text">
  364. <xsl:with-param name="current-style" select="$current-style"/>
  365. </xsl:call-template>
  366. <xsl:apply-templates select="(following-sibling::*)[1]" mode="text-in-block">
  367. <xsl:with-param name="current-style" select="$current-style"/>
  368. </xsl:apply-templates>
  369. </xsl:variable>
  370. <!-- If text comes after </block>, jump to the next line, then leave a blank line. -->
  371. <xsl:if test="(preceding-sibling::*)[last()][self::block|self::section|self::sectiontitle]">
  372. <xsl:call-template name="force-two-newlines"/>
  373. </xsl:if>
  374. <!-- Process all text up to the next block. If the parent block had lead-in text, and
  375. it has not been used, pass that to the function. -->
  376. <xsl:call-template name="wrap">
  377. <xsl:with-param name="string" select="normalize-space($upToBlock)"/>
  378. <xsl:with-param name="leadin">
  379. <xsl:if test="../@leadin">
  380. <xsl:if test="not(preceding-sibling::*)"><xsl:value-of select="../@leadin"/></xsl:if>
  381. </xsl:if>
  382. </xsl:with-param>
  383. </xsl:call-template>
  384. <xsl:apply-templates select="(following-sibling::block|following-sibling::section)[1]"/>
  385. </xsl:template>
  386. <!-- This matches text when sequentially moving through text blocks. Process
  387. the contents, and move on to the next element. -->
  388. <xsl:template match="text" mode="text-in-block">
  389. <xsl:param name="current-style"/>
  390. <xsl:call-template name="format-text">
  391. <xsl:with-param name="current-style" select="$current-style"/>
  392. </xsl:call-template>
  393. <xsl:apply-templates select="following-sibling::*[1]" mode="text-in-block"/>
  394. </xsl:template>
  395. <!-- When moving through text elements, stop at block or section. -->
  396. <xsl:template match="block|section" mode="text-in-block"/>
  397. </xsl:stylesheet>