Class: MarkupParser::Default
- Inherits:
-
Object
- Object
- MarkupParser::Default
- Defined in:
- lib/markup_parser/default.rb
Instance Attribute Summary collapse
-
#html_text ⇒ Object
readonly
Instantiates the html_text via this markup parser.
-
#lexer_proc ⇒ Object
readonly
Returns the value of attribute lexer_proc.
-
#nokogiri_parser ⇒ Object
readonly
Instantiates a Nokoguri::HTML fragment parser.
-
#original_text ⇒ Object
readonly
Returns the value of attribute original_text.
Instance Method Summary collapse
-
#initialize(text = '', &lexer) ⇒ Default
constructor
A new instance of Default.
-
#stylize_code_blocks ⇒ Object
Stylizes the code blocks in the html_text.
-
#to_html ⇒ Object
Returns the fully stylized and sanitized HTML.
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_text ⇒ Object (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_proc ⇒ Object (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_parser ⇒ Object (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_text ⇒ Object (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_blocks ⇒ Object
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.}. Continueing code block parsing. ******************\n" end end self end |
#to_html ⇒ Object
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.}. Putting error message into the output. ****************** ERROR return "<p class='parse_error'>Error in parsing in #{self.class}: #{e.}.</p>" end end |