Created
December 7, 2011 14:02
-
-
Save ttscoff/1442912 to your computer and use it in GitHub Desktop.
Indented or Markdown list to HTML Unordered List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.