Created
August 24, 2013 09:32
-
-
Save ttscoff/6327114 to your computer and use it in GitHub Desktop.
Quick conversion to Markdown for Curio list text output (Copy As Plain Text)
This file contains hidden or 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 | |
# encoding: utf-8 | |
# Quick conversion to Markdown for Curio list text output (Copy As Plain Text) | |
# Separates notes into subparagraphs, builds headers and lists based on max_headers setting | |
# You can insert a line at the top with "max headers: 5" to override the default setting for that text | |
# Usage: | |
# pbpaste|curio2md.rb > markdown_file.md | |
# Also works as a system service | |
max_headers = 3 | |
input = STDIN.read | |
o = "" | |
input.split("\n").delete_if {|line| line =~ /^\s*$/ }.each { |line| | |
if line.strip =~ /^max(?:[-_ ]headers?)?: (\d)$/i | |
max_headers = $1.to_i | |
next | |
end | |
numeric = false | |
addnewline = false | |
line.gsub!(/ {4}/,"\t") if line =~ /^ {4,}\w/ | |
if line =~ /(\t*)(?:\d+\.|\(\d+\)|i+[\)\.])\t(\w.*$)/ | |
line = "#{$1}#{$2}" | |
numeric = true | |
elsif line =~ /(\t*)\S{1,3}[\)\.]?\t(\w.*$)/i | |
line = "#{$1}#{$2}" | |
end | |
matches = line.scan(/\t/).length | |
if matches < max_headers | |
prefix = "\n" | |
(matches + 1).times { prefix += "#" } | |
prefix += " " | |
addnewline = true | |
else | |
prefix = "" | |
(matches-max_headers).times { prefix += "\t" } | |
prefix += numeric ? "1. " : "* " | |
end | |
if line =~ /^(.+?) - (.+)/ | |
note = $2 | |
line = $1 | |
else | |
note = false | |
end | |
o += prefix + line.strip + "\n" | |
if note | |
indent = "" | |
(matches-max_headers+1).times do | |
indent += "\t" | |
end | |
o += "\n#{indent}#{note.strip}\n" | |
end | |
o += "\n" if addnewline | |
addnewline = false | |
} | |
print o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment