Created
September 26, 2012 19:48
-
-
Save ttscoff/3790161 to your computer and use it in GitHub Desktop.
Convert indented text lists to Markdown outlines
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/env ruby | |
# tabs2md | |
# Brett Terpstra 2012 | |
# | |
# Convert indented text to Markdown | |
# | |
# Great for apps that can copy out indented outlines but not OPML | |
# For basic outlines (mind maps) to Markdown, faster than OPML | |
# | |
# Indents can be tabs or 4 spaces | |
# no indent = h1 | |
# 1 indent = h2, 2 indents = h3, then nested list items | |
# Beyond six indents become paragraphs nested in the preceding list item | |
unless ARGV[0].nil? | |
input = File.new(ARGV[0],'r').read | |
else | |
input = STDIN.read | |
end | |
input.split("\n").each { |line| | |
next if line =~ /^\s*$/ | |
line.gsub!(/ {4}/,"\t") if line =~ / {4,}\w/ | |
matches = line.scan(/\t/).length | |
case matches | |
when 0 | |
prefix = "\n# " | |
addnewline = true | |
when 1 | |
prefix = "\n## " | |
addnewline = true | |
when 2 | |
prefix = "\n### " | |
addnewline = true | |
when 3 | |
prefix = "* " | |
when 4 | |
prefix = " * " | |
when 5 | |
prefix = " * " | |
when 6 | |
prefix = " * " | |
else | |
prefix = "\n " | |
addnewline = true | |
end | |
# suffix = addnewline ? "\n\n" : "\n" | |
puts prefix + line.strip | |
puts if addnewline | |
addnewline = false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment