class Lookbook::Page

def all

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

def any?

def any?
  all.any?
end

def content

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

def create(path, base_path)

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

def exists?(path)

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

def file_contents

def file_contents
  File.read(full_path)
end

def find(path)

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

def footer?

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

def full_path

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

def get(key)

def get(key)
  options[key]
end

def header?

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

def hidden

def hidden
  options[:hidden]
end

def hidden?

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

def id

def id
  options[:id]
end

def initialize(path, base_path)

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

def label

def label
  options[:label]
end

def markdown?

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

def markdown_file?

def markdown_file?
  @pathname.basename(@pathname.extname).to_s.end_with?(".md")
end

def matchers

def matchers
  normalize_matchers(label)
end

def method_missing(method_name, *args, &block)

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

def name

def name
  @page_name
end

def options

def options
  return @options if @options
  begin
    frontmatter = (get_frontmatter(file_contents) || {}).deep_symbolize_keys
  rescue => exception
    frontmatter = {}
    line_number_match = exception.message.match(/.*line\s(\d+)/)
    @errors.push(Lookbook::Error.new(exception, **{
      title: "YAML frontmatter parsing error",
      file_path: @pathname.to_s,
      line_number: line_number_match ? line_number_match[1] : false
    }))
  end
  @options = Lookbook.config.page_options.deep_merge(frontmatter).with_indifferent_access
  @options[:id] = generate_id(@options[:id] || lookup_path)
  @options[:label] ||= name.titleize
  @options[:title] ||= @options[:label]
  @options[:hidden] ||= false
  @options[:landing] ||= false
  @options[:position] = @options[:position] ? @options[:position].to_i : get_position_prefix(path_name)
  @options[:markdown] ||= markdown_file?
  @options[:header] = true unless @options.key? :header
  @options[:footer] = true unless @options.key? :footer
  @options
end

def page_paths

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

def parent_collections_names

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

def path_name

def path_name
  @pathname.basename(@pathname.extname).to_s.gsub(/\.(html|md)$/, "")
end

def position

def position
  options[:position]
end

def respond_to_missing?(method_name, include_private = false)

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

def section_path?(path)

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

def type

def type
  :page
end

def url_path

def url_path
  lookbook_page_path lookup_path
end