-
Notifications
You must be signed in to change notification settings - Fork 2
/
handle-indent.xsl
213 lines (202 loc) · 10.5 KB
/
handle-indent.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:word200x="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:dbk="http://docbook.org/ns/docbook"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
xmlns:exsl="http://exslt.org/common"
xmlns:saxon="http://saxon.sf.net/"
xmlns:tr="http://transpect.io"
xmlns:hub="http://transpect.io/hub"
xmlns="http://docbook.org/ns/docbook"
version="2.0"
xpath-default-namespace="http://docbook.org/ns/docbook"
exclude-result-prefixes = "w o v wx xs dbk pkg r rel word200x exsl saxon fn tr hub">
<!-- Toleranz bei Einzügen (1 entspricht 1/20 pt) -->
<xsl:variable name="hub:indent-epsilon" select="30" as="xs:integer"/>
<xsl:variable name="hub:list-by-indent-exception-role-regex" select="'^TOC'" as="xs:string"/>
<!-- For some people it might be useful if paras with tables inside are not indented. Or empty paras. Those can be given here.
Also a possibility to define that only certain paras (with a list style format for instance) are processed.
The result should be false(), if its input should not be indented. -->
<xsl:function name="hub:condition-that-stops-indenting-apart-from-role-regex" as="xs:boolean">
<xsl:param name="input" as="element(*)*"/>
<!-- to allow other conversions to stay unaffected. Override in adaptations -->
<xsl:sequence select="true()"/>
</xsl:function>
<xsl:function name="hub:lists-permitted-here" as="xs:boolean">
<xsl:param name="input" as="element(*)"/>
<!-- had to be refactorized. In some cases lists in bibliodivs are needed for example-->
<xsl:sequence select="exists(
$input[
not(self::footnote)
and not(ancestor-or-self::toc)
and not(ancestor-or-self::bibliography)
and not(ancestor-or-self::info[
not($input/local-name() = ('abstract', 'formalpara', 'legalnotice', 'printhistory'))
])
and not(self::remark[@role = 'endnote'])
])"/>
</xsl:function>
<!-- phrase/@role='hub:identifier' have been marked in mode hub:identifiers -->
<xsl:template match="*[
*[
@margin-left > $hub:indent-epsilon
and not(matches(@role, $hub:list-by-indent-exception-role-regex))
]
]" mode="hub:handle-indent">
<xsl:choose>
<xsl:when test="hub:lists-permitted-here(.)">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:for-each-group select="* | comment() | processing-instruction()"
group-adjacent="hub:is-list-member-candidate(.)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:call-template name="create-list">
<xsl:with-param name="nodes" select="current-group()"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" mode="#current"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:next-match/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:function name="hub:is-list-member-candidate" as="xs:boolean">
<xsl:param name="para" as="node()?"/>
<xsl:sequence
select="exists(
$para/(self::processing-instruction() | self::comment())[hub:is-list-member-candidate(preceding-sibling::*[1])]
union
$para/self::*[
(
(
@margin-left
and @margin-left > $hub:indent-epsilon
)
(: why should a positive text-indent indicate that there is a list?
or
(
@text-indent
and @text-indent > $hub:indent-epsilon
)
:)
)
(: cf. discussion in https://letexml.slack.com/archives/hub2app/p1426695436000042
and not(
exists(@text-indent)
and exists(@margin-left)
and @text-indent + @margin-left = 0
and not(.//tab)
):)
and not(matches(@role, $hub:list-by-indent-exception-role-regex))
and not(self::*[local-name() = ('title', 'subtitle', 'titleabbrev', 'bridgehead', 'entry')])
and not(self::para[@role = $hub:equation-roles]
[empty(preceding-sibling::*[not(@role = $hub:equation-roles)][1]/self::para[hub:is-list-member-candidate(.)]
[$equations-after-list-paras-belong-to-list = 'yes']
)
]
)
and not(self::para[starts-with(@role, 'heading') or starts-with(@role, 'berschrift')])
and hub:condition-that-stops-indenting-apart-from-role-regex(.)
and not(self::biblioentry[count(child::*)=1 and bibliomisc[not(child::node())]]
)
]
)"/>
</xsl:function>
<xsl:template name="create-list">
<xsl:param name="nodes" as="node()+"/>
<xsl:param name="count" select="1" as="xs:integer"/>
<xsl:if test="$count gt 20">
<xsl:message terminate="yes">Terminated at list nesting depth 20. This list detection stylesheet is probably looping.</xsl:message>
</xsl:if>
<!--<xsl:variable name="indent" select="($nodes[1]/@margin-left, 0)[1] + ($nodes[1]/@text-indent, 0)[1]"/>-->
<xsl:variable name="indent" select="min( for $n in $nodes
return (($n/@margin-left, 0)[1] + ($n/@text-indent, 0)[1])
)" as="xs:double"/>
<xsl:variable name="temporary-list" as="document-node()">
<xsl:document>
<xsl:for-each-group select="$nodes"
group-adjacent="abs($indent - (@margin-left, 0)[1] - (@text-indent, 0)[1]) le $hub:indent-epsilon">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:for-each select="current-group()">
<listitem>
<xsl:attribute name="hub:indent" select="$indent"/>
<xsl:if test="abs((@margin-left, 0)[1] - (preceding-sibling::*[1]/@margin-left, 0)[1]) gt $hub:indent-epsilon
(:and abs((@text-indent, 0)[1]) gt $hub:indent-epsilon
and abs((preceding-sibling::*[1]/@text-indent, 0)[1]) gt $hub:indent-epsilon:)">
<xsl:attribute name="hub:margin-shift" select="(@margin-left, 0)[1] - (preceding-sibling::*[1]/@margin-left, 0)[1]"/>
</xsl:if>
<xsl:apply-templates select="." mode="#current"/>
</listitem>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="not($nodes[1]/@margin-left) and exists($nodes[1]/@text-indent) and ($nodes[1]/@text-indent = $indent)">
<xsl:for-each select="current-group()">
<listitem hub:indent="textindent">
<xsl:apply-templates select="." mode="#current"/>
</listitem>
</xsl:for-each>
</xsl:when>
<xsl:when test="$indent - (@margin-left, 0)[1] - (@text-indent, 0)[1] gt $hub:indent-epsilon">
<xsl:call-template name="create-list">
<xsl:with-param name="nodes" select="current-group()"/>
<xsl:with-param name="count" select="$count + 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<listitem>
<xsl:call-template name="create-list">
<xsl:with-param name="nodes" select="current-group()"/>
<xsl:with-param name="count" select="$count + 1"/>
</xsl:call-template>
</listitem>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:document>
</xsl:variable>
<!-- <xsl:sequence select="$temporary-list"></xsl:sequence>-->
<xsl:for-each-group select="$temporary-list/node()" group-adjacent="local-name()">
<xsl:choose>
<xsl:when test="current-grouping-key() = 'listitem'">
<xsl:for-each-group select="current-group()"
group-starting-with="*[@hub:margin-shift]
[@hub:indent = preceding-sibling::*[1]/self::listitem/@hub:indent]">
<orderedlist>
<xsl:attribute name="hub:margin-shift-group-position" select="position()"/>
<xsl:sequence select="current-group()"/>
</orderedlist>
</xsl:for-each-group>
</xsl:when>
<xsl:when test="current-grouping-key() = 'orderedlist'">
<xsl:sequence select="current-group()"/>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
Unexpected node <xsl:value-of select="current-grouping-key()"/> in create-list!
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="@hub:margin-shift | @hub:indent | @hub:margin-shift-group-position" mode="hub:lists"/>
</xsl:stylesheet>