Created
January 18, 2013 05:36
-
-
Save jerico/4562584 to your computer and use it in GitHub Desktop.
dayone-ruby.rb
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 | |
# Requires chronic and trollop: gem install chronic trollop | |
# I mapped it to 'j' by adding 'alias j=~/path/dayone-ruby.rb' to .bashrc | |
# j --date "yesterday" --starred "entry text" | |
# j -d "4pm" -s "entry text" | |
# j "entry text" | |
# j -f textfile.txt | |
require 'rexml/document' | |
require 'securerandom' | |
require 'time' | |
require 'chronic' | |
require 'trollop' | |
opts = Trollop::options do | |
opt :date, "Use custom date", type: String | |
opt :starred, "Mark entry as starred" | |
opt :file, "Use a files as an input", type: String | |
end | |
dayone_path = File.expand_path('~/Dropbox/Apps/Day One/Journal.dayone/entries/') | |
#dayone_path = File.expand_path('./') | |
uuid = SecureRandom.uuid.upcase.gsub(/-/, '') | |
date = Time.new | |
if !opts[:date].nil? | |
date = Chronic.parse(opts[:date]).utc.iso8601 | |
else | |
date = Time.now.utc.iso8601 | |
end | |
starred = opts[:starred] | |
if opts[:file] | |
entry = File.read(opts[:file]) | |
elsif ARGV.empty? | |
system "$EDITOR ./#{uuid}.doentry" | |
entry = File.read("./#{uuid}.doentry") | |
else | |
entry = ARGV.join(" ") | |
end | |
tags_re = /\B#([a-zA-Z_0-9]+{2,})/ | |
tags = "" | |
entry.scan(tags_re).each do |tag| | |
tags << "\n <string>#{tag.first}</string>" | |
end | |
all_tags = "" | |
if !tags.empty? | |
all_tags = "\n <key>Tags</key>\n <array>#{tags}\n </array>" | |
end | |
dayone_template = <<DAYONE | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Creation Date</key> | |
<date>#{date}</date> | |
<key>Entry Text</key> | |
<string>#{entry}</string> | |
<key>Starred</key> | |
<#{starred.to_s}/>#{all_tags.chomp} | |
<key>Time Zone</key> | |
<string>Asia/Manila</string> | |
<key>UUID</key> | |
<string>#{uuid}</string> | |
</dict> | |
</plist> | |
DAYONE | |
xml_doc = REXML::Document.new(dayone_template) | |
File.open("#{dayone_path}/#{uuid}.doentry", 'w') do |file| | |
file.write(xml_doc) | |
file.close | |
end | |
if ARGV.empty? && !opts[:file] | |
File.delete("./#{uuid}.doentry") | |
end | |
puts dayone_path | |
puts uuid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment