Module: MarkdownIt::Common::Utils

Constant Summary collapse

UNESCAPE_MD_RE =
/\\([\!\"\#\$\%\&\'\(\)\*\+\,\-.\/:;<=>?@\[\\\]^_`{|}~])/
ENTITY_RE =
/&([a-z#][a-z0-9]{1,31});/i
UNESCAPE_ALL_RE =
Regexp.new(UNESCAPE_MD_RE.source + '|' + ENTITY_RE.source, 'i')
DIGITAL_ENTITY_TEST_RE =
/^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))/i
HTML_ESCAPE_TEST_RE =
/[&<>"]/
HTML_ESCAPE_REPLACE_RE =
/[&<>"]/
HTML_REPLACEMENTS =
{
  '&' => '&amp;',
  '<' => '&lt;',
  '>' => '&gt;',
  '"' => '&quot;'
}
REGEXP_ESCAPE_RE =
/[.?*+^$\[\]\\(){}|-]/
UNICODE_PUNCT_RE =
UCMicro::Categories::P::REGEX

Instance Method Summary collapse

Instance Method Details

#arrayReplaceAt(src, pos, newElements) ⇒ Object

Remove element from array and put another array at those position. Useful for some operations with tokens




22
23
24
25
26
# File 'lib/motion-markdown-it/common/utils.rb', line 22

def arrayReplaceAt(src, pos, newElements)
  src[pos] = newElements
  src.flatten!(1)
  return src
end

#assign(obj, *args) ⇒ Object

Merge multiple hashes


Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/motion-markdown-it/common/utils.rb', line 7

def assign(obj, *args)
  raise(ArgumentError, "#{obj} must be a Hash") if !obj.is_a?(Hash)

  args.each do |source|
    next if source.nil?
    raise(ArgumentError, "#{source} must be a Hash") if !source.is_a?(Hash)
    obj.merge!(source)
  end

  return obj
end

#escapeHtml(str) ⇒ Object




110
111
112
113
114
115
# File 'lib/motion-markdown-it/common/utils.rb', line 110

def escapeHtml(str)
  if HTML_ESCAPE_TEST_RE =~ str
    return str.gsub(HTML_ESCAPE_REPLACE_RE, HTML_REPLACEMENTS)
  end
  return str
end

#escapeRE(str) ⇒ Object




120
121
122
# File 'lib/motion-markdown-it/common/utils.rb', line 120

def escapeRE(str)
  str.gsub(REGEXP_ESCAPE_RE) {|s| '\\' + s}
end

#fromCharCode(c) ⇒ Object




55
56
57
# File 'lib/motion-markdown-it/common/utils.rb', line 55

def fromCharCode(c)
  c.chr
end

#fromCodePoint(c) ⇒ Object




50
51
52
# File 'lib/motion-markdown-it/common/utils.rb', line 50

def fromCodePoint(c)
  c.chr(Encoding::UTF_8)
end

#isMdAsciiPunct(ch) ⇒ Object

Markdown ASCII punctuation characters.

!, “, #, $, %, &, ‘, (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, @, [, , ], ^, _, `, {, |, }, or ~ spec.commonmark.org/0.15/#ascii-punctuation-character

Don’t confuse with unicode punctuation !!! It lacks some chars in ascii range.




172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/motion-markdown-it/common/utils.rb', line 172

def isMdAsciiPunct(ch)
  case ch
  when 0x21,  # !
       0x22,  # "
       0x23,  # #
       0x24,  # $
       0x25,  # %
       0x26,  # &
       0x27,  # '
       0x28,  # (
       0x29,  # )
       0x2A,  # *
       0x2B,  # +
       0x2C,  # ,
       0x2D,  # -
       0x2E,  # .
       0x2F,  # /
       0x3A,  # :
       0x3B,  # ;
       0x3C,  # <
       0x3D,  # =
       0x3E,  # >
       0x3F,  # ?
       0x40,  # @
       0x5B,  # [
       0x5C,  # \
       0x5D,  # ]
       0x5E,  # ^
       0x5F,  # _
       0x60,  # `
       0x7B,  # {
       0x7C,  # |
       0x7D,  # }
       0x7E   # ~
    return true
  else
    return false
  end
end

#isPunctChar(ch) ⇒ Object

Currently without astral characters support.




160
161
162
# File 'lib/motion-markdown-it/common/utils.rb', line 160

def isPunctChar(ch)
  return UNICODE_PUNCT_RE =~ ch
end

#isSpace(code) ⇒ Object




125
126
127
128
129
130
131
132
133
# File 'lib/motion-markdown-it/common/utils.rb', line 125

def isSpace(code)
  case code
  when 0x09,
       0x20
    return true
  end

  return false
end

#isValidEntityCode(c) ⇒ Object




29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/motion-markdown-it/common/utils.rb', line 29

def isValidEntityCode(c)
  # broken sequence
  return false if (c >= 0xD800 && c <= 0xDFFF)

  # never used
  return false if (c >= 0xFDD0 && c <= 0xFDEF)
  return false if ((c & 0xFFFF) === 0xFFFF || (c & 0xFFFF) === 0xFFFE)

  # control codes
  return false if (c >= 0x00 && c <= 0x08)
  return false if (c === 0x0B)
  return false if (c >= 0x0E && c <= 0x1F)
  return false if (c >= 0x7F && c <= 0x9F)

  # out of range
  return false if (c > 0x10FFFF)

  return true
end

#isWhiteSpace(code) ⇒ Object

Zs (unicode class) || [tfvrn]




137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/motion-markdown-it/common/utils.rb', line 137

def isWhiteSpace(code)
  return true if (code >= 0x2000 && code <= 0x200A)
  case code
  when 0x09, # \t
       0x0A, # \n
       0x0B, # \v
       0x0C, # \f
       0x0D, # \r
       0x20,
       0xA0,
       0x1680,
       0x202F,
       0x205F,
       0x3000
    return true
  end
  return false
end

#normalizeReference(str) ⇒ Object

Hepler to unify [reference labels].




214
215
216
217
218
219
# File 'lib/motion-markdown-it/common/utils.rb', line 214

def normalizeReference(str)
  # use .toUpperCase() instead of .toLowerCase()
  # here to avoid a conflict with Object.prototype
  # members (most notably, `__proto__`)
  return str.strip.gsub(/\s+/, ' ').upcase
end

#replaceEntityPattern(match, name) ⇒ Object




68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/motion-markdown-it/common/utils.rb', line 68

def replaceEntityPattern(match, name)
  code = 0

  return fromCodePoint(MarkdownIt::HTMLEntities::MAPPINGS[name]) if MarkdownIt::HTMLEntities::MAPPINGS[name]

  if (name.charCodeAt(0) == 0x23 && DIGITAL_ENTITY_TEST_RE =~ name) # '#'
    code = name[1].downcase == 'x' ? name.slice_to_end(2).to_i(16) : name.slice_to_end(1).to_i
    if (isValidEntityCode(code))
      return fromCodePoint(code)
    end
  end

  return match
end

#unescapeAll(str) ⇒ Object




90
91
92
93
94
95
96
97
# File 'lib/motion-markdown-it/common/utils.rb', line 90

def unescapeAll(str)
  return str if (str.index('\\').nil? && str.index('&').nil?)

  return str.gsub(UNESCAPE_ALL_RE) do |match|
    next $1 if ($1)
    next replaceEntityPattern(match, $2)
  end
end

#unescapeMd(str) ⇒ Object




84
85
86
87
# File 'lib/motion-markdown-it/common/utils.rb', line 84

def unescapeMd(str)
  return str if !str.include?('\\')
  return str.gsub(UNESCAPE_MD_RE, '\1')
end