Class: ActionMailer::Markdown::Renderer::Text
- Inherits:
-
Object
- Object
- ActionMailer::Markdown::Renderer::Text
- Defined in:
- lib/action_mailer/markdown/renderer/text.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #extract ⇒ Object
-
#initialize(source) ⇒ Text
constructor
A new instance of Text.
- #process_code ⇒ Object
- #process_h1 ⇒ Object
- #process_h2 ⇒ Object
- #process_hr ⇒ Object
- #process_links ⇒ Object
- #process_lists ⇒ Object
Constructor Details
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/action_mailer/markdown/renderer/text.rb', line 7 def root @root end |
#source ⇒ Object (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
#extract ⇒ Object
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_code ⇒ Object
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_h1 ⇒ Object
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_h2 ⇒ Object
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_hr ⇒ Object
37 38 39 |
# File 'lib/action_mailer/markdown/renderer/text.rb', line 37 def process_hr root.css("hr").each(&:remove) end |
#process_links ⇒ Object
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_lists ⇒ Object
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 |