Class: Minidown::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/minidown/document.rb

Constant Summary collapse

RefRegexp =
{
  link_ref_define: /\A\s*\[(.+)\]\:\s+(\S+)\s*(.*)/,
  link_title: /((?<=\").+?(?=\"))/
}
TagRegexp =
{
  h1h6: /\Ah[1-6]\z/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, options = {}) ⇒ Document

Returns a new instance of Document.



15
16
17
18
19
20
21
# File 'lib/minidown/document.rb', line 15

def initialize lines, options = {}
  @options = options
  @lines = lines
  @nodes = []
  @within_block = false
  @links_ref = {}
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



3
4
5
# File 'lib/minidown/document.rb', line 3

def lines
  @lines
end

Returns the value of attribute links_ref.



3
4
5
# File 'lib/minidown/document.rb', line 3

def links_ref
  @links_ref
end

#nodesObject

Returns the value of attribute nodes.



3
4
5
# File 'lib/minidown/document.rb', line 3

def nodes
  @nodes
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/minidown/document.rb', line 3

def options
  @options
end

#within_blockObject (readonly)

Returns the value of attribute within_block.



4
5
6
# File 'lib/minidown/document.rb', line 4

def within_block
  @within_block
end

Instance Method Details

#parseObject



23
24
25
26
27
28
29
# File 'lib/minidown/document.rb', line 23

def parse
  parse_references

  while line = @lines.shift
    parse_line line
  end
end

#parse_line(line) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/minidown/document.rb', line 60

def parse_line line
  regexp = Minidown::Utils::Regexp
  case
  when regexp[:blank_line] =~ line
    # blankline
    newline line
  when !pre_blank? && (result = regexp[:h1_or_h2] =~ line; next_line = $2; result) && ParagraphElement === nodes.last
    # ======== or -------
    break_if_list line do
      lines.unshift next_line if next_line && !next_line.empty?
      html_tag nodes.pop, (line[0] == '='.freeze ? 'h1'.freeze : 'h2'.freeze)
    end
  when regexp[:start_with_shape] =~ line
    # ####h4
    break_if_list line do
      text $2
      html_tag nodes.pop, "h#{$1.size}"
    end
  when regexp[:start_with_quote] =~ line
    # > blockquote
    inblock{block $1}
  when regexp[:dividing_line] =~ line
    # * * * - - -
    break_if_list line do
      dividing_line line
    end
  when regexp[:unorder_list] =~ line
    # * + -
    indent, str = $1.size, $2
    inblock{ul str, indent}
  when regexp[:order_list] =~ line
    # 1. order
    indent, str = $1.size, $2
    inblock{ol str, indent}
  when regexp[:code_block] =~ line
    # ``` or ~~~
    inblock{code_block $1}
  when !@within_block && pre_blank? && regexp[:indent_code] =~ line
    #    code
    indent_code $1
  when regexp[:pipe_symbol] =~ line && regexp[:table] =~ line
    # column1 | column2 | ...
    table = TableElement.new self, line, $1
    raw_column_spec = @lines.shift
    if table.check_column_spec raw_column_spec
      inblock{table.parse}
    else
      @lines.unshift raw_column_spec if raw_column_spec
      paragraph line
    end
  else
    # paragraph
    paragraph line
  end
end

#parse_referencesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/minidown/document.rb', line 44

def parse_references
  while line = @lines.pop
    line.gsub! RefRegexp[:link_ref_define] do
      id, url = $1, $2
      $3 =~ RefRegexp[:link_title]
      title = $1
      links_ref[id.downcase] = {url: url, title: title}
      ''
    end
    unless line.empty?
      @lines << line
      break
    end
  end
end

#to_htmlObject



31
32
33
34
35
# File 'lib/minidown/document.rb', line 31

def to_html
  @html ||= (doc = ''
   @nodes.each{|e| doc << e.to_html}
   doc)
end