Class: Postwave::Post
Constant Summary
collapse
- KNOWN_FIELDS =
%w(title date tags title_slug body draft)
- REQUIRED_FIELDS =
%w(title date)
- MEATADATA_DELIMTER =
"---"
- @@markdown =
Redcarpet::Markdown.new(Redcarpet::Render::HTML, fenced_code_blocks: true)
BlogUtilities::CONFIG_FILE_NAME, BlogUtilities::INDEX_FILE_NAME, BlogUtilities::META_DIR, BlogUtilities::POSTS_DIR, BlogUtilities::RSS_FILE_NAME, BlogUtilities::SUMMARY_FILE_NAME, BlogUtilities::TAGS_DIR
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#config_values, #directory_paths, #file_paths, #find_missing_paths, #is_set_up?
Constructor Details
#initialize(file_name, field_content = {}) ⇒ Post
Returns a new instance of Post.
61
62
63
64
65
66
67
68
|
# File 'lib/postwave/post.rb', line 61
def initialize(file_name, field_content = {})
@file_name = file_name
field_content.each do |field, value|
instance_variable_set("@#{field}", value) unless self.instance_variables.include?("@#{field}".to_sym)
self.class.send(:attr_reader, field) unless self.public_methods.include?(field.to_sym)
end
end
|
Instance Attribute Details
#file_name ⇒ Object
Returns the value of attribute file_name.
12
13
14
|
# File 'lib/postwave/post.rb', line 12
def file_name
@file_name
end
|
Class Method Details
.new_from_file_path(path) ⇒ Object
16
17
18
19
20
21
22
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
56
57
58
59
|
# File 'lib/postwave/post.rb', line 16
def self.new_from_file_path(path)
metadata_delimter_count = 0
body_buffer_count = 0
field_content = { "body" => "" }
File.readlines(path).each do |line|
clean_line = line.strip
if clean_line == MEATADATA_DELIMTER
metadata_delimter_count += 1
next
end
if metadata_delimter_count == 0
next
elsif metadata_delimter_count == 1
field, value = clean_line.split(":", 2).map(&:strip)
field_content[field] = value
else
if body_buffer_count == 0
body_buffer_count += 1
next if clean_line.empty?
end
field_content["body"] += "#{line}"
end
end
field_content["date"] = Time.parse(field_content["date"])
if field_content["tags"]
field_content["tags"] = field_content["tags"].split(",").map do |tag|
tag.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
end
if field_content["draft"]
field_content["draft"] = field_content["draft"].downcase == "true"
end
self.new(path, field_content)
end
|
Instance Method Details
#body_html ⇒ Object
82
83
84
|
# File 'lib/postwave/post.rb', line 82
def body_html
@@markdown.render(@body)
end
|
#generated_file_name ⇒ Object
86
87
88
89
|
# File 'lib/postwave/post.rb', line 86
def generated_file_name
"#{@date.to_s[..9]}-#{slug}.md"
end
|
#slug ⇒ Object
74
75
76
|
# File 'lib/postwave/post.rb', line 74
def slug
@slug ||= @title_slug
end
|
#slug=(new_slug) ⇒ Object
78
79
80
|
# File 'lib/postwave/post.rb', line 78
def slug=(new_slug)
@slug = new_slug
end
|
#title_slug ⇒ Object
70
71
72
|
# File 'lib/postwave/post.rb', line 70
def title_slug
@title_slug ||= @title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
|
#update_file_name! ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/postwave/post.rb', line 91
def update_file_name!
desired_file_name = generated_file_name
return false if @file_name == desired_file_name
File.rename(@file_name, File.join(Dir.pwd, POSTS_DIR, desired_file_name))
@file_name = desired_file_name
end
|