Module: Jekyll::Geolexica::Hooks

Defined in:
lib/jekyll/geolexica/hooks.rb

Class Method Summary collapse

Class Method Details

.convert_math(page) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jekyll/geolexica/hooks.rb', line 32

def convert_math(page)
  prefix_hash = {
    asciimath: "stem",
    latex: "latexmath",
  }

  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]

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

      normalize_mathml(mathml_equation, page.html?)
    end
  end
rescue => e
  # Skipping broken formulas
  Jekyll.logger.info(e.message)
end

.expose_glossary(page_or_document, liquid_drop) ⇒ Object



28
29
30
# File 'lib/jekyll/geolexica/hooks.rb', line 28

def expose_glossary(page_or_document, liquid_drop)
  liquid_drop["glossary"] = page_or_document.site.glossary
end

.hook(event, target, action) ⇒ Object



66
67
68
# File 'lib/jekyll/geolexica/hooks.rb', line 66

def hook event, target, action
  Jekyll::Hooks.register target, event, &method(action)
end

.initialize_glossary(site) ⇒ Object

Adds Jekyll::Site#glossary method, and initializes an empty glossary.



18
19
20
21
# File 'lib/jekyll/geolexica/hooks.rb', line 18

def initialize_glossary(site)
  site.class.attr_reader :glossary
  site.instance_variable_set "@glossary", Glossary.new(site)
end

.load_glossary(site) ⇒ Object

Loads concept data into glossary.



24
25
26
# File 'lib/jekyll/geolexica/hooks.rb', line 24

def load_glossary(site)
  site.glossary.load_glossary
end

.normalize_mathml(mathml_equation, is_html) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll/geolexica/hooks.rb', line 54

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

.register_all_hooksObject



9
10
11
12
13
14
15
# File 'lib/jekyll/geolexica/hooks.rb', line 9

def register_all_hooks
  hook :after_init, :site, :initialize_glossary
  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