Class: I18n::Transformers::Collection::Markdown

Inherits:
Base
  • Object
show all
Defined in:
lib/i18n/transformers/collection/markdown.rb

Instance Method Summary collapse

Methods inherited from Base

#name

Constructor Details

#initialize(key_pattern: /(\b|_|\.)md$/, adapter: nil, **options, &block) ⇒ Markdown

Returns a new instance of Markdown.



5
6
7
8
9
# File 'lib/i18n/transformers/collection/markdown.rb', line 5

def initialize(key_pattern: /(\b|_|\.)md$/, adapter: nil, **options, &block)
  super options, &block
  self.key_pattern = key_pattern
  self.adapter = adapter
end

Instance Method Details

#adapterObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/i18n/transformers/collection/markdown.rb', line 29

def adapter
  @adapter ||= begin
    current_adapter = nil
    available_adapters.each do |a|
      begin
        self.adapter = a
        current_adapter = a
        break
      rescue LoadError
        next
      end
    end
    current_adapter
  end
end

#adapter=(adptr) ⇒ Object



15
16
17
18
19
20
# File 'lib/i18n/transformers/collection/markdown.rb', line 15

def adapter=(adptr)
  return unless adptr

  require adptr
  @adapter = adptr
end

#available_adaptersObject



45
46
47
48
# File 'lib/i18n/transformers/collection/markdown.rb', line 45

def available_adapters
  @available_adapters ||= private_methods
    .grep(/^transform_with_(.+)$/) { |m| $~[1].to_s }
end

#key_pattern=(pattern) ⇒ Object



11
12
13
# File 'lib/i18n/transformers/collection/markdown.rb', line 11

def key_pattern=(pattern)
  @key_pattern = pattern.is_a?(Regexp) ? pattern : Regexp.quote(pattern.to_s)
end

#transform(key, value) ⇒ Object



22
23
24
25
26
27
# File 'lib/i18n/transformers/collection/markdown.rb', line 22

def transform(key, value)
  return value unless @key_pattern =~ key.to_s

  res = @block ? @block.call(key, value) : markdown_to_html(value)
  res.respond_to?(:html_safe) ? res.html_safe : res
end