class MarkdownIt::ParserInline

Constants

RULES
RULES2

Attributes

ruler[RW]
ruler2[RW]

Public Class Methods

new() click to toggle source
# File lib/motion-markdown-it/parser_inline.rb, line 36
def initialize
  # ParserInline#ruler -> Ruler
  #
  # [[Ruler]] instance. Keep configuration of inline rules.
  @ruler = Ruler.new

  RULES.each do |rule|
    @ruler.push(rule[0], rule[1])
  end

  # ParserInline#ruler2 -> Ruler
  #
  # [[Ruler]] instance. Second ruler used for post-processing
  # (e.g. in emphasis-like rules).
  @ruler2 = Ruler.new

  RULES2.each do |rule|
    @ruler2.push(rule[0], rule[1])
  end
end

Public Instance Methods

parse(str, md, env, outTokens) click to toggle source

ParserInline.parse(str, md, env, outTokens)

Process input string and push inline tokens into `outTokens`

# File lib/motion-markdown-it/parser_inline.rb, line 144
def parse(str, md, env, outTokens)
  state = RulesInline::StateInline.new(str, md, env, outTokens)

  tokenize(state)

  rules = @ruler2.getRules('')
  len = rules.length

  0.upto(len - 1) do |i|
    rules[i].call(state)
  end
end
skipToken(state) click to toggle source

Skip single token by running all rules in validation mode; returns `true` if any rule reported success

# File lib/motion-markdown-it/parser_inline.rb, line 60
def skipToken(state)
  pos        = state.pos
  rules      = @ruler.getRules('')
  len        = rules.length
  maxNesting = state.md.options[:maxNesting]
  cache      = state.cache
  ok         = false

  if cache[pos] != nil
    state.pos = cache[pos]
    return
  end

  if state.level < maxNesting
    0.upto(len -1) do |i|
      # Increment state.level and decrement it later to limit recursion.
      # It's harmless to do here, because no tokens are created. But ideally,
      # we'd need a separate private state variable for this purpose.
      state.level += 1
      ok = rules[i].call(state, true)
      state.level -= 1

      break if ok
    end
  else
    # Too much nesting, just skip until the end of the paragraph.
    #
    # NOTE: this will cause links to behave incorrectly in the following case,
    #       when an amount of `[` is exactly equal to `maxNesting + 1`:
    #
    #       [[[[[[[[[[[[[[[[[[[[[foo]()
    #
    # TODO: remove this workaround when CM standard will allow nested links
    #       (we can replace it by preventing links from being parsed in
    #       validation mode)
    state.pos = state.posMax
  end

  state.pos += 1 if !ok
  cache[pos] = state.pos
end
tokenize(state) click to toggle source

Generate tokens for input range

# File lib/motion-markdown-it/parser_inline.rb, line 104
def tokenize(state)
  rules      = @ruler.getRules('')
  len        = rules.length
  end_pos    = state.posMax
  maxNesting = state.md.options[:maxNesting]

  while state.pos < end_pos
    # Try all possible rules.
    # On success, rule should:
    #
    # - update `state.pos`
    # - update `state.tokens`
    # - return true

    ok = false
    if state.level < maxNesting
      0.upto(len - 1) do |i|
        ok = rules[i].call(state, false)
        break if ok
      end
    end

    if ok
      break if state.pos >= end_pos
      next
    end

    state.pending += state.src[state.pos]
    state.pos     += 1
  end

  unless state.pending.empty?
    state.pushPending
  end
end