概要
TODOフォーマットからJSONへの変換
内容
TODOフォーマットからJSONへの変換をします。
TODOフォーマット例
20130906_todo.yml
title: 2013/09/05 contents: - 準備,日次 作業準備 アクティビティの割り当て メールチェック,0.5,2013/09/06 01:32:20,2013/09/06 01:48:40,0:16:20 - アルゴリズム,アルゴリズム クイックソート,2,2013/09/06 01:48:42,2013/09/06 02:00:24,0:11:42 - 学習管理,月次集計機能要件定義、ストーリー作成,2,2013/09/06 02:00:24,2013/09/06 02:05:38,0:05:14 - 片付,日次 作業まとめ,0.5,2013/09/06 02:18:44,2013/09/06 02:20:32,0:01:48
TODO履歴ファイルのフォルダ構成
TODOは下記のようなフォルダ構成で保存されている。
勉強しなかった日はファイル自体がない
└─2013 │ ├─06 │ 20130609_todo.yml │ : │ 20130630_todo.yml │ ├─07 │ 20130701_todo.yml │ : │ 20130731_todo.yml │ ├─08 │ 20130801_todo.yml │ : │ 20130831_todo.yml │ └─09 20130901_todo.yml : 20130906_todo.yml
ソース
# encoding: utf-8 require "./env" require "yaml" require 'json' require "pp" class StudyLog2Json attr_accessor :ym, :year, :month COLUMNS = [:id, :todo_date, :subject, :summary, :estimate, :start, :end, :total] class << self def check_args if $*.size == 0 puts "1 args need" exit end unless $*[0] =~ /^\d{6}$/ puts "args have to yyyymm format" exit end @ym = $*[0] @year = $*[0].slice(0, 4) @month = $*[0].slice(4, 2) end def convert(env) files = get_files(env) todos = [] files.each do |file_name| contents = File.open(file_name) {|f|f.read} yaml = YAML.load contents yaml["contents"].each_with_index {|todo, i|todos << todo_to_hash(todo, yaml["title"], i)} end json_todos = todos.to_json json_todos = "var todos_#{@ym} = '#{json_todos}';" File.open(get_output_js(files[0]), "w") {|f|f.puts json_todos} end private def get_files(env) files = [] Dir.glob("#{TODO_PATH}/#{@ym[0..3]}/#{@ym[4..6]}/*.yml") {|f|files << f} files end def todo_to_hash(todo, todo_date, index) todo_hash = {} todo_hash[COLUMNS[0]] = "#{todo_date.gsub('/', '')}_#{(index + 1).to_s}" todo_hash[COLUMNS[1]] = todo_date todo.split(",").each_with_index {|v, i|todo_hash[COLUMNS[i + 2]] = v} todo_hash end def get_output_js(file_name) dirname = File.dirname(file_name) dir_path = dirname.slice(0, dirname.size - 2) dir_path + @ym + ".js" end end end StudyLog2Json.check_args StudyLog2Json.convert TODO_PATH
設定ファイル
TODOフォルダのルートディレクトリへのパスを記載します。
env.rb
TODO_PATH = 'C:/・・(任意のパス)・・/todo'
テスト
テスト入力サンプル
20130801_todo.yml
title: 2013/08/01 contents: - 準備,日次 作業準備 アクティビティの割り当て メールチェック,0.5,2013/08/02 23:49:00,2013/08/03 00:26:37,0:37:37 - Rails,カスタムダイアログの作成,1,2013/08/03 00:26:38,2013/08/03 01:22:51,0:56:13 - 片付,日次 作業まとめ,0.5,2013/08/03 01:23:08,2013/08/03 01:30:23,0:07:15
20130803_todo.yml
title: 2013/08/03 contents: - 準備,日次 作業準備 アクティビティの割り当て メールチェック,0.5,2013/08/03 20:29:36,2013/08/03 20:47:04,0:17:28 - Rails,JavaScriptをCoffee Scriptに乗せ替える,1,2013/08/03 21:02:43,2013/08/04 00:00:56,2:58:13 - Rails,===でエスケープなしの出力,1,2013/08/04 00:01:07,2013/08/04 00:13:50,0:12:43 - Rails,JavaScriptをCoffee Scriptに乗せ替える,1,2013/08/04 00:13:50,2013/08/04 01:02:03,0:48:13 - Rails,document.readyをcoffee scriptで記述,1,2013/08/04 01:02:03,2013/08/04 01:23:04,0:21:01 - その他,メール,0.5,2013/08/04 02:10:28,2013/08/04 02:12:06,0:01:38 - 片付,日次 作業まとめ,0.5,2013/08/04 02:12:08,2013/08/04 02:13:42,0:01:34
テスト出力サンプル
※便宜上サンプルは改行してますが、実際は改行してません。
201308.js
var todos_201308 = '[ {"id":"20130801_1","todo_date":"2013/08/01","subject":"準備","summary":"日次 作業準備 アクティビティの割り当て メールチェック","estimate":"0.5","start":"2013/08/02 23:49:00","end":"2013/08/03 00:26:37","total":"0:37:37"} ,{"id":"20130801_2","todo_date":"2013/08/01","subject":"Rails","summary":"カスタムダイアログの作成","estimate":"1","start":"2013/08/03 00:26:38","end":"2013/08/03 01:22:51","total":"0:56:13"} ,{"id":"20130801_3","todo_date":"2013/08/01","subject":"片付","summary":"日次 作業まとめ","estimate":"0.5","start":"2013/08/03 01:23:08","end":"2013/08/03 01:30:23","total":"0:07:15"} ,{"id":"20130803_1","todo_date":"2013/08/03","subject":"準備","summary":"日次 作業準備 アクティビティの割り当て メールチェック","estimate":"0.5","start":"2013/08/03 20:29:36","end":"2013/08/03 20:47:04","total":"0:17:28"} ,{"id":"20130803_2","todo_date":"2013/08/03","subject":"Rails","summary":"JavaScriptをCoffee Scriptに乗せ替える","estimate":"1","start":"2013/08/03 21:02:43","end":"2013/08/04 00:00:56","total":"2:58:13"} ,{"id":"20130803_3","todo_date":"2013/08/03","subject":"Rails","summary":"===でエスケープなしの出力","estimate":"1","start":"2013/08/04 00:01:07","end":"2013/08/04 00:13:50","total":"0:12:43"} ,{"id":"20130803_4","todo_date":"2013/08/03","subject":"Rails","summary":"JavaScriptをCoffee Scriptに乗せ替える","estimate":"1","start":"2013/08/04 00:13:50","end":"2013/08/04 01:02:03","total":"0:48:13"} ,{"id":"20130803_5","todo_date":"2013/08/03","subject":"Rails","summary":"document.readyをcoffee scriptで記述","estimate":"1","start":"2013/08/04 01:02:03","end":"2013/08/04 01:23:04","total":"0:21:01"} ,{"id":"20130803_6","todo_date":"2013/08/03","subject":"その他","summary":"メール","estimate":"0.5","start":"2013/08/04 02:10:28","end":"2013/08/04 02:12:06","total":"0:01:38"} ,{"id":"20130803_7","todo_date":"2013/08/03","subject":"片付","summary":"日次 作業まとめ","estimate":"0.5","start":"2013/08/04 02:12:08","end":"2013/08/04 02:13:42","total":"0:01:34"} ]';