Class: Postwave::Post

Inherits:
Object
  • Object
show all
Includes:
BlogUtilities
Defined in:
lib/postwave/post.rb

Constant Summary collapse

KNOWN_FIELDS =
%w(title date tags title_slug body draft)
REQUIRED_FIELDS =
%w(title date)
METADATA_DELIMITER =
"---"
FILE_NAME_DATE_LEN =

YYYY-MM-DD-

11
@@markdown =
Redcarpet::Markdown.new(Redcarpet::Render::HTML, fenced_code_blocks: true)

Constants included from BlogUtilities

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

Methods included from BlogUtilities

#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_nameObject

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)
   = 0
  body_buffer_count = 0
  field_content = { "body" => "" }

  File.readlines(path).each do |line|
    clean_line = line.strip
    if clean_line == 
       += 1
      next
    end

    if  == 0
      next
    elsif  == 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

  # turn "date" into a Time object
  field_content["date"] = Time.parse(field_content["date"])

  # turn "tags" into an array
  if field_content["tags"]
    field_content["tags"] = field_content["tags"].split(",").map do |tag|
      tag.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
    end
  end

  # turn "draft" into boolean
  if field_content["draft"]
    field_content["draft"] = field_content["draft"].downcase == "true"
  end

  self.new(path, field_content)
end

Instance Method Details

#body_htmlObject



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

  # Turn common block boundaries into newlines first
  text = text.gsub(/<(br|\/p|\/div|\/h\d|\/li)[^>]*>/i, "\n")
  # Strip remaining tags
  text = text.gsub(/<[^>]*>/, "")
  # Decode entities
  text = CGI.unescapeHTML(text)
  # Collapse spaces but keep single newlines
  text = text.gsub(/[ \t]+/, " ").gsub(/\n+/, "\n").strip

  # Unicode-safe character counting (grapheme clusters)
  graphemes = text.scan(/\X/)
  return text if graphemes.length <= limit

  candidate = graphemes.first(limit).join

  # If we cut mid-word, trim back to the previous word boundary.
  # Word characters = letters, numbers, marks, connector punctuation (e.g., underscore)
  # This removes a trailing partial word if present.
  cut = candidate.sub(/[\p{L}\p{N}\p{M}\p{Pc}]+\z/u, "").rstrip

  # If the very first "word" exceeds the limit, we err under limit and just show an ellipsis.
  cut.empty? ? ellipsis : "#{cut}#{ellipsis}"
end

#file_name_slugObject



78
79
80
81
# File 'lib/postwave/post.rb', line 78

def file_name_slug
  # YYYY-MM-DD-slug.md
  File.basename(@file_name, ".md")[FILE_NAME_DATE_LEN..]
end

#generated_file_nameObject



122
123
124
125
# File 'lib/postwave/post.rb', line 122

def generated_file_name
  # YYYY-MM-DD-slug-from-title.md
  "#{@date.to_s[..9]}-#{slug}.md"
end

#slugObject



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_slugObject



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