Sha256: 2c182cf55ce81eb0406407d57acf196641aa6bd36f4c104aace1d10b082b23e9

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require 'commonmarker'
require 'yaml'
require 'fileutils'

module Florby
  class Page
    attr_reader :meta

    def initialize(file:)
      @file = file
      @meta = {}
      @markdown = File.read(@file)
      frontmatter, @body = @markdown.split("---\n", 3).last(2)
      @frontmatter = YAML.unsafe_load(frontmatter)
    end

    def filename
      @filename ||= File.basename(@file)
    end

    def title
      @frontmatter['title'] ||filename.gsub(/\.md$/, '')
    end

    def permalink
      link = @frontmatter['permalink'] || title
      link = "/#{link}" unless link =~ /^\//
      link = "#{link}/" unless link =~ /\/$/
      link
    end

    def created
      if @frontmatter['created'].is_a?(String)
        Date.parse(@frontmatter['created'])
      else
        @frontmatter['created'] || File::Stat.new(@file).birthtime.to_date
      end
    end

    def updated
      if @frontmatter['updated'].is_a?(String)
        Date.parse(@frontmatter['updated'])
      else
        @frontmatter['updated'] || File::Stat.new(@file).mtime.to_date
      end
    end

    def exclude_from_collections?
      @frontmatter['exclude_from_collections'] == 'true'
    end

    def layout
      @frontmatter['layout'] || 'default'
    end

    def aliases
      @frontmatter['aliases'] || []
    end

    def content
      @content ||= Commonmarker.to_html(@body, options: { extension: { tagfilter: false, autolink: true }, render: { unsafe: true } })
    end

    def content=(html)
      @content = html
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
florby-0.1.0 lib/florby/page.rb