Skip to content

Commit 5b31a6e

Browse files
committed
Move xml_encode_email_char_at_random to utils
1 parent a9d0bd6 commit 5b31a6e

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/markdown2.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
xml_oneliner_re_from_tab_width,
124124
hr_tag_re_from_tab_width,
125125
xml_escape_attr,
126+
xml_encode_email_char_at_random,
126127
)
127128

128129
# ---- globals
@@ -2363,7 +2364,7 @@ def _encode_email_address(self, addr):
23632364
#
23642365
# Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
23652366
# mailing list: <http://tinyurl.com/yu7ue>
2366-
chars = [_xml_encode_email_char_at_random(ch)
2367+
chars = [xml_encode_email_char_at_random(ch)
23672368
for ch in "mailto:" + addr]
23682369
# Strip the mailto: from the visible part.
23692370
addr = '<a href="%s">%s</a>' \
@@ -2482,20 +2483,6 @@ class UnicodeWithAttrs(str):
24822483
toc_html = None
24832484

24842485

2485-
def _xml_encode_email_char_at_random(ch):
2486-
r = random()
2487-
# Roughly 10% raw, 45% hex, 45% dec.
2488-
# '@' *must* be encoded. I [John Gruber] insist.
2489-
# Issue 26: '_' must be encoded.
2490-
if r > 0.9 and ch not in "@_":
2491-
return ch
2492-
elif r < 0.45:
2493-
# The [1:] is to drop leading '0': 0x63 -> x63
2494-
return '&#%s;' % hex(ord(ch))[1:]
2495-
else:
2496-
return '&#%s;' % ord(ch)
2497-
2498-
24992486
def _html_escape_url(attr, safe_mode=False):
25002487
"""Replace special characters that are potentially malicious in url string."""
25012488
escaped = (attr

lib/utils.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from random import random
12
import re
23

34

@@ -286,12 +287,23 @@ def xml_escape_attr(ampersand_re, attr, skip_single_quote=True):
286287
By default this doesn't bother with escaping `'` to `&#39;`, presuming that
287288
the tag attribute is surrounded by double quotes.
288289
"""
289-
escaped = ampersand_re.sub('&amp;', attr)
290+
escaped = ampersand_re.sub("&amp;", attr)
290291

291-
escaped = (attr
292-
.replace('"', '&quot;')
293-
.replace('<', '&lt;')
294-
.replace('>', '&gt;'))
292+
escaped = attr.replace('"', "&quot;").replace("<", "&lt;").replace(">", "&gt;")
295293
if not skip_single_quote:
296294
escaped = escaped.replace("'", "&#39;")
297-
return escaped
295+
return escaped
296+
297+
298+
def xml_encode_email_char_at_random(ch):
299+
r = random()
300+
# Roughly 10% raw, 45% hex, 45% dec.
301+
# '@' *must* be encoded. I [John Gruber] insist.
302+
# Issue 26: '_' must be encoded.
303+
if r > 0.9 and ch not in "@_":
304+
return ch
305+
elif r < 0.45:
306+
# The [1:] is to drop leading '0': 0x63 -> x63
307+
return "&#%s;" % hex(ord(ch))[1:]
308+
else:
309+
return "&#%s;" % ord(ch)

0 commit comments

Comments
 (0)