module MarkdownIt::Common::Utils
Constants
- DIGITAL_ENTITY_TEST_RE
- ENTITY_RE
- HTML_ESCAPE_REPLACE_RE
- HTML_ESCAPE_TEST_RE
- HTML_REPLACEMENTS
- REGEXP_ESCAPE_RE
- UNESCAPE_ALL_RE
- UNESCAPE_MD_RE
- UNICODE_PUNCT_RE
Public Instance Methods
arrayReplaceAt(src, pos, newElements)
click to toggle source
Remove element from array and put another array at those position. Useful for some operations with tokens
# 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)
click to toggle source
Merge multiple hashes
# 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)
click to toggle source
# File lib/motion-markdown-it/common/utils.rb, line 60 def charCodeAt(str, ch) str[ch].ord unless str[ch].nil? end
escapeHtml(str)
click to toggle source
# 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)
click to toggle source
# File lib/motion-markdown-it/common/utils.rb, line 124 def escapeRE(str) str.gsub(REGEXP_ESCAPE_RE) {|s| '\\' + s} end
fromCharCode(c)
click to toggle source
# File lib/motion-markdown-it/common/utils.rb, line 55 def fromCharCode(c) c.chr end
fromCodePoint(c)
click to toggle source
# File lib/motion-markdown-it/common/utils.rb, line 50 def fromCodePoint(c) c.chr(Encoding::UTF_8) end
isMdAsciiPunct(ch)
click to toggle source
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.
# 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)
click to toggle source
Currently without astral characters support.
# File lib/motion-markdown-it/common/utils.rb, line 164 def isPunctChar(ch) return UNICODE_PUNCT_RE =~ ch end
isSpace(code)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Zs (unicode class) || [tfvrn]
# 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)
click to toggle source
Hepler to unify [reference labels].
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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