Class: MarkdownIt::RulesInline::StateInline

Inherits:
Object
  • Object
show all
Includes:
Common::Utils
Defined in:
lib/motion-markdown-it/rules_inline/state_inline.rb

Constant Summary

Constants included from Common::Utils

Common::Utils::DIGITAL_ENTITY_TEST_RE, Common::Utils::ENTITY_RE, Common::Utils::HTML_ESCAPE_REPLACE_RE, Common::Utils::HTML_ESCAPE_TEST_RE, Common::Utils::HTML_REPLACEMENTS, Common::Utils::REGEXP_ESCAPE_RE, Common::Utils::UNESCAPE_ALL_RE, Common::Utils::UNESCAPE_MD_RE, Common::Utils::UNICODE_PUNCT_RE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::Utils

#arrayReplaceAt, #assign, #charCodeAt, #escapeHtml, #escapeRE, #fromCharCode, #fromCodePoint, #isMdAsciiPunct, #isPunctChar, #isSpace, #isValidEntityCode, #isWhiteSpace, #normalizeReference, #replaceEntityPattern, #unescapeAll, #unescapeMd

Constructor Details

#initialize(src, md, env, outTokens) ⇒ StateInline




12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



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

def cache
  @cache
end

#delimitersObject

Returns the value of attribute delimiters.



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

def delimiters
  @delimiters
end

#envObject

Returns the value of attribute env.



8
9
10
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 8

def env
  @env
end

#levelObject

Returns the value of attribute level.



8
9
10
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 8

def level
  @level
end

#mdObject

Returns the value of attribute md.



8
9
10
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 8

def md
  @md
end

#pendingObject

Returns the value of attribute pending.



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

def pending
  @pending
end

#pendingLevelObject

Returns the value of attribute pendingLevel.



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

def pendingLevel
  @pendingLevel
end

#posObject

Returns the value of attribute pos.



8
9
10
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 8

def pos
  @pos
end

#posMaxObject

Returns the value of attribute posMax.



8
9
10
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 8

def posMax
  @posMax
end

#srcObject

Returns the value of attribute src.



8
9
10
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 8

def src
  @src
end

#tokensObject

Returns the value of attribute tokens.



8
9
10
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 8

def tokens
  @tokens
end

Instance Method Details

#push(type, tag, nesting) ⇒ Object

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




44
45
46
47
48
49
50
51
52
53
54
55
# 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

#pushPendingObject

Flush pending text




32
33
34
35
36
37
38
39
# 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) ⇒ Object

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



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
# 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