Skip to content
Snippets Groups Projects
Commit 9e8256ae authored by Richard Berger's avatar Richard Berger Committed by Axel Kohlmeyer
Browse files

Refactored code and escape RST special character '_'

(cherry picked from commit 4629a464f701f919633fd220860b6610d097c291)
parent 925f1bfb
No related branches found
No related tags found
No related merge requests found
...@@ -65,6 +65,13 @@ class RSTMarkup(Markup): ...@@ -65,6 +65,13 @@ class RSTMarkup(Markup):
def escape_rst_chars(self, text): def escape_rst_chars(self, text):
text = text.replace('*', '\\*') text = text.replace('*', '\\*')
text = text.replace('^', '\\^') text = text.replace('^', '\\^')
text = re.sub(r'([^"])_', r'\1\\_', text)
return text
def unescape_rst_chars(self, text):
text = text.replace('\\*', '*')
text = text.replace('\\^', '^')
text = text.replace('\\_', '_')
return text return text
def inline_math(self, text): def inline_math(self, text):
...@@ -74,8 +81,7 @@ class RSTMarkup(Markup): ...@@ -74,8 +81,7 @@ class RSTMarkup(Markup):
while start_pos >= 0 and end_pos >= 0: while start_pos >= 0 and end_pos >= 0:
original = text[start_pos:end_pos+2] original = text[start_pos:end_pos+2]
formula = original[2:-2] formula = original[2:-2]
formula = formula.replace('\\*', '*') formula = self.unescape_rst_chars(formula)
formula = formula.replace('\\^', '^')
replacement = ":math:`" + formula.replace('\n', ' ').strip() + "`" replacement = ":math:`" + formula.replace('\n', ' ').strip() + "`"
text = text.replace(original, replacement) text = text.replace(original, replacement)
...@@ -88,6 +94,8 @@ class RSTMarkup(Markup): ...@@ -88,6 +94,8 @@ class RSTMarkup(Markup):
content = content.strip() content = content.strip()
content = content.replace('\n', ' ') content = content.replace('\n', ' ')
href = self.unescape_rst_chars(href)
anchor_pos = href.find('#') anchor_pos = href.find('#')
if anchor_pos >= 0: if anchor_pos >= 0:
...@@ -130,11 +138,11 @@ class RSTFormatting(Formatting): ...@@ -130,11 +138,11 @@ class RSTFormatting(Formatting):
link.lower().endswith('.jpeg') or link.lower().endswith('.jpeg') or
link.lower().endswith('.png') or link.lower().endswith('.png') or
link.lower().endswith('.gif')): link.lower().endswith('.gif')):
converted = ".. thumbnail:: " + link + "\n" converted = ".. thumbnail:: " + self.markup.unescape_rst_chars(link) + "\n"
else: else:
converted = ".. image:: " + file + "\n" converted = ".. image:: " + self.markup.unescape_rst_chars(file) + "\n"
if link: if link:
converted += " :target: " + link + "\n" converted += " :target: " + self.markup.unescape_rst_chars(link) + "\n"
if "c" in self.current_command_list: if "c" in self.current_command_list:
converted += " :align: center\n" converted += " :align: center\n"
...@@ -303,8 +311,7 @@ class RSTFormatting(Formatting): ...@@ -303,8 +311,7 @@ class RSTFormatting(Formatting):
start = "" start = ""
body = parts[0] body = parts[0]
body = body.replace('\\*', '*') body = self.markup.unescape_rst_chars(body)
body = body.replace('\\^', '^')
if len(start) > 0: if len(start) > 0:
text += start + "\n" text += start + "\n"
......
...@@ -85,6 +85,10 @@ class TestMarkup(unittest.TestCase): ...@@ -85,6 +85,10 @@ class TestMarkup(unittest.TestCase):
s = self.markup.convert("x^2") s = self.markup.convert("x^2")
self.assertEqual("x\^2", s) self.assertEqual("x\^2", s)
def test_escape_underscore(self):
s = self.markup.convert("x_")
self.assertEqual("x\_", s)
def test_paragraph_with_italic(self): def test_paragraph_with_italic(self):
self.assertEqual("A sentence with a *italic* word", self.markup.convert("A sentence with a {italic} word")) self.assertEqual("A sentence with a *italic* word", self.markup.convert("A sentence with a {italic} word"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment