Class: Florby::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/florby/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:) ⇒ Page

Returns a new instance of Page.



9
10
11
12
13
14
15
# File 'lib/florby/page.rb', line 9

def initialize(file:)
  @file = file
  @meta = {}
  @markdown = File.read(@file)
  frontmatter, @body = @markdown.split("---\n", 3).last(2)
  @frontmatter = YAML.unsafe_load(frontmatter)
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



7
8
9
# File 'lib/florby/page.rb', line 7

def meta
  @meta
end

Instance Method Details

#aliasesObject



56
57
58
# File 'lib/florby/page.rb', line 56

def aliases
  @frontmatter['aliases'] || []
end

#contentObject



60
61
62
# File 'lib/florby/page.rb', line 60

def content
  @content ||= Commonmarker.to_html(@body, options: { extension: { tagfilter: false, autolink: true }, render: { unsafe: true } })
end

#content=(html) ⇒ Object



64
65
66
# File 'lib/florby/page.rb', line 64

def content=(html)
  @content = html
end

#createdObject



32
33
34
35
36
37
38
# File 'lib/florby/page.rb', line 32

def created
  if @frontmatter['created'].is_a?(String)
    Date.parse(@frontmatter['created'])
  else
    @frontmatter['created'] || File::Stat.new(@file).birthtime.to_date
  end
end

#exclude_from_collections?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/florby/page.rb', line 48

def exclude_from_collections?
  @frontmatter['exclude_from_collections'] == 'true'
end

#filenameObject



17
18
19
# File 'lib/florby/page.rb', line 17

def filename
  @filename ||= File.basename(@file)
end

#layoutObject



52
53
54
# File 'lib/florby/page.rb', line 52

def layout
  @frontmatter['layout'] || 'default'
end


25
26
27
28
29
30
# File 'lib/florby/page.rb', line 25

def permalink
  link = @frontmatter['permalink'] || title
  link = "/#{link}" unless link =~ /^\//
  link = "#{link}/" unless link =~ /\/$/
  link
end

#titleObject



21
22
23
# File 'lib/florby/page.rb', line 21

def title
  @frontmatter['title'] ||filename.gsub(/\.md$/, '')
end

#updatedObject



40
41
42
43
44
45
46
# File 'lib/florby/page.rb', line 40

def updated
  if @frontmatter['updated'].is_a?(String)
    Date.parse(@frontmatter['updated'])
  else
    @frontmatter['updated'] || File::Stat.new(@file).mtime.to_date
  end
end