step2.xsl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. <xsl:import href="step2-base.xsl"/>
  28. <xsl:output method="text"
  29. encoding="UTF-8"
  30. indent="no"
  31. omit-xml-declaration = "yes"
  32. />
  33. <xsl:template name="force-newline">
  34. <xsl:value-of select="$newline"/>.br<xsl:value-of select="$newline"/>
  35. </xsl:template>
  36. <xsl:template name="force-two-newlines">
  37. <xsl:value-of select="$newline"/>.sp 2<xsl:value-of select="$newline"/>
  38. </xsl:template>
  39. <!-- Set the default justification -->
  40. <xsl:template name="set-default-justification">
  41. <xsl:choose>
  42. <xsl:when test="/dita/@dir='rtl'">
  43. <xsl:value-of select="$newline"/>.ad r<xsl:value-of select="$newline"/>
  44. </xsl:when>
  45. <xsl:otherwise>
  46. <xsl:value-of select="$newline"/>.ad l<xsl:value-of select="$newline"/>
  47. </xsl:otherwise>
  48. </xsl:choose>
  49. </xsl:template>
  50. <!-- Set the default line length. Tables are 72, so set it to 72. -->
  51. <xsl:template name="set-default-linelength">
  52. <xsl:value-of select="$newline"/>.ll 72<xsl:value-of select="$newline"/>
  53. </xsl:template>
  54. <!-- Turn on centering -->
  55. <xsl:template name="start-centering">
  56. <!-- Centers the next 1000 lines, or until centering is turned off -->
  57. <xsl:value-of select="$newline"/>.ce 1000<xsl:value-of select="$newline"/>
  58. </xsl:template>
  59. <!-- Turn on centering -->
  60. <xsl:template name="stop-centering">
  61. <xsl:value-of select="$newline"/>.ce 0<xsl:value-of select="$newline"/>
  62. </xsl:template>
  63. <!-- root rule -->
  64. <xsl:template match="/">
  65. <xsl:call-template name="set-default-justification"/>
  66. <xsl:call-template name="set-default-linelength"/>
  67. <xsl:apply-templates select="*[1]"/>
  68. </xsl:template>
  69. <!-- Based on step1, section titles should come first in the section. If this is
  70. a *ROFF format, use the .SH macro to get roff's section-like formatting. -->
  71. <xsl:template match="sectiontitle">
  72. <xsl:if test="preceding-sibling::*">
  73. <xsl:call-template name="force-two-newlines"/>
  74. </xsl:if>
  75. <xsl:value-of select="$newline"/>.SH "<xsl:apply-templates select="*[1]"/>"<xsl:value-of select="$newline"/>
  76. <xsl:call-template name="start-bold"/>
  77. <xsl:apply-templates select="*[1]">
  78. <xsl:with-param name="current-style" select="'bold'"/>
  79. </xsl:apply-templates>
  80. <xsl:call-template name="start-normal"/>
  81. <!-- Do not process following siblings: those come through from section -->
  82. </xsl:template>
  83. <!-- The following three functions will switch the current style to the
  84. proper formatting. For plain text, nothing is generated. -->
  85. <xsl:template name="start-bold">
  86. <xsl:text>\fB</xsl:text>
  87. </xsl:template>
  88. <xsl:template name="start-italics">
  89. <xsl:text>\fI</xsl:text>
  90. </xsl:template>
  91. <xsl:template name="start-underlined">
  92. <!-- No underlined in basic troff, use italic -->
  93. <xsl:text>\fI</xsl:text>
  94. </xsl:template>
  95. <!-- Default is already tt for these formats, so use normal font -->
  96. <xsl:template name="start-tt">
  97. <xsl:text>\fR</xsl:text>
  98. </xsl:template>
  99. <xsl:template name="start-normal">
  100. <xsl:text>\fR</xsl:text>
  101. </xsl:template>
  102. <!-- This is called to process the contents of <text> elements. It will set
  103. the correct style if needed, and process children, and then return the
  104. style to normal. -->
  105. <xsl:template name="format-text">
  106. <xsl:param name="current-style" select="'normal'"/>
  107. <xsl:choose>
  108. <xsl:when test="not(@style)"><xsl:apply-templates/></xsl:when>
  109. <xsl:when test="@style='bold'">
  110. <xsl:call-template name="start-bold"/>
  111. <xsl:apply-templates>
  112. <xsl:with-param name="current-style" select="@style"/>
  113. </xsl:apply-templates>
  114. </xsl:when>
  115. <xsl:when test="@style='italics'">
  116. <xsl:call-template name="start-italics"/>
  117. <xsl:apply-templates>
  118. <xsl:with-param name="current-style" select="@style"/>
  119. </xsl:apply-templates>
  120. </xsl:when>
  121. <xsl:when test="@style='underlined'">
  122. <xsl:call-template name="start-underlined"/>
  123. <xsl:apply-templates>
  124. <xsl:with-param name="current-style" select="@style"/>
  125. </xsl:apply-templates>
  126. </xsl:when>
  127. <xsl:when test="@style='tt'">
  128. <xsl:call-template name="start-tt"/>
  129. <xsl:apply-templates>
  130. <xsl:with-param name="current-style" select="@style"/>
  131. </xsl:apply-templates>
  132. </xsl:when>
  133. <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
  134. </xsl:choose>
  135. <!-- If there was a style, return to original style -->
  136. <xsl:if test="@style='bold' or @style='italics' or @style='underlined' or @style='tt'">
  137. <xsl:choose>
  138. <xsl:when test="$current-style='bold'"><xsl:call-template name="start-bold"/></xsl:when>
  139. <xsl:when test="$current-style='italics'"><xsl:call-template name="start-italics"/></xsl:when>
  140. <xsl:when test="$current-style='underlined'"><xsl:call-template name="start-underlined"/></xsl:when>
  141. <xsl:when test="$current-style='tt'"><xsl:call-template name="start-tt"/></xsl:when>
  142. <xsl:otherwise><xsl:call-template name="start-normal"/></xsl:otherwise>
  143. </xsl:choose>
  144. </xsl:if>
  145. </xsl:template>
  146. </xsl:stylesheet>