Class: Ottogen::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/ottogen/post.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

POSTS_DIR =
'_posts'
DRAFTS_DIR =
'_drafts'
FILENAME_PATTERN =
/\A(\d{4})-(\d{2})-(\d{2})-(.+)\.adoc\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, date:, slug:, front_matter:, body:) ⇒ Post

Returns a new instance of Post.



55
56
57
58
59
60
61
# File 'lib/ottogen/post.rb', line 55

def initialize(path:, date:, slug:, front_matter:, body:)
  @path = path
  @date = date
  @slug = slug
  @front_matter = front_matter
  @body = body
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



101
102
103
104
105
106
# File 'lib/ottogen/post.rb', line 101

def method_missing(name, *args)
  key = name.to_s
  return @front_matter[key] if @front_matter.key?(key)

  super
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



52
53
54
# File 'lib/ottogen/post.rb', line 52

def body
  @body
end

#dateObject (readonly)

Returns the value of attribute date.



52
53
54
# File 'lib/ottogen/post.rb', line 52

def date
  @date
end

#front_matterObject (readonly)

Returns the value of attribute front_matter.



52
53
54
# File 'lib/ottogen/post.rb', line 52

def front_matter
  @front_matter
end

#pathObject (readonly)

Returns the value of attribute path.



52
53
54
# File 'lib/ottogen/post.rb', line 52

def path
  @path
end

Returns the value of attribute permalink.



53
54
55
# File 'lib/ottogen/post.rb', line 53

def permalink
  @permalink
end

#slugObject (readonly)

Returns the value of attribute slug.



52
53
54
# File 'lib/ottogen/post.rb', line 52

def slug
  @slug
end

Class Method Details

.discover(dir = POSTS_DIR) ⇒ Object



40
41
42
43
44
# File 'lib/ottogen/post.rb', line 40

def self.discover(dir = POSTS_DIR)
  return [] unless Dir.exist?(dir)

  Dir.glob(File.join(dir, '*.adoc')).map { |path| read(path) }
end

.discover_drafts(dir = DRAFTS_DIR) ⇒ Object



46
47
48
49
50
# File 'lib/ottogen/post.rb', line 46

def self.discover_drafts(dir = DRAFTS_DIR)
  return [] unless Dir.exist?(dir)

  Dir.glob(File.join(dir, '*.adoc')).map { |path| read_draft(path) }
end

.read(path) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ottogen/post.rb', line 15

def self.read(path)
  date, slug = parse_filename(path)
  front_matter, body = FrontMatter.split(File.read(path), path)
  new(path: path, date: date, slug: slug, front_matter: front_matter, body: body)
rescue FrontMatter::Error => e
  raise Error, e.message
end

.read_draft(path) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/ottogen/post.rb', line 23

def self.read_draft(path)
  slug = File.basename(path, '.adoc')
  front_matter, body = FrontMatter.split(File.read(path), path)
  new(path: path, date: Date.today, slug: slug, front_matter: front_matter, body: body)
rescue FrontMatter::Error => e
  raise Error, e.message
end

Instance Method Details

#asciidoctor_attributesObject



87
88
89
90
91
92
93
94
95
# File 'lib/ottogen/post.rb', line 87

def asciidoctor_attributes
  attrs = @front_matter.transform_keys { |key| "page_#{key}" }
  attrs['page_title'] ||= title
  attrs['page_date'] = date.iso8601
  attrs['page_slug'] = slug
  attrs['page_url'] = url
  attrs.delete('page_permalink')
  attrs
end

#categoriesObject



71
72
73
# File 'lib/ottogen/post.rb', line 71

def categories
  Array(@front_matter['categories'])
end

#output_path(build_dir) ⇒ Object



81
82
83
84
85
# File 'lib/ottogen/post.rb', line 81

def output_path(build_dir)
  return @permalink.output_path(build_dir) if @permalink

  File.join(build_dir, "#{slug}.html")
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/ottogen/post.rb', line 97

def respond_to_missing?(name, include_private = false)
  name == :title || name == :url || @front_matter.key?(name.to_s) || super
end

#tagsObject



67
68
69
# File 'lib/ottogen/post.rb', line 67

def tags
  Array(@front_matter['tags'])
end

#titleObject



63
64
65
# File 'lib/ottogen/post.rb', line 63

def title
  @front_matter['title'] || slug.split('-').map(&:capitalize).join(' ')
end

#urlObject



75
76
77
78
79
# File 'lib/ottogen/post.rb', line 75

def url
  return @permalink.url if @permalink

  "/#{slug}.html"
end