forked from EpiDoc/Stylesheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htm-tpl-sqbrackets.xsl
113 lines (99 loc) · 5.66 KB
/
htm-tpl-sqbrackets.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xd:doc xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> Nov 2, 2011</xd:p>
<xd:p><xd:b>Author:</xd:b> gbodard</xd:p>
<xd:p><xd:b>Author:</xd:b> flawrence</xd:p>
<xd:p><xd:b>Author:</xd:b> rviglianti</xd:p>
<xd:p></xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="comment()" mode="sqbrackets">
<xsl:sequence select="."/>
</xsl:template>
<xsl:template match="*" mode="sqbrackets">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="sqbrackets"/>
</xsl:element>
</xsl:template>
<xsl:template match="br" mode="sqbrackets">
<xsl:sequence select="."/>
</xsl:template>
<xsl:template match="text()" mode="sqbrackets">
<xsl:variable name="me" select="."/>
<xsl:variable name="startspace" select="if (matches(substring(.,1,1),'[\n\r\s\t]')) then ' ' else ''"/>
<xsl:variable name="endspace" select="if (matches(substring(.,string-length(.)),'[\n\r\s\t]')) then ' ' else ''"/>
<xsl:variable name="ignorableThingsBetweenAdjacentBrackets">
<regexselectors>
<!-- space-->
<selector>\s</selector>
<!-- keraia -->
<selector>´</selector>
<!-- editorial comma-->
<selector>,</selector>
<!-- add another one below if something else
needs to be ignored when removing adjacent ][ -->
</regexselectors>
</xsl:variable>
<xsl:variable name="closingadjacentbrackettypes">
<closingbrackets>
<!-- supplied, square bracket must be escaped for regex-->
<bracket>\]</bracket>
<!-- del-->
<bracket>⟧</bracket>
</closingbrackets>
</xsl:variable>
<xsl:variable name="openingadjacentbrackettypes">
<closingbrackets>
<!-- supplied, square bracket must be escaped for regex-->
<bracket>\[</bracket>
<!-- del-->
<bracket>⟦</bracket>
</closingbrackets>
</xsl:variable>
<xsl:variable name="regex" select="concat('([^\]]?)[',string-join($closingadjacentbrackettypes//text(),''),']([',string-join($ignorableThingsBetweenAdjacentBrackets//text(),''),']*)[',string-join($openingadjacentbrackettypes//text(),''),']([^\[]?)')"/>
<xsl:variable name="current" select="replace(normalize-space(.), $regex, '$1$2$3')" />
<xsl:variable name="strlength" select="string-length($current)"/>
<xsl:variable name="firstletter" select="substring($current, 1, 1)"/>
<xsl:variable name="lastletter" select="substring($current, $strlength)"/>
<xsl:value-of select="$startspace"/>
<xsl:choose>
<xsl:when test="$firstletter = '[' or $lastletter = ']'">
<xsl:variable name="previous" select="preceding::text()[1]" />
<xsl:variable name="after" select="following::text()[1]" />
<!-- get the first text node before the next br (if this node matches current text node then there is a br between this text node and the next text node so don't strip brackets) -->
<xsl:variable name="beforenextbr" select="following::br[1]/preceding::text()[1]"/>
<!-- get the first next node following the preceding br (if this node matches current text node then there is a br between this text node and the previous text node so don't strip brackets)-->
<xsl:variable name="afterprevbr" select="preceding::br[1]/following::text()[1]"/>
<xsl:variable name="afirstletter" select="substring(normalize-space($after), 1, 1)"/>
<xsl:variable name="plastletter" select="substring(normalize-space($previous), string-length(normalize-space($previous)))"/>
<xsl:choose>
<xsl:when test="$firstletter = '[' and $lastletter = ']'
and $plastletter = ']' and $afirstletter = '['
and generate-id($afterprevbr) != generate-id(.)
and generate-id($beforenextbr) != generate-id(.)">
<xsl:value-of select="substring($current, 2, string-length($current)-2)"/>
</xsl:when>
<xsl:when test="$plastletter = ']' and $firstletter = '['
and generate-id($afterprevbr) != generate-id(.)">
<xsl:value-of select="substring($current, 2)"/>
</xsl:when>
<xsl:when test="$afirstletter = '[' and $lastletter = ']'
and generate-id($beforenextbr) != generate-id(.)">
<xsl:value-of select="substring($current, 1, string-length($current)-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$current"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$current"/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$endspace"/>
</xsl:template>
</xsl:stylesheet>