Module: MDOM::Parser

Extended by:
Parser
Included in:
Parser
Defined in:
lib/mdom/parser.rb

Defined Under Namespace

Classes: Reader

Constant Summary collapse

HEADING_RE =
/\A(\#{1,6})[ \t]+(.*?)[ \t]*\z/.freeze
SETEXT_RE =
/\A\s*([=-]+)\s*\z/.freeze
FENCE_RE =
/\A([ ]{0,3})(`{3,}|~{3,})(.*)\z/.freeze
HRULE_RE =
/\A\s*((\*\s*){3,}|(-\s*){3,}|(_\s*){3,})\z/.freeze
QUOTE_RE =
/\A[ ]{0,3}>[ \t]?(.*)\z/.freeze
LIST_RE =
/\A([ ]*)([-*+])([ \t]+)(.*)\z/.freeze
ORDERED_RE =
/\A([ ]*)(\d{1,9})([.)])([ \t]+)(.*)\z/.freeze
TASK_RE =
/\A\[([ xX])\][ \t]+(.*)\z/.freeze

Instance Method Summary collapse

Instance Method Details

#expand_leading_tabs(line) ⇒ Object

Expand leading tab characters in line into spaces at 4-column tab stops. This normalizes list/quote indentation so the block parser can reason about column widths, while leaving the non-indentation content (including tabs inside code or text) untouched. A tab stop positions the next character at the next multiple of 4.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mdom/parser.rb', line 33

def expand_leading_tabs(line)
  i = 0
  col = 0
  out = +""
  while i < line.length
    ch = line[i]
    case ch
    when " "
      out << " "
      col += 1
      i += 1
    when "\t"
      target = (col / 4 + 1) * 4
      out << " " * (target - col)
      col = target
      i += 1
    else
      out << line[i..]
      break
    end
  end
  out
end

#normalize(source) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/mdom/parser.rb', line 20

def normalize(source)
  s = source.to_s
  s = s.gsub(/\r\n?/, "\n").sub(/\A\n+/, "").chomp
  return [] if s.empty?

  s.split("\n", -1).map { |line| expand_leading_tabs(line) }
end

#read(source) ⇒ Object



16
17
18
# File 'lib/mdom/parser.rb', line 16

def read(source)
  Reader.new(normalize(source)).parse
end