Module: MarkdownIt::Common::Utils
- Included in:
- Helpers::ParseLinkDestination, Parser, Renderer, Renderer, RulesBlock::Blockquote, RulesBlock::Fence, RulesBlock::Heading, RulesBlock::Hr, RulesBlock::HtmlBlock, RulesBlock::Lheading, RulesBlock::List, RulesBlock::Reference, RulesBlock::StateBlock, RulesBlock::Table, RulesCore::Smartquotes, RulesInline::Autolink, RulesInline::Backticks, RulesInline::Emphasis, RulesInline::Entity, RulesInline::Escape, RulesInline::HtmlInline, RulesInline::Image, RulesInline::Link, RulesInline::Newline, RulesInline::StateInline, RulesInline::Strikethrough, RulesInline::Text
- 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 ——————————————————————————.
-
#charCodeAt(str, ch) ⇒ Object
——————————————————————————.
-
#escapeHtml(str) ⇒ Object
——————————————————————————.
-
#escapeRE(str) ⇒ Object
——————————————————————————.
-
#fromCharCode(c) ⇒ Object
——————————————————————————.
-
#fromCodePoint(c) ⇒ Object
——————————————————————————.
-
#isMdAsciiPunct(ch) ⇒ Object
Markdown ASCII punctuation characters.
-
#isPunctChar(ch) ⇒ Object
Currently without astral characters support.
-
#isSpace(code) ⇒ Object
——————————————————————————.
-
#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 |
#charCodeAt(str, ch) ⇒ Object
60 61 62 |
# File 'lib/motion-markdown-it/common/utils.rb', line 60 def charCodeAt(str, ch) str[ch].ord unless str[ch].nil? end |
#escapeHtml(str) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/motion-markdown-it/common/utils.rb', line 114 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
124 125 126 |
# File 'lib/motion-markdown-it/common/utils.rb', line 124 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.
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 211 212 213 214 |
# File 'lib/motion-markdown-it/common/utils.rb', line 176 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.
164 165 166 |
# File 'lib/motion-markdown-it/common/utils.rb', line 164 def isPunctChar(ch) return UNICODE_PUNCT_RE =~ ch end |
#isSpace(code) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/motion-markdown-it/common/utils.rb', line 129 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]
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/motion-markdown-it/common/utils.rb', line 141 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].
218 219 220 221 222 223 |
# File 'lib/motion-markdown-it/common/utils.rb', line 218 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/motion-markdown-it/common/utils.rb', line 72 def replaceEntityPattern(match, name) code = 0 return fromCodePoint(MarkdownIt::HTMLEntities::MAPPINGS[name]) if MarkdownIt::HTMLEntities::MAPPINGS[name] if (charCodeAt(name, 0) == 0x23 && DIGITAL_ENTITY_TEST_RE =~ name) # '#' code = name[1].downcase == 'x' ? name[2..-1].to_i(16) : name[1..-1].to_i if (isValidEntityCode(code)) return fromCodePoint(code) end end return match end |
#unescapeAll(str) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/motion-markdown-it/common/utils.rb', line 94 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
88 89 90 91 |
# File 'lib/motion-markdown-it/common/utils.rb', line 88 def unescapeMd(str) return str if !str.include?('\\') return str.gsub(UNESCAPE_MD_RE, '\1') end |