Class: Ottogen::Page

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(front_matter:, body:, path: nil) ⇒ Page

Returns a new instance of Page.



20
21
22
23
24
# File 'lib/ottogen/page.rb', line 20

def initialize(front_matter:, body:, path: nil)
  @path = path
  @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



51
52
53
54
55
56
# File 'lib/ottogen/page.rb', line 51

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.



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

def body
  @body
end

#front_matterObject (readonly)

Returns the value of attribute front_matter.



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

def front_matter
  @front_matter
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Returns the value of attribute permalink.



18
19
20
# File 'lib/ottogen/page.rb', line 18

def permalink
  @permalink
end

Class Method Details

.read(path) ⇒ Object



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

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

Instance Method Details

#asciidoctor_attributesObject



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

def asciidoctor_attributes
  attrs = @front_matter.transform_keys { |key| "page_#{key}" }
  page_url = url
  attrs['page_url'] = page_url if page_url
  attrs
end

#output_path(build_dir) ⇒ Object



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

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

  relative = @path.sub(%r{^pages/}, '').sub(/\.adoc\z/, '.html')
  File.join(build_dir, relative)
end

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

Returns:

  • (Boolean)


47
48
49
# File 'lib/ottogen/page.rb', line 47

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

#urlObject



26
27
28
29
30
31
# File 'lib/ottogen/page.rb', line 26

def url
  return @permalink.url if @permalink
  return nil unless @path

  "/#{@path.sub(%r{^pages/}, '').sub(/\.adoc\z/, '.html')}"
end