Class: Lookbook::Page

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

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

Constructor Details

#initialize(path, base_path) ⇒ Page

Returns a new instance of Page.



21
22
23
24
25
26
27
# File 'lib/lookbook/page.rb', line 21

def initialize(path, base_path)
  @pathname = Pathname.new path
  @base_path = Pathname.new base_path
  @options = nil
  @errors = []
  @tabs = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



98
99
100
101
102
103
104
# File 'lib/lookbook/page.rb', line 98

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.



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

def errors
  @errors
end

#tabsObject

Returns the value of attribute tabs.



19
20
21
# File 'lib/lookbook/page.rb', line 19

def tabs
  @tabs
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, tabs =
    Array(page_paths).flat_map do |dir|
      Dir["#{dir}/**/*.html.*", "#{dir}/**/*.md.*"].sort.map do |page|
        page = Lookbook::Page.new(page, 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_tabs = tabs.sort_by { |tab| [tab.position, tab.label] }

  sorted_tabs.each do |tab|
    page_dict[tab.path].tabs << tab
  end

  PageCollection.new(sorted_pages)
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.find(path) ⇒ Object



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

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
  Lookbook.config.page_paths.select { |dir| Dir.exist? dir }
end

Instance Method Details

#contentObject



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

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

#footer?Boolean

Returns:

  • (Boolean)


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

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

#full_pathObject



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

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

#get(key) ⇒ Object



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

def get(key)
  options[key]
end

#header?Boolean

Returns:

  • (Boolean)


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

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

#hidden?Boolean

Returns:

  • (Boolean)


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

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

#hierarchy_depthObject



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

def hierarchy_depth
  path.split("/").size
end

#lookup_pathObject



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

def lookup_path
  @lookup_path ||= to_lookup_path(path)
end

#markdown?Boolean

Returns:

  • (Boolean)


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

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

#matchersObject



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

def matchers
  normalize_matchers(label)
end

#nameObject



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

def name
  remove_position_prefix(path_name)
end

#parent_collections_namesObject



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

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

#pathObject



29
30
31
32
33
34
35
# File 'lib/lookbook/page.rb', line 29

def path
  rel_path = @pathname.relative_path_from(@base_path)

  _path = (rel_path.dirname.to_s == "." ? name : "#{rel_path.dirname}/#{name}")
  _path.gsub!("[#{tab}]", "") if tab?
  _path
end

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

Returns:

  • (Boolean)


106
107
108
# File 'lib/lookbook/page.rb', line 106

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

#tabObject



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

def tab
  matches = full_path.to_s.match(%r{\[(?<tab>\w+)\]})
  matches ? remove_position_prefix(matches[:tab]) : nil
end

#tab?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/lookbook/page.rb', line 94

def tab?
  tab.present?
end

#typeObject



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

def type
  tab? ? :tab : :page
end