Class: Lookbook::Page

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

Direct Known Subclasses

PageSection

Constant Summary collapse

FRONTMATTER_FIELDS =
[
  :id,
  :label,
  :title,
  :hidden,
  :landing,
  :position,
  :markdown,
  :header,
  :footer,
  :data
]

Constants included from Utils

Utils::FRONTMATTER_REGEX, Utils::POSITION_PREFIX_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#hierarchy_depth, #lookup_path, #path

Constructor Details

#initialize(path, base_path) ⇒ Page



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lookbook/page.rb', line 19

def initialize(path, base_path)
  @pathname = Pathname.new path
  @base_path = Pathname.new base_path
  @options = nil
  @errors = []
  @sections = []
  @frontmatter = {}
  @content = ""
  @page_name = remove_position_prefix(path_name)
  @rel_path = @pathname.relative_path_from(@base_path)
  page_path = @rel_path.dirname.to_s == "." ? @page_name : "#{@rel_path.dirname}/#{@page_name}"
  extract_frontmatter(file_contents)
  super(page_path)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/lookbook/page.rb', line 94

def method_missing(method_name, *args, &block)
  if args.none? && !block
    options[method_name]
  else
    super
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



16
17
18
# File 'lib/lookbook/page.rb', line 16

def content
  @content
end

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/lookbook/page.rb', line 16

def errors
  @errors
end

#frontmatterObject (readonly)

Returns the value of attribute frontmatter.



16
17
18
# File 'lib/lookbook/page.rb', line 16

def frontmatter
  @frontmatter
end

#rel_pathObject (readonly)

Returns the value of attribute rel_path.



16
17
18
# File 'lib/lookbook/page.rb', line 16

def rel_path
  @rel_path
end

#sectionsObject

Returns the value of attribute sections.



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

def sections
  @sections
end

Class Method Details

.allObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lookbook/page.rb', line 159

def all
  pages, sections =
    Array(page_paths).flat_map do |dir|
      Dir["#{dir}/**/*.html.*", "#{dir}/**/*.md.*"].sort.map do |path|
        create(path, dir)
      end
    end.partition { |page| page.type == :page }

  sorted_pages = pages
    .uniq { |page| page.path }
    .sort_by { |page| [page.position, page.label] }

  page_dict = sorted_pages.index_by(&:path)
  sorted_sections = sections.sort_by { |section| [section.position, section.label] }

  sorted_sections.each do |section|
    page_dict[section.path].sections << section
  end

  PageCollection.new(sorted_pages)
end

.any?Boolean



155
156
157
# File 'lib/lookbook/page.rb', line 155

def any?
  all.any?
end

.create(path, base_path) ⇒ Object



189
190
191
# File 'lib/lookbook/page.rb', line 189

def create(path, base_path)
  (section_path?(path) ? PageSection : Page).new(path, base_path)
end

.exists?(path) ⇒ Boolean



151
152
153
# File 'lib/lookbook/page.rb', line 151

def exists?(path)
  !!find(path)
end

.find(path) ⇒ Object



147
148
149
# File 'lib/lookbook/page.rb', line 147

def find(path)
  all.find { |p| p.lookup_path == path }
end

.page_pathsObject



181
182
183
# File 'lib/lookbook/page.rb', line 181

def page_paths
  PathUtils.normalize_all(Lookbook.config.page_paths)
end

.section_path?(path) ⇒ Boolean



185
186
187
# File 'lib/lookbook/page.rb', line 185

def section_path?(path)
  !!path.match(%r{\[(.*?\w+)\]})
end

Instance Method Details

#footer?Boolean



58
59
60
# File 'lib/lookbook/page.rb', line 58

def footer?
  options[:footer] == true
end

#full_pathObject



38
39
40
# File 'lib/lookbook/page.rb', line 38

def full_path
  Pathname.new Rails.root.join(@pathname.to_s)
end

#get(key) ⇒ Object



62
63
64
# File 'lib/lookbook/page.rb', line 62

def get(key)
  options[key]
end

#header?Boolean



54
55
56
# File 'lib/lookbook/page.rb', line 54

def header?
  options[:header] == true
end

#hiddenObject



86
87
88
# File 'lib/lookbook/page.rb', line 86

def hidden
  options[:hidden]
end

#hidden?Boolean



46
47
48
# File 'lib/lookbook/page.rb', line 46

def hidden?
  options[:hidden] == true
end

#idObject



78
79
80
# File 'lib/lookbook/page.rb', line 78

def id
  options[:id]
end

#labelObject



90
91
92
# File 'lib/lookbook/page.rb', line 90

def label
  options[:label]
end

#markdown?Boolean



50
51
52
# File 'lib/lookbook/page.rb', line 50

def markdown?
  options[:markdown] == true
end

#matchersObject



66
67
68
# File 'lib/lookbook/page.rb', line 66

def matchers
  normalize_matchers(label)
end

#nameObject



42
43
44
# File 'lib/lookbook/page.rb', line 42

def name
  @page_name
end

#parent_collections_namesObject



70
71
72
# File 'lib/lookbook/page.rb', line 70

def parent_collections_names
  File.dirname(path).split("/")
end

#positionObject



82
83
84
# File 'lib/lookbook/page.rb', line 82

def position
  options[:position]
end

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



102
103
104
# File 'lib/lookbook/page.rb', line 102

def respond_to_missing?(method_name, include_private = false)
  FRONTMATTER_FIELDS.include? method_name
end

#typeObject



74
75
76
# File 'lib/lookbook/page.rb', line 74

def type
  :page
end

#url_pathObject



34
35
36
# File 'lib/lookbook/page.rb', line 34

def url_path
  lookbook_page_path lookup_path
end