step2.xsl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!--
  3. This file is part of the DITA Open Toolkit project.
  4. Copyright 2004, 2006 IBM Corporation
  5. See the accompanying LICENSE file for applicable license.
  6. -->
  7. <!-- Second step in the DITA to text transform. This takes an intermediate
  8. format, and converts it to text output. The text style is determined by
  9. the OUTFORMAT parameter. Currently supported values are plaintext, troff,
  10. and nroff (troff and nroff match at the moment).
  11. The first step creates an intermediate format that uses only a few elements.
  12. It has a root <dita> element, and everything else fits in to these elements:
  13. <section> : used for <section> and <example>. This can nest any of the following elements.
  14. <sectiontitle> : used for the titles of <section> and <example>. This will nest the <text> element.
  15. <block> : all other block-like elements. The reason section does not use <block>
  16. is that it maps well to troff-style sections that use the .SH macro
  17. for highlighting and indenting. This can nest any number of <block>
  18. or <text> elements. Attributes set lead-in text (such as list item numbers
  19. that must appear before the list item text), as well as indent values.
  20. Other attributes are described below.
  21. <text> : all text nodes and phrases. This can include text or additional <text> elements.
  22. Text will be wrapped, with the width determined by the LINELENGTH parameter.
  23. Formatters such as troff may reflow the text as needed. Line breaks should only
  24. be forced in pre-formatted text, or between blocks.
  25. -->
  26. <xsl:stylesheet version="2.0"
  27. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  28. >
  29. <xsl:import href="step2-base.xsl"/>
  30. <xsl:output method="text"
  31. encoding="UTF-8"
  32. indent="no"
  33. omit-xml-declaration = "yes"
  34. />
  35. <xsl:template name="force-newline">
  36. <xsl:value-of select="$newline"/>.br<xsl:value-of select="$newline"/>
  37. </xsl:template>
  38. <xsl:template name="force-two-newlines">
  39. <xsl:value-of select="$newline"/>.sp 2<xsl:value-of select="$newline"/>
  40. </xsl:template>
  41. <!-- Set the default justification -->
  42. <xsl:template name="set-default-justification">
  43. <xsl:choose>
  44. <xsl:when test="/dita/@dir='rtl'">
  45. <xsl:value-of select="$newline"/>.ad r<xsl:value-of select="$newline"/>
  46. </xsl:when>
  47. <xsl:otherwise>
  48. <xsl:value-of select="$newline"/>.ad l<xsl:value-of select="$newline"/>
  49. </xsl:otherwise>
  50. </xsl:choose>
  51. </xsl:template>
  52. <!-- Set the default line length. Tables are 72, so set it to 72. -->
  53. <xsl:template name="set-default-linelength">
  54. <xsl:value-of select="$newline"/>.ll 72<xsl:value-of select="$newline"/>
  55. </xsl:template>
  56. <!-- Turn on centering -->
  57. <xsl:template name="start-centering">
  58. <!-- Centers the next 1000 lines, or until centering is turned off -->
  59. <xsl:value-of select="$newline"/>.ce 1000<xsl:value-of select="$newline"/>
  60. </xsl:template>
  61. <!-- Turn on centering -->
  62. <xsl:template name="stop-centering">
  63. <xsl:value-of select="$newline"/>.ce 0<xsl:value-of select="$newline"/>
  64. </xsl:template>
  65. <!-- root rule -->
  66. <xsl:template match="/">
  67. <xsl:call-template name="set-default-justification"/>
  68. <xsl:call-template name="set-default-linelength"/>
  69. <xsl:apply-templates select="*[1]"/>
  70. </xsl:template>
  71. <!-- Based on step1, section titles should come first in the section. If this is
  72. a *ROFF format, use the .SH macro to get roff's section-like formatting. -->
  73. <xsl:template match="sectiontitle">
  74. <xsl:if test="preceding-sibling::*">
  75. <xsl:call-template name="force-two-newlines"/>
  76. </xsl:if>
  77. <xsl:value-of select="$newline"/>.SH "<xsl:apply-templates select="*[1]"/>"<xsl:value-of select="$newline"/>
  78. <xsl:call-template name="start-bold"/>
  79. <xsl:apply-templates select="*[1]">
  80. <xsl:with-param name="current-style" select="'bold'"/>
  81. </xsl:apply-templates>
  82. <xsl:call-template name="start-normal"/>
  83. <!-- Do not process following siblings: those come through from section -->
  84. </xsl:template>
  85. <!-- The following three functions will switch the current style to the
  86. proper formatting. For plain text, nothing is generated. -->
  87. <xsl:template name="start-bold">
  88. <xsl:text>\fB</xsl:text>
  89. </xsl:template>
  90. <xsl:template name="start-italics">
  91. <xsl:text>\fI</xsl:text>
  92. </xsl:template>
  93. <xsl:template name="start-underlined">
  94. <!-- No underlined in basic troff, use italic -->
  95. <xsl:text>\fI</xsl:text>
  96. </xsl:template>
  97. <!-- Default is already tt for these formats, so use normal font -->
  98. <xsl:template name="start-tt">
  99. <xsl:text>\fR</xsl:text>
  100. </xsl:template>
  101. <xsl:template name="start-normal">
  102. <xsl:text>\fR</xsl:text>
  103. </xsl:template>
  104. <!-- This is called to process the contents of <text> elements. It will set
  105. the correct style if needed, and process children, and then return the
  106. style to normal. -->
  107. <xsl:template name="format-text">
  108. <xsl:param name="current-style" select="'normal'"/>
  109. <xsl:choose>
  110. <xsl:when test="not(@style)"><xsl:apply-templates/></xsl:when>
  111. <xsl:when test="@style='bold'">
  112. <xsl:call-template name="start-bold"/>
  113. <xsl:apply-templates>
  114. <xsl:with-param name="current-style" select="@style"/>
  115. </xsl:apply-templates>
  116. </xsl:when>
  117. <xsl:when test="@style='italics'">
  118. <xsl:call-template name="start-italics"/>
  119. <xsl:apply-templates>
  120. <xsl:with-param name="current-style" select="@style"/>
  121. </xsl:apply-templates>
  122. </xsl:when>
  123. <xsl:when test="@style='underlined'">
  124. <xsl:call-template name="start-underlined"/>
  125. <xsl:apply-templates>
  126. <xsl:with-param name="current-style" select="@style"/>
  127. </xsl:apply-templates>
  128. </xsl:when>
  129. <xsl:when test="@style='tt'">
  130. <xsl:call-template name="start-tt"/>
  131. <xsl:apply-templates>
  132. <xsl:with-param name="current-style" select="@style"/>
  133. </xsl:apply-templates>
  134. </xsl:when>
  135. <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
  136. </xsl:choose>
  137. <!-- If there was a style, return to original style -->
  138. <xsl:if test="@style='bold' or @style='italics' or @style='underlined' or @style='tt'">
  139. <xsl:choose>
  140. <xsl:when test="$current-style='bold'"><xsl:call-template name="start-bold"/></xsl:when>
  141. <xsl:when test="$current-style='italics'"><xsl:call-template name="start-italics"/></xsl:when>
  142. <xsl:when test="$current-style='underlined'"><xsl:call-template name="start-underlined"/></xsl:when>
  143. <xsl:when test="$current-style='tt'"><xsl:call-template name="start-tt"/></xsl:when>
  144. <xsl:otherwise><xsl:call-template name="start-normal"/></xsl:otherwise>
  145. </xsl:choose>
  146. </xsl:if>
  147. </xsl:template>
  148. </xsl:stylesheet>