Skip to content

Instantly share code, notes, and snippets.

@extratone
Forked from ttscoff/tabs2md.rb
Created January 29, 2023 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save extratone/4f90bc1663764480f7784b8553774341 to your computer and use it in GitHub Desktop.
Save extratone/4f90bc1663764480f7784b8553774341 to your computer and use it in GitHub Desktop.
Convert indented text lists to Markdown outlines
#!/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