Skip to content

Commit

Permalink
Adding support for stem equations
Browse files Browse the repository at this point in the history
  • Loading branch information
HassanAkbar committed Mar 26, 2024
1 parent 5587781 commit 758416b
Show file tree
Hide file tree
Showing 2 changed files with 38 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
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

0 comments on commit 758416b

Please sign in to comment.