Posted By

dbug13 on 03/27/09


Tagged

textmate ruby parser


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

webstic
dbald


TaskPaper Parser


 / Published in: Ruby
 

  1. #!/usr/bin/env ruby
  2.  
  3. require 'pp'
  4.  
  5. module TaskPaper
  6. class TaskParser
  7. attr_reader :task_string, :tags
  8.  
  9. def initialize(&observer)
  10. @observers = []
  11. @tags = []
  12. @task_string = ""
  13.  
  14. if block_given?
  15. @observers << observer
  16. end
  17. end
  18.  
  19. def on_task_parsed(&observer)
  20. @observers << observer
  21. end
  22.  
  23. def parseTask(taskdata)
  24. @tags = []
  25. $task_string = ""
  26. matches = /^((-)*(.*?))((@.*)*)*$/.match(taskdata)
  27. @task_string = matches[1]
  28. tag_string = matches[4]
  29.  
  30. if tag_string
  31. @tags = tag_string.split(/[,\s]+/)
  32. end
  33.  
  34. @observers.each do |observer|
  35. observer.call(self)
  36. end
  37.  
  38. end
  39. end
  40.  
  41. class Task
  42. attr_accessor :tags, :task_string
  43.  
  44. def initialize(task_string_data="", task_tag_array=[])
  45. @task_string = task_string_data
  46. @tags = task_tag_array
  47. end
  48.  
  49. def to_s
  50. tags = ""
  51. tags = @tags.join(" ") if @tags.length > 0
  52. "#{@task_string} #{tags}"
  53. end
  54.  
  55. def empty?
  56. @task_string.strip.empty?
  57. end
  58.  
  59. def is_project?
  60. @task_string =~ /.*:$/
  61. end
  62.  
  63. def is_task?
  64. @task_string =~ /^-.*/
  65. end
  66.  
  67. def is_comment?
  68. !empty? && !is_task? && !is_project?
  69. end
  70.  
  71. end
  72. end
  73.  
  74. # task_list = []
  75. #
  76. # t = TaskPaper::TaskParser.new do |task_data|
  77. # task = TaskPaper::Task.new(task_data.task_string, task_data.tags)
  78. # task_list << task
  79. # puts task
  80. # puts "Is a Task" if task.is_task?
  81. # puts "Is a Comment" if task.is_comment?
  82. # puts "Is a Project" if task.is_project?
  83. # puts "Is Empty" if task.empty?
  84. # puts task.tags.inspect if task.tags.length > 0
  85. # puts
  86. # end
  87. #
  88. # tasks = %{Chores:
  89. # - Take out the trash @home
  90. # only do this if it's not raining
  91. #
  92. #
  93. # - Buy a new door knob @store @hardware
  94. # }
  95. #
  96. #
  97. # tasks.split(/\n/).each do |line|
  98. # t.parseTask(line)
  99. # end
  100. #
  101. # pp task_list

Report this snippet  

You need to login to post a comment.