diff --git a/_includes/script.html b/_includes/script.html index d61b3e1..ba0b263 100644 --- a/_includes/script.html +++ b/_includes/script.html @@ -13,7 +13,22 @@ }; var replace_math_symb = function(s){ - return s.replace(/(`[^`]+`)/g, '<>'); + // 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('<>'); + }); + } + + text = s.innerText; + + // replace asciimath + text = text.replace(/(`[^`]+`)/g, '<>'); + + return text; }; var copyBtn = d.getElementById('engTermCopy'); @@ -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'; }; }; diff --git a/jekyll-geolexica.gemspec b/jekyll-geolexica.gemspec index a5d84b3..1fbcfe2 100644 --- a/jekyll-geolexica.gemspec +++ b/jekyll-geolexica.gemspec @@ -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" diff --git a/lib/jekyll/geolexica.rb b/lib/jekyll/geolexica.rb index f4a8b4f..3d36d94 100644 --- a/lib/jekyll/geolexica.rb +++ b/lib/jekyll/geolexica.rb @@ -3,6 +3,7 @@ require "jekyll" require "glossarist" +require "plurimath" module Jekyll module Geolexica diff --git a/lib/jekyll/geolexica/hooks.rb b/lib/jekyll/geolexica/hooks.rb index 41df45b..ff9e869 100644 --- a/lib/jekyll/geolexica/hooks.rb +++ b/lib/jekyll/geolexica/hooks.rb @@ -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. @@ -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 diff --git a/spec/unit/jekyll/geolexica/hooks_spec.rb b/spec/unit/jekyll/geolexica/hooks_spec.rb new file mode 100644 index 0000000..47272c0 --- /dev/null +++ b/spec/unit/jekyll/geolexica/hooks_spec.rb @@ -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 + + + a + 2 + + + + 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 a 2 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