Skip to content

Commit

Permalink
Adding support for stem equations (#24)
Browse files Browse the repository at this point in the history
* Adding support for stem equations

* added test cases
  • Loading branch information
HassanAkbar authored Mar 27, 2024
1 parent 5587781 commit 8e36f55
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
19 changes: 17 additions & 2 deletions _includes/script.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@
};

var replace_math_symb = function(s){
return s.replace(/(`[^`]+`)/g, '<<math_symbol>>');
// replace mathjax
mathjax_containers = s.querySelectorAll('math, mjx-container');

if(mathjax_containers.length > 0) {
s = s.cloneNode(true);
s.querySelectorAll('math, mjx-container').forEach(container => {
container.replaceWith('<<math_symbol>>');
});
}

text = s.innerText;

// replace asciimath
text = text.replace(/(`[^`]+`)/g, '<<math_symbol>>');

return text;
};

var copyBtn = d.getElementById('engTermCopy');
Expand Down Expand Up @@ -43,7 +58,7 @@
for (var _elm of elmsToCopy) {
if (_elm.innerText.trim().length > 0) {
// textToCopy = textToCopy + _elm.innerText + '\r\n\r\n';
textToCopy = textToCopy + replace_math_symb(_elm.innerText) + '\r\n\r\n';
textToCopy = textToCopy + replace_math_symb(_elm) + '\r\n\r\n';
};
};

Expand Down
2 changes: 2 additions & 0 deletions jekyll-geolexica.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Gem::Specification.new do |spec|
# Pin logger to <= 1.5.3 due to incompatibility of logger 1.6.0 with Jekyll 4.3.2
spec.add_runtime_dependency "logger", "<= 1.5.3"
spec.add_runtime_dependency "relaton"
spec.add_runtime_dependency "unitsml"
spec.add_runtime_dependency "plurimath"

# Zeitwerk::Loader#push_dir supports :namespace argument from v. 2.4.
spec.add_runtime_dependency "zeitwerk", "~> 2.4"
Expand Down
1 change: 1 addition & 0 deletions lib/jekyll/geolexica.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require "jekyll"
require "glossarist"
require "plurimath"

module Jekyll
module Geolexica
Expand Down
21 changes: 21 additions & 0 deletions lib/jekyll/geolexica/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def register_all_hooks
hook :post_read, :site, :load_glossary
hook :pre_render, :documents, :expose_glossary
hook :pre_render, :pages, :expose_glossary
hook :post_render, :pages, :convert_math
end

# Adds Jekyll::Site#glossary method, and initializes an empty glossary.
Expand All @@ -28,6 +29,26 @@ def expose_glossary(page_or_document, liquid_drop)
liquid_drop["glossary"] = page_or_document.site.glossary
end

def convert_math(page)
page.output.gsub!(/stem:\[([^\]]*?)\]/) do
ascii_equation = CGI.unescapeHTML(Regexp.last_match[1])

mathml_equation = ::Plurimath::Math
.parse(ascii_equation, :asciimath)
.to_mathml

# temporary hack to use display inline for math equations because
# currently there is no option to use display inline in plurimath
mathml_equation.gsub!("display=\"block\"", "display=\"inline\"")

# Removing newlines(\n) and escaping double quotes(")
# because they will cause parsing issues in json
mathml_equation.gsub!("\n", "").gsub!("\"", "\\\"") unless page.html?

mathml_equation
end
end

def hook event, target, action
Jekyll::Hooks.register target, event, &method(action)
end
Expand Down
70 changes: 70 additions & 0 deletions spec/unit/jekyll/geolexica/hooks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# (c) Copyright 2020 Ribose Inc.
#

RSpec.describe ::Jekyll::Geolexica::Hooks do
subject do
w = Object.new
w.extend(described_class)
w
end

let(:page) do
instance_double(
Jekyll::Geolexica::ConceptPage::HTML,
output: page_output,
html?: is_html,
)
end

describe ".convert_math" do
context "when page is HTML" do
let(:is_html) { true }

let(:page_output) do
"foo stem:[a_2] bar"
end

let(:expected_output) do
<<~OUTPUT.strip
foo <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
<mstyle displaystyle="true">
<msub>
<mi>a</mi>
<mn>2</mn>
</msub>
</mstyle>
</math>
bar
OUTPUT
end

it "should convert stem:[] to MathML" do
expect { subject.send(:convert_math, page) }
.to change { page.output }
.from(page_output)
.to(expected_output)
end
end

context "when page is not HTML" do
let(:is_html) { false }

let(:page_output) do
"foo stem:[a_2] bar"
end

let(:expected_output) do
<<~OUTPUT.strip
foo <math xmlns=\\"http://www.w3.org/1998/Math/MathML\\" display=\\"inline\\"> <mstyle displaystyle=\\"true\\"> <msub> <mi>a</mi> <mn>2</mn> </msub> </mstyle></math> bar
OUTPUT
end

it "should convert stem:[] to MathML" do
expect { subject.send(:convert_math, page) }
.to change { page.output }
.from(page_output)
.to(expected_output)
end
end
end
end

0 comments on commit 8e36f55

Please sign in to comment.