Class: MarkdownIt::RulesCore::Replacements

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-markdown-it/rules_core/replacements.rb

Constant Summary collapse

RARE_RE =

TODO (from original)

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

  • miltiplication 2 x 4 -> 2 × 4

/\+-|\.\.|\?\?\?\?|!!!!|,,|--/
SCOPED_ABBR_RE =
/\((c|tm|r|p)\)/i
SCOPED_ABBR =
{
  'c' => '©',
  'r' => '®',
  'p' => '§',
  'tm' => ''
}

Class Method Summary collapse

Class Method Details

.replace(state) ⇒ Object




71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/motion-markdown-it/rules_core/replacements.rb', line 71

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

.replace_rare(inlineTokens) ⇒ Object




48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/motion-markdown-it/rules_core/replacements.rb', line 48

def self.replace_rare(inlineTokens)
  (inlineTokens.length - 1).downto(0) do |i|
    token = inlineTokens[i]
    if (token.type == 'text')
      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
  end
end

.replace_scoped(inlineTokens) ⇒ Object




38
39
40
41
42
43
44
45
# File 'lib/motion-markdown-it/rules_core/replacements.rb', line 38

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

.replaceFn(match, name) ⇒ Object




33
34
35
# File 'lib/motion-markdown-it/rules_core/replacements.rb', line 33

def self.replaceFn(match, name)
  return SCOPED_ABBR[name.downcase]
end