Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for latexmath:[...] syntax #25

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions lib/jekyll/geolexica/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,37 @@ def expose_glossary(page_or_document, liquid_drop)
end

def convert_math(page)
page.output.gsub!(/stem:\[([^\]]*?)\]/) do
ascii_equation = CGI.unescapeHTML(Regexp.last_match[1])
prefix_hash = {
asciimath: "stem",
latex: "latexmath",
}

mathml_equation = ::Plurimath::Math
.parse(ascii_equation, :asciimath)
.to_mathml
prefix_hash.each do |math_notation, prefix|
page.output.gsub!(/#{prefix}:(?<re>\[((?>[^\[\]]+)|\g<re>)*\])/) do
ascii_equation = CGI.unescapeHTML(Regexp.last_match[1])[1..-2]

# 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\"")
mathml_equation = ::Plurimath::Math
.parse(ascii_equation, math_notation)
.to_mathml

# 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
normalize_mathml(mathml_equation, page.html?)
end
end
rescue => e
# Skipping broken formulas
Jekyll.logger.info(e.message)
end

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

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

mathml_equation
end

def hook event, target, action
Expand Down
Loading