Class: MarkdownIt::ParserBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-markdown-it/parser_block.rb

Constant Summary collapse

RULES =
[
  # First 2 params - rule name & source. Secondary array - list of rules,
  # which can be terminated by this one.
  [ 'table',        lambda { |state, startLine, endLine, silent| RulesBlock::Table.table(state, startLine, endLine, silent) },      [ 'paragraph', 'reference' ] ],
  [ 'code',         lambda { |state, startLine, endLine, silent| RulesBlock::Code.code(state, startLine, endLine, silent) } ],
  [ 'fence',        lambda { |state, startLine, endLine, silent| RulesBlock::Fence.fence(state, startLine, endLine, silent) },      [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
  [ 'blockquote',   lambda { |state, startLine, endLine, silent| RulesBlock::Blockquote.blockquote(state, startLine, endLine, silent) }, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
  [ 'hr',           lambda { |state, startLine, endLine, silent| RulesBlock::Hr.hr(state, startLine, endLine, silent) },         [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
  [ 'list',         lambda { |state, startLine, endLine, silent| RulesBlock::List.list(state, startLine, endLine, silent) },       [ 'paragraph', 'reference', 'blockquote' ] ],
  [ 'reference',    lambda { |state, startLine, endLine, silent| RulesBlock::Reference.reference(state, startLine, endLine, silent) } ],
  [ 'heading',      lambda { |state, startLine, endLine, silent| RulesBlock::Heading.heading(state, startLine, endLine, silent) },    [ 'paragraph', 'reference', 'blockquote' ] ],
  [ 'lheading',     lambda { |state, startLine, endLine, silent| RulesBlock::Lheading.lheading(state, startLine, endLine, silent) } ],
  [ 'html_block',   lambda { |state, startLine, endLine, silent| RulesBlock::HtmlBlock.html_block(state, startLine, endLine, silent) }, [ 'paragraph', 'reference', 'blockquote' ] ],
  [ 'paragraph',    lambda { |state, startLine, endLine, silent| RulesBlock::Paragraph.paragraph(state, startLine) } ]
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParserBlock

new ParserBlock()




30
31
32
33
34
35
36
37
38
39
# File 'lib/motion-markdown-it/parser_block.rb', line 30

def initialize
  # ParserBlock#ruler -> Ruler
  #
  # [[Ruler]] instance. Keep configuration of block rules.
  @ruler = Ruler.new

  RULES.each do |rule|
    @ruler.push(rule[0], rule[1], {alt: (rule[2] || []) })
  end
end

Instance Attribute Details

#rulerObject

Returns the value of attribute ruler.



9
10
11
# File 'lib/motion-markdown-it/parser_block.rb', line 9

def ruler
  @ruler
end

Instance Method Details

#parse(src, md, env, outTokens) ⇒ Object

ParserBlock.parse(src, md, env, outTokens)

Process input string and push block tokens into ‘outTokens`




100
101
102
103
104
105
106
107
# File 'lib/motion-markdown-it/parser_block.rb', line 100

def parse(src, md, env, outTokens)

  return if !src

  state = RulesBlock::StateBlock.new(src, md, env, outTokens)

  tokenize(state, state.line, state.lineMax)
end

#tokenize(state, startLine, endLine, ignored = false) ⇒ Object

Generate tokens for input range




44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/motion-markdown-it/parser_block.rb', line 44

def tokenize(state, startLine, endLine, ignored = false)
  rules         = @ruler.getRules('')
  len           = rules.length
  line          = startLine
  hasEmptyLines = false
  maxNesting    = state.md.options[:maxNesting]

  while line < endLine
    state.line = line = state.skipEmptyLines(line)
    break if line >= endLine

    # Termination condition for nested calls.
    # Nested calls currently used for blockquotes & lists
    break if state.sCount[line] < state.blkIndent

    # If nesting level exceeded - skip tail to the end. That's not ordinary
    # situation and we should not care about content.
    if state.level >= maxNesting
      state.line = endLine
      break
    end

    # Try all possible rules.
    # On success, rule should:
    #
    # - update `state.line`
    # - update `state.tokens`
    # - return true
    0.upto(len - 1) do |i|
      ok = rules[i].call(state, line, endLine, false)
      break if ok
    end

    # set state.tight if we had an empty line before current tag
    # i.e. latest empty line should not count
    state.tight = !hasEmptyLines

    # paragraph might "eat" one newline after it in nested lists
    if state.isEmpty(state.line - 1)
      hasEmptyLines = true
    end

    line = state.line

    if line < endLine && state.isEmpty(line)
      hasEmptyLines = true
      line += 1
      state.line = line
    end
  end
end