Class: MarkupParser::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/markup_parser/default.rb

Direct Known Subclasses

Html, Markdown, Rdoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = '', &lexer) ⇒ Default

Returns a new instance of Default.



7
8
9
10
# File 'lib/markup_parser/default.rb', line 7

def initialize(text='', &lexer)
  @original_text = hot_fixes(text)
  @lexer_proc = lexer || default_lexer
end

Instance Attribute Details

#html_textObject (readonly)

Instantiates the html_text via this markup parser



46
47
48
# File 'lib/markup_parser/default.rb', line 46

def html_text
  @html_text
end

#lexer_procObject (readonly)

Returns the value of attribute lexer_proc.



5
6
7
# File 'lib/markup_parser/default.rb', line 5

def lexer_proc
  @lexer_proc
end

#nokogiri_parserObject (readonly)

Instantiates a Nokoguri::HTML fragment parser



31
32
33
# File 'lib/markup_parser/default.rb', line 31

def nokogiri_parser
  @nokogiri_parser
end

#original_textObject (readonly)

Returns the value of attribute original_text.



5
6
7
# File 'lib/markup_parser/default.rb', line 5

def original_text
  @original_text
end

Instance Method Details

#stylize_code_blocksObject

Stylizes the code blocks in the html_text. Uses either a passed in lexer Proc or the default_lexer



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/markup_parser/default.rb', line 62

def stylize_code_blocks
    nokogiri_parser.search('pre').each do |node|
      begin
        next unless lang = node['lang']
        text = node.inner_text
        html = @lexer_proc.call(text, lang)
        node.replace(html)
      rescue => e
        puts "
        \n******************
        Error in parsing <pre lang=''> block.
        Reason: #{e.message}.
        Continueing code block parsing.
        ******************\n"
      end
    end
  self
end

#to_htmlObject

Returns the fully stylized and sanitized HTML



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/markup_parser/default.rb', line 14

def to_html
  begin
    nokogiri_parser.to_xhtml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XHTML)
  rescue => e
    puts <<-ERROR
    ******************
    Error in #{self.class}#to_html.
    Reason: #{e.message}.
    Putting error message into the output.
    ******************
    ERROR
    return "<p class='parse_error'>Error in parsing in #{self.class}: #{e.message}.</p>"
  end
end