Instantly share code, notes, and snippets.

Embed
What would you like to do?
Indented or Markdown list to HTML Unordered List
#!/usr/bin/ruby
# ruby script to make an unordered list from indented data.
data = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
result = "<ul>\n"
last_leading_space = ""
g_tab_width = 4
g_list_level = 0
last_list_level = 0
item = leading_space = ''
data.split("\n").each {|line|
next if line =~ /^[\s\t]*$/
parts = line.match(/^([\t ]*)(\d+\. |[\*\+\-] )?\s*(.*)/)
leading_space = parts[1]
item = parts[3]
leading_space.gsub!(/\t/,' ')
if leading_space.length > last_leading_space.length + 3
last_list_level = g_list_level
g_list_level += 1
last_leading_space = leading_space
last_list_level = g_list_level
result += "<ul>\n"
elsif leading_space.length + 3 < last_leading_space.length
g_list_level = leading_space.length / 4
last_leading_space = leading_space
g_list_level-last_list_level.times do
result += "</ul>\n"
end
last_list_level = g_list_level
end
indent = ""
result += "<li>#{item}</li>\n"
}
puts result + "</ul>"
@ttscoff

This comment has been minimized.

Show comment
Hide comment
@ttscoff

ttscoff Dec 7, 2011

I didn't mention it in the comments, but this is heavily cribbed from one of Fletcher Penney's TextMate commands. Just for the record.

Owner

ttscoff commented Dec 7, 2011

I didn't mention it in the comments, but this is heavily cribbed from one of Fletcher Penney's TextMate commands. Just for the record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment