Class: ReverseMarkdown
- Inherits:
-
Object
- Object
- ReverseMarkdown
- Defined in:
- lib/reverse_markdown.rb
Overview
TODO
-
ol numbering is buggy, in fact doesn’t matter for markdown code
-
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
-
#initialize ⇒ ReverseMarkdown
constructor
set basic variables: - @li_counter: numbering list item (li) tags in an ordered list (ol) - @links: hold the links for adding them to the bottom of the @output this means ‘reference style’, please take a look at daringfireball.net/projects/markdown/syntax#link - @outout: fancy markdown code in here! - @indent: control indention level for nested lists - @errors: appearing errors, like unknown tags, go into this array.
-
#parse_element(element, parent = nil) ⇒ Object
Parsing an element and its children (recursive) and writing its markdown code to @output 1.
-
#parse_string(string) ⇒ Object
Invokes the HTML parsing by using a string.
Constructor Details
#initialize ⇒ ReverseMarkdown
set basic variables:
-
@li_counter: numbering list item (li) tags in an ordered list (ol)
-
@links: hold the links for adding them to the bottom of the @output
this means 'reference style', please take a look at http://daringfireball.net/projects/markdown/syntax#link
-
@outout: fancy markdown code in here!
-
@indent: control indention level for nested lists
-
@errors: appearing errors, like unknown tags, go into this array
25 26 27 28 29 30 31 |
# File 'lib/reverse_markdown.rb', line 25 def initialize() @li_counter = 0 @links = [] @output = "" @indent = 0 @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/reverse_markdown.rb', line 16 def errors @errors end |
Instance Method Details
#parse_element(element, parent = nil) ⇒ Object
Parsing an element and its children (recursive) and writing its markdown code to @output
-
do indent for nested list items
-
add the markdown opening tag for this element
3a. if element only contains text, handle it like a text node 3b. if element is a container handle its children, which may be text- or element nodes
-
finally add the markdown ending tag for this element
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/reverse_markdown.rb', line 48 def parse_element(element, parent = nil) name = element.name.to_sym # 1. @output << (" " * @indent) if name.eql?(:li) # 2. @output << opening(element, parent) # 3a. if element.children.size == 1 && element.children.first.text? @output << text_node(element, parent) else # 3b. element.children.each do |child| # increase indent if nested list @indent += 1 if nested_list?(element, parent) if child.element? parse_element(child, element.name.to_sym) else @output << if parent.eql?(:blockquote) child.inner_text.gsub("\n ", "\n>") else child.inner_text end end # decrease indent if end of nested list @indent -= 1 if nested_list?(element, parent) end end # 4. @output << ending(element, parent) end |
#parse_string(string) ⇒ Object
Invokes the HTML parsing by using a string. Returns the markdown code in @output. To garantuee well-formed xml for REXML a <root> element will be added, but has no effect. After parsing all elements, the ‘reference style’-links will be inserted.
36 37 38 39 40 |
# File 'lib/reverse_markdown.rb', line 36 def parse_string(string) parse_element(Nokogiri::HTML.fragment(string)) insert_links @output end |