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 : tei =" http://www.tei-c.org/ns/1.0"
5+ xmlns : t2m =" http://tei-2-markdown"
6+ exclude-result-prefixes =" xs"
7+ version =" 2.0" >
8+
9+ <xsl : output method =" xml" version =" 1.0" omit-xml-declaration =" no" encoding =" UTF-8" indent =" yes" />
10+
11+ <xsl : template name =" tei-to-markdown" >
12+ <xsl : apply-templates select =" ." mode =" tei-to-markdown" />
13+ </xsl : template >
14+
15+ <!-- Replace tei:hi h1 Elements with heading 1 Markdown -->
16+ <xsl : template match =" tei:hi[@rend eq 'h1']" mode =" tei-to-markdown" >
17+ <xsl : variable name =" children" as =" node()*" >
18+ <xsl : apply-templates select =" node()" mode =" #current" />
19+ </xsl : variable >
20+ <xsl : text ># </xsl : text ><xsl : sequence select =" t2m:trim-text($children)" /><xsl : text >
 </xsl : text >
21+ </xsl : template >
22+
23+ <!-- Replace tei:hi h2 Elements with heading 2 Markdown -->
24+ <xsl : template match =" tei:hi[@rend eq 'h2']" mode =" tei-to-markdown" >
25+ <xsl : variable name =" children" as =" node()*" >
26+ <xsl : apply-templates select =" node()" mode =" #current" />
27+ </xsl : variable >
28+ <xsl : text >## </xsl : text ><xsl : apply-templates select =" t2m:trim-text($children)" mode =" #current" /><xsl : text >
 </xsl : text >
29+ </xsl : template >
30+
31+ <!-- Replace tei:hi h3 Elements with heading 3 Markdown -->
32+ <xsl : template match =" tei:hi[@rend eq 'h3']" mode =" tei-to-markdown" >
33+ <xsl : variable name =" children" as =" node()*" >
34+ <xsl : apply-templates select =" node()" mode =" #current" />
35+ </xsl : variable >
36+ <xsl : text >### </xsl : text ><xsl : apply-templates select =" t2m:trim-text($children)" mode =" #current" /><xsl : text >
 </xsl : text >
37+ </xsl : template >
38+
39+ <!-- Replace tei:hi h1, h2, h3 Elements that contain only whitespace with a single space character -->
40+ <xsl : template match =" tei:hi[@rend = ('h1', 'h2', 'h3')][matches(., '^\s+$')]" mode =" tei-to-markdown" >
41+ <xsl : text > </xsl : text >
42+ </xsl : template >
43+
44+ <!-- Drop empty tei:hi Elements -->
45+ <xsl : template match =" tei:hi[empty(child::node())]" mode =" tei-to-markdown" />
46+
47+ <!-- Replace tei:emph bold Elements with bold Markdown -->
48+ <xsl : template match =" tei:emph[@rend eq 'bold']" mode =" tei-to-markdown" >
49+ <xsl : text >**</xsl : text ><xsl : apply-templates select =" node()" mode =" #current" /><xsl : text >**</xsl : text >
50+ </xsl : template >
51+
52+ <!-- Replace tei:emph italic Elements with bold Markdown -->
53+ <xsl : template match =" tei:emph[@rend eq 'italic']" mode =" tei-to-markdown" >
54+ <xsl : text >*</xsl : text ><xsl : apply-templates select =" node()" mode =" #current" /><xsl : text >*</xsl : text >
55+ </xsl : template >
56+
57+ <!-- Replace tei:emph bold or italic Elements that contain only whitespace with a single space character -->
58+ <xsl : template match =" tei:emph[@rend = ('bold', 'italic')][matches(., '^\s+$')]" mode =" tei-to-markdown" >
59+ <xsl : text > </xsl : text >
60+ </xsl : template >
61+
62+ <!-- Drop empty tei:emph Elements -->
63+ <xsl : template match =" tei:emph[empty(child::node())]" mode =" tei-to-markdown" />
64+
65+ <!-- Replace runs of whitespace between tei:emph Elements with a single space character -->
66+ <xsl : template match =" text()[preceding-sibling::tei:emph][following-sibling::tei:emph][matches(., '^\s+$')]" mode =" tei-to-markdown" >
67+ <xsl : text > </xsl : text >
68+ </xsl : template >
69+
70+ <!-- Remove space characters in text that occurs before tei:lb Elements -->
71+ <xsl : template match =" text()[ends-with(., ' ')][following-sibling::tei:lb]" mode =" tei-to-markdown" >
72+ <xsl : value-of select =" replace(replace(., '^(.*)\s+$', '$1'), '^\s+(.*)$', '$1')" />
73+ </xsl : template >
74+
75+ <!-- Remove space characters in text that occurs after tei:lb Elements -->
76+ <xsl : template match =" text()[starts-with(., ' ')][preceding-sibling::tei:lb]" mode =" tei-to-markdown" >
77+ <xsl : value-of select =" replace(replace(., '^\s+(.*)$', '$1'), '^(.*)\s+$', '$1')" />
78+ </xsl : template >
79+
80+ <!-- Replace tei:lb Elements with a line-break character when they have a following sibling (that is not a tei:lb) -->
81+ <xsl : template match =" tei:lb[following-sibling::node()[not(self::tei:lb)]]" mode =" tei-to-markdown" >
82+ <xsl : text >
 </xsl : text >
83+ </xsl : template >
84+
85+ <!-- Drop tei:lb Elements when they do not have a following sibling -->
86+ <xsl : template match =" tei:lb[empty(following-sibling::node())]" mode =" tei-to-markdown" />
87+
88+ <!-- Separate tei:p Elements with a single line-break character (i.e.: '\n') -->
89+ <xsl : template match =" tei:p[following-sibling::tei:p]" mode =" tei-to-markdown" >
90+ <xsl : apply-templates select =" node()|@*" mode =" #current" />
91+ <xsl : text >
 </xsl : text >
92+ </xsl : template >
93+
94+ <!-- Remove unknown Elements -->
95+ <xsl : template match =" element()" mode =" tei-to-markdown" >
96+ <xsl : apply-templates select =" node()|@*" mode =" #current" />
97+ </xsl : template >
98+
99+ <!-- Default: Identity trasform everything execept Elements (which will be dealt with above) -->
100+ <xsl : template match =" node()[not(. instance of element())]|@*" mode =" tei-to-markdown" priority =" -1" >
101+ <xsl : copy >
102+ <xsl : apply-templates select =" node()|@*" mode =" #current" />
103+ </xsl : copy >
104+ </xsl : template >
105+
106+ <xsl : function name =" t2m:trim-text" as =" node()*" >
107+ <xsl : param name =" input" required =" yes" as =" node()*" />
108+ <xsl : variable name =" length" select =" count($input)" />
109+ <xsl : choose >
110+ <xsl : when test =" $length eq 1" >
111+ <xsl : value-of select =" normalize-space($input)" />
112+ </xsl : when >
113+ <xsl : otherwise >
114+ <xsl : sequence select =" t2m:trim-text-replace(head($input), '^\s*(.+)$')" />
115+ <xsl : sequence select =" subsequence($input, 2, $length - 1)" />
116+ <xsl : sequence select =" t2m:trim-text-replace($input[$length], '^(.+)\s*$')" />
117+ </xsl : otherwise >
118+ </xsl : choose >
119+ </xsl : function >
120+
121+ <xsl : function name =" t2m:trim-text-replace" as =" node()?" >
122+ <xsl : param name =" node" as =" node()?" />
123+ <xsl : param name =" extract-pattern" as =" xs:string" />
124+ <xsl : choose >
125+ <xsl : when test =" $node instance of text()" >
126+ <xsl : value-of select =" replace($node, $extract-pattern, '$1')" />
127+ </xsl : when >
128+ <xsl : otherwise >
129+ <xsl : sequence select =" $node" />
130+ </xsl : otherwise >
131+ </xsl : choose >
132+ </xsl : function >
133+
134+ </xsl : stylesheet >
0 commit comments