Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request: Add support for h3 through h6 #1

Merged
merged 1 commit into from
May 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is just a little scripting work, feel free to use it, change it, damn it or

Features
--------
* supported tags: h1, h2, p, em, strong, blockquote, code, img, a, hr, li, ol, ul
* supported tags: h1, h2, h3, h4, h5, h6, p, em, strong, blockquote, code, img, a, hr, li, ol, ul
* nested lists
* inline and block code

Expand All @@ -16,8 +16,8 @@ Usage
1. Include ReverseMarkdown class to your application
2. get an instance: r = ReverseMarkdown.new
3. parse a HTML string and save the return value: markdown = r.parse_string(html_string)

Look at
-------
wmd-editor: http://wmd-editor.com/
markdown syntax: http://daringfireball.net/projects/markdown/
markdown syntax: http://daringfireball.net/projects/markdown/
50 changes: 33 additions & 17 deletions reverse_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

# TODO
# - ol numbering is buggy, in fact doesn't matter for markdown code
# -
# -

class 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
Expand All @@ -31,7 +31,7 @@ def initialize()
@indent = 0
@errors = []
end

# 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.
Expand All @@ -41,7 +41,7 @@ def parse_string(string)
insert_links()
@output
end

# Parsing an element and its children (recursive) and writing its markdown code to @output
# 1. do indent for nested list items
# 2. add the markdown opening tag for this element
Expand All @@ -54,18 +54,18 @@ def parse_element(element, parent)
@output << indent() if name.eql?(:li)
# 2.
@output << opening(element, parent)

# 3a.
if (element.has_text? and element.children.size < 2)
@output << text_node(element, parent)
end

# 3b.
if element.has_elements?
element.children.each do |child|
# increase indent if nested list
@indent += 1 if element.name=~/(ul|ol)/ and parent.eql?(:li)

if child.node_type.eql?(:element)
parse_element(child, element.name.to_sym)
else
Expand All @@ -75,12 +75,12 @@ def parse_element(element, parent)
@output << child.to_s
end
end

# decrease indent if end of nested list
@indent -= 1 if element.name=~/(ul|ol)/ and parent.eql?(:li)
end
end

# 4.
@output << ending(element, parent)
end
Expand All @@ -99,6 +99,14 @@ def opening(type, parent)
""
when :h2
"## "
when :h3
"### "
when :h4
"#### "
when :h5
"##### "
when :h6
"###### "
when :em
"*"
when :strong
Expand All @@ -122,14 +130,22 @@ def opening(type, parent)
""
end
end

# Returns the closing markdown tag, like opening()
def ending(type, parent)
case type.name.to_sym
when :h1
" #\n\n"
when :h2
" ##\n\n"
when :h3
" ###\n\n"
when :h4
" ####\n\n"
when :h5
" #####\n\n"
when :h6
" ######\n\n"
when :p
parent.eql?(:root) ? "\n\n" : "\n"
when :ol
Expand Down Expand Up @@ -160,7 +176,7 @@ def ending(type, parent)
""
end
end

# Perform indent: two space, @indent times - quite simple! :)
def indent
str = ""
Expand All @@ -169,7 +185,7 @@ def indent
end
str
end

# Return the content of element, which should be just text.
# If its a code block to indent of 4 spaces.
# For block quotation add a leading '>'
Expand All @@ -182,30 +198,30 @@ def text_node(element, parent)
element.text
end
end

# Insert the mentioned reference style links.
def insert_links
@output << "\n"
@links.each_index do |index|
@output << " [#{index+1}]: #{@links[index]}\n"
end
end

# Print out all errors, that occured and have been written to @errors.
def print_errors
@errors.each do |error|
puts error
end
end

# Perform a benchmark on a given string n-times.
def speed_benchmark(string, n)
initialize()
bm(15) do |test|
test.report("reverse markdown:") { n.times do; parse_string(string); initialize(); end; }
end
end

end

if __FILE__ == $0
Expand Down Expand Up @@ -264,7 +280,7 @@ def speed_benchmark(string, n)
dolore magna aliquyam erat, sed</p>
</blockquote>

This should also be shown, even if it's not wrapped in an element.
This should also be shown, even if it's not wrapped in an element.

<p>nur ein text! nur eine maschine!</p>

Expand Down