lib/lookbook/page.rb in lookbook-1.2.1 vs lib/lookbook/page.rb in lookbook-1.3.0

- old
+ new

@@ -11,22 +11,25 @@ :header, :footer, :data ] - attr_reader :errors, :rel_path + attr_reader :errors, :rel_path, :content, :frontmatter attr_accessor :sections 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 def url_path lookbook_page_path lookup_path @@ -58,14 +61,10 @@ def get(key) options[key] end - def content - @content ||= strip_frontmatter(file_contents).strip - end - def matchers normalize_matchers(label) end def parent_collections_names @@ -110,21 +109,10 @@ File.read(full_path) end 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 @@ -132,9 +120,20 @@ @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 extract_frontmatter(content) + @frontmatter, @content = FrontmatterExtractor.call(content) + rescue => exception + 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 def path_name @pathname.basename(@pathname.extname).to_s.gsub(/\.(html|md)$/, "") end