Skip to content

Commit

Permalink
Move xml_encode_email_char_at_random to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Einenlum committed Jun 16, 2022
1 parent a9d0bd6 commit 5b31a6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
17 changes: 2 additions & 15 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
xml_oneliner_re_from_tab_width,
hr_tag_re_from_tab_width,
xml_escape_attr,
xml_encode_email_char_at_random,
)

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


def _xml_encode_email_char_at_random(ch):
r = random()
# Roughly 10% raw, 45% hex, 45% dec.
# '@' *must* be encoded. I [John Gruber] insist.
# Issue 26: '_' must be encoded.
if r > 0.9 and ch not in "@_":
return ch
elif r < 0.45:
# The [1:] is to drop leading '0': 0x63 -> x63
return '&#%s;' % hex(ord(ch))[1:]
else:
return '&#%s;' % ord(ch)


def _html_escape_url(attr, safe_mode=False):
"""Replace special characters that are potentially malicious in url string."""
escaped = (attr
Expand Down
24 changes: 18 additions & 6 deletions lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from random import random
import re


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

escaped = (attr
.replace('"', '&quot;')
.replace('<', '&lt;')
.replace('>', '&gt;'))
escaped = attr.replace('"', "&quot;").replace("<", "&lt;").replace(">", "&gt;")
if not skip_single_quote:
escaped = escaped.replace("'", "&#39;")
return escaped
return escaped


def xml_encode_email_char_at_random(ch):
r = random()
# Roughly 10% raw, 45% hex, 45% dec.
# '@' *must* be encoded. I [John Gruber] insist.
# Issue 26: '_' must be encoded.
if r > 0.9 and ch not in "@_":
return ch
elif r < 0.45:
# The [1:] is to drop leading '0': 0x63 -> x63
return "&#%s;" % hex(ord(ch))[1:]
else:
return "&#%s;" % ord(ch)

0 comments on commit 5b31a6e

Please sign in to comment.