Module: TextTube::Baby::InsideBlock

Extended by:
Filterable
Defined in:
lib/texttube/baby/inside_block.rb

Overview

This finds html tags with “markdown=‘1’” as an attribute, runs markdown over the contents, then removes the markdown attribute, allowing markdown within html blocks

Class Method Summary collapse

Class Method Details

.run(content, options = {}) ⇒ Object

Parameters:

  • content (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • The (Constant)

    markdown parser to use. I’m not sure this bit really works for other parsers than RDiscount.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/texttube/baby/inside_block.rb', line 19

def self.run( content, options={})   
  options ||= {} 
  if options[:markdown_parser].nil?
    require 'rdiscount'
    markdown_parser=RDiscount
  end
  doc = Oga.parse_html(content)

  doc.xpath("*[@markdown='1']").each do |ele|
    inner_text = ele.children.inject(""){|mem,child| mem << child.to_xml }
    html_fragment = markdown_parser.new(inner_text).to_html
    ele.children= Oga.parse_html( html_fragment ).children
    ele.unset "markdown"
  end

  doc.to_xml
end