forked from snytkine/LampCMS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plaintext.xsl
204 lines (167 loc) · 5.65 KB
/
plaintext.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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"
exclude-result-prefixes="php">
<!-- omit-xml-declaration="yes" -->
<xsl:output method="text" xml:lang="en" indent="no"
encoding="UTF-8" />
<xsl:variable name="tab" select="'	'" />
<xsl:variable name="spacer" select="' '" />
<xsl:variable name="hr"
select="'
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
'" />
<xsl:variable name="li" select="'
	'" />
<xsl:variable name="quote"
select="'QUOTE: 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'" />
<xsl:variable name="endquote"
select="'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
END QUOTE
'" />
<xsl:variable name="lf" select="'
'" />
<xsl:variable name="td" select="'|	'" />
<xsl:variable name="lf2" select="'

'" />
<xsl:variable name="p" select="'

	'" />
<xsl:variable name="cr" select="' '" />
<xsl:strip-space elements="*" />
<xsl:preserve-space elements="pre" />
<xsl:namespace-alias stylesheet-prefix="php"
result-prefix="xsl" />
<xsl:template match="/">
<xsl:element name="plaintext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
<!-- elements that we want to completely drop -->
<xsl:template
match="head|script|object|embed|video|form|button|select|option|style" />
<!-- // elements to drop -->
<xsl:template match="p">
<xsl:value-of select="$p" />
<xsl:apply-templates />
<xsl:value-of select="$lf2" />
</xsl:template>
<xsl:template match="h1">
<xsl:value-of select="$p" />
<!-- <xsl:text> # </xsl:text> -->
<xsl:value-of select="normalize-space(php:function('strtoupper', string(.)))" />
<xsl:apply-templates />
<!-- <xsl:text> # </xsl:text> -->
<xsl:value-of select="$lf" />
</xsl:template>
<xsl:template match="h2">
<xsl:value-of select="$p" />
<xsl:text> </xsl:text>
<xsl:value-of select="normalize-space(php:function('strtoupper', string(.)))" />
<xsl:text> </xsl:text>
<xsl:value-of select="$lf" />
</xsl:template>
<xsl:template match="h3|h4|h5|h6">
<xsl:value-of select="$p" />
<xsl:text> </xsl:text>
<xsl:value-of select="normalize-space(php:function('ucwords', string(.)))" />
<xsl:text> </xsl:text>
<xsl:value-of select="$lf" />
</xsl:template>
<xsl:template match="br">
<xsl:value-of select="$lf" />
</xsl:template>
<xsl:template match="div|span">
<xsl:value-of select="$lf" />
<xsl:apply-templates />
</xsl:template>
<xsl:template match="i|em">
<xsl:text> </xsl:text>
<xsl:text>_</xsl:text>
<xsl:apply-templates />
<xsl:text>_</xsl:text>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="b|strong|big">
<xsl:text> </xsl:text>
<!-- <xsl:value-of select="php:function('strtoupper', string(.))" /> -->
<xsl:text>**</xsl:text>
<xsl:apply-templates />
<xsl:text>**</xsl:text>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="ul">
<xsl:value-of select="$lf" />
<xsl:for-each select="./li">
<xsl:value-of select="$li" />
<xsl:text>* </xsl:text>
<xsl:apply-templates />
</xsl:for-each>
<xsl:value-of select="$lf" />
</xsl:template>
<xsl:template match="ol">
<xsl:value-of select="$lf" />
<xsl:for-each select="./li">
<xsl:value-of select="$li" />
<xsl:value-of select="concat(position(), ') ')" />
<xsl:apply-templates />
</xsl:for-each>
<xsl:value-of select="$lf" />
</xsl:template>
<xsl:template match="hr">
<xsl:value-of select="$hr" />
</xsl:template>
<xsl:template match="a">
<xsl:choose>
<xsl:when
test="starts-with(./@href, 'http') or starts-with(./@href, 'ftp')">
<xsl:value-of select="normalize-space(.)" />
<xsl:text> [Link: </xsl:text>
<xsl:value-of select="./@href" />
<xsl:text> ] </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="img">
<xsl:if test="starts-with(./@src, 'http')">
<xsl:text> [IMAGE: </xsl:text>
<xsl:value-of select="./@src" />
<xsl:text> ] </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="blockquote|cite">
<xsl:value-of select="$quote" />
<xsl:apply-templates />
<xsl:value-of select="$endquote" />
</xsl:template>
<xsl:template match="table">
<xsl:apply-templates />
<xsl:value-of select="$lf" />
</xsl:template>
<xsl:template match="tr">
<xsl:value-of select="$hr" />
<xsl:apply-templates select="td|th" />
<xsl:value-of select="$tab" />
<xsl:text>|</xsl:text>
</xsl:template>
<xsl:template match="td">
<xsl:variable name="ret">
<xsl:value-of select="$td" />
<xsl:apply-templates />
<xsl:value-of select="$tab" />
</xsl:variable>
<xsl:value-of select="normalize-space($ret)" />
</xsl:template>
<xsl:template match="th">
<xsl:variable name="ret">
<xsl:value-of select="$td" />
<xsl:value-of select="php:function('strtoupper', string(.))" />
<xsl:value-of select="$tab" />
</xsl:variable>
<xsl:value-of select="normalize-space($ret)" />
</xsl:template>
<!-- removes spaces, tabs, line feeds from string -->
<xsl:template name="removeSpaces">
<xsl:param name="str" />
<xsl:value-of select="translate($str, ' 	,

, ', '')" />
</xsl:template>
<!-- // removes spaces -->
</xsl:stylesheet>