public
Description: The source code for the Django Plugables application index.
Homepage: http://djangoplugables.com/
Clone URL: git://github.com/revyver/django-plugables.git
Search Repo:
Installing typogrify correctly this time.

git-svn-id: 
http://svn.revyver.com/djangopluggables.com/trunk/applications@200 
5c07df8f-9e68-4da1-b9d9-3219ff5d644b
revyver (author)
Sat Apr 05 20:05:56 -0700 2008
commit  110bf832de1e421b8e3586206ee3ec77f1a88721
tree    da00e3367143b6d48fdfbaca56b340abe6e4722e
parent  bd9fab5fb4d76cf2cf3a93e0c52ccc0a961210c4
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
0
@@ -0,0 +1,251 @@
0
+# from django.conf import settings
0
+import re
0
+from django.conf import settings
0
+from django import template
0
+from django.template.defaultfilters import stringfilter
0
+
0
+register = template.Library()
0
+
0
+def amp(text):
0
+ """Wraps apersands in HTML with ``<span class="amp">`` so they can be
0
+ styled with CSS. Apersands are also normalized to ``&amp;``. Requires
0
+ ampersands to have whitespace or an ``&nbsp;`` on both sides.
0
+
0
+ >>> amp('One & two')
0
+ 'One <span class="amp">&amp;</span> two'
0
+ >>> amp('One &amp; two')
0
+ 'One <span class="amp">&amp;</span> two'
0
+ >>> amp('One &#38; two')
0
+ 'One <span class="amp">&amp;</span> two'
0
+
0
+ >>> amp('One&nbsp;&amp;&nbsp;two')
0
+ 'One&nbsp;<span class="amp">&amp;</span>&nbsp;two'
0
+
0
+ It won't mess up & that are already wrapped, in entities or URLs
0
+
0
+ >>> amp('One <span class="amp">&amp;</span> two')
0
+ 'One <span class="amp">&amp;</span> two'
0
+ >>> amp('&ldquo;this&rdquo; & <a href="/?that&amp;test">that</a>')
0
+ '&ldquo;this&rdquo; <span class="amp">&amp;</span> <a href="/?that&amp;test">that</a>'
0
+
0
+ It should ignore standalone amps that are in attributes
0
+ >>> amp('<link href="xyz.html" title="One & Two">xyz</link>')
0
+ '<link href="xyz.html" title="One & Two">xyz</link>'
0
+ """
0
+ # tag_pattern from http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
0
+ # it kinda sucks but it fixes the standalone amps in attributes bug
0
+ tag_pattern = '</?\w+((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)/?>'
0
+ amp_finder = re.compile(r"(\s|&nbsp;)(&|&amp;|&\#38;)(\s|&nbsp;)")
0
+ intra_tag_finder = re.compile(r'(?P<prefix>(%s)?)(?P<text>([^<]*))(?P<suffix>(%s)?)' % (tag_pattern, tag_pattern))
0
+ def _amp_process(groups):
0
+ prefix = groups.group('prefix') or ''
0
+ text = amp_finder.sub(r"""\1<span class="amp">&amp;</span>\3""", groups.group('text'))
0
+ suffix = groups.group('suffix') or ''
0
+ return prefix + text + suffix
0
+ return intra_tag_finder.sub(_amp_process, text)
0
+# amp = stringfilter(amp)
0
+
0
+def caps(text):
0
+ """Wraps multiple capital letters in ``<span class="caps">``
0
+ so they can be styled with CSS.
0
+
0
+ >>> caps("A message from KU")
0
+ 'A message from <span class="caps">KU</span>'
0
+
0
+ Uses the smartypants tokenizer to not screw with HTML or with tags it shouldn't.
0
+
0
+ >>> caps("<PRE>CAPS</pre> more CAPS")
0
+ '<PRE>CAPS</pre> more <span class="caps">CAPS</span>'
0
+
0
+ >>> caps("A message from 2KU2 with digits")
0
+ 'A message from <span class="caps">2KU2</span> with digits'
0
+
0
+ >>> caps("Dotted caps followed by spaces should never include them in the wrap D.O.T. like so.")
0
+ 'Dotted caps followed by spaces should never include them in the wrap <span class="caps">D.O.T.</span> like so.'
0
+
0
+ >>> caps("<i>D.O.T.</i>HE34T<b>RFID</b>")
0
+ '<i><span class="caps">D.O.T.</span></i><span class="caps">HE34T</span><b><span class="caps">RFID</span></b>'
0
+ """
0
+ try:
0
+ import smartypants
0
+ except ImportError:
0
+ if settings.DEBUG:
0
+ raise template.TemplateSyntaxError, "Error in {% caps %} filter: The Python SmartyPants library isn't installed."
0
+ return text
0
+
0
+ tokens = smartypants._tokenize(text)
0
+ result = []
0
+ in_skipped_tag = False
0
+
0
+ cap_finder = re.compile(r"""(
0
+ (\b[A-Z\d]* # Group 2: Any amount of caps and digits
0
+ [A-Z]\d*[A-Z] # A cap string much at least include two caps (but they can have digits between them)
0
+ [A-Z\d]*\b) # Any amount of caps and digits
0
+ | (\b[A-Z]+\.\s? # OR: Group 3: Some caps, followed by a '.' and an optional space
0
+ (?:[A-Z]+\.\s?)+) # Followed by the same thing at least once more
0
+ (?:\s|\b|$))
0
+ """, re.VERBOSE)
0
+
0
+ def _cap_wrapper(matchobj):
0
+ """This is necessary to keep dotted cap strings to pick up extra spaces"""
0
+ if matchobj.group(2):
0
+ return """<span class="caps">%s</span>""" % matchobj.group(2)
0
+ else:
0
+ if matchobj.group(3)[-1] == " ":
0
+ caps = matchobj.group(3)[:-1]
0
+ tail = ' '
0
+ else:
0
+ caps = matchobj.group(3)
0
+ tail = ''
0
+ return """<span class="caps">%s</span>%s""" % (caps, tail)
0
+
0
+ tags_to_skip_regex = re.compile("<(/)?(?:pre|code|kbd|script|math)[^>]*>", re.IGNORECASE)
0
+
0
+
0
+ for token in tokens:
0
+ if token[0] == "tag":
0
+ # Don't mess with tags.
0
+ result.append(token[1])
0
+ close_match = tags_to_skip_regex.match(token[1])
0
+ if close_match and close_match.group(1) == None:
0
+ in_skipped_tag = True
0
+ else:
0
+ in_skipped_tag = False
0
+ else:
0
+ if in_skipped_tag:
0
+ result.append(token[1])
0
+ else:
0
+ result.append(cap_finder.sub(_cap_wrapper, token[1]))
0
+
0
+ return "".join(result)
0
+# caps = stringfilter(caps)
0
+
0
+def initial_quotes(text):
0
+ """Wraps initial quotes in ``class="dquo"`` for double quotes or
0
+ ``class="quo"`` for single quotes. Works in these block tags ``(h1-h6, p, li, dt, dd)``
0
+ and also accounts for potential opening inline elements ``a, em, strong, span, b, i``
0
+
0
+ >>> initial_quotes('"With primes"')
0
+ '<span class="dquo">"</span>With primes"'
0
+ >>> initial_quotes("'With single primes'")
0
+ '<span class="quo">\\'</span>With single primes\\''
0
+
0
+ >>> initial_quotes('<a href="#">"With primes and a link"</a>')
0
+ '<a href="#"><span class="dquo">"</span>With primes and a link"</a>'
0
+
0
+ >>> initial_quotes('&#8220;With smartypanted quotes&#8221;')
0
+ '<span class="dquo">&#8220;</span>With smartypanted quotes&#8221;'
0
+ """
0
+ quote_finder = re.compile(r"""((<(p|h[1-6]|li|dt|dd)[^>]*>|^) # start with an opening p, h1-6, li, dd, dt or the start of the string
0
+ \s* # optional white space!
0
+ (<(a|em|span|strong|i|b)[^>]*>\s*)*) # optional opening inline tags, with more optional white space for each.
0
+ (("|&ldquo;|&\#8220;)|('|&lsquo;|&\#8216;)) # Find me a quote! (only need to find the left quotes and the primes)
0
+ # double quotes are in group 7, singles in group 8
0
+ """, re.VERBOSE)
0
+ def _quote_wrapper(matchobj):
0
+ if matchobj.group(7):
0
+ classname = "dquo"
0
+ quote = matchobj.group(7)
0
+ else:
0
+ classname = "quo"
0
+ quote = matchobj.group(8)
0
+ return """%s<span class="%s">%s</span>""" % (matchobj.group(1), classname, quote)
0
+
0
+ return quote_finder.sub(_quote_wrapper, text)
0
+# initial_quotes = stringfilter(initial_quotes)
0
+
0
+def smartypants(text):
0
+ """Applies smarty pants to curl quotes.
0
+
0
+ >>> smartypants('The "Green" man')
0
+ 'The &#8220;Green&#8221; man'
0
+ """
0
+ try:
0
+ import smartypants
0
+ except ImportError:
0
+ if settings.DEBUG:
0
+ raise template.TemplateSyntaxError, "Error in {% smartypants %} filter: The Python smartypants library isn't installed."
0
+ return text
0
+ else:
0
+ return smartypants.smartyPants(text)
0
+# smartypants = stringfilter(smartypants)
0
+
0
+def typogrify(text):
0
+ """The super typography filter
0
+
0
+ Applies the following filters: widont, smartypants, caps, amp, initial_quotes
0
+
0
+ >>> typogrify('<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>')
0
+ '<h2><span class="dquo">&#8220;</span>Jayhawks&#8221; <span class="amp">&amp;</span> <span class="caps">KU</span> fans act extremely&nbsp;obnoxiously</h2>'
0
+ """
0
+ text = amp(text)
0
+ text = widont(text)
0
+ text = smartypants(text)
0
+ text = caps(text)
0
+ text = initial_quotes(text)
0
+ return text
0
+# typogrify = stringfilter(typogrify)
0
+
0
+def widont(text):
0
+ """Replaces the space between the last two words in a string with ``&nbsp;``
0
+ Works in these block tags ``(h1-h6, p, li, dd, dt)`` and also accounts for
0
+ potential closing inline elements ``a, em, strong, span, b, i``
0
+
0
+ >>> widont('A very simple test')
0
+ 'A very simple&nbsp;test'
0
+
0
+ Single word items shouldn't be changed
0
+ >>> widont('Test')
0
+ 'Test'
0
+ >>> widont(' Test')
0
+ ' Test'
0
+ >>> widont('<ul><li>Test</p></li><ul>')
0
+ '<ul><li>Test</p></li><ul>'
0
+ >>> widont('<ul><li> Test</p></li><ul>')
0
+ '<ul><li> Test</p></li><ul>'
0
+
0
+ >>> widont('<p>In a couple of paragraphs</p><p>paragraph two</p>')
0
+ '<p>In a couple of&nbsp;paragraphs</p><p>paragraph&nbsp;two</p>'
0
+
0
+ >>> widont('<h1><a href="#">In a link inside a heading</i> </a></h1>')
0
+ '<h1><a href="#">In a link inside a&nbsp;heading</i> </a></h1>'
0
+
0
+ >>> widont('<h1><a href="#">In a link</a> followed by other text</h1>')
0
+ '<h1><a href="#">In a link</a> followed by other&nbsp;text</h1>'
0
+
0
+ Empty HTMLs shouldn't error
0
+ >>> widont('<h1><a href="#"></a></h1>')
0
+ '<h1><a href="#"></a></h1>'
0
+
0
+ >>> widont('<div>Divs get no love!</div>')
0
+ '<div>Divs get no love!</div>'
0
+
0
+ >>> widont('<pre>Neither do PREs</pre>')
0
+ '<pre>Neither do PREs</pre>'
0
+
0
+ >>> widont('<div><p>But divs with paragraphs do!</p></div>')
0
+ '<div><p>But divs with paragraphs&nbsp;do!</p></div>'
0
+ """
0
+ widont_finder = re.compile(r"""((?:</?(?:a|em|span|strong|i|b)[^>]*>)|[^<>\s]) # must be proceeded by an approved inline opening or closing tag or a nontag/nonspace
0
+ \s+ # the space to replace
0
+ ([^<>\s]+ # must be flollowed by non-tag non-space characters
0
+ \s* # optional white space!
0
+ (</(a|em|span|strong|i|b)>\s*)* # optional closing inline tags with optional white space after each
0
+ ((</(p|h[1-6]|li|dt|dd)>)|$)) # end with a closing p, h1-6, li or the end of the string
0
+ """, re.VERBOSE)
0
+ return widont_finder.sub(r'\1&nbsp;\2', text)
0
+# widont = stringfilter(widont)
0
+
0
+register.filter('amp', amp)
0
+register.filter('caps', caps)
0
+register.filter('initial_quotes', initial_quotes)
0
+register.filter('smartypants', smartypants)
0
+register.filter('typogrify', typogrify)
0
+register.filter('widont', widont)
0
+
0
+def _test():
0
+ import doctest
0
+ doctest.testmod()
0
+
0
+if __name__ == "__main__":
0
+ _test()

Comments

    No one has commented yet.