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




13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 13

def initialize(src, md, env, outTokens)
  @src          = src
  @env          = env
  @md           = md
  @tokens       = outTokens
  @tokens_meta  = Array.new(outTokens.length)

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

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

  # Stack of delimiter lists for upper level tags
  @_prev_delimiters = [];

  # backtick length => last seen position
  @backticks = {}
  @backticksScanned = false
end

Instance Attribute Details

#backticksObject

Returns the value of attribute backticks.



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

def backticks
  @backticks
end

#backticksScannedObject

Returns the value of attribute backticksScanned.



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

def backticksScanned
  @backticksScanned
end

#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

#tokens_metaObject

Returns the value of attribute tokens_meta.



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

def tokens_meta
  @tokens_meta
end

Instance Method Details

#push(type, tag, nesting) ⇒ Object

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




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
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 56

def push(type, tag, nesting)
  pushPending unless @pending.empty?

  token      = Token.new(type, tag, nesting)
  token_meta = nil

  if nesting < 0
    # closing tag
    @level -= 1 
    @delimiters = @_prev_delimiters.pop
  end

  token.level = @level

  if nesting > 0
    # opening tag
    @level += 1
    @_prev_delimiters.push(@delimiters)
    @delimiters = []
    token_meta = { delimiters: @delimiters }
  end

  @pendingLevel = @level
  @tokens.push(token)
  @tokens_meta.push(token_meta)

  return token
end

#pushPendingObject

Flush pending text




44
45
46
47
48
49
50
51
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 44

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/motion-markdown-it/rules_inline/state_inline.rb', line 91

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