Class: Postwave::Post
Constant Summary
collapse
- KNOWN_FIELDS =
%w(title date tags title_slug body draft)
- REQUIRED_FIELDS =
%w(title date)
- METADATA_DELIMITER =
"---"
- FILE_NAME_DATE_LEN =
11
- @@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.
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/postwave/post.rb', line 63
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
@slug = file_name_slug&.empty? ? title_slug : file_name_slug
end
|
Instance Attribute Details
#file_name ⇒ Object
Returns the value of attribute file_name.
14
15
16
|
# File 'lib/postwave/post.rb', line 14
def file_name
@file_name
end
|
Class Method Details
.new_from_file_path(path) ⇒ Object
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
60
61
|
# File 'lib/postwave/post.rb', line 18
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 == METADATA_DELIMITER
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
91
92
93
|
# File 'lib/postwave/post.rb', line 91
def body_html
@body_html ||= @@markdown.render(@body)
end
|
#body_preview(limit = 100, ellipsis = "...") ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/postwave/post.rb', line 95
def body_preview(limit = 100, ellipsis = "...")
text = body_html.to_s
text = text.gsub(/<(br|\/p|\/div|\/h\d|\/li)[^>]*>/i, "\n")
text = text.gsub(/<[^>]*>/, "")
text = CGI.unescapeHTML(text)
text = text.gsub(/[ \t]+/, " ").gsub(/\n+/, "\n").strip
graphemes = text.scan(/\X/)
return text if graphemes.length <= limit
candidate = graphemes.first(limit).join
cut = candidate.sub(/[\p{L}\p{N}\p{M}\p{Pc}]+\z/u, "").rstrip
cut.empty? ? ellipsis : "#{cut}#{ellipsis}"
end
|
#file_name_slug ⇒ Object
78
79
80
81
|
# File 'lib/postwave/post.rb', line 78
def file_name_slug
File.basename(@file_name, ".md")[FILE_NAME_DATE_LEN..]
end
|
#generated_file_name ⇒ Object
122
123
124
125
|
# File 'lib/postwave/post.rb', line 122
def generated_file_name
"#{@date.to_s[..9]}-#{slug}.md"
end
|
#slug ⇒ Object
83
84
85
|
# File 'lib/postwave/post.rb', line 83
def slug
@slug ||= @title_slug
end
|
#slug=(new_slug) ⇒ Object
87
88
89
|
# File 'lib/postwave/post.rb', line 87
def slug=(new_slug)
@slug = new_slug
end
|
#title_slug ⇒ Object
74
75
76
|
# File 'lib/postwave/post.rb', line 74
def title_slug
@title_slug ||= @title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
|
#update_file_name! ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/postwave/post.rb', line 127
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
|