ast2markdown.xsl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns:ast="com.elovirta.dita.markdown"
  5. exclude-result-prefixes="xs ast"
  6. version="2.0">
  7. <xsl:variable name="linefeed" as="xs:string" select="'&#xA;'"/>
  8. <!-- Block -->
  9. <xsl:template match="pandoc" mode="ast">
  10. <xsl:apply-templates mode="ast"/>
  11. </xsl:template>
  12. <xsl:template match="div" mode="ast">
  13. <xsl:apply-templates mode="ast"/>
  14. </xsl:template>
  15. <xsl:template match="para" mode="ast">
  16. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  17. <xsl:call-template name="process-inline-contents"/>
  18. <xsl:value-of select="$linefeed"/>
  19. <xsl:value-of select="$linefeed"/>
  20. </xsl:template>
  21. <xsl:template match="plain" mode="ast">
  22. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  23. <!-- XXX; why is indent here? -->
  24. <xsl:value-of select="$indent"/>
  25. <xsl:call-template name="process-inline-contents"/>
  26. <xsl:value-of select="$linefeed"/>
  27. <xsl:if test="parent::li and following-sibling::*[not(self::bulletlist | self::orderedlist)]">
  28. <xsl:value-of select="$linefeed"/>
  29. </xsl:if>
  30. </xsl:template>
  31. <xsl:template match="header" mode="ast">
  32. <xsl:for-each select="1 to xs:integer(@level)">#</xsl:for-each>
  33. <xsl:text> </xsl:text>
  34. <!--xsl:apply-templates mode="ast"/-->
  35. <xsl:call-template name="process-inline-contents"/>
  36. <xsl:call-template name="ast-attibutes"/>
  37. <xsl:value-of select="$linefeed"/>
  38. <xsl:value-of select="$linefeed"/>
  39. </xsl:template>
  40. <xsl:template name="ast-attibutes">
  41. <xsl:if test="@id or @class">
  42. <xsl:text> {</xsl:text>
  43. <xsl:if test="@id">
  44. <xsl:text>#</xsl:text>
  45. <xsl:value-of select="@id"/>
  46. </xsl:if>
  47. <xsl:for-each select="tokenize(@class, '\s+')">
  48. <xsl:text> .</xsl:text>
  49. <xsl:value-of select="."/>
  50. </xsl:for-each>
  51. <xsl:text>}</xsl:text>
  52. </xsl:if>
  53. </xsl:template>
  54. <xsl:template match="bulletlist | orderedlist" mode="ast">
  55. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  56. <xsl:variable name="nested" select="ancestor::bulletlist or ancestor::orderedlist"/>
  57. <xsl:variable name="lis" select="li"/>
  58. <xsl:apply-templates select="$lis" mode="ast"/>
  59. <xsl:if test="not($nested)">
  60. <xsl:value-of select="$linefeed"/><!-- because last li will not write one -->
  61. </xsl:if>
  62. </xsl:template>
  63. <xsl:variable name="default-indent" select="' '" as="xs:string"/>
  64. <xsl:template match="li" mode="ast">
  65. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  66. <xsl:value-of select="$indent"/>
  67. <xsl:choose>
  68. <xsl:when test="parent::bulletlist">
  69. <xsl:text>- </xsl:text>
  70. </xsl:when>
  71. <xsl:otherwise>
  72. <xsl:variable name="label" select="concat(position(), '.')" as="xs:string"/>
  73. <xsl:value-of select="$label"/>
  74. <xsl:value-of select="substring($default-indent, string-length($label) + 1)"/>
  75. </xsl:otherwise>
  76. </xsl:choose>
  77. <xsl:apply-templates select="*[1]" mode="ast">
  78. <xsl:with-param name="indent" tunnel="yes" select="''"/>
  79. </xsl:apply-templates>
  80. <xsl:apply-templates select="*[position() ne 1]" mode="ast">
  81. <xsl:with-param name="indent" tunnel="yes" select="concat($indent, $default-indent)"/>
  82. </xsl:apply-templates>
  83. <!--xsl:if test="following-sibling::li">
  84. <xsl:value-of select="$linefeed"/>
  85. </xsl:if-->
  86. </xsl:template>
  87. <xsl:template match="definitionlist" mode="ast">
  88. <xsl:apply-templates mode="ast"/>
  89. </xsl:template>
  90. <xsl:template match="dlentry" mode="ast">
  91. <xsl:apply-templates mode="ast"/>
  92. </xsl:template>
  93. <xsl:template match="dt" mode="ast">
  94. <xsl:call-template name="process-inline-contents"/>
  95. <xsl:value-of select="$linefeed"/>
  96. </xsl:template>
  97. <xsl:template match="dd" mode="ast">
  98. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  99. <xsl:value-of select="$indent"/>
  100. <xsl:text>: </xsl:text>
  101. <xsl:apply-templates select="*[1]" mode="ast">
  102. <xsl:with-param name="indent" tunnel="yes" select="''"/>
  103. </xsl:apply-templates>
  104. <xsl:apply-templates select="*[position() ne 1]" mode="ast">
  105. <xsl:with-param name="indent" tunnel="yes" select="concat($indent, $default-indent)"/>
  106. </xsl:apply-templates>
  107. </xsl:template>
  108. <xsl:template match="codeblock" mode="ast">
  109. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  110. <xsl:value-of select="$indent"/>
  111. <xsl:text>```</xsl:text>
  112. <xsl:choose>
  113. <xsl:when test="empty(@id) and @class and not(contains(@class, ' '))">
  114. <xsl:value-of select="@class"/>
  115. </xsl:when>
  116. <xsl:otherwise>
  117. <xsl:call-template name="ast-attibutes"/>
  118. </xsl:otherwise>
  119. </xsl:choose>
  120. <xsl:value-of select="$linefeed"/>
  121. <xsl:call-template name="process-inline-contents"/>
  122. <xsl:value-of select="$linefeed"/>
  123. <xsl:value-of select="$indent"/>
  124. <xsl:text>```</xsl:text>
  125. <xsl:value-of select="$linefeed"/>
  126. <xsl:value-of select="$linefeed"/>
  127. </xsl:template>
  128. <xsl:template match="blockquote" mode="ast">
  129. <xsl:param name="prefix" tunnel="yes" as="xs:string?" select="()"/>
  130. <xsl:apply-templates mode="ast">
  131. <xsl:with-param name="prefix" tunnel="yes" select="concat($prefix, '> ')"/>
  132. </xsl:apply-templates>
  133. </xsl:template>
  134. <xsl:template name="process-inline-contents">
  135. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  136. <xsl:param name="prefix" tunnel="yes" as="xs:string?" select="()"/>
  137. <xsl:variable name="contents" as="xs:string">
  138. <xsl:value-of>
  139. <xsl:apply-templates mode="ast"/>
  140. </xsl:value-of>
  141. </xsl:variable>
  142. <xsl:variable name="idnt" select="if (ancestor-or-self::tablecell) then () else $indent" as="xs:string?"/>
  143. <xsl:for-each select="tokenize($contents, '\n')">
  144. <xsl:value-of select="$idnt"/>
  145. <xsl:value-of select="$prefix"/>
  146. <xsl:value-of select="."/>
  147. <xsl:if test="position() ne last()">
  148. <xsl:value-of select="$linefeed"/>
  149. </xsl:if>
  150. </xsl:for-each>
  151. </xsl:template>
  152. <xsl:template match="table" mode="ast">
  153. <xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>
  154. <xsl:for-each select="thead">
  155. <xsl:value-of select="$indent"/>
  156. <xsl:for-each select="tr">
  157. <xsl:text>|</xsl:text>
  158. <xsl:for-each select="tablecell">
  159. <xsl:call-template name="process-inline-contents"/>
  160. <xsl:text>|</xsl:text>
  161. </xsl:for-each>
  162. <xsl:value-of select="$linefeed"/>
  163. </xsl:for-each>
  164. <xsl:for-each select="tr">
  165. <xsl:value-of select="$indent"/>
  166. <xsl:text>|</xsl:text>
  167. <xsl:for-each select="tablecell">
  168. <xsl:variable name="colnum" as="xs:integer" select="position()"/>
  169. <xsl:variable name="align" select="ancestor::table[1]/col[$colnum]/@align"/>
  170. <xsl:variable name="content">
  171. <xsl:call-template name="process-inline-contents"/>
  172. </xsl:variable>
  173. <xsl:value-of select="if ($align = ('left', 'center')) then ':' else '-'"/>
  174. <xsl:for-each select="3 to string-length($content)">-</xsl:for-each>
  175. <xsl:value-of select="if ($align = ('right', 'center')) then ':' else '-'"/>
  176. <xsl:text>|</xsl:text>
  177. </xsl:for-each>
  178. <xsl:value-of select="$linefeed"/>
  179. </xsl:for-each>
  180. </xsl:for-each>
  181. <xsl:for-each select="tbody">
  182. <xsl:for-each select="tr">
  183. <xsl:value-of select="$indent"/>
  184. <xsl:text>|</xsl:text>
  185. <xsl:for-each select="tablecell">
  186. <!--xsl:apply-templates mode="ast"/-->
  187. <xsl:call-template name="process-inline-contents"/>
  188. <xsl:text>|</xsl:text>
  189. </xsl:for-each>
  190. <xsl:value-of select="$linefeed"/>
  191. </xsl:for-each>
  192. </xsl:for-each>
  193. <xsl:value-of select="$linefeed"/>
  194. </xsl:template>
  195. <!-- Inline -->
  196. <xsl:template match="strong" mode="ast">
  197. <xsl:text>**</xsl:text>
  198. <xsl:apply-templates mode="ast"/>
  199. <xsl:text>**</xsl:text>
  200. </xsl:template>
  201. <xsl:template match="emph" mode="ast">
  202. <xsl:text>*</xsl:text>
  203. <xsl:apply-templates mode="ast"/>
  204. <xsl:text>*</xsl:text>
  205. </xsl:template>
  206. <xsl:template match="cite" mode="ast">
  207. <xsl:text>*</xsl:text>
  208. <xsl:apply-templates mode="ast"/>
  209. <xsl:text>*</xsl:text>
  210. </xsl:template>
  211. <xsl:template match="code" mode="ast">
  212. <xsl:text>`</xsl:text>
  213. <xsl:apply-templates mode="ast"/>
  214. <xsl:text>`</xsl:text>
  215. </xsl:template>
  216. <xsl:template match="link[empty(@href | @keyref)]" mode="ast">
  217. <xsl:apply-templates mode="ast"/>
  218. </xsl:template>
  219. <xsl:template match="link[@href]" mode="ast">
  220. <xsl:text>[</xsl:text>
  221. <xsl:apply-templates mode="ast"/>
  222. <xsl:text>]</xsl:text>
  223. <xsl:text>(</xsl:text>
  224. <xsl:value-of select="@href"/>
  225. <xsl:text>)</xsl:text>
  226. </xsl:template>
  227. <xsl:template match="link[empty(@href) and @keyref]" mode="ast">
  228. <xsl:text>[</xsl:text>
  229. <xsl:value-of select="@keyref"/>
  230. <xsl:text>]</xsl:text>
  231. </xsl:template>
  232. <xsl:template match="image" mode="ast">
  233. <xsl:text>![</xsl:text>
  234. <xsl:value-of select="@alt"/>
  235. <xsl:apply-templates mode="ast"/>
  236. <xsl:text>]</xsl:text>
  237. <xsl:text>(</xsl:text>
  238. <xsl:value-of select="@href"/>
  239. <xsl:if test="@title">
  240. <xsl:text> "</xsl:text>
  241. <xsl:value-of select="@title"/>
  242. <xsl:text>"</xsl:text>
  243. </xsl:if>
  244. <xsl:text>)</xsl:text>
  245. <xsl:if test="@placement = 'break'">
  246. <xsl:value-of select="$linefeed"/>
  247. <xsl:value-of select="$linefeed"/>
  248. </xsl:if>
  249. </xsl:template>
  250. <xsl:template match="image[empty(@href) and @keyref]" mode="ast">
  251. <xsl:text>![</xsl:text>
  252. <xsl:value-of select="@keyref"/>
  253. <xsl:text>]</xsl:text>
  254. </xsl:template>
  255. <xsl:template match="span" mode="ast">
  256. <xsl:apply-templates mode="ast"/>
  257. </xsl:template>
  258. <xsl:template match="linebreak" mode="ast">
  259. <xsl:text> </xsl:text>
  260. <xsl:value-of select="$linefeed"/>
  261. </xsl:template>
  262. <xsl:template match="text()" mode="ast"
  263. name="text">
  264. <xsl:param name="text" select="." as="xs:string"/>
  265. <xsl:variable name="head" select="substring($text, 1, 1)" as="xs:string"/>
  266. <xsl:if test="contains('\`*_{}[]()>#|', $head)"><!--{}+-.!-->
  267. <xsl:text>\</xsl:text>
  268. </xsl:if>
  269. <xsl:value-of select="$head"/>
  270. <xsl:variable name="tail" select="substring($text, 2)" as="xs:string"/>
  271. <xsl:if test="string-length($tail) gt 0">
  272. <xsl:call-template name="text">
  273. <xsl:with-param name="text" select="substring($text, 2)" as="xs:string"/>
  274. </xsl:call-template>
  275. </xsl:if>
  276. </xsl:template>
  277. <xsl:template match="code/text() |
  278. codeblock/text()"
  279. mode="ast" priority="10">
  280. <xsl:value-of select="."/>
  281. </xsl:template>
  282. <xsl:template match="node()" mode="ast" priority="-10">
  283. <xsl:message>ERROR: Unsupported AST node <xsl:value-of select="name()"/></xsl:message>
  284. <xsl:apply-templates mode="ast"/>
  285. </xsl:template>
  286. <!-- Whitespace cleanup -->
  287. <xsl:template match="text()"
  288. mode="ast-clean">
  289. <xsl:variable name="normalized" select="normalize-space(.)" as="xs:string"/>
  290. <xsl:choose>
  291. <xsl:when test="$normalized">
  292. <xsl:if test="preceding-sibling::node() and matches(., '^\s') and $normalized">
  293. <xsl:text> </xsl:text>
  294. </xsl:if>
  295. <xsl:value-of select="$normalized"/>
  296. <xsl:if test="following-sibling::node() and matches(., '\s$') and $normalized">
  297. <xsl:text> </xsl:text>
  298. </xsl:if>
  299. </xsl:when>
  300. <xsl:otherwise>
  301. <xsl:if test="preceding-sibling::node() and following-sibling::node()">
  302. <xsl:text> </xsl:text>
  303. </xsl:if>
  304. </xsl:otherwise>
  305. </xsl:choose>
  306. </xsl:template>
  307. <xsl:template match="pandoc/text() |
  308. div/text() |
  309. bulletlist/text() |
  310. orderedlist/text() |
  311. definitionlist/text() |
  312. dlentry/text() |
  313. table/text() |
  314. thead/text() |
  315. tbody/text() |
  316. tr/text()"
  317. mode="ast-clean" priority="10">
  318. <!--xsl:value-of select="normalize-space(.)"/-->
  319. </xsl:template>
  320. <xsl:template match="codeblock//text()"
  321. mode="ast-clean" priority="20">
  322. <xsl:value-of select="."/>
  323. </xsl:template>
  324. <xsl:template match="@* | node()"
  325. mode="ast-clean" priority="-10">
  326. <xsl:copy>
  327. <xsl:apply-templates select="@* | node()" mode="ast-clean"/>
  328. </xsl:copy>
  329. </xsl:template>
  330. <!-- Flatten -->
  331. <xsl:function name="ast:is-container-block" as="xs:boolean">
  332. <xsl:param name="node" as="node()"/>
  333. <xsl:sequence select="$node/self::rawblock or
  334. $node/self::blockquote or
  335. (:$node/self::orderedlist or
  336. $node/self::bulletlist or:)
  337. $node/self::li or
  338. (:$node/self::definitionlist or $node/self::dt or:) $node/self::dd or
  339. (:$node/self::table or $node/self::thead or $node/self::tbody or $node/self::tr or $node/self::tablecell or:)
  340. $node/self::div or
  341. $node/self::null"/>
  342. </xsl:function>
  343. <xsl:function name="ast:is-block" as="xs:boolean">
  344. <xsl:param name="node" as="node()"/>
  345. <xsl:sequence select="$node/self::plain or
  346. $node/self::para or
  347. $node/self::codeblock or
  348. $node/self::rawblock or
  349. $node/self::blockquote or
  350. $node/self::orderedlist or
  351. $node/self::bulletlist or
  352. $node/self::definitionlist or $node/self::dlentry or $node/self::dt or $node/self::dd or
  353. $node/self::header or
  354. $node/self::horizontalrule or
  355. $node/self::table or $node/self::thead or $node/self::tbody or $node/self::tr or $node/self::tablecell or
  356. $node/self::div or
  357. $node/self::null"/>
  358. </xsl:function>
  359. <xsl:template match="@* | node()" mode="flatten" priority="-1000">
  360. <xsl:copy>
  361. <xsl:apply-templates select="@* | node()" mode="flatten"/>
  362. </xsl:copy>
  363. </xsl:template>
  364. <!--xsl:template match="*[contains(@class, ' task/step ') or
  365. contains(@class, ' task/substep ')]" mode="flatten" priority="100">
  366. <xsl:copy>
  367. <xsl:apply-templates select="@* | *" mode="flatten"/>
  368. </xsl:copy>
  369. </xsl:template-->
  370. <xsl:template match="para" mode="flatten" priority="100">
  371. <xsl:choose>
  372. <xsl:when test="empty(node())"/>
  373. <xsl:when test="count(*) eq 1 and
  374. (*[ast:is-container-block(.)]) and
  375. empty(text()[normalize-space(.)])">
  376. <xsl:apply-templates mode="flatten"/>
  377. </xsl:when>
  378. <xsl:when test="descendant::*[ast:is-block(.)]">
  379. <xsl:variable name="current" select="." as="element()"/>
  380. <xsl:variable name="first" select="node()[1]" as="node()?"/>
  381. <xsl:for-each-group select="node()" group-adjacent="ast:is-block(.)">
  382. <xsl:choose>
  383. <xsl:when test="current-grouping-key()">
  384. <xsl:apply-templates select="current-group()" mode="flatten"/>
  385. </xsl:when>
  386. <xsl:when test="count(current-group()) eq 1 and current-group()/self::text() and not(normalize-space(current-group()))"/>
  387. <xsl:when test="parent::li and $first is current-group()[1]">
  388. <plain>
  389. <xsl:apply-templates select="current-group()" mode="flatten"/>
  390. </plain>
  391. </xsl:when>
  392. <xsl:otherwise>
  393. <para gen="1">
  394. <xsl:apply-templates select="$current/@* except $current/@id | current-group()" mode="flatten"/>
  395. </para>
  396. </xsl:otherwise>
  397. </xsl:choose>
  398. </xsl:for-each-group>
  399. </xsl:when>
  400. <xsl:otherwise>
  401. <xsl:copy>
  402. <xsl:apply-templates select="@* | node()" mode="flatten"/>
  403. </xsl:copy>
  404. </xsl:otherwise>
  405. </xsl:choose>
  406. </xsl:template>
  407. <!-- wrapper elements -->
  408. <xsl:template match="*[ast:is-container-block(.)]" mode="flatten" priority="10">
  409. <xsl:copy>
  410. <xsl:apply-templates select="@*" mode="flatten"/>
  411. <xsl:variable name="first" select="node()[1]" as="node()?"/>
  412. <xsl:for-each-group select="node()" group-adjacent="ast:is-block(.)">
  413. <xsl:choose>
  414. <xsl:when test="current-grouping-key()">
  415. <xsl:apply-templates select="current-group()" mode="flatten"/>
  416. </xsl:when>
  417. <xsl:when test="count(current-group()) eq 1 and current-group()/self::text() and not(normalize-space(current-group()))"/>
  418. <xsl:when test="parent::li and $first is current-group()[1]">
  419. <plain>
  420. <xsl:apply-templates select="current-group()" mode="flatten"/>
  421. </plain>
  422. </xsl:when>
  423. <xsl:otherwise>
  424. <para>
  425. <xsl:apply-templates select="current-group()" mode="flatten"/>
  426. </para>
  427. </xsl:otherwise>
  428. </xsl:choose>
  429. </xsl:for-each-group>
  430. </xsl:copy>
  431. </xsl:template>
  432. <!-- YAML -->
  433. <xsl:template match="head" mode="ast">
  434. <xsl:text>---&#xA;</xsl:text>
  435. <xsl:apply-templates select="*" mode="#current"/>
  436. <xsl:text>---&#xA;&#xA;</xsl:text>
  437. </xsl:template>
  438. <xsl:template match="map" mode="ast">
  439. <xsl:for-each select="entry">
  440. <xsl:value-of select="@key"/>
  441. <xsl:text>: </xsl:text>
  442. <xsl:apply-templates mode="#current"/>
  443. <xsl:text>&#xA;</xsl:text>
  444. </xsl:for-each>
  445. </xsl:template>
  446. <xsl:template match="array" mode="ast">
  447. <xsl:text>[</xsl:text>
  448. <xsl:for-each select="entry">
  449. <xsl:if test="position() ne 1">, </xsl:if>
  450. <xsl:apply-templates mode="#current"/>
  451. </xsl:for-each>
  452. <xsl:text>]</xsl:text>
  453. </xsl:template>
  454. </xsl:stylesheet>