|
49 | 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 | 50 | --> |
51 | 51 |
|
| 52 | + <xsl:template match="text()" mode="markdown-to-tei-container"> |
| 53 | + <!-- First we split on /n/n+ whilst ignoring misc whitespace to create headings or paragraphs --> |
| 54 | + <xsl:variable name="paragraphs" select="tokenize(., '\n[ \t\r]*\n+')[not(matches(., '^[ \t\r]+$'))][string-length(.) gt 0]"/> |
| 55 | + <xsl:for-each select="$paragraphs"> |
| 56 | + <xsl:variable name="paragraph" select="."/> |
| 57 | + <xsl:choose> |
| 58 | + <xsl:when test="matches($paragraph, '^#\s+.+')"> |
| 59 | + <!-- This is a Markdown heading 1 --> |
| 60 | + <xsl:apply-templates select="m2t:h1($paragraph)" mode="#current"/> |
| 61 | + </xsl:when> |
| 62 | + |
| 63 | + <xsl:when test="matches($paragraph, '^##\s+.+')"> |
| 64 | + <!-- This is a Markdown heading 2 --> |
| 65 | + <xsl:apply-templates select="m2t:h2($paragraph)" mode="#current"/> |
| 66 | + </xsl:when> |
| 67 | + |
| 68 | + <xsl:when test="matches($paragraph, '^###\s+.+')"> |
| 69 | + <!-- This is a Markdown heading 3 --> |
| 70 | + <xsl:apply-templates select="m2t:h3($paragraph)" mode="#current"/> |
| 71 | + </xsl:when> |
| 72 | + |
| 73 | + <xsl:otherwise> |
| 74 | + <!-- This is a Markdown paragraph --> |
| 75 | + <tei:p> |
| 76 | + <!-- We now split on /n whilst ignoring misc whitespace to create lines within a paragraph --> |
| 77 | + <xsl:variable name="lines" select="tokenize($paragraph, '\n')[not(matches(., '^[ \t\r]+$'))][string-length(.) gt 0]"/> |
| 78 | + <xsl:for-each select="(1 to count($lines))"> |
| 79 | + <xsl:variable name="i" select="." as="xs:integer"/> |
| 80 | + <xsl:variable name="line" select="$lines[$i]"/> |
| 81 | + <xsl:if test="$i gt 1"><tei:lb/><xsl:text>
</xsl:text></xsl:if> |
| 82 | + <xsl:sequence select="m2t:bold-and-italic($line)"/> |
| 83 | + </xsl:for-each> |
| 84 | + </tei:p> |
| 85 | + </xsl:otherwise> |
| 86 | + </xsl:choose> |
| 87 | + </xsl:for-each> |
| 88 | + </xsl:template> |
52 | 89 |
|
53 | 90 | <!-- Handle Raw text nodes inside some container Element --> |
54 | | - <xsl:template match="text()" mode="markdown-to-tei" priority="-1"> |
| 91 | + <!-- xsl:template match="text()" mode="markdown-to-tei" priority="-1"> |
55 | 92 | <xsl:for-each select="tokenize(., '\n[ \t\r]*\n+')[not(matches(., '^[ \t\r]+$'))][string-length(.) gt 0]"> |
56 | 93 | <xsl:variable name="lines" select="tokenize(., '\n')[not(matches(., '^[ \t\r]+$'))][string-length(.) gt 0]"/> |
57 | 94 | <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>
</xsl:text></xsl:if><xsl:value-of select="$lines[$i]"/></xsl:for-each></tei:p> |
58 | 95 | </xsl:for-each> |
59 | | - </xsl:template> |
| 96 | + </xsl:template --> |
60 | 97 |
|
61 | 98 | <!-- Default: Identity trasform everything --> |
62 | 99 | <xsl:template match="node()|@*" mode="markdown-to-tei" priority="-3"> |
|
66 | 103 | </xsl:template> |
67 | 104 |
|
68 | 105 | <xsl:function name="m2t:h1" as="node()+"> |
69 | | - <xsl:param name="markdown" as="text()" required="true"/> |
| 106 | + <xsl:param name="markdown" required="true"/> |
70 | 107 | <xsl:analyze-string select="$markdown" regex="(^|[^#])#\s+(.+)"> |
71 | 108 | <xsl:matching-substring> |
72 | | - <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:hi rend="h1"><xsl:value-of select="normalize-space(regex-group(2))"/></tei:hi> |
| 109 | + <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:hi rend="h1"><xsl:sequence select="m2t:bold-and-italic(normalize-space(regex-group(2)))"/></tei:hi> |
73 | 110 | </xsl:matching-substring> |
74 | 111 | <xsl:non-matching-substring> |
75 | 112 | <xsl:value-of select="."/> |
|
78 | 115 | </xsl:function> |
79 | 116 |
|
80 | 117 | <xsl:function name="m2t:h2" as="node()+"> |
81 | | - <xsl:param name="markdown" as="text()" required="true"/> |
| 118 | + <xsl:param name="markdown" required="true"/> |
82 | 119 | <xsl:analyze-string select="$markdown" regex="(^|[^#])##\s+(.+)"> |
83 | 120 | <xsl:matching-substring> |
84 | | - <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:hi rend="h2"><xsl:value-of select="normalize-space(regex-group(2))"/></tei:hi> |
| 121 | + <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:hi rend="h2"><xsl:sequence select="m2t:bold-and-italic(normalize-space(regex-group(2)))"/></tei:hi> |
85 | 122 | </xsl:matching-substring> |
86 | 123 | <xsl:non-matching-substring> |
87 | 124 | <xsl:value-of select="."/> |
|
90 | 127 | </xsl:function> |
91 | 128 |
|
92 | 129 | <xsl:function name="m2t:h3" as="node()+"> |
93 | | - <xsl:param name="markdown" as="text()" required="true"/> |
| 130 | + <xsl:param name="markdown" required="true"/> |
94 | 131 | <xsl:analyze-string select="$markdown" regex="(^|[^#])###\s+(.+)"> |
95 | 132 | <xsl:matching-substring> |
96 | | - <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:hi rend="h3"><xsl:value-of select="normalize-space(regex-group(2))"/></tei:hi> |
| 133 | + <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:hi rend="h3"><xsl:sequence select="m2t:bold-and-italic(normalize-space(regex-group(2)))"/></tei:hi> |
97 | 134 | </xsl:matching-substring> |
98 | 135 | <xsl:non-matching-substring> |
99 | 136 | <xsl:value-of select="."/> |
|
102 | 139 | </xsl:function> |
103 | 140 |
|
104 | 141 | <xsl:function name="m2t:bold" as="node()+"> |
105 | | - <xsl:param name="markdown" as="text()" required="true"/> |
| 142 | + <xsl:param name="markdown" required="true"/> |
106 | 143 | <xsl:analyze-string select="$markdown" regex="(^|[^\\])\*\*([^*]*[^*\\])\*\*"> |
107 | 144 | <xsl:matching-substring> |
108 | 145 | <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:emph rend="bold"><xsl:value-of select="regex-group(2)"/></tei:emph> |
|
114 | 151 | </xsl:function> |
115 | 152 |
|
116 | 153 | <xsl:function name="m2t:italic" as="node()+"> |
117 | | - <xsl:param name="markdown" as="text()" required="true"/> |
| 154 | + <xsl:param name="markdown" required="true"/> |
118 | 155 | <xsl:analyze-string select="$markdown" regex="(^|[^*\\])\*([^*]*[^*\\])\*([^*]|$)"> |
119 | 156 | <xsl:matching-substring> |
120 | 157 | <xsl:if test="m2t:non-empty(regex-group(1))"><xsl:value-of select="regex-group(1)"/></xsl:if><tei:emph rend="italic"><xsl:value-of select="regex-group(2)"/></tei:emph><xsl:if test="m2t:non-empty(regex-group(3))"><xsl:value-of select="regex-group(3)"/></xsl:if> |
|
124 | 161 | </xsl:non-matching-substring> |
125 | 162 | </xsl:analyze-string> |
126 | 163 | </xsl:function> |
| 164 | + |
| 165 | + <xsl:function name="m2t:bold-and-italic" as="node()+"> |
| 166 | + <xsl:param name="markdown" required="true"/> |
| 167 | + <xsl:variable name="after-bold" select="for $x in $markdown return if ($x instance of text() or $x instance of xs:string) then m2t:bold($x) else $x"/> |
| 168 | + <xsl:variable name="after-italic" select="for $x in $after-bold return if ($x instance of text() or $x instance of xs:string) then m2t:italic($x) else $x"/> |
| 169 | + <xsl:sequence select="$after-italic"/> |
| 170 | + </xsl:function> |
127 | 171 |
|
128 | 172 | <xsl:function name="m2t:non-empty" as="xs:string*"> |
129 | 173 | <xsl:param name="inputs" as="xs:string*" required="true"/> |
|
0 commit comments