Fixed metadata handling.
This commit is contained in:
parent
5ab65ff1e7
commit
bb110bb05a
2 changed files with 12 additions and 17 deletions
1
bin/ace
1
bin/ace
|
@ -43,7 +43,6 @@ rules.rules.each do |klass, files|
|
||||||
if File.binread(file).match(/^-{3,5}\s*$/) # TODO: this should be a filter or lazy-loaded
|
if File.binread(file).match(/^-{3,5}\s*$/) # TODO: this should be a filter or lazy-loaded
|
||||||
puts "~ Read #{file} with parse"
|
puts "~ Read #{file} with parse"
|
||||||
raw_item = Ace::RawItem.new(file).tap(&:parse)
|
raw_item = Ace::RawItem.new(file).tap(&:parse)
|
||||||
raw_item.check_metadata_created_at(file)
|
|
||||||
item = klass.create(raw_item.metadata, raw_item.content)
|
item = klass.create(raw_item.metadata, raw_item.content)
|
||||||
else
|
else
|
||||||
puts "~ Read #{file} without parse"
|
puts "~ Read #{file} without parse"
|
||||||
|
|
28
lib/ace.rb
28
lib/ace.rb
|
@ -21,28 +21,24 @@ module Ace
|
||||||
class RawItem
|
class RawItem
|
||||||
attr_accessor :path, :metadata, :content
|
attr_accessor :path, :metadata, :content
|
||||||
def initialize(path)
|
def initialize(path)
|
||||||
|
@path = path
|
||||||
@data = File.read(path)
|
@data = File.read(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_metadata_created_at(path)
|
|
||||||
if self.metadata[:title]
|
|
||||||
year, month, day = File.basename(path).slice(0,10).split('-')
|
|
||||||
self.metadata[:created_at] ||= Date.new(year.to_i, month.to_i, day.to_i)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse
|
def parse
|
||||||
pieces = @data.split(/^-{3,5}\s*$/)
|
pieces = @data.split(/^-{3,5}\s*$/)
|
||||||
# if pieces.size < 3
|
if pieces.length == 1 || @data.empty?
|
||||||
# raise RuntimeError.new(
|
self.metadata = Hash.new
|
||||||
# "The file '#{path}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format."
|
self.content = pieces.first
|
||||||
# )
|
else
|
||||||
# end
|
self.metadata = begin
|
||||||
|
YAML.load(pieces[1]).inject(Hash.new) { |metadata, pair| metadata.merge(pair[0].to_sym => pair[1]) } || Hash.new
|
||||||
|
end
|
||||||
|
self.content = pieces[2..-1].join.strip
|
||||||
|
end
|
||||||
|
|
||||||
# Parse
|
self.metadata[:created_at] ||= File.ctime(self.path)
|
||||||
self.metadata = YAML.load(pieces[1]).inject(Hash.new) { |metadata, pair| metadata.merge(pair[0].to_sym => pair[1]) } || Hash.new
|
self.metadata[:updated_at] ||= File.mtime(self.path)
|
||||||
# TODO: check metadata[:created_at] and supply it from filename
|
|
||||||
self.content = pieces[2..-1].join.strip
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue