Class: Mato::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/mato/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Processor

Returns a new instance of Processor.



13
14
15
# File 'lib/mato/processor.rb', line 13

def initialize(config)
  @config = config
end

Instance Attribute Details

#configMato::Config (readonly)

Returns:



11
12
13
# File 'lib/mato/processor.rb', line 11

def config
  @config
end

Instance Method Details

#convert(content, flavor:) ⇒ Object



63
64
65
# File 'lib/mato/processor.rb', line 63

def convert(content, flavor:)
  Mato::Converter.new(self, content, flavor).run
end

#parse_html(html) ⇒ Nokogiri::HTML4::DocumentFragment

Parameters:

  • html (String)

Returns:

  • (Nokogiri::HTML4::DocumentFragment)


59
60
61
# File 'lib/mato/processor.rb', line 59

def parse_html(html)
  config.html_parser.parse(html)
end

#parse_markdown(text) ⇒ Commonmarker::Node

Parameters:

  • text (String)

Returns:

  • (Commonmarker::Node)


47
48
49
# File 'lib/mato/processor.rb', line 47

def parse_markdown(text)
  config.markdown_parser.parse(text, options: config.markdown_options)
end

#process(input) ⇒ Mato::Document

Parameters:

  • input (String)

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mato/processor.rb', line 19

def process(input)
  text = input.dup

  config.text_filters.each do |filter|
    # A text filter returns a mutated text
    text = filter.call(text)
  end

  markdown_node = parse_markdown(text)

  config.markdown_filters.each do |filter|
    # A markdown filter mutates the argument
    filter.call(markdown_node)
  end

  html = render_to_html(markdown_node)
  doc = parse_html(html)

  config.html_filters.each do |filter|
    # An HTML filter mutates the argument
    filter.call(doc)
  end

  config.document_factory.new(doc.freeze)
end

#render_to_html(markdown_node) ⇒ String

Parameters:

  • markdown_node (Commonmarker::Node)

Returns:

  • (String)


53
54
55
# File 'lib/mato/processor.rb', line 53

def render_to_html(markdown_node)
  markdown_node.to_html(options: config.markdown_options)
end