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

Returns a new instance of Page.



19
20
21
22
23
24
25
26
27
28
29
# 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 = []
  @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}"
  super(page_path)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
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



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

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

Returns:

  • (Boolean)


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

def any?
  all.any?
end

.create(path, base_path) ⇒ Object



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

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

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.find(path) ⇒ Object



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

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

.page_pathsObject



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

def page_paths
  Lookbook.config.page_paths.select { |dir| Dir.exist? dir }
end

.section_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#contentObject



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

def content
  @content ||= strip_frontmatter(file_contents).strip
end

#footer?Boolean

Returns:

  • (Boolean)


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

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

#full_pathObject



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

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

#get(key) ⇒ Object



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

def get(key)
  options[key]
end

#header?Boolean

Returns:

  • (Boolean)


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

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

#hiddenObject



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

def hidden
  options[:hidden]
end

#hidden?Boolean

Returns:

  • (Boolean)


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

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

#idObject



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

def id
  options[:id]
end

#labelObject



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

def label
  options[:label]
end

#markdown?Boolean

Returns:

  • (Boolean)


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

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

#matchersObject



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

def matchers
  normalize_matchers(label)
end

#nameObject



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

def name
  @page_name
end

#parent_collections_namesObject



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

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

#positionObject



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

def position
  options[:position]
end

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

Returns:

  • (Boolean)


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

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

#typeObject



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

def type
  :page
end

#url_pathObject



31
32
33
# File 'lib/lookbook/page.rb', line 31

def url_path
  lookbook_page_path lookup_path
end