Skip to content

Commit 8264b5e

Browse files
committed
new template for smart_breaking long words
1 parent d4e5311 commit 8264b5e

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

smart_break.xsl

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3+
4+
<!--
5+
smart_break
6+
Breaks long words at weakies (if possible) and inserts a space.
7+
8+
Long word can break tables. This templates splits word either at default
9+
(or given weakies) after a given max-length. You can break words with CSS
10+
(see "word-wrap") when using pixel-units, ems will not work.
11+
12+
<xsl:call-template name="smart_break">
13+
<xsl:with-param name="string" select="'Supercalifragilisticexpialidocious'"/>
14+
<xsl:with-param name="max_length" select="'20'"/>
15+
</xsl:call-template>
16+
Returns: Supercalifragilistic expialidocious
17+
-->
18+
<xsl:template name="smart_break">
19+
<xsl:param name="string"/>
20+
<xsl:param name="max_length" select="65"/>
21+
<xsl:param name="weakies" select="'/=_+%-.@'"/>
22+
23+
<xsl:choose>
24+
<!--
25+
if a space is found, print the first part and call this template
26+
again with the rest of the string for further breaks.
27+
-->
28+
<xsl:when test="contains(substring($string, 1, $max_length), ' ')">
29+
<xsl:value-of select="substring-before($string, ' ')"/>
30+
<xsl:text> </xsl:text>
31+
<xsl:call-template name="smart_break">
32+
<xsl:with-param name="string" select="substring-after($string, ' ')"/>
33+
<xsl:with-param name="max_length" select="$max_length"/>
34+
<xsl:with-param name="weakies" select="$weakies"/>
35+
</xsl:call-template>
36+
</xsl:when>
37+
<!-- string length is too long, we have to break it anywhere. -->
38+
<xsl:when test="string-length($string) &gt; $max_length">
39+
<!-- find last weakie position in string -->
40+
<xsl:variable name="weakie_position">
41+
<xsl:call-template name="weakie_position">
42+
<xsl:with-param name="weakies" select="$weakies"/>
43+
<xsl:with-param name="string" select="substring($string, 1, $max_length)"/>
44+
</xsl:call-template>
45+
</xsl:variable>
46+
47+
<!--
48+
actual break position, when no weakie is found, we break at
49+
max-length.
50+
-->
51+
<xsl:variable name="break_position">
52+
<xsl:choose>
53+
<xsl:when test="$weakie_position > 0">
54+
<xsl:value-of select="$weakie_position"/>
55+
</xsl:when>
56+
<xsl:otherwise>
57+
<xsl:value-of select="$max_length"/>
58+
</xsl:otherwise>
59+
</xsl:choose>
60+
</xsl:variable>
61+
62+
<xsl:value-of select="substring($string, 1, $break_position)"/>
63+
<xsl:text> </xsl:text>
64+
65+
<!-- call smart_break again with the rest of the string -->
66+
<xsl:call-template name="smart_break">
67+
<xsl:with-param name="string" select="substring($string, $break_position+1)"/>
68+
<xsl:with-param name="max_length" select="$max_length"/>
69+
<xsl:with-param name="weakies" select="$weakies"/>
70+
</xsl:call-template>
71+
</xsl:when>
72+
73+
<!-- string is not long enough for smart breaking. just return it -->
74+
<xsl:otherwise>
75+
<xsl:value-of select="$string"/>
76+
</xsl:otherwise>
77+
</xsl:choose>
78+
79+
</xsl:template>
80+
81+
<xsl:template name="weakie_position">
82+
<xsl:param name="string"/>
83+
<xsl:param name="weakies"/>
84+
<xsl:param name="position" select="0"/>
85+
86+
<xsl:variable name="weakie" select="substring($weakies, 1, 1)"/>
87+
88+
<!--
89+
loop through all weakies and search for the last position of one single
90+
weakie.
91+
-->
92+
<xsl:choose>
93+
<!--
94+
when actual weakie is found in string, we recursively call this
95+
function again with the rest of the string and check to see if
96+
this weakie is found again.
97+
-->
98+
<xsl:when test="contains($string, $weakie)">
99+
<xsl:variable name="weakie_position" select="string-length(substring-before($string, $weakie))+1"/>
100+
<xsl:call-template name="weakie_position">
101+
<xsl:with-param name="string" select="substring($string, $weakie_position+1)"/>
102+
<xsl:with-param name="weakies" select="$weakies"/>
103+
<xsl:with-param name="position" select="$weakie_position"/>
104+
</xsl:call-template>
105+
</xsl:when>
106+
<!--
107+
when weakie is not found, we move one weakie forward, unless no
108+
weakies are left.
109+
-->
110+
<xsl:when test="not(contains($string, $weakie)) and string-length($weakies) > 1">
111+
<xsl:call-template name="weakie_position">
112+
<xsl:with-param name="string" select="$string"/>
113+
<xsl:with-param name="weakies" select="substring($weakies, 2)"/>
114+
<xsl:with-param name="position" select="$position"/>
115+
</xsl:call-template>
116+
</xsl:when>
117+
<xsl:otherwise>
118+
<xsl:value-of select="$position"/>
119+
</xsl:otherwise>
120+
</xsl:choose>
121+
122+
</xsl:template>
123+
124+
</xsl:stylesheet>

0 commit comments

Comments
 (0)