Class: Mail2FrontMatter::AutomaticEmbed

Inherits:
PreProcessor
  • Object
show all
Extended by:
AutoHtml
Defined in:
lib/mail2frontmatter/automatic-embeds.rb

Class Method Summary collapse

Class Method Details

.register(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mail2frontmatter/automatic-embeds.rb', line 13

def self.register(options = {})
  super

  # I *could* introspect on auto_html here but I haven't vetted them all yet
  if @options[:white_list]
    @filters = @options[:white_list].map(&:to_sym)
  elsif @options[:black_list]
    @filters = @all_filters - @options[:black_list].map(&:to_sym)
  else
    @filters = @all_filters
  end

end

.run(metadata, body) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mail2frontmatter/automatic-embeds.rb', line 27

def self.run(, body)
  body = unwrap_links(body)

  body = auto_html(body, { filters: @filters, options: @options[:filters] }) { 
    # use options passed to us...
    @options[:filters].each do |filter|
      options_for_filter = @options[:options] ? (@options[:options][filter.to_sym] || {}) : {}
      self.send(filter.to_sym, options_for_filter)
    end

    # be sure to return @text here
    @text
  }

  return , body
end


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mail2frontmatter/automatic-embeds.rb', line 44

def self.unwrap_links(body)
  parsed_body = Nokogiri::HTML::DocumentFragment.parse(body)

  parsed_body.elements.css("a").each do |element|
    href = element.attributes["href"].value

    if href == element.inner_html
      body.gsub!(element.to_s, href)
    end
  end

  body
end