-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart_break.xsl
124 lines (110 loc) · 5.04 KB
/
smart_break.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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
smart_break
Breaks long words at weakies (if possible) and inserts a space.
Long word can break tables. This templates splits word either at default
(or given weakies) after a given max-length. You can break words with CSS
(see "word-wrap") when using pixel-units, ems will not work.
<xsl:call-template name="smart_break">
<xsl:with-param name="string" select="'Supercalifragilisticexpialidocious'"/>
<xsl:with-param name="max_length" select="'20'"/>
</xsl:call-template>
Returns: Supercalifragilistic expialidocious
-->
<xsl:template name="smart_break">
<xsl:param name="string"/>
<xsl:param name="max_length" select="65"/>
<xsl:param name="weakies" select="'/=_+%-.@'"/>
<xsl:choose>
<!--
if a space is found, print the first part and call this template
again with the rest of the string for further breaks.
-->
<xsl:when test="contains(substring($string, 1, $max_length), ' ')">
<xsl:value-of select="substring-before($string, ' ')"/>
<xsl:text> </xsl:text>
<xsl:call-template name="smart_break">
<xsl:with-param name="string" select="substring-after($string, ' ')"/>
<xsl:with-param name="max_length" select="$max_length"/>
<xsl:with-param name="weakies" select="$weakies"/>
</xsl:call-template>
</xsl:when>
<!-- string length is too long, we have to break it anywhere. -->
<xsl:when test="string-length($string) > $max_length">
<!-- find last weakie position in string -->
<xsl:variable name="weakie_position">
<xsl:call-template name="weakie_position">
<xsl:with-param name="weakies" select="$weakies"/>
<xsl:with-param name="string" select="substring($string, 1, $max_length)"/>
</xsl:call-template>
</xsl:variable>
<!--
actual break position, when no weakie is found, we break at
max-length.
-->
<xsl:variable name="break_position">
<xsl:choose>
<xsl:when test="$weakie_position > 0">
<xsl:value-of select="$weakie_position"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$max_length"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="substring($string, 1, $break_position)"/>
<xsl:text> </xsl:text>
<!-- call smart_break again with the rest of the string -->
<xsl:call-template name="smart_break">
<xsl:with-param name="string" select="substring($string, $break_position+1)"/>
<xsl:with-param name="max_length" select="$max_length"/>
<xsl:with-param name="weakies" select="$weakies"/>
</xsl:call-template>
</xsl:when>
<!-- string is not long enough for smart breaking. just return it -->
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="weakie_position">
<xsl:param name="string"/>
<xsl:param name="weakies"/>
<xsl:param name="position" select="0"/>
<xsl:variable name="weakie" select="substring($weakies, 1, 1)"/>
<!--
loop through all weakies and search for the last position of one single
weakie.
-->
<xsl:choose>
<!--
when actual weakie is found in string, we recursively call this
function again with the rest of the string and check to see if
this weakie is found again.
-->
<xsl:when test="contains($string, $weakie)">
<xsl:variable name="weakie_position" select="string-length(substring-before($string, $weakie))+1"/>
<xsl:call-template name="weakie_position">
<xsl:with-param name="string" select="substring($string, $weakie_position+1)"/>
<xsl:with-param name="weakies" select="$weakies"/>
<xsl:with-param name="position" select="$weakie_position"/>
</xsl:call-template>
</xsl:when>
<!--
when weakie is not found, we move one weakie forward, unless no
weakies are left.
-->
<xsl:when test="not(contains($string, $weakie)) and string-length($weakies) > 1">
<xsl:call-template name="weakie_position">
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="weakies" select="substring($weakies, 2)"/>
<xsl:with-param name="position" select="$position"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$position"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>