Add MathJax backticks to possibility messages.

parent ff2b297b
...@@ -19,8 +19,12 @@ class Possibility(object): ...@@ -19,8 +19,12 @@ class Possibility(object):
if self.handler in MESSAGES: if self.handler in MESSAGES:
msg = MESSAGES[self.handler] msg = MESSAGES[self.handler]
# Surround math notation with backticks # Surround math notation with backticks. If there are any backticks
msg = re.sub('({[^ ]+)', r'`\1`', msg) # already, do not add additional backticks. The add_backticks
# lambda is necessary otherwise because \1 and \2 are not matched
# both at the same time.
add_backticks = lambda x: '`%s`' % ''.join(x.groups(''))
msg = re.sub('`([^`]*)`|\(?({[^. ]+)', add_backticks, msg)
if callable(msg): if callable(msg):
msg = msg(self.root, self.args) msg = msg(self.root, self.args)
......
...@@ -295,7 +295,7 @@ def sinus(root, args): ...@@ -295,7 +295,7 @@ def sinus(root, args):
return cos(root[0][0]) return cos(root[0][0])
MESSAGES[sinus] = _('Apply standard derivative d/dx sin(x) = cos(x).') MESSAGES[sinus] = _('Apply standard derivative `d/dx sin(x)` is `cos(x)`.')
def cosinus(root, args): def cosinus(root, args):
...@@ -305,7 +305,7 @@ def cosinus(root, args): ...@@ -305,7 +305,7 @@ def cosinus(root, args):
return -sin(root[0][0]) return -sin(root[0][0])
MESSAGES[cosinus] = _('Apply standard derivative d/dx cos(x) = -sin(x).') MESSAGES[cosinus] = _('Apply standard derivative `d/dx cos(x)` is `-sin(x)`.')
def tangens(root, args): def tangens(root, args):
......
...@@ -46,7 +46,7 @@ def division_by_one(root, args): ...@@ -46,7 +46,7 @@ def division_by_one(root, args):
return args[0].negate(root.negated) return args[0].negate(root.negated)
MESSAGES[division_by_one] = _('Division by 1 yields the nominator.') MESSAGES[division_by_one] = _('Division by `1` yields the nominator.')
def division_of_zero(root, args): def division_of_zero(root, args):
...@@ -56,7 +56,7 @@ def division_of_zero(root, args): ...@@ -56,7 +56,7 @@ def division_of_zero(root, args):
return L(0, negated=root.negated) return L(0, negated=root.negated)
MESSAGES[division_of_zero] = _('Division of 0 by {1} reduces to 0.') MESSAGES[division_of_zero] = _('Division of `0` by {1} reduces to `0`.')
def division_by_self(root, args): def division_by_self(root, args):
...@@ -66,7 +66,7 @@ def division_by_self(root, args): ...@@ -66,7 +66,7 @@ def division_by_self(root, args):
return L(1, negated=root.negated) return L(1, negated=root.negated)
MESSAGES[division_by_self] = _('Division of {1} by itself reduces to 1.') MESSAGES[division_by_self] = _('Division of {1} by itself reduces to `1`.')
def match_add_fractions(node): def match_add_fractions(node):
...@@ -159,7 +159,7 @@ def equalize_denominators(root, args): ...@@ -159,7 +159,7 @@ def equalize_denominators(root, args):
MESSAGES[equalize_denominators] = \ MESSAGES[equalize_denominators] = \
_('Equalize the denominators of divisions' ' {2} and {3} to {4}.') _('Equalize the denominators of divisions {2} and {3} to {4}.')
def constant_to_fraction(root, args): def constant_to_fraction(root, args):
...@@ -271,7 +271,8 @@ def divide_fraction(root, args): ...@@ -271,7 +271,8 @@ def divide_fraction(root, args):
return negate(a / (b * c), root.negated) return negate(a / (b * c), root.negated)
MESSAGES[divide_fraction] = _('Move {3} to denominator of fraction {1} / {2}.') MESSAGES[divide_fraction] = \
_('Move {3} to denominator of fraction `{1} / {2}`.')
def divide_by_fraction(root, args): def divide_by_fraction(root, args):
...@@ -285,7 +286,7 @@ def divide_by_fraction(root, args): ...@@ -285,7 +286,7 @@ def divide_by_fraction(root, args):
MESSAGES[divide_by_fraction] = \ MESSAGES[divide_by_fraction] = \
_('Move {3} to nominator of fraction {1} / {2}.') _('Move {3} to nominator of fraction `{1} / {2}`.')
def is_power_combination(a, b): def is_power_combination(a, b):
...@@ -398,7 +399,7 @@ def extract_fraction_terms(root, args): ...@@ -398,7 +399,7 @@ def extract_fraction_terms(root, args):
return negate(div, root.negated) return negate(div, root.negated)
MESSAGES[extract_fraction_terms] = _('Extract {3} / {4} from fraction {0}.') MESSAGES[extract_fraction_terms] = _('Extract `{3} / {4}` from fraction {0}.')
def divide_fraction_by_term(root, args): def divide_fraction_by_term(root, args):
......
...@@ -44,7 +44,7 @@ def add_quadrants(root, args): ...@@ -44,7 +44,7 @@ def add_quadrants(root, args):
return scope.as_nary_node() return scope.as_nary_node()
MESSAGES[add_quadrants] = _('Add the sinus and cosinus quadrants to 1.') MESSAGES[add_quadrants] = _('Add the sinus and cosinus quadrants to `1`.')
def factor_out_quadrant_negation(root, args): def factor_out_quadrant_negation(root, args):
...@@ -59,7 +59,7 @@ def factor_out_quadrant_negation(root, args): ...@@ -59,7 +59,7 @@ def factor_out_quadrant_negation(root, args):
MESSAGES[factor_out_quadrant_negation] = _('Factor out the negations of {2} ' \ MESSAGES[factor_out_quadrant_negation] = _('Factor out the negations of {2} ' \
'and {3} to be able to reduce the quadrant addition to 1.') 'and {3} to be able to reduce the quadrant addition to `1`.')
def match_negated_parameter(node): def match_negated_parameter(node):
......
...@@ -69,7 +69,7 @@ def solve_indef(root, args): ...@@ -69,7 +69,7 @@ def solve_indef(root, args):
def solve_indef_msg(root, args): # pragma: nocover def solve_indef_msg(root, args): # pragma: nocover
return _('Solve indefinite integral {0} using substitution ' \ return _('Solve indefinite integral {0} using substitution ' \
'of %s with {0[2]} and {0[1]}.' % find_variable(root[0])) 'of `%s` with {0[2]} and {0[1]}.' % find_variable(root[0]))
MESSAGES[solve_indef] = solve_indef_msg MESSAGES[solve_indef] = solve_indef_msg
...@@ -106,7 +106,7 @@ def integrate_variable_root(root, args): ...@@ -106,7 +106,7 @@ def integrate_variable_root(root, args):
MESSAGES[integrate_variable_root] = _('Apply standard integral ' \ MESSAGES[integrate_variable_root] = _('Apply standard integral ' \
'int(x ^ n) = 1 / (n + 1) * x ^ (n + 1) + c.') '`int(x ^ n) = 1 / (n + 1) * x ^ (n + 1) + c`.')
def integrate_variable_exponent(root, args): def integrate_variable_exponent(root, args):
...@@ -119,7 +119,7 @@ def integrate_variable_exponent(root, args): ...@@ -119,7 +119,7 @@ def integrate_variable_exponent(root, args):
MESSAGES[integrate_variable_exponent] = \ MESSAGES[integrate_variable_exponent] = \
_('Apply standard integral int(g ^ x) = g ^ x / ln(g) + c.') _('Apply standard integral `int(g ^ x) = g ^ x / ln(g) + c`.')
def match_constant_integral(node): def match_constant_integral(node):
...@@ -147,8 +147,8 @@ def single_variable_integral(root, args): ...@@ -147,8 +147,8 @@ def single_variable_integral(root, args):
return integral(root[0] ** 1, *root[1:]) return integral(root[0] ** 1, *root[1:])
MESSAGES[single_variable_integral] = _('Rewrite {0[0]} to {0[0]} ^ 1 and ' \ MESSAGES[single_variable_integral] = _('Rewrite {0[0]} to `{0[0]} ^ 1` and ' \
'apply the standard integral for {0[0]} ^ n.') 'apply the standard integral for `{0[0]} ^ n`.')
def constant_integral(root, args): def constant_integral(root, args):
...@@ -197,7 +197,7 @@ def split_negation_to_constant(root, args): ...@@ -197,7 +197,7 @@ def split_negation_to_constant(root, args):
MESSAGES[split_negation_to_constant] = _('Write the negation of {0[0]} as an' \ MESSAGES[split_negation_to_constant] = _('Write the negation of {0[0]} as an' \
' explicit -1 and bring it outside of the integral.') ' explicit `-1` and bring it outside of the integral.')
def factor_out_constant(root, args): def factor_out_constant(root, args):
...@@ -239,7 +239,7 @@ def division_integral(root, args): ...@@ -239,7 +239,7 @@ def division_integral(root, args):
MESSAGES[division_integral] = \ MESSAGES[division_integral] = \
_('1 / {0[1]} has the standard anti-derivative ln|{0[1]}| + c.') _('`1 / {0[1]}` has the standard anti-derivative `ln|{0[1]}| + c`.')
def extend_division_integral(root, args): def extend_division_integral(root, args):
...@@ -252,7 +252,7 @@ def extend_division_integral(root, args): ...@@ -252,7 +252,7 @@ def extend_division_integral(root, args):
MESSAGES[extend_division_integral] = _('Bring nominator {0[0][0]} out of the' \ MESSAGES[extend_division_integral] = _('Bring nominator {0[0][0]} out of the' \
' fraction to obtain a standard 1 / {0[0][1]} integral.') ' fraction to obtain a standard `1 / {0[0][1]}` integral.')
def match_function_integral(node): def match_function_integral(node):
...@@ -289,8 +289,8 @@ def logarithm_integral(root, args): ...@@ -289,8 +289,8 @@ def logarithm_integral(root, args):
return solve_integral(root, (x * ln(x) - x) / ln(g)) return solve_integral(root, (x * ln(x) - x) / ln(g))
MESSAGES[logarithm_integral] = _('log_g(x) has the standard anti-derivative ' \ MESSAGES[logarithm_integral] = _('`log_g(x)` has the standard ' \
'(xln(x) - x) / log_g(x) + c.') 'anti-derivative `(xln(x) - x) / log_g(x) + c`.')
def sinus_integral(root, args): def sinus_integral(root, args):
...@@ -301,7 +301,7 @@ def sinus_integral(root, args): ...@@ -301,7 +301,7 @@ def sinus_integral(root, args):
MESSAGES[sinus_integral] = \ MESSAGES[sinus_integral] = \
_('{0[0]} has the standard anti-derivative -cos({0[0][0]}) + c.') _('{0[0]} has the standard anti-derivative `-cos({0[0][0]}) + c`.')
def cosinus_integral(root, args): def cosinus_integral(root, args):
...@@ -312,7 +312,7 @@ def cosinus_integral(root, args): ...@@ -312,7 +312,7 @@ def cosinus_integral(root, args):
MESSAGES[cosinus_integral] = \ MESSAGES[cosinus_integral] = \
_('{0[0]} has the standard anti-derivative sin({0[0][0]}) + c.') _('{0[0]} has the standard anti-derivative `sin({0[0][0]}) + c`.')
def match_sum_rule_integral(node): def match_sum_rule_integral(node):
......
...@@ -28,7 +28,7 @@ class TestPossibilities(unittest.TestCase): ...@@ -28,7 +28,7 @@ class TestPossibilities(unittest.TestCase):
def test___str__(self): def test___str__(self):
self.assertEqual(str(self.p0), self.assertEqual(str(self.p0),
'<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>') '<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>')
self.assertEqual(str(self.p1), 'foo 1 + 2 bar') self.assertEqual(str(self.p1), 'foo `1` + `2` bar')
def test___repr__(self): def test___repr__(self):
self.assertEqual(repr(self.p0), self.assertEqual(repr(self.p0),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment