SHARE
TWEET

tpaper2org.rb

tristan_collins Jul 22nd, 2014 251 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env ruby
  2. # -*- coding: ascii-8bit -*-
  3. #
  4. # Converts Taskpaper files to Emacs org-mode files.
  5. #
  6. # Author: Anupam Sengupta, 2010
  7. #
  8. # Distributed under the BSD license (<a href="http://www.opensource.org/licenses/bsd-license.php">http://www.opensource.org/licenses/bsd-license.php</a>)
  9. #
  10. # Usage: From the command line, enter the command:
  11. #
  12. #  ./tpaper2org.rb <taskpaperfilename>
  13. #
  14. # The output is on STDOUT, which can be redirected to an Org-mode file.
  15. # Whether the generated org-mode file should use odd-level prefix stars
  16. # See <a href="http://orgmode.org/manual/Clean-view.html">http://orgmode.org/manual/Clean-view.html</a> for details.
  17. ORG_USES_ODD_LEVELS = false
  18.  
  19. LINE_PATTERN = /^(\t*)          # Leading tabs
  20.                -                # Followed by a dash (the taskpaper task identifier)
  21.                (.*?)            # The task description
  22.                ((@\w+\s*)*)     # The tags, if any
  23.                $/x
  24.  
  25. all_tags = Hash.new(0)
  26.  
  27. Shiftlvl = ORG_USES_ODD_LEVELS ? 2 : 1 # Determine the number of stars to use in Org-mode entries
  28.  
  29. while (line = gets());
  30.   line.chomp!
  31.    md = LINE_PATTERN.match(line)          # Match and extract each line
  32.   if md then                                                # ................ A Task line
  33.     tags = md[3].split(/ +/).reject {|tag| "@done" == tag } # get the tags, except @done tags
  34.     tags = ['', tags, ''].flatten unless tags.empty?
  35.     puts '*' * (1 + Shiftlvl * (md[1].length + 1)) + (line =~ /@done/ ? " DONE" : " TODO") + md[2] + tags.join(':')
  36.     tags.each { |tag| all_tags[tag] += 1} if tags # Keep a list of all tags
  37.   elsif line =~/:$/                               # ................ A project line
  38.     print "* "
  39.     puts line.chomp(":")
  40.   else                          # ................ Any other line
  41.     puts line
  42.   end
  43. end
  44.  
  45. # Lets do a summary of the tags used.
  46. puts <<END
  47. # The tags used till now.
  48. #+TAGS:#{all_tags.keys.sort.join(' ')}
  49. END
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top