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




89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

.replace_rare(inlineTokens) ⇒ Object




56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object




36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

.replaceFn(match, name) ⇒ Object




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

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