Skip to content

Commit 4462d84

Browse files
committed
Fix link replacer to properly escape special HTML characters.
1 parent 96eebd8 commit 4462d84

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

pelican/contents.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@
2626

2727
logger = logging.getLogger(__name__)
2828

29+
try:
30+
import html
31+
except ImportError:
32+
# html.escape()/html.unescape() is since Python 3.2, do this for py2.7
33+
# https://wiki.python.org/moin/EscapingHtml
34+
from xml.sax.saxutils import escape, unescape
35+
36+
class html(object):
37+
_html_escape_table = {'"': """,
38+
"'": "'"}
39+
_html_unescape_table = {'"': '"',
40+
''': "'"}
41+
42+
@classmethod
43+
def escape(cls, v): return escape(v, cls._html_escape_table)
44+
45+
@classmethod
46+
def unescape(cls, v): return unescape(v, cls._html_unescape_table)
47+
2948

3049
@python_2_unicode_compatible
3150
class Content(object):
@@ -230,9 +249,9 @@ def get_url_setting(self, key):
230249

231250
def _link_replacer(self, siteurl, m):
232251
what = m.group('what')
233-
value = urlparse(m.group('value'))
252+
value = urlparse(html.unescape(m.group('value')))
234253
path = value.path
235-
origin = m.group('path')
254+
origin = html.unescape(m.group('path'))
236255

237256
# XXX Put this in a different location.
238257
if what in {'filename', 'attach'}:
@@ -285,7 +304,7 @@ def _link_replacer(self, siteurl, m):
285304
# keep all other parts, such as query, fragment, etc.
286305
parts = list(value)
287306
parts[2] = origin
288-
origin = urlunparse(parts)
307+
origin = html.escape(urlunparse(parts))
289308

290309
return ''.join((m.group('markup'), m.group('quote'), origin,
291310
m.group('quote')))

0 commit comments

Comments
 (0)