class MarkdownIt::RulesCore::Replacements

Constants

RARE_RE

TODO (from original)

  • fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾

  • miltiplication 2 x 4 -> 2 × 4

SCOPED_ABBR
SCOPED_ABBR_RE

Public Class Methods

replace(state) click to toggle source
# File lib/motion-markdown-it/rules_core/replacements.rb, line 89
def self.replace(state)
  return if (!state.md.options[:typographer])

  (state.tokens.length - 1).downto(0) do |blkIdx|
    next if (state.tokens[blkIdx].type != 'inline')

    if (SCOPED_ABBR_RE =~ state.tokens[blkIdx].content)
      replace_scoped(state.tokens[blkIdx].children)
    end

    if (RARE_RE =~ state.tokens[blkIdx].content)
      replace_rare(state.tokens[blkIdx].children)
    end

  end
end
replaceFn(match, name) click to toggle source
# File lib/motion-markdown-it/rules_core/replacements.rb, line 31
def self.replaceFn(match, name)
  return SCOPED_ABBR[name.downcase]
end
replace_rare(inlineTokens) click to toggle source
# File lib/motion-markdown-it/rules_core/replacements.rb, line 56
def self.replace_rare(inlineTokens)
  inside_autolink = 0

  (inlineTokens.length - 1).downto(0) do |i|
    token = inlineTokens[i]
    if token.type == 'text' && inside_autolink == 0
      if (RARE_RE =~ token.content)
        token.content = token.content.
                    gsub(/\+-/, '±').
                    # .., ..., ....... -> …
                    # but ?..... & !..... -> ?.. & !..
                    gsub(/\.{2,}/, '…').gsub(/([?!])…/, "\\1..").
                    gsub(/([?!]){4,}/, '\\1\\1\\1').gsub(/,{2,}/, ',').
                    # em-dash
                    gsub(/(^|[^-])---([^-]|$)/m, "\\1\u2014\\2").
                    # en-dash
                    gsub(/(^|\s)--(\s|$)/m, "\\1\u2013\\2").
                    gsub(/(^|[^-\s])--([^-\s]|$)/m, "\\1\u2013\\2")
      end
    end

    if token.type == 'link_open' && token.info == 'auto'
      inside_autolink -= 1
    end

    if token.type == 'link_close' && token.info == 'auto'
      inside_autolink += 1
    end
  end
end
replace_scoped(inlineTokens) click to toggle source
# File lib/motion-markdown-it/rules_core/replacements.rb, line 36
def self.replace_scoped(inlineTokens)
  inside_autolink = 0

  (inlineTokens.length - 1).downto(0) do |i|
    token = inlineTokens[i]
    if token.type == 'text' && inside_autolink == 0
      token.content = token.content.gsub(SCOPED_ABBR_RE) {|match| self.replaceFn(match, $1)}
    end

    if token.type == 'link_open' && token.info == 'auto'
      inside_autolink -= 1
    end

    if token.type == 'link_close' && token.info == 'auto'
      inside_autolink += 1
    end
  end
end