class MarkdownIt::RulesInline::StateInline

Attributes

cache[RW]
delimiters[RW]
env[RW]
level[RW]
md[RW]
pending[RW]
pendingLevel[RW]
pos[RW]
posMax[RW]
src[RW]
tokens[RW]

Public Class Methods

new(src, md, env, outTokens) click to toggle source
# File lib/motion-markdown-it/rules_inline/state_inline.rb, line 12
def initialize(src, md, env, outTokens)
  @src          = src
  @env          = env
  @md           = md
  @tokens       = outTokens

  @pos          = 0
  @posMax       = @src.length
  @level        = 0
  @pending      = ''
  @pendingLevel = 0

  @cache        = {}      # Stores { start: end } pairs. Useful for backtrack
                          # optimization of pairs parse (emphasis, strikes).
  @delimiters   = []
end

Public Instance Methods

push(type, tag, nesting) click to toggle source

Push new token to “stream”. If pending text exists - flush it as text token

# File lib/motion-markdown-it/rules_inline/state_inline.rb, line 44
def push(type, tag, nesting)
  pushPending unless @pending.empty?

  token       = Token.new(type, tag, nesting);
  @level     -= 1 if nesting < 0
  token.level = @level
  @level     += 1 if nesting > 0

  @pendingLevel = @level
  @tokens.push(token)
  return token
end
pushPending() click to toggle source

Flush pending text

# File lib/motion-markdown-it/rules_inline/state_inline.rb, line 32
def pushPending
  token         = Token.new('text', '', 0)
  token.content = @pending
  token.level   = @pendingLevel
  @tokens.push(token)
  @pending      = ''
  return token
end
scanDelims(start, canSplitWord) click to toggle source

Scan a sequence of emphasis-like markers, and determine whether it can start an emphasis sequence or end an emphasis sequence.

- start - position to scan from (it should point at a valid marker);
- canSplitWord - determine if these markers can be found inside a word
# File lib/motion-markdown-it/rules_inline/state_inline.rb, line 63
def scanDelims(start, canSplitWord)
  pos            = start
  left_flanking  = true
  right_flanking = true
  max            = @posMax
  marker         = charCodeAt(@src, start)

  # treat beginning of the line as a whitespace
  lastChar = start > 0 ? charCodeAt(@src, start - 1) : 0x20

  while (pos < max && charCodeAt(@src, pos) == marker)
    pos += 1
  end

  count = pos - start

  # treat end of the line as a whitespace
  nextChar = pos < max ? charCodeAt(@src, pos) : 0x20

  isLastPunctChar = isMdAsciiPunct(lastChar) || isPunctChar(fromCodePoint(lastChar))
  isNextPunctChar = isMdAsciiPunct(nextChar) || isPunctChar(fromCodePoint(nextChar))

  isLastWhiteSpace = isWhiteSpace(lastChar)
  isNextWhiteSpace = isWhiteSpace(nextChar)

  if (isNextWhiteSpace)
    left_flanking = false
  elsif (isNextPunctChar)
    if (!(isLastWhiteSpace || isLastPunctChar))
      left_flanking = false
    end
  end

  if isLastWhiteSpace
    right_flanking = false
  elsif isLastPunctChar
    if !(isNextWhiteSpace || isNextPunctChar)
      right_flanking = false
    end
  end

  if !canSplitWord
    can_open  = left_flanking  && (!right_flanking || isLastPunctChar)
    can_close = right_flanking && (!left_flanking  || isNextPunctChar)
  else
    can_open  = left_flanking
    can_close = right_flanking
  end

  return { can_open: can_open, can_close: can_close, length: count }
end