-
Notifications
You must be signed in to change notification settings - Fork 1
/
stuff.rb
72 lines (53 loc) · 2.13 KB
/
stuff.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'rubygems'
require 'nokogiri'
require 'erb'
require 'optparse'
class Stuff
TODAY_FOCUS_TYPE = 65536.freeze
DEFAULT_DB_PATH = '~/Library/Application Support/Cultured Code/Things/Database.xml'.freeze
attr_accessor :xml
def initialize(options = {})
@db_path = File.expand_path options[:db_path] || DEFAULT_DB_PATH
@template_path = File.expand_path File.join(File.dirname(__FILE__), '/stuff.html.erb')
@areas = options[:areas]
@xml = Nokogiri::XML(open(@db_path))
@things_by_area = {}
end
def run
@areas.each do |area|
@xml.xpath("//object[@type='TODO']/attribute[text()='#{area}']").each do |node|
children_node = node.parent.xpath("relationship[@name='children']").first
todays_todos = get_todays_todos children_node['idrefs'].split
@things_by_area[area] = todays_todos if todays_todos.size > 0
end
end
puts ERB.new(open(@template_path).read).result(binding)
end
def get_todays_todos(idrefs)
todos = []
idrefs.each do |idref|
attribute = @xml.xpath("//object[@type='TODO'][@id='#{idref}']/attribute[@name='focustype'][text()='#{TODAY_FOCUS_TYPE}']").first
next if !attribute
parent_node = attribute.parent
title_node = parent_node.xpath("attribute[@name='title']").first
completed_node = parent_node.xpath("attribute[@name='datecompleted']").first
description_node = parent_node.xpath("attribute[@name='content']").first
todos << {
:title => title_node.text,
:completed => !completed_node.nil?,
:description => description_node ? description_node.text.gsub('\u3c00note xml:space="preserve"\u3e00', '').gsub('\u3c00/note\u3e00', '').strip : nil
}
end
todos
end
end
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: stuff.rb Area1 Area2 Area3 [options]"
opts.on("-p", "--db-path [PATH]", String, "Path to DB (default: #{Stuff::DEFAULT_DB_PATH})") do |path|
options[:db_path] = path
end
end
parser.parse!
options = { areas: ARGV }.merge(options)
options.empty? ? puts(parser.help) : Stuff.new(options).run