Skip to content

Commit ea86943

Browse files
committed
fixup! New implementation of TEI to Markdown, and Markdown to TEI conversions using XSLT
1 parent ec75437 commit ea86943

3 files changed

Lines changed: 320 additions & 11 deletions

File tree

src/main/xar-resources/services/manuforma-form-to-tei.xslt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<xsl:param name="user-name" as="xs:string" select="'Guest'"/>
2020

2121

22-
<xsl:import href="tei-to-markdown.xslt"/>
22+
<xsl:import href="markdown-to-tei.xslt"/>
2323

2424
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
2525

@@ -33,7 +33,7 @@
3333
<xsl:template match="tei:note|tei:summary[not(parent::tei:layoutDesc)]|tei:quote">
3434
<xsl:copy>
3535
<xsl:apply-templates select="@*"/>
36-
<xsl:apply-templates select="node()" mode="tei-to-markdown"/>
36+
<xsl:apply-templates select="node()" mode="markdown-to-tei"/>
3737
</xsl:copy>
3838
</xsl:template>
3939

@@ -47,7 +47,7 @@
4747
<xsl:template match="tei:ab">
4848
<xsl:copy>
4949
<xsl:apply-templates select="@*"/>
50-
<xsl:apply-templates select="node()" mode="tei-to-markdown"/>
50+
<xsl:apply-templates select="node()" mode="markdown-to-tei"/>
5151
</xsl:copy>
5252
</xsl:template>
5353

src/main/xar-resources/services/markdown-to-tei.xslt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:xs="http://www.w3.org/2001/XMLSchema"
55
xmlns:tei="http://www.tei-c.org/ns/1.0"
66
xmlns:m2t="http://markdown-2-tei"
7-
exclude-result-prefixes="xs"
7+
exclude-result-prefixes="xs m2t"
88
version="2.0">
99

1010
<!--
@@ -40,10 +40,24 @@
4040
</xsl:template>
4141

4242
<!-- Replace italic Markdown with tei:emph italic Element -->
43-
<xsl:template match="text()[matches(., '(^|[^*\\])\*[^*]*[^*\\]\*([^*]|$)]')]" mode="markdown-to-tei">
43+
<xsl:template match="text()[matches(., '(^|[^*\\])\*[^*]*[^*\\]\*([^*]|$)')]" mode="markdown-to-tei">
4444
<xsl:apply-templates select="m2t:italic(.)" mode="#current"/>
4545
</xsl:template>
4646

47+
<!-- TODO(AR) the rule below adds a <tei:p> wrapper, the tei:p should only be applied when the parent of the text node is an appropriate container e.g. <tei:note>
48+
Ideally we want to keep tei:note and those kind of elements out of this XSLT - they should go in manuforma-form-to-tei.xspec
49+
Figure out whether it's best to move the tei:p creation to the manuforma-form-to-tei.xspec or to send some sort of mode/flag through to here and use different templates
50+
-->
51+
52+
53+
<!-- Handle Raw text nodes inside some container Element -->
54+
<xsl:template match="text()" mode="markdown-to-tei" priority="-1">
55+
<xsl:for-each select="tokenize(., '\n[ \t\r]*\n+')[not(matches(., '^[ \t\r]+$'))][string-length(.) gt 0]">
56+
<xsl:variable name="lines" select="tokenize(., '\n')[not(matches(., '^[ \t\r]+$'))][string-length(.) gt 0]"/>
57+
<tei:p><xsl:for-each select="(1 to count($lines))"><xsl:variable name="i" select="." as="xs:integer"/><xsl:if test="$i gt 1"><tei:lb/><xsl:text>&#xA;</xsl:text></xsl:if><xsl:value-of select="$lines[$i]"/></xsl:for-each></tei:p>
58+
</xsl:for-each>
59+
</xsl:template>
60+
4761
<!-- Default: Identity trasform everything -->
4862
<xsl:template match="node()|@*" mode="markdown-to-tei" priority="-3">
4963
<xsl:copy>

src/test/xar-resources/services/manuforma-form-to-tei.xspec

Lines changed: 301 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@
5959
</tei:note>
6060
</x:context>
6161

62-
<x:expect label="Then convert to Markdown">
63-
<tei:note>**Hello** there and welcome&#xa;&#xa;Goodbye, and see you *again* soon</tei:note>
62+
<x:expect label="Then copy as is">
63+
<tei:note>
64+
<tei:p><tei:emph rend="bold">Hello</tei:emph> there and welcome</tei:p>
65+
<tei:p>Goodbye, and see you <tei:emph rend="italic">again</tei:emph> soon</tei:p>
66+
</tei:note>
6467
</x:expect>
6568

6669
</x:scenario>
@@ -73,8 +76,9 @@
7376
</tei:note>
7477
</x:context>
7578

76-
<x:expect label="Then convert to Markdown">
77-
<tei:note>**Hello** there and welcomeGoodbye, and see you *again* soon</tei:note>
79+
<x:expect label="Then copy as is">
80+
<something><tei:emph rend="bold">Hello</tei:emph> there <unknown>and</unknown> welcome</something>
81+
<other>Goodbye, and see you <tei:emph rend="italic">again</tei:emph> soon</other>
7882
</x:expect>
7983

8084
</x:scenario>
@@ -87,11 +91,302 @@
8791
</tei:note>
8892
</x:context>
8993

90-
<x:expect label="Then convert to Markdown">
91-
<tei:note>**Hello** there and welcomeGoodbye, and see you *again* soon</tei:note>
94+
<x:expect label="Then copy as is">
95+
<tei:p><tei:emph rend="bold">Hello</tei:emph> there <unknown>and</unknown> welcome</tei:p>
96+
<other>Goodbye, and see you <tei:emph rend="italic">again</tei:emph> soon</other>
97+
</x:expect>
98+
99+
</x:scenario>
100+
101+
<!-- START AR - inverse tests of tei-to-manuforma-form.xspec -->
102+
<x:scenario label="When it has a single markdown paragraph">
103+
104+
<x:context>
105+
<tei:TEI>
106+
<tei:note>Hello World</tei:note>
107+
</tei:TEI>
108+
</x:context>
109+
110+
<x:expect label="Wrap text in a p element">
111+
<tei:TEI>
112+
<tei:note>
113+
<tei:p>Hello World</tei:p>
114+
</tei:note>
115+
</tei:TEI>
116+
</x:expect>
117+
118+
</x:scenario>
119+
120+
<x:scenario label="When it has two markdown paragraphs">
121+
122+
<x:context>
123+
<tei:TEI>
124+
<tei:note>Some text.
125+
126+
Some more text.</tei:note>
127+
</tei:TEI>
128+
</x:context>
129+
130+
<x:expect label="Wrap each paragraph in a p element">
131+
<tei:TEI>
132+
<tei:note>
133+
<tei:p>Some text.</tei:p>
134+
<tei:p>Some more text.</tei:p>
135+
</tei:note>
136+
</tei:TEI>
137+
</x:expect>
138+
139+
</x:scenario>
140+
141+
<x:scenario label="When it has a single markdown paragraph with line breaks">
142+
143+
<x:context>
144+
<tei:TEI>
145+
<tei:note>Once upon
146+
a time
147+
in
148+
the
149+
West</tei:note>
150+
</tei:TEI>
151+
</x:context>
152+
153+
<x:expect label="Wrap text in a p element with lb elements">
154+
<tei:TEI>
155+
<tei:note>
156+
<tei:p>Once upon<tei:lb/>
157+
a time<tei:lb/>
158+
in<tei:lb/>
159+
the<tei:lb/>
160+
West</tei:p>
161+
</tei:note>
162+
</tei:TEI>
163+
</x:expect>
164+
165+
</x:scenario>
166+
167+
<x:scenario label="When it has two markdown paragraphs with line breaks (without hidden whitespace)">
168+
169+
<x:context>
170+
<tei:TEI>
171+
<tei:note>Once upon
172+
a time
173+
in
174+
the
175+
West
176+
177+
Some more
178+
text.</tei:note>
179+
</tei:TEI>
180+
</x:context>
181+
182+
<x:expect label="Wrap each paragraph in a p element with lb elements">
183+
<tei:TEI>
184+
<tei:note>
185+
<tei:p>Once upon<tei:lb/>
186+
a time<tei:lb/>
187+
in<tei:lb/>
188+
the<tei:lb/>
189+
West</tei:p>
190+
<tei:p>Some more<tei:lb/>
191+
text.</tei:p>
192+
</tei:note>
193+
</tei:TEI>
194+
</x:expect>
195+
196+
</x:scenario>
197+
198+
<x:scenario label="When it has two markdown paragraphs with line breaks (with hidden whitespace)">
199+
200+
<x:context>
201+
<tei:TEI>
202+
<tei:note>Once upon
203+
a time
204+
in
205+
the
206+
West
207+
208+
Some more
209+
text.</tei:note>
210+
</tei:TEI>
211+
</x:context>
212+
213+
<x:expect label="Wrap each paragraph in a p element with lb elements">
214+
<tei:TEI>
215+
<tei:note>
216+
<tei:p>Once upon<tei:lb/>
217+
a time<tei:lb/>
218+
in<tei:lb/>
219+
the<tei:lb/>
220+
West</tei:p>
221+
<tei:p>Some more<tei:lb/>
222+
text.</tei:p>
223+
</tei:note>
224+
</tei:TEI>
225+
</x:expect>
226+
227+
</x:scenario>
228+
229+
<x:scenario label="When it has a single markdown paragraph with italic">
230+
231+
<x:context>
232+
<tei:TEI>
233+
<tei:note>Once upon *a time* in the *West*</tei:note>
234+
</tei:TEI>
235+
</x:context>
236+
237+
<x:expect label="Wrap text in a p element with italic emph elements">
238+
<tei:TEI>
239+
<tei:note>
240+
<tei:p>Once upon <tei:emph rend="italic">a time</tei:emph> in the <tei:emph rend="italic">West</tei:emph></tei:p>
241+
</tei:note>
242+
</tei:TEI>
92243
</x:expect>
244+
245+
</x:scenario>
246+
247+
<x:scenario label="When it has a single markdown paragraph with bold">
248+
249+
<x:context>
250+
<tei:TEI>
251+
<tei:note>Once upon **a time** in the **West**.</tei:note>
252+
</tei:TEI>
253+
</x:context>
254+
255+
<x:expect label="Wrap text in a p element with bold emph elements">
256+
<tei:TEI>
257+
<tei:note>
258+
<tei:p>Once upon <tei:emph rend="bold">a time</tei:emph> in the <tei:emph rend="bold">West</tei:emph>.</tei:p>
259+
</tei:note>
260+
</tei:TEI>
261+
</x:expect>
262+
263+
</x:scenario>
264+
265+
<x:scenario label="When it has a single level 1 heading and a single markdown paragraph">
266+
267+
<x:context>
268+
<tei:TEI>
269+
<tei:note># This is a level 1 heading
270+
Some text here.</tei:note>
271+
</tei:TEI>
272+
</x:context>
273+
274+
<x:expect label="Wrap heading in a h1 element and text in a p element">
275+
<tei:TEI>
276+
<tei:note>
277+
<tei:hi rend="h1">This is a level 1 heading</tei:hi>
278+
<tei:p>Some text here.</tei:p>
279+
</tei:note>
280+
</tei:TEI>
281+
</x:expect>
282+
283+
</x:scenario>
284+
285+
<x:scenario label="When it has a single level 2 heading and a single markdown paragraph">
286+
287+
<x:context>
288+
<tei:TEI>
289+
<tei:note># This is a level 2 heading
290+
Some text here.</tei:note>
291+
</tei:TEI>
292+
</x:context>
293+
294+
<x:expect label="Wrap heading in a h2 element and text in a p element">
295+
<tei:TEI>
296+
<tei:note>
297+
<tei:hi rend="h2">This is a level 2 heading</tei:hi>
298+
<tei:p>Some text here.</tei:p>
299+
</tei:note>
300+
</tei:TEI>
301+
</x:expect>
302+
303+
</x:scenario>
304+
305+
<x:scenario label="When it has a single level 3 heading and a single markdown paragraph">
306+
307+
<x:context>
308+
<tei:TEI>
309+
<tei:note># This is a level 3 heading
310+
Some text here.</tei:note>
311+
</tei:TEI>
312+
</x:context>
313+
314+
<x:expect label="Wrap heading in a h3 element and text in a p element">
315+
<tei:TEI>
316+
<tei:note>
317+
<tei:hi rend="h3">This is a level 3 heading</tei:hi>
318+
<tei:p>Some text here.</tei:p>
319+
</tei:note>
320+
</tei:TEI>
321+
</x:expect>
322+
323+
</x:scenario>
324+
325+
<x:scenario label="When it has multiple heading levels and markdown paragraphs">
326+
327+
<x:context>
328+
<tei:TEI>
329+
<tei:note># This is a level 1 heading
330+
Paragraph 1
331+
332+
## This is a level 2 heading
333+
Paragraph 2-1.
334+
335+
Paragraph 2-2.
336+
337+
### This is a level 3 heading
338+
Paragraph 3-1.
93339

340+
Paragraph 3-2
341+
342+
Paragraph 3-3.</tei:note>
343+
</tei:TEI>
344+
</x:context>
345+
346+
<x:expect label="Wrap headings in a h* elements and text in p elements">
347+
<tei:TEI>
348+
<tei:note>
349+
<tei:hi rend="h1">This is a level 1 heading</tei:hi>
350+
<tei:p>Paragraph 1</tei:p>
351+
<tei:hi rend="h2">This is a level 2 heading</tei:hi>
352+
<tei:p>Paragraph 2-1.</tei:p>
353+
<tei:p>Paragraph 2-2.</tei:p>
354+
<tei:hi rend="h3">This is a level 3 heading</tei:hi>
355+
<tei:p>Paragraph 3-1.</tei:p>
356+
<tei:p>Paragraph 3-2</tei:p>
357+
<tei:p>Paragraph 3-3.</tei:p>
358+
</tei:note>
359+
</tei:TEI>
360+
</x:expect>
361+
362+
</x:scenario>
363+
364+
<x:scenario label="When it has paragraps that contain carriage return characters">
365+
366+
<x:context>
367+
<tei:TEI>
368+
<tei:note>Once &#xD;upon&#xD;&#xA;a time&#xD; in the &#xD;&#xA;&#xD; West
369+
370+
There was a little Panda called Jennifer
371+
372+
who was a sleepy little panda</tei:note>
373+
</tei:TEI>
374+
</x:context>
375+
376+
<!-- TODO(AR) check this with Max - should &#xD;&#xA;&#xD; produce one or two <tei:lb> in the output -->
377+
378+
<x:expect label="Wrap each paragraph in a p element abd normalize paragraph breaks with lb elements">
379+
<tei:TEI>
380+
<tei:note>
381+
<tei:p>Once<tei:lb/>upon<tei:lb/>a time<tei:lb/>in the<tei:lb/>West</tei:p>
382+
<tei:p>&#xA;There was a little Panda called Jennifer&#xA;</tei:p>
383+
<tei:p>who was a sleepy little panda</tei:p>
384+
</tei:note>
385+
</tei:TEI>
386+
</x:expect>
387+
94388
</x:scenario>
389+
<!-- END AR - inverse tests of tei-to-manuforma-form.xspec -->
95390

96391
</x:scenario>
97392

0 commit comments

Comments
 (0)