2018-04-10 18:11:33 +02:00
|
|
|
class Post
|
|
|
|
@@posts = nil
|
|
|
|
|
2019-12-07 10:31:19 +01:00
|
|
|
attr_reader :year , :month , :day , :ext
|
2018-04-10 18:11:33 +02:00
|
|
|
|
|
|
|
def initialize(path)
|
2019-12-07 10:31:19 +01:00
|
|
|
year , rest = *path.split("/")
|
|
|
|
@year = parse_int(year , 2100 , "year")
|
|
|
|
base , @ext = File.basename(rest).split(".")
|
2018-04-10 19:00:56 +02:00
|
|
|
raise "must be partial, start with _ not:#{base}" unless base[0] == "_"
|
2018-04-10 18:11:33 +02:00
|
|
|
@words = base.split("-")
|
|
|
|
raise "Invalid path #{path}" unless @words
|
2019-12-07 10:31:19 +01:00
|
|
|
@month = parse_int(@words.shift[1 .. -1] , 12 , "month")
|
|
|
|
@day = parse_int(@words.shift , 32 , "week")
|
2018-04-10 18:11:33 +02:00
|
|
|
end
|
2018-04-10 19:00:56 +02:00
|
|
|
|
2018-04-10 18:11:33 +02:00
|
|
|
def slug
|
|
|
|
@words.join("-").downcase
|
|
|
|
end
|
|
|
|
def title
|
|
|
|
@words.join(" ")
|
|
|
|
end
|
|
|
|
def template_name
|
2019-12-07 10:31:19 +01:00
|
|
|
"#{year}/#{month.to_s.rjust(2, '0')}-#{day.to_s.rjust(2, '0')}-#{@words.join("-")}"
|
|
|
|
end
|
|
|
|
def template_file
|
|
|
|
"#{year}/_#{month.to_s.rjust(2, '0')}-#{day.to_s.rjust(2, '0')}-#{@words.join("-")}"
|
2018-04-10 18:11:33 +02:00
|
|
|
end
|
|
|
|
def date
|
2018-04-22 12:35:13 +02:00
|
|
|
Date.new(year,month,day)
|
2018-04-10 18:11:33 +02:00
|
|
|
end
|
2019-12-07 10:31:19 +01:00
|
|
|
def parse_int( value , max , name)
|
2018-04-10 18:11:33 +02:00
|
|
|
ret = value.to_i
|
2019-12-07 10:31:19 +01:00
|
|
|
raise "invalid #{name} #{value} > #{max}" if ret > max or ret < 1
|
2018-04-10 18:11:33 +02:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
def content
|
2019-12-07 10:31:19 +01:00
|
|
|
File.open("#{self.class.blog_path}/#{template_file}.#{ext}" ).read
|
2018-04-10 18:11:33 +02:00
|
|
|
end
|
|
|
|
def summary
|
|
|
|
ret = content.split("%h2").first.gsub("%p", "<br/>").html_safe
|
|
|
|
ret[0 .. 400]
|
|
|
|
end
|
|
|
|
def self.posts
|
|
|
|
return @@posts if @@posts
|
2018-04-10 21:30:17 +02:00
|
|
|
posts ={}
|
2019-12-07 10:31:19 +01:00
|
|
|
Dir["#{self.blog_path}/2*/_*.haml"].reverse.each do |file|
|
|
|
|
parts = file.split("/")
|
|
|
|
post_name = parts[-2] + "/" + parts[-1]
|
|
|
|
post = Post.new(post_name)
|
2018-04-10 21:30:17 +02:00
|
|
|
posts[post.slug] = post
|
2018-04-10 18:11:33 +02:00
|
|
|
end
|
2018-04-10 21:30:17 +02:00
|
|
|
@@posts = posts.sort_by { |slug, post| post.sort_key }.reverse.to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_key
|
2019-12-07 10:31:19 +01:00
|
|
|
year*1000 + month*10 + day
|
2018-04-10 18:11:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.blog_path
|
|
|
|
Rails.configuration.blog_path
|
|
|
|
end
|
|
|
|
end
|