Class: Jekyll::Spaceship::MathjaxProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/jekyll-spaceship/processors/mathjax-processor.rb

Constant Summary

Constants inherited from Processor

Processor::DEFAULT_PRIORITY, Processor::PRIORITY_MAP

Instance Attribute Summary

Attributes inherited from Processor

#exclusions, #handled, #logger, #page, #priority, #registers

Instance Method Summary collapse

Methods inherited from Processor

#after_exclude, #converter, #dispatch, exclude, #ext, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, #name, #on_handle_html_block, #on_handled, #output_ext, #pre_exclude, priority, register

Constructor Details

This class inherits a constructor from Jekyll::Spaceship::Processor

Instance Method Details

#has_mathjax_expression?(doc) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll-spaceship/processors/mathjax-processor.rb', line 32

def has_mathjax_expression?(doc)
  doc.css('*').each do |node|
    if node.content.match(/(?<!\\)\$.+(?<!\\)\$/)
      return true
    end
    if node.content.match(/(?<!\\)\\\(.+(?<!\\)\\\)/)
      return true
    end
  end

  doc.css('script').each do |node|
    type = node['type']
    if type and type.match(/math\/tex/)
      return true
    end
  end
  false
end

#on_handle_html(content) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jekyll-spaceship/processors/mathjax-processor.rb', line 11

def on_handle_html(content)
  # use nokogiri to parse html
  doc = Nokogiri::HTML(content)

  head = doc.at('head')
  return content if head.nil?
  return content if not self.has_mathjax_expression? doc

  self.handled = true

  params = "config=TeX-AMS-MML_HTMLorMML"
  src = "//cdn.mathjax.org/mathjax/latest/MathJax.js?#{params}"
  config = "MathJax.Hub.Config({ \
    tex2jax: { inlineMath: [['$','$'], ['\\\\(','\\\\)']] } \
  });"

  head.add_child("<script src=\"#{src}\">#{config}</script>")

  doc.to_html
end

#process?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/jekyll-spaceship/processors/mathjax-processor.rb', line 7

def process?
  return true if Type.html?(output_ext)
end