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
|
# File 'lib/post-modern/read.rb', line 23
def read(file, locals = {})
match, categories, date, slug, ext = *file.gsub("#{root_dir}/", "").match(/^(.+\/)*(?:(\d+-\d+-\d+)-)?(.*)(\.[^.]+)$/)
content = IO.read(file)
unless locals[:filters].present?
locals[:filters] = [:haml, :highlight, :pretty]
else
locals[:filters] = Array(locals[:filters])
end
if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
data = YAML.load($1).symbolize_keys
content = content[($1.size + $2.size)..-1]
else
data = {}
end
data[:published_at] ||= date if date.present?
data[:format] = ext.split(".").last
data[:title] ||= slug.titleize if slug.present?
data[:tags] = data[:tags].present? ? data[:tags].split(/,\s*/) : []
data[:keywords] = data[:keywords].present? ? data[:keywords].split(/,\s*/) : []
data[:slug] ||= slug
data[:file] = file.gsub("#{root_dir}/", "")
[:published_at, :edited_at].each do |date|
data[date] = Time.parse(data[date].to_s) if data[date].present?
end
data[:categories] = categories.split("/").select(&:present?) if categories
data[:published] = false unless data[:published].present?
data[:content] = render(content, data.merge(locals))
data
end
|