Return to Snippet

Revision: 12767
at March 27, 2009 05:29 by dbug13


Updated Code
#!/usr/bin/env ruby

require 'pp'

module TaskPaper
  class TaskParser
    attr_reader :task_string, :tags
  
    def initialize(&observer)
      @observers = []
      @tags = []
      @task_string = ""
    
      if block_given?
        @observers << observer
      end
    end
  
    def on_task_parsed(&observer)
      @observers << observer
    end
  
    def parseTask(taskdata)
      @tags = []
      $task_string = ""
      matches = /^((-)*(.*?))((@.*)*)*$/.match(taskdata)
      @task_string = matches[1]
      tag_string = matches[4]
    
      if tag_string
        @tags = tag_string.split(/[,\s]+/)
      end
      
      @observers.each do |observer|
        observer.call(self)
      end
      
    end    
  end
  
  class Task
    attr_accessor :tags, :task_string
    
    def initialize(task_string_data="", task_tag_array=[])
      @task_string = task_string_data
      @tags = task_tag_array
    end
    
    def to_s
      tags = ""
      tags = @tags.join(" ") if @tags.length > 0
      "#{@task_string} #{tags}"
    end
    
    def empty?
      @task_string.strip.empty?
    end
    
    def is_project?
      @task_string =~ /.*:$/
    end
  
    def is_task?
      @task_string =~ /^-.*/
    end
  
    def is_comment?
      !empty? && !is_task? && !is_project? 
    end 
    
  end
end

# task_list = []
# 
# t = TaskPaper::TaskParser.new do |task_data|
#   task = TaskPaper::Task.new(task_data.task_string, task_data.tags)
#   task_list << task
#   puts task
#   puts "Is a Task" if task.is_task?
#   puts "Is a Comment" if task.is_comment?
#   puts "Is a Project" if task.is_project?
#   puts "Is Empty" if task.empty?
#   puts task.tags.inspect if task.tags.length > 0
#   puts
# end
# 
# tasks = %{Chores:
# - Take out the trash @home
#   only do this if it's not raining
#     
#     
# - Buy a new door knob @store @hardware
# }
# 
# 
# tasks.split(/\n/).each do |line|
#   t.parseTask(line)
# end
# 
# pp task_list

Revision: 12766
at March 27, 2009 05:20 by dbug13


Initial Code

                                

Initial URL

                                

Initial Description

                                

Initial Title
TaskPaper Parser

Initial Tags
textmate, ruby

Initial Language
Ruby