Module: MarkdownIt::Common::Utils
- Included in:
- Parser, Renderer, Renderer, RulesBlock::Reference, RulesCore::Smartquotes, RulesInline::Emphasis, RulesInline::Entity, RulesInline::Image, RulesInline::Link, RulesInline::Strikethrough
- Defined in:
- lib/motion-markdown-it/common/utils.rb
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 =
{ '&' => '&', '<' => '<', '>' => '>', '"' => '"' }
- REGEXP_ESCAPE_RE =
/[.?*+^$\[\]\\(){}|-]/
- UNICODE_PUNCT_RE =
UCMicro::Categories::P::REGEX
Instance Method Summary collapse
-
#arrayReplaceAt(src, pos, newElements) ⇒ Object
Remove element from array and put another array at those position.
-
#assign(obj, *args) ⇒ Object
Merge multiple hashes ——————————————————————————.
-
#escapeHtml(str) ⇒ Object
——————————————————————————.
-
#escapeRE(str) ⇒ Object
——————————————————————————.
-
#fromCodePoint(c) ⇒ Object
——————————————————————————.
-
#isMdAsciiPunct(ch) ⇒ Object
Markdown ASCII punctuation characters.
-
#isPunctChar(char) ⇒ Object
Currently without astral characters support.
-
#isValidEntityCode(c) ⇒ Object
——————————————————————————.
-
#isWhiteSpace(code) ⇒ Object
Zs (unicode class) || [tfvrn] ——————————————————————————.
-
#normalizeReference(str) ⇒ Object
Hepler to unify [reference labels].
-
#replaceEntityPattern(match, name) ⇒ Object
——————————————————————————.
-
#unescapeAll(str) ⇒ Object
——————————————————————————.
-
#unescapeMd(str) ⇒ Object
——————————————————————————.
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
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
105 106 107 108 109 110 |
# File 'lib/motion-markdown-it/common/utils.rb', line 105 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
115 116 117 |
# File 'lib/motion-markdown-it/common/utils.rb', line 115 def escapeRE(str) str.gsub(REGEXP_ESCAPE_RE) {|s| '\\' + s} 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.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/motion-markdown-it/common/utils.rb', line 157 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(char) ⇒ Object
Currently without astral characters support.
145 146 147 |
# File 'lib/motion-markdown-it/common/utils.rb', line 145 def isPunctChar(char) return UNICODE_PUNCT_RE =~ char 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]
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/motion-markdown-it/common/utils.rb', line 122 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].
199 200 201 202 203 204 |
# File 'lib/motion-markdown-it/common/utils.rb', line 199 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/motion-markdown-it/common/utils.rb', line 63 def replaceEntityPattern(match, name) code = 0 return HTMLEntities::MAPPINGS[name].chr(Encoding::UTF_8) if 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
85 86 87 88 89 90 91 92 |
# File 'lib/motion-markdown-it/common/utils.rb', line 85 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
79 80 81 82 |
# File 'lib/motion-markdown-it/common/utils.rb', line 79 def unescapeMd(str) return str if !str.include?('\\') return str.gsub(UNESCAPE_MD_RE, '\1') end |