require 'time' require 'nukumi2/entry' class FileBackend class Parser def self.parse(io) entry = Nukumi2::Entry.new fields = {} while line = io.gets line.chomp! break if line.empty? if line =~ /^(\w\S+?):\s*(.*)/ field = $1.downcase fields[field] = $2 elsif field fields[field] << " " << line.lstrip end end fields.each { |field, value| case field when "category" entry.categories.concat value.split.map { |c| c.strip } when "date" entry.time = Time.parse value when "subject" entry.title = value else if entry.respond_to? field + "=" entry.send field + "=", value else entry.fields[field] = value end end } entry.content = io.read entry end end def self.load(file) file = file.backend_data if file.kind_of? Nukumi2::Entry File.open(file) { |handle| entry = Parser.parse handle entry.change_time = handle.mtime entry.backend = self entry.backend_data = File.expand_path(file) entry } end def self.outdated?(entry) File.mtime(entry.backend_data) != entry.change_time end def initialize(dir) @dir = File.expand_path dir end def get Dir[File.join(@dir, "**/*")].map { |file| if File.file? file and File.readable? file if file =~ /~$/ # Backup file? nil else self.class.load file end end }.compact.uniq end end