Class: MinimalMarkdown::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/minimal_markdown/parser.rb

Constant Summary collapse

DEFAULT_PARSERS =
[
  Parsers::UnorderedList,
  Parsers::Bold,
  Parsers::Italic,
]
STYLES =
[:markdown, :slack]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, style: :markdown, parsers: DEFAULT_PARSERS) ⇒ Parser

Returns a new instance of Parser.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
# File 'lib/minimal_markdown/parser.rb', line 13

def initialize(text, style: :markdown, parsers: DEFAULT_PARSERS)
  raise ArgumentError, 'invalid style' unless STYLES.include?(style)

  @text = text
  @style = style
  @parsers = parsers
end

Instance Attribute Details

#parsersObject (readonly)

Returns the value of attribute parsers.



11
12
13
# File 'lib/minimal_markdown/parser.rb', line 11

def parsers
  @parsers
end

#textObject (readonly)

Returns the value of attribute text.



11
12
13
# File 'lib/minimal_markdown/parser.rb', line 11

def text
  @text
end

Instance Method Details

#parseObject



21
22
23
24
25
26
27
28
29
# File 'lib/minimal_markdown/parser.rb', line 21

def parse
  prepared_text = text.strip.gsub("\r", "")

  preline_parsers, postline_parsers = parsers.map { |parser| parser.new(@style) }.partition(&:multiline?)

  preline_tree = run_parsers(preline_parsers, [prepared_text])
  postline_tree = convert_to_lines(preline_tree)
  run_parsers(postline_parsers, postline_tree)
end