class ActionMailer::Markdown::Renderer::Text

Attributes

root[R]
source[R]

Public Class Methods

extract(source) click to toggle source
# File lib/action_mailer/markdown/renderer/text.rb, line 9
def self.extract(source)
  new(source).extract
end
new(source) click to toggle source
# File lib/action_mailer/markdown/renderer/text.rb, line 13
def initialize(source)
  @source = source
  @root = Nokogiri::HTML(Markdown.html(source)).css("body")
end

Public Instance Methods

extract() click to toggle source
# 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_code() click to toggle source
# 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_h1() click to toggle source
# 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_h2() click to toggle source
# 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_hr() click to toggle source
# File lib/action_mailer/markdown/renderer/text.rb, line 37
def process_hr
  root.css("hr").each(&:remove)
end
process_lists() click to toggle source
# 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

Private Instance Methods

build_heading(text, separator) click to toggle source
# File lib/action_mailer/markdown/renderer/text.rb, line 83
        def build_heading(text, separator)
  "#{text}\n#{separator * text.size}"
end