Class: ActionMailer::Markdown::Renderer::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mailer/markdown/renderer/text.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Text

Returns a new instance of Text.



13
14
15
16
# File 'lib/action_mailer/markdown/renderer/text.rb', line 13

def initialize(source)
  @source = source
  @root = Nokogiri::HTML(Markdown.html(source)).css("body")
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/action_mailer/markdown/renderer/text.rb', line 7

def root
  @root
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/action_mailer/markdown/renderer/text.rb', line 7

def source
  @source
end

Class Method Details

.extract(source) ⇒ Object



9
10
11
# File 'lib/action_mailer/markdown/renderer/text.rb', line 9

def self.extract(source)
  new(source).extract
end

Instance Method Details

#extractObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/action_mailer/markdown/renderer/text.rb', line 18

def extract
  process_hr
  process_links
  process_h1
  process_h2
  process_lists
  process_code

  root.text
end

#process_codeObject



29
30
31
32
33
34
35
# File 'lib/action_mailer/markdown/renderer/text.rb', line 29

def process_code
  root.css("code").each do |code|
    next if code.parent.name == "pre"

    code.content = "`#{code.content}`"
  end
end

#process_h1Object



41
42
43
44
45
# File 'lib/action_mailer/markdown/renderer/text.rb', line 41

def process_h1
  root.css("h1").each do |heading|
    heading.content = build_heading(heading.text, "=")
  end
end

#process_h2Object



47
48
49
50
51
# File 'lib/action_mailer/markdown/renderer/text.rb', line 47

def process_h2
  root.css("h2").each do |heading|
    heading.content = "\n#{build_heading(heading.text, '-')}"
  end
end

#process_hrObject



37
38
39
# File 'lib/action_mailer/markdown/renderer/text.rb', line 37

def process_hr
  root.css("hr").each(&:remove)
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/action_mailer/markdown/renderer/text.rb', line 62

def process_links
  links = []

  root.css("a").each do |link|
    href = link["href"]

    if href.starts_with?("mailto:")
      link.content = href.sub("mailto:", "")
    elsif link.content.match?(%r{^(?!\w+://)})
      links << href unless links.include?(href)
      index = links.index(href) + 1
      link.content = "#{link.content}[#{index}]"
    end
  end

  links_list = links.map.with_index(1) {|link, index| "[#{index}]: #{link}" }.join("\n")
  node = Nokogiri::HTML.fragment "<pre>\n#{links_list}\n</pre>"

  root << node
end

#process_listsObject



53
54
55
56
57
58
59
60
# File 'lib/action_mailer/markdown/renderer/text.rb', line 53

def process_lists
  root.css("ol, ul").each do |list|
    list.css("li").to_enum(:each).with_index(1) do |item, index|
      prefix = (list.name == "ol" ? "#{index}." : "-")
      item.content = "#{prefix} #{item.text}"
    end
  end
end