Commit 925f1bfb authored by Richard Berger's avatar Richard Berger Committed by Axel Kohlmeyer
Browse files

Escape RST special character '^' and take care of special math cases

(cherry picked from commit 453521c8e682c6563226dfcc57c689052dacd21f)
parent 3f312244
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ class RSTMarkup(Markup):

    def escape_rst_chars(self, text):
        text = text.replace('*', '\\*')
        text = text.replace('^', '\\^')
        return text

    def inline_math(self, text):
@@ -73,6 +74,8 @@ class RSTMarkup(Markup):
        while start_pos >= 0 and end_pos >= 0:
            original = text[start_pos:end_pos+2]
            formula = original[2:-2]
            formula = formula.replace('\\*', '*')
            formula = formula.replace('\\^', '^')
            replacement = ":math:`" + formula.replace('\n', ' ').strip() + "`"
            text = text.replace(original, replacement)

@@ -300,6 +303,9 @@ class RSTFormatting(Formatting):
                start = ""
                body = parts[0]

            body = body.replace('\\*', '*')
            body = body.replace('\\^', '^')

            if len(start) > 0:
                text += start + "\n"
            text += "\n.. math::\n\n"
+17 −0
Original line number Diff line number Diff line
@@ -81,6 +81,10 @@ class TestMarkup(unittest.TestCase):
        s = self.markup.convert("[*bold] and {italic*}")
        self.assertEqual("**\*bold** and *italic\**", s)

    def test_escape_hat_character(self):
        s = self.markup.convert("x^2")
        self.assertEqual("x\^2", s)

    def test_paragraph_with_italic(self):
        self.assertEqual("A sentence with a *italic* word", self.markup.convert("A sentence with a {italic} word"))

@@ -411,6 +415,16 @@ class TestMathMarkup(unittest.TestCase):
                         "   \\frac{s_{ij} r_{ij} }{2} \\right)\n"
                         "   \\exp \\left( - s_{ij} r_{ij} \\right) \\end{equation}\n\n", s)

    def test_detect_latex_equation_with_mult(self):
        s = self.txt2rst.convert("\\begin\\{equation\\} a = b * c \\end\\{equation\\}\n")
        self.assertEqual("\n.. math::\n\n"
                         "   \\begin{equation} a = b * c \\end{equation}\n\n", s)

    def test_detect_latex_equation_with_pow(self):
        s = self.txt2rst.convert("\\begin\\{equation\\} a = b^c \\end\\{equation\\}\n")
        self.assertEqual("\n.. math::\n\n"
                         "   \\begin{equation} a = b^c \\end{equation}\n\n", s)

    def test_detect_inline_latex_equation(self):
        s = self.txt2rst.convert("Masses: \\begin\\{equation\\} M' = M + m \\end\\{equation\\}\n"
                                 "\\begin\\{equation\\} m' = \\frac \\{M\\, m \\} \\{M'\\} \\end\\{equation\\}\n")
@@ -426,6 +440,9 @@ class TestMathMarkup(unittest.TestCase):
    def test_detect_inline_math(self):
        self.assertEqual(":math:`x^2`", self.markup.convert("\\( x^2 \\)"))

    def test_detect_inline_math_mult(self):
        self.assertEqual(":math:`x * 2`", self.markup.convert("\\( x * 2 \\)"))

    def test_detect_multiline_inline_math(self):
        line = "\\(\\sqrt \\{ \\frac \\{2\, k_B \\mathtt\\{Tcom\\}\, m'\\}\n" \
               "\\{\\mathrm dt\\, \\mathtt\\{damp\\_com\\} \\}\n" \