Last active
April 3, 2025 19:48
-
-
Save ttscoff/511174 to your computer and use it in GitHub Desktop.
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/env ruby | |
# Usage: tp2md.rb filename.taskpaper > output.md | |
# Updated 2025-03-19 | |
# - Fix block quote indentation | |
# - use GFM-style - [ ] markers | |
# - convert @tags to #tags | |
# - <self-link> bare urls | |
# - Accept input piped on STDIN | |
if $stdin.stat.size.positive? | |
input = $stdin.read | |
title = "TODO" | |
else | |
input = File.read(ARGV[0]) | |
title = File.basename(ARGV[0],'.taskpaper').capitalize | |
end | |
output = "# #{title} #\n\n" | |
prevlevel = 0 | |
begin | |
input.each_line do |line| | |
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 | |
marker = task =~ /@done/ ? '- [x]' : '- [ ]' | |
if tabs.length - prevlevel > 1 | |
tabs = "\t" | |
prevlevel.times {|i| tabs += "\t"} | |
end | |
tabs = '' if prevlevel == 0 && tabs.length > 1 | |
output += "#{tabs.gsub(/^\t/,'')}#{marker} #{task.strip}\n" | |
prevlevel = tabs.length | |
else | |
next if line =~ /^\s*$/ | |
tabs = "" | |
prevlevel.times {|i| tabs += "\t"} | |
output += "#{tabs}> #{line.strip}\n\n" | |
end | |
end | |
rescue => err | |
puts "Exception: #{err}" | |
err | |
end | |
output.gsub!(/(?mi)(?<!\]\(|\]: )\b((?:[\w-]+?:\/\/)(?:\S+?))(?=[\s\n)]|$)/, '<\1>') | |
puts output.gsub(/@(?=\S)/, '#') |
Updated for modern systems, see comments at top for changes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.