Class: MarkdownIt::Ruler

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

Instance Method Summary collapse

Constructor Details

#initializeRuler

Returns a new instance of Ruler.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/motion-markdown-it/ruler.rb', line 21

def initialize
  # // List of added rules. Each element is:
  # //
  # // {
  # //   name: XXX,
  # //   enabled: Boolean,
  # //   fn: Function(),
  # //   alt: [ name2, name3 ]
  # // }
  @__rules__ = []

  # // Cached rule chains.
  # //
  # // First level - chain name, '' for default.
  # // Second level - diginal anchor for fast filtering by charcodes.
  @__cache__ = nil
end

Instance Method Details

#__compile__Object

// Build rules lookup cache




55
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
# File 'lib/motion-markdown-it/ruler.rb', line 55

def __compile__
  chains = [ '' ]

  # // collect unique names
  @__rules__.each do |rule|
    next if !rule[:enabled]

    rule[:alt].each do |altName|
      if !chains.include?(altName)
        chains.push(altName)
      end
    end
  end

  @__cache__ = {}

  chains.each do |chain|
    @__cache__[chain] = []
    @__rules__.each do |rule|
      next if !rule[:enabled]
      next if (chain && !rule[:alt].include?(chain))

      @__cache__[chain].push(rule[:fn])
    end
  end
end

#__find__(name) ⇒ Object

// Find rule index by name




45
46
47
48
49
50
# File 'lib/motion-markdown-it/ruler.rb', line 45

def __find__(name)
  @__rules__.each_with_index do |rule, index|
    return index if (rule[:name] == name)
  end
  return -1
end

#after(afterName, ruleName, fn, opt = {}) ⇒ Object

  • Ruler.after(afterName, ruleName, fn [, options])

    • afterName (String): new rule will be added after this one.

    • ruleName (String): name of added rule.

    • fn (Function): rule function.

    • options (Object): rule options (not mandatory).

*

  • Add new rule to chain after one with given name. See also

  • [[Ruler.before]], [[Ruler.push]].

*

  • ##### Options:

*

    • __alt__ - array with names of “alternate” chains.

*

  • ##### Example

*

  • “‘javascript

  • var md = require(‘markdown-it’)();

*

  • md.inline.ruler.after(‘text’, ‘my_rule’, function replace(state)

  • //…

  • );

  • “‘


Raises:

  • (StandardError)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/motion-markdown-it/ruler.rb', line 180

def after(afterName, ruleName, fn, opt = {})
  index = __find__(afterName)

  raise(StandardError, "Parser rule not found: #{afterName}") if index == -1

  @__rules__.insert(index + 1, {
    name: ruleName,
    enabled: true,
    fn: fn,
    alt: (opt[:alt] || [''])
  })

  @__cache__ = nil
end

#at(name, fn, opt = {}) ⇒ Object

  • Ruler.at(name, fn [, options])

    • name (String): rule name to replace.

    • fn (Function): new rule function.

    • options (Object): new rule options (not mandatory).

*

  • Replace rule by name with new function & options. Throws error if name not

  • found.

*

  • ##### Options:

*

    • __alt__ - array with names of “alternate” chains.

*

  • ##### Example

*

  • Replace existing typographer replacement rule with new one:

*

  • “‘javascript

  • var md = require(‘markdown-it’)();

*

  • md.core.ruler.at(‘replacements’, function replace(state)

  • //…

  • );

  • “‘


Raises:

  • (StandardError)


107
108
109
110
111
112
113
114
115
# File 'lib/motion-markdown-it/ruler.rb', line 107

def at(name, fn, opt = {})
  index = __find__(name)

  raise(StandardError, "Parser rule not found: #{name}") if index == -1

  @__rules__[index][:fn] = fn
  @__rules__[index][:alt] = opt[:alt] || ['']
  @__cache__ = nil
end

#before(beforeName, ruleName, fn, opt = {}) ⇒ Object

  • Ruler.before(beforeName, ruleName, fn [, options])

    • beforeName (String): new rule will be added before this one.

    • ruleName (String): name of added rule.

    • fn (Function): rule function.

    • options (Object): rule options (not mandatory).

*

  • Add new rule to chain before one with given name. See also

  • [[Ruler.after]], [[Ruler.push]].

*

  • ##### Options:

*

    • __alt__ - array with names of “alternate” chains.

*

  • ##### Example

*

  • “‘javascript

  • var md = require(‘markdown-it’)();

*

  • md.block.ruler.before(‘paragraph’, ‘my_rule’, function replace(state)

  • //…

  • );

  • “‘


Raises:

  • (StandardError)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/motion-markdown-it/ruler.rb', line 141

def before(beforeName, ruleName, fn, opt = {})
  index = __find__(beforeName)

  raise(StandardError, "Parser rule not found: #{beforeName}") if index == -1

  @__rules__.insert(index, {
    name: ruleName,
    enabled: true,
    fn: fn,
    alt: (opt[:alt] || [''])
  })

  @__cache__ = nil
end

#disable(list, ignoreInvalid = false) ⇒ Object

  • Ruler.disable(list [, ignoreInvalid]) -> Array

    • list (String|Array): list of rule names to disable.

    • ignoreInvalid (Boolean): set ‘true` to ignore errors when rule not found.

*

  • Disable rules with given names. If any rule name not found - throw Error.

  • Errors can be disabled by second param.

*

  • Returns list of found rule names (if no exception happened).

*

  • See also [[Ruler.enable]], [[Ruler.enableOnly]].




289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/motion-markdown-it/ruler.rb', line 289

def disable(list, ignoreInvalid = false)
  list = [ list ] if !list.is_a?(Array)
  result = []

  # // Search by name and disable
  list.each do |name|
    idx = __find__(name)

    if idx < 0
      next if ignoreInvalid
      raise(StandardError, "Rules manager: invalid rule name #{name}")
    end
    @__rules__[idx][:enabled] = false
    result.push(name)
  end

  @__cache__ = nil
  return result
end

#enable(list, ignoreInvalid = false) ⇒ Object

  • Ruler.enable(list [, ignoreInvalid]) -> Array

    • list (String|Array): list of rule names to enable.

    • ignoreInvalid (Boolean): set ‘true` to ignore errors when rule not found.

*

  • Enable rules with given names. If any rule name not found - throw Error.

  • Errors can be disabled by second param.

*

  • Returns list of found rule names (if no exception happened).

*

  • See also [[Ruler.disable]], [[Ruler.enableOnly]].




239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/motion-markdown-it/ruler.rb', line 239

def enable(list, ignoreInvalid = false)
  list = [ list ] if !list.is_a?(Array)
  result = []

  # // Search by name and enable
  list.each do |name|
    idx = __find__(name)

    if idx < 0
      next if ignoreInvalid
      raise(StandardError, "Rules manager: invalid rule name #{name}")
    end
    @__rules__[idx][:enabled] = true
    result.push(name)
  end

  @__cache__ = nil
  return result
end

#enableOnly(list, ignoreInvalid = false) ⇒ Object

  • Ruler.enableOnly(list [, ignoreInvalid])

    • list (String|Array): list of rule names to enable (whitelist).

    • ignoreInvalid (Boolean): set ‘true` to ignore errors when rule not found.

*

  • Enable rules with given names, and disable everything else. If any rule name

  • not found - throw Error. Errors can be disabled by second param.

*

  • See also [[Ruler.disable]], [[Ruler.enable]].




269
270
271
272
273
274
275
# File 'lib/motion-markdown-it/ruler.rb', line 269

def enableOnly(list, ignoreInvalid = false)
  list = [ list ] if !list.is_a?(Array)

  @__rules__.each { |rule| rule[:enabled] = false }

  enable(list, ignoreInvalid)
end

#getRules(chainName) ⇒ Object

  • Ruler.getRules(chainName) -> Array

*

  • Return array of active functions (rules) for given chain name. It analyzes

  • rules configuration, compiles caches if not exists and returns result.

*

  • Default chain name is ‘”` (empty string). It can’t be skipped. That’s

  • done intentionally, to keep signature monomorphic for high speed.




318
319
320
321
322
323
324
325
# File 'lib/motion-markdown-it/ruler.rb', line 318

def getRules(chainName)
  if @__cache__ == nil
    __compile__
  end

  # // Chain can be empty, if rules disabled. But we still have to return Array.
  return @__cache__[chainName] || []
end

#push(ruleName, fn, opt = {}) ⇒ Object

  • Ruler.push(ruleName, fn [, options])

    • ruleName (String): name of added rule.

    • fn (Function): rule function.

    • options (Object): rule options (not mandatory).

*

  • Push new rule to the end of chain. See also

  • [[Ruler.before]], [[Ruler.after]].

*

  • ##### Options:

*

    • __alt__ - array with names of “alternate” chains.

*

  • ##### Example

*

  • “‘javascript

  • var md = require(‘markdown-it’)();

*

  • md.core.ruler.push(‘my_rule’, function replace(state)

  • //…

  • );

  • “‘




217
218
219
220
221
222
223
224
225
# File 'lib/motion-markdown-it/ruler.rb', line 217

def push(ruleName, fn, opt = {})
  @__rules__.push({
    name: ruleName,
    enabled: true,
    fn: fn,
    alt: (opt[:alt] ? [''] + opt[:alt] : [''])
  })
  @__cache__ = nil
end