Class: MarkdownIt::RulesInline::Strikethrough

Inherits:
Object
  • Object
show all
Extended by:
Common::Utils
Defined in:
lib/motion-markdown-it/rules_inline/strikethrough.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

Class Method Summary collapse

Methods included from Common::Utils

arrayReplaceAt, assign, escapeHtml, escapeRE, fromCodePoint, isMdAsciiPunct, isPunctChar, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd

Class Method Details

.scanDelims(state, start) ⇒ Object

parse sequence of markers, “start” should point at a valid marker



10
11
12
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/motion-markdown-it/rules_inline/strikethrough.rb', line 10

def self.scanDelims(state, start)
  pos       = start
  can_open  = true
  can_close = true
  max       = state.posMax
  marker    = state.src.charCodeAt(start)

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

  while (pos < max && state.src.charCodeAt(pos) == marker)
    pos += 1
  end

  if (pos >= max)
    can_open = false
  end

  count = pos - start

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

  isLastPunctChar = isMdAsciiPunct(lastChar) || isPunctChar(lastChar.chr(Encoding::UTF_8))
  isNextPunctChar = isMdAsciiPunct(nextChar) || isPunctChar(nextChar.chr(Encoding::UTF_8))

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

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

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

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

.strikethrough(state, silent) ⇒ Object




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

def self.strikethrough(state, silent)
  max    = state.posMax
  start  = state.pos
  marker = state.src.charCodeAt(start)

  return false if (marker != 0x7E) # ~
  return false if (silent)  # don't run any pairs in validation mode

  res        = scanDelims(state, start)
  startCount = res[:delims]
  if (!res[:can_open])
    state.pos += startCount
    # Earlier we checked !silent, but this implementation does not need it
    state.pending += state.src.slice(start...state.pos)
    return true
  end

  stack = (startCount / 2).floor
  return false if (stack <= 0)
  state.pos = start + startCount

  while (state.pos < max)
    if (state.src.charCodeAt(state.pos) == marker)
      res      = scanDelims(state, state.pos)
      count    = res[:delims]
      tagCount = (count / 2).floor
      if (res[:can_close])
        if (tagCount >= stack)
          state.pos += count - 2
          found = true
          break
        end
        stack     -= tagCount
        state.pos += count
        next
      end

      stack += tagCount if (res[:can_open])
      state.pos += count
      next
    end

    state.md.inline.skipToken(state)
  end

  if (!found)
    # parser failed to find ending tag, so it's not valid emphasis
    state.pos = start
    return false
  end

  # found!
  state.posMax = state.pos
  state.pos    = start + 2

  # Earlier we checked !silent, but this implementation does not need it
  token        = state.push('s_open', 's', 1)
  token.markup = '~~'

  state.md.inline.tokenize(state)

  token        = state.push('s_close', 's', -1)
  token.markup = '~~'

  state.pos    = state.posMax + 2
  state.posMax = max
  return true
end