Class: SimplePosts::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_posts/posts.rb

Constant Summary collapse

DATE_REGEX =
/\d{4}-\d{2}-\d{2}/
SHORT_DATE_FORMAT =
"%Y-%m-%d"
DATE_FORMAT =
"%Y-%m-%d %H:%M:%S"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, renderer) ⇒ Post

Returns a new instance of Post.



151
152
153
154
155
156
157
# File 'lib/simple_posts/posts.rb', line 151

def initialize(filename, renderer)
  @file = filename
  @original_filename = filename
  @name = self.class.normalize_name(filename)
  @renderer = renderer
  parse_post
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



149
150
151
# File 'lib/simple_posts/posts.rb', line 149

def body
  @body
end

#docidObject

Returns the value of attribute docid.



148
149
150
# File 'lib/simple_posts/posts.rb', line 148

def docid
  @docid
end

#headersObject (readonly)

Returns the value of attribute headers.



149
150
151
# File 'lib/simple_posts/posts.rb', line 149

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



149
150
151
# File 'lib/simple_posts/posts.rb', line 149

def name
  @name
end

#original_bodyObject (readonly)

Returns the value of attribute original_body.



149
150
151
# File 'lib/simple_posts/posts.rb', line 149

def original_body
  @original_body
end

#original_filenameObject (readonly)

Returns the value of attribute original_filename.



149
150
151
# File 'lib/simple_posts/posts.rb', line 149

def original_filename
  @original_filename
end

Class Method Details

.normalize_name(post) ⇒ Object



172
173
174
# File 'lib/simple_posts/posts.rb', line 172

def self.normalize_name(post)
  return post.downcase.strip.sub(/\.(html|md|pdf)(\.erb)?$/,'').sub(/\d{4}-\d{2}-\d{2}-/, '')
end

Instance Method Details

#[](key) ⇒ Object



227
228
229
# File 'lib/simple_posts/posts.rb', line 227

def [](key)
  return @headers[key]
end


239
240
241
242
243
244
245
# File 'lib/simple_posts/posts.rb', line 239

def alternate_links
  if @headers.has_key?('alternate_links')
    return @headers['alternate_links'].split(/\s+/)
  else
    return []
  end
end

#contentsObject



212
213
214
215
216
# File 'lib/simple_posts/posts.rb', line 212

def contents
  @contents ||= File.open(filename, 'r:utf-8') do |file|
    file.read
  end
end

#dateObject



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/simple_posts/posts.rb', line 263

def date
  if is_blog_post?
    if @headers['date']
      Time.strptime(@headers['date'], DATE_FORMAT)
    else
      Time.strptime(@file, SHORT_DATE_FORMAT)
    end
  elsif @headers['date']
    Time.strptime(@headers['date'], SHORT_DATE_FORMAT)
  end
end

#filenameObject



218
219
220
# File 'lib/simple_posts/posts.rb', line 218

def filename
  Rails.root.join("app", "posts", @file).to_s
end

#has_tag(tag) ⇒ Object



247
248
249
# File 'lib/simple_posts/posts.rb', line 247

def has_tag(tag)
  tags.detect { |t| t == tag }
end

#html_pathObject



291
292
293
# File 'lib/simple_posts/posts.rb', line 291

def html_path
  "/blog/#{@name}"
end

#idObject



259
260
261
# File 'lib/simple_posts/posts.rb', line 259

def id
  post_id
end

#id_matches?(id) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
206
# File 'lib/simple_posts/posts.rb', line 203

def id_matches?(id)
  links = alternate_links + [self.post_id]
  links.include? id
end

#is_blog_post?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/simple_posts/posts.rb', line 176

def is_blog_post?
  return filename =~ DATE_REGEX
end

#is_erb?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/simple_posts/posts.rb', line 195

def is_erb?
  original_filename.end_with?('.erb')
end

#is_html?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/simple_posts/posts.rb', line 199

def is_html?
  original_filename =~ /\.html(\.erb)?$/
end

#layoutObject



299
300
301
# File 'lib/simple_posts/posts.rb', line 299

def layout
  @headers.has_key?('layout') ? @headers['layout'].to_sym : nil
end

#matches_path(path) ⇒ Object



222
223
224
225
# File 'lib/simple_posts/posts.rb', line 222

def matches_path(path)
  normalized = self.class.normalize_name(path)
  return @name == normalized || @headers['id'] == normalized
end

#natural_dateObject



279
280
281
# File 'lib/simple_posts/posts.rb', line 279

def natural_date
  date ? date.strftime("%e %B %Y") : ''
end

#parse_body(body_text) ⇒ Object



166
167
168
169
170
# File 'lib/simple_posts/posts.rb', line 166

def parse_body(body_text)
  @original_body = body_text
  @before_fold, after_fold = body_text.split("--fold--")
  @body = body_text.sub("--fold--", '')
end

#parse_postObject



159
160
161
162
163
164
# File 'lib/simple_posts/posts.rb', line 159

def parse_post
  if contents =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
    @headers = YAML.load($1)
    parse_body($3)
  end
end

#post_idObject



255
256
257
# File 'lib/simple_posts/posts.rb', line 255

def post_id
  @headers['id']
end

#reading_timeObject



275
276
277
# File 'lib/simple_posts/posts.rb', line 275

def reading_time
  ([body.split(/\s+/).length / 180.0, 1].max).to_i
end

#render(renderer = nil, app = nil) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/simple_posts/posts.rb', line 180

def render(renderer=nil, app=nil)
  content = is_erb? ? render_erb(@body, app) : @body
  if is_html?
    content
  else
    (renderer || @renderer).render(content)
  end
end

#render_before_foldObject



208
209
210
# File 'lib/simple_posts/posts.rb', line 208

def render_before_fold
  @renderer.render(@before_fold)
end

#render_erb(content, app) ⇒ Object



189
190
191
192
193
# File 'lib/simple_posts/posts.rb', line 189

def render_erb(content, app)
  template = ERB.new(content)
  @app = app
  template.result(binding)
end

#short_dateObject



283
284
285
# File 'lib/simple_posts/posts.rb', line 283

def short_date
  date ? date.strftime("%e %b %Y") : ''
end

#show_byline?Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/simple_posts/posts.rb', line 303

def 
  is_blog_post?
end

#tagsObject



231
232
233
234
235
236
237
# File 'lib/simple_posts/posts.rb', line 231

def tags
  if @headers.has_key?('tags')
    return @headers['tags'].split(/,\s+/)
  else
    return []
  end
end

#titleObject



251
252
253
# File 'lib/simple_posts/posts.rb', line 251

def title
  @headers['title']
end

#topicObject



287
288
289
# File 'lib/simple_posts/posts.rb', line 287

def topic
  headers['topic']
end

#viewObject



295
296
297
# File 'lib/simple_posts/posts.rb', line 295

def view
  @headers.has_key?('view') ? @headers['view'].to_sym : nil
end