Class: MarkdownUI::Content::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown-ui/content/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(content = nil) ⇒ Parser

Returns a new instance of Parser.



7
8
9
# File 'lib/markdown-ui/content/parser.rb', line 7

def initialize(content = nil)
  @content = content
end

Instance Method Details

#parseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/markdown-ui/content/parser.rb', line 11

def parse
  if @content.is_a? Array
    final_content = []

    @content.each do |c|
      content = c.split(":")
      final_content << process(content)
    end

    final_content.join
  else
    content = if !(@content =~ /\:/).nil?
      @content.split(":")
    else
      @content.split("\n")
    end

    process(content)
  end
end

#process(content) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/markdown-ui/content/parser.rb', line 32

def process(content)
  content_type, actual_content = content[0], content[1]
  klass = content[2] if content.size > 2

  mode = OpenStruct.new(
    :text?      => !(content_type =~ /text/i).nil?,
    :icon?      => !(content_type =~ /icon/i).nil?,
    :flag?      => !(content_type =~ /flag/i).nil?,
    :image?     => !(content_type =~ /image/i).nil?,
    :header?    => !(content_type =~ /header/i).nil?,
    :list?      => !(content_type =~ /list/i).nil?,
    :unordered? => !(content_type =~ /unordered/i).nil?,
    :ordered?   => !(content_type =~ /ordered/i).nil?
  )

  if mode.text?
    MarkdownUI::Content::Text.new(actual_content, klass).render
  elsif mode.icon?
    MarkdownUI::Content::Icon.new(actual_content, klass).render
  elsif mode.header?
    MarkdownUI::Content::Header.new(actual_content, klass).render
  elsif mode.list? && mode.ordered?
    MarkdownUI::Content::List.new(actual_content, klass, :ordered).render
  elsif mode.list? && mode.unordered?
    MarkdownUI::Content::List.new(actual_content, klass, :unordered).render
  elsif mode.list?
    MarkdownUI::Content::List.new(actual_content, klass).render
  else
    MarkdownUI::Content::Custom.new(content.join(" "), klass).render
  end
end