map2markdownImpl.xsl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  4. xmlns:ditamsg="http://dita-ot.sourceforge.net/ns/200704/ditamsg" version="2.0"
  5. exclude-result-prefixes="dita-ot ditamsg">
  6. <xsl:variable name="msgprefix">DOTX</xsl:variable>
  7. <xsl:param name="OUTEXT" select="'.html'"/>
  8. <xsl:param name="WORKDIR">
  9. <xsl:apply-templates select="/processing-instruction('workdir-uri')[1]" mode="get-work-dir"/>
  10. </xsl:param>
  11. <xsl:template match="*[contains(@class, ' map/map ')]" mode="toc">
  12. <xsl:param name="pathFromMaplist"/>
  13. <xsl:if test="descendant::*[contains(@class, ' map/topicref ')]
  14. [not(@toc = 'no')]
  15. [not(@processing-role = 'resource-only')]">
  16. <bulletlist>
  17. <xsl:call-template name="commonattributes"/>
  18. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  19. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  20. </xsl:apply-templates>
  21. </bulletlist>
  22. </xsl:if>
  23. </xsl:template>
  24. <xsl:template match="*[contains(@class, ' map/topicref ')]
  25. [not(@toc = 'no')]
  26. [not(@processing-role = 'resource-only')]"
  27. mode="toc">
  28. <xsl:param name="pathFromMaplist"/>
  29. <xsl:variable name="title">
  30. <xsl:apply-templates select="." mode="get-navtitle"/>
  31. </xsl:variable>
  32. <xsl:choose>
  33. <xsl:when test="normalize-space($title)">
  34. <li>
  35. <xsl:call-template name="commonattributes"/>
  36. <xsl:choose>
  37. <!-- If there is a reference to a DITA or HTML file, and it is not external: -->
  38. <xsl:when test="normalize-space(@href)">
  39. <link>
  40. <xsl:attribute name="href">
  41. <xsl:choose>
  42. <xsl:when test="@copy-to and not(contains(@chunk, 'to-content')) and
  43. (not(@format) or @format = 'dita' or @format = 'ditamap') ">
  44. <xsl:if test="not(@scope = 'external')">
  45. <xsl:value-of select="$pathFromMaplist"/>
  46. </xsl:if>
  47. <xsl:call-template name="replace-extension">
  48. <xsl:with-param name="filename" select="@copy-to"/>
  49. <xsl:with-param name="extension" select="$OUTEXT"/>
  50. </xsl:call-template>
  51. <xsl:if test="not(contains(@copy-to, '#')) and contains(@href, '#')">
  52. <xsl:value-of select="concat('#', substring-after(@href, '#'))"/>
  53. </xsl:if>
  54. </xsl:when>
  55. <xsl:when test="not(@scope = 'external') and (not(@format) or @format = 'dita' or @format = 'ditamap')">
  56. <xsl:if test="not(@scope = 'external')">
  57. <xsl:value-of select="$pathFromMaplist"/>
  58. </xsl:if>
  59. <xsl:call-template name="replace-extension">
  60. <xsl:with-param name="filename" select="@href"/>
  61. <xsl:with-param name="extension" select="$OUTEXT"/>
  62. </xsl:call-template>
  63. </xsl:when>
  64. <xsl:otherwise><!-- If non-DITA, keep the href as-is -->
  65. <xsl:if test="not(@scope = 'external')">
  66. <xsl:value-of select="$pathFromMaplist"/>
  67. </xsl:if>
  68. <xsl:value-of select="@href"/>
  69. </xsl:otherwise>
  70. </xsl:choose>
  71. </xsl:attribute>
  72. <!--xsl:if test="@scope = 'external' or not(not(@format) or @format = 'dita' or @format = 'ditamap')">
  73. <xsl:attribute name="target">_blank</xsl:attribute>
  74. </xsl:if-->
  75. <xsl:value-of select="$title"/>
  76. </link>
  77. </xsl:when>
  78. <xsl:otherwise>
  79. <xsl:value-of select="$title"/>
  80. </xsl:otherwise>
  81. </xsl:choose>
  82. <!-- If there are any children that should be in the TOC, process them -->
  83. <xsl:if test="descendant::*[contains(@class, ' map/topicref ')]
  84. [not(@toc = 'no')]
  85. [not(@processing-role = 'resource-only')]">
  86. <bulletlist>
  87. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  88. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  89. </xsl:apply-templates>
  90. </bulletlist>
  91. </xsl:if>
  92. </li>
  93. </xsl:when>
  94. <xsl:otherwise><!-- if it is an empty topicref -->
  95. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  96. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  97. </xsl:apply-templates>
  98. </xsl:otherwise>
  99. </xsl:choose>
  100. </xsl:template>
  101. <!-- If toc=no, but a child has toc=yes, that child should bubble up to the top -->
  102. <xsl:template match="*[contains(@class, ' map/topicref ')]
  103. [@toc = 'no']
  104. [not(@processing-role = 'resource-only')]"
  105. mode="toc">
  106. <xsl:param name="pathFromMaplist"/>
  107. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  108. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  109. </xsl:apply-templates>
  110. </xsl:template>
  111. <xsl:template match="*" mode="toc" priority="-1"/>
  112. <xsl:template match="*" mode="get-navtitle">
  113. <xsl:choose>
  114. <!-- If navtitle is specified -->
  115. <xsl:when test="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' topic/navtitle ')]">
  116. <xsl:apply-templates select="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' topic/navtitle ')]"
  117. mode="dita-ot:text-only"/>
  118. </xsl:when>
  119. <xsl:when test="@navtitle">
  120. <xsl:value-of select="@navtitle"/>
  121. </xsl:when>
  122. <!-- If there is no title and none can be retrieved, check for <linktext> -->
  123. <xsl:when test="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]">
  124. <xsl:apply-templates select="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]"
  125. mode="dita-ot:text-only"/>
  126. </xsl:when>
  127. <!-- No local title, and not targeting a DITA file. Could be just a container setting
  128. metadata, or a file reference with no title. Issue message for the second case. -->
  129. <xsl:otherwise>
  130. <xsl:if test="normalize-space(@href)">
  131. <xsl:apply-templates select="." mode="ditamsg:could-not-retrieve-navtitle-using-fallback">
  132. <xsl:with-param name="target" select="@href"/>
  133. <xsl:with-param name="fallback" select="@href"/>
  134. </xsl:apply-templates>
  135. <xsl:value-of select="@href"/>
  136. </xsl:if>
  137. </xsl:otherwise>
  138. </xsl:choose>
  139. </xsl:template>
  140. <xsl:template match="*" mode="ditamsg:missing-target-file-no-navtitle">
  141. <xsl:call-template name="output-message">
  142. <xsl:with-param name="id">DOTX008W</xsl:with-param>
  143. <xsl:with-param name="msgparams">%1=<xsl:value-of select="@href"/></xsl:with-param>
  144. </xsl:call-template>
  145. </xsl:template>
  146. <xsl:template match="*" mode="ditamsg:could-not-retrieve-navtitle-using-fallback">
  147. <xsl:param name="target"/>
  148. <xsl:param name="fallback"/>
  149. <xsl:call-template name="output-message">
  150. <xsl:with-param name="id">DOTX009W</xsl:with-param>
  151. <xsl:with-param name="msgparams">%1=<xsl:value-of select="$target"/>;%2=<xsl:value-of select="$fallback"/></xsl:with-param>
  152. </xsl:call-template>
  153. </xsl:template>
  154. </xsl:stylesheet>