Created
August 6, 2010 11:04
-
-
Save ttscoff/511174 to your computer and use it in GitHub Desktop.
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 -rjcode -Ku | |
# TaskPaper to Markdown converter | |
# Usage: tp2md.rb filename.taskpaper > output.md | |
require 'ftools' | |
infile = ARGV[0] | |
title = File.basename(infile,'.taskpaper').upcase | |
output = "# #{title} #\n\n" | |
prevlevel = 0 | |
begin | |
file = File.new(infile, "r") | |
while (line = file.gets) | |
if line =~ /^(\t+)?(.*?):(?:\s+)?$/ | |
tabs = $1 | |
project = $2 | |
if tabs.nil? | |
output += "\n## #{project} ##\n\n" | |
prevlevel = 0 | |
else | |
output += "#{tabs.gsub(/^\t/,'')}* **#{project}**\n" | |
prevlevel = tabs.length | |
end | |
elsif line =~ /^(\t+)?\- (.*)$/ | |
task = $2 | |
tabs = $1.nil? ? '' : $1 | |
task = "*#{task}*" if task =~ /@done/ | |
if tabs.length - prevlevel > 1 | |
tabs = "\t" | |
prevlevel.times {|i| tabs += "\t"} | |
end | |
tabs = '' if prevlevel == 0 && tabs.length > 1 | |
output += "#{tabs.gsub(/^\t/,'')}* #{task.strip}\n" | |
prevlevel = tabs.length | |
else | |
next if line =~ /^\s*$/ | |
tabs = "" | |
prevlevel-1.times {|i| tabs += "\t"} | |
output += "#{tabs}> #{line.strip}\n\n" | |
end | |
end | |
file.close | |
rescue => err | |
puts "Exception: #{err}" | |
err | |
end | |
puts output |
I tend to keep scripts like this in a scripts folder off of my home directory (~/scripts) and put that directory in my path (in .bash_profile). If you wanted to make it executable (chmod a+x tp2md.rb) and stick it in /usr/local/bin, you could get away with that, too.
Note that it outputs the result to stdout, so you can redirect the output to anywhere you like (> filename.md) or pipe it to your Markdown processor directly (|/path/to/Markdown.pl)
Any chance you have one that does it in reverse?
I appreciate tp2md.rb and have come to rely on it but I don't know ruby at all myself. I note that the version of ruby shipped with OS X Mavericks isn't compatible with the required ftools gem. Any chance this could get an update? Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very cool. Btw, where would I put the script? /usr/bin ?