From 5cbe0246307408bf97ef80757c842c9783d5ea8a Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sat, 18 Feb 2023 16:26:18 +0900 Subject: [PATCH] escape < and > in MathJax --- lib/review/htmlbuilder.rb | 4 ++-- test/test_htmlbuilder.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index d61749cf1..421838af6 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -656,7 +656,7 @@ def texequation_body(lines) p = MathML::LaTeX::Parser.new(symbol: MathML::Symbol::CharacterReference) print p.parse(lines.join("\n") + "\n", true) elsif @book.config['math_format'] == 'mathjax' - puts "$$#{lines.join("\n")}$$" + puts "$$#{lines.join("\n").gsub('<', '\lt{}').gsub('>', '\gt{}')}$$" elsif @book.config['math_format'] == 'imgmath' fontsize = @book.config['imgmath_options']['fontsize'].to_f lineheight = @book.config['imgmath_options']['lineheight'].to_f @@ -1051,7 +1051,7 @@ def inline_m(str) parser = MathML::LaTeX::Parser.new(symbol: MathML::Symbol::CharacterReference) %Q(#{parser.parse(str, nil)}) elsif @book.config['math_format'] == 'mathjax' - %Q(\\( #{str} \\)) + %Q(\\( #{str.gsub('<', '\lt{}').gsub('>', '\gt{}')} \\)) elsif @book.config['math_format'] == 'imgmath' math_str = '$' + str + '$' key = Digest::SHA256.hexdigest(str) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 42b4aceaa..52237ecb8 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -451,6 +451,9 @@ def test_inline_mathjax actual = compile_inline('@{\\frac{-b \\pm \\sqrt{b^2 - 4ac\\}\\}{2a\\}}') assert_equal %Q(\\( \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a} \\)), actual + actual = compile_inline('@{a < b, b > c, a < b > c}') + assert_equal %Q(\\( a \\lt{} b, b \\gt{} c, a \\lt{} b \\gt{} c \\)), actual + content = <<-EOF //texequation{ \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a} @@ -459,6 +462,17 @@ def test_inline_mathjax actual = compile_block(content) expected = %Q(
\n$$\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$$\n
\n) assert_equal expected, actual + + content = <<-EOF +//texequation{ +\\begin{aligned} +a < b & b > c & a < b > c +\\end{aligned} +//} +EOF + actual = compile_block(content) + expected = %Q(
\n$$\\begin{aligned}\na \\lt{} b & b \\gt{} c & a \\lt{} b \\gt{} c\n\\end{aligned}$$\n
\n) + assert_equal expected, actual end def test_inline_img