diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 2c8327b..3bb345a 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -2,6 +2,12 @@ from lxml.etree import tostring from lxml.html.soupparser import fromstring +def escape(text): + if not text: + return '' + return text.replace('_', r'\_') + + class MarkdownConverter(object): def __init__(self, strip=None, keep=None): if strip is not None and keep is not None: @@ -16,20 +22,20 @@ class MarkdownConverter(object): return soup.text def convert_tag(self, node): - text = node.text or '' + text = escape(node.text) # Convert the children first for el in node.findall('*'): self.convert_tag(el) convert_fn = getattr(self, 'convert_%s' % el.tag, None) - tail = el.tail or '' + tail = escape(el.tail) el.tail = '' if convert_fn: text += convert_fn(el) else: - text += el.text or '' + text += el.text text += tail diff --git a/tests.py b/tests.py index b1e96b1..5cb370d 100644 --- a/tests.py +++ b/tests.py @@ -10,6 +10,9 @@ class BasicTests(unittest.TestCase): def test_soup(self): self.assertEqual(md('