Skip to content

Instantly share code, notes, and snippets.

@willwade
Created March 20, 2018 20:14
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 willwade/2c771fdcb4adfc0c1588324bf59c4d11 to your computer and use it in GitHub Desktop.
Save willwade/2c771fdcb4adfc0c1588324bf59c4d11 to your computer and use it in GitHub Desktop.
Brett Terpstra's "Markdown to Mindnmap" script. Takes any indented/mix list and outputs a non-formatted list
# encoding: utf-8
require 'shellwords'
if RUBY_VERSION.to_f > 1.9
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
input = STDIN.read.force_encoding('utf-8')
else
input = STDIN.read
end
def outdent(input)
shortest = false
input.scan(/^\s*/m).each do |leader|
if shortest == false || leader.length < shortest.length
shortest = leader
end
end
input.gsub(/^#{shortest}/m,'').strip
end
# initial outdent and create array
lines = outdent(input).split("\n")
# Remove blank lines
lines.delete_if {|line| line =~ /^\s*$/}
# Handle converting headlines and lists to indented outline
last_level = 0
lines.map! {|line|
if line =~ /^#\s/
line.gsub!(/^# /,"")
last_level = 1
elsif line =~ /^## /
line.gsub!(/^## /,"\t")
last_level = 2
elsif line =~ /^### /
line.gsub!(/^### /,"\t\t")
last_level = 3
elsif line =~ /^#### /
line.gsub!(/^#### /,"\t\t\t")
last_level = 4
elsif line =~ /^##### /
line.gsub!(/^##### /,"\t\t\t\t")
last_level = 5
elsif line =~ /^###### /
line.gsub!(/^###### /,"\t\t\t\t\t")
last_level = 6
else
line.gsub!(/(\s*)(\d\.|[\-\*\+])\s/,"\\1")
line.gsub!(/ {4}/,"\t") unless line.nil?
last_level.times do
line = "\t" + line
end
end
line.chomp
}
# Copy to cliipboard
%x{printf "%b" #{Shellwords.escape(outdent(lines.join("\n")).strip)}|LANG=en_US.UTF-8 pbcopy}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment