lib/lookbook/page.rb in lookbook-0.8.3 vs lib/lookbook/page.rb in lookbook-0.9.0
- old
+ new
@@ -14,21 +14,26 @@
:footer,
:data
]
attr_reader :errors
+ attr_accessor :tabs
def initialize(path, base_path)
@pathname = Pathname.new path
@base_path = Pathname.new base_path
@options = nil
@errors = []
+ @tabs = []
end
def path
rel_path = @pathname.relative_path_from(@base_path)
- (rel_path.dirname.to_s == "." ? name : "#{rel_path.dirname}/#{name}")
+
+ _path = (rel_path.dirname.to_s == "." ? name : "#{rel_path.dirname}/#{name}")
+ _path.gsub!("[#{tab}]", "") if tab?
+ _path
end
def lookup_path
@lookup_path ||= to_lookup_path(path)
end
@@ -76,13 +81,22 @@
def parent_collections_names
File.dirname(path).split("/")
end
def type
- :page
+ tab? ? :tab : :page
end
+ def tab
+ matches = full_path.to_s.match(%r{\[(?<tab>\w+)\]})
+ matches ? remove_position_prefix(matches[:tab]) : nil
+ end
+
+ def tab?
+ tab.present?
+ end
+
def method_missing(method_name, *args, &block)
if args.none? && !block
options[method_name]
else
super
@@ -112,11 +126,11 @@
line_number: line_number_match ? line_number_match[1] : false
}))
end
@options = Lookbook.config.page_options.deep_merge(frontmatter).with_indifferent_access
@options[:id] = @options[:id] ? generate_id(@options[:id]) : generate_id(lookup_path)
- @options[:label] ||= name.titleize
+ @options[:label] ||= (tab? ? tab : 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?
@@ -141,15 +155,27 @@
def exists?(path)
!!find(path)
end
def all
- pages = Array(page_paths).map do |dir|
- Dir["#{dir}/**/*.html.*", "#{dir}/**/*.md.*"].sort.map do |page|
- Lookbook::Page.new(page, dir)
- end
+ 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
- sorted_pages = pages.flatten.uniq { |p| p.path }.sort_by { |page| [page.position, page.label] }
+
PageCollection.new(sorted_pages)
end
def page_paths
Lookbook.config.page_paths.select { |dir| Dir.exist? dir }