Sha256: b0ada173652e9e9134e30af82290a31371f2e87316917552a01570f974e41bb9

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require_relative 'filters'

module JekyllPagesApi
  # wrapper for a Jekyll::Page
  class Page
    HTML_EXTS = %w(.html .md .markdown .textile).to_set
    attr_reader :page, :site

    # Jekyll::StaticFile doesn't expose a `site` accessor, so we require an
    # explicit `site` argument here.
    def initialize(page, site)
      @page = page
      @site = site
    end

    def html?
      path = self.page.path
      path.end_with?('/') || HTML_EXTS.include?(File.extname(path))
    end

    def filterer
      @filterer ||= Filters.new
    end

    def title
      title = self.page.data['title'] if self.page.respond_to?(:data)
      title ||= self.page.title if self.page.respond_to?(:title)
      self.filterer.decode_html(title || '')
    end

    def base_url
      self.site.baseurl
    end

    def rel_path
      path = self.page.url if self.page.respond_to?(:url)
      path ||= self.page.relative_path if self.page.respond_to?(:relative_path)
      path
    end

    def url
      [self.base_url, rel_path].join
    end

    def body_text
      output = self.page.content if self.page.respond_to?(:content)
      output ||= File.read(self.page.path)
      self.filterer.text_only(output)
    end

    def tags
      (self.page.data['tags'] if self.page.respond_to?(:data)) || []
    end

    def to_json
      {
        title: self.title,
        url: self.url,
        tags: self.tags,
        body: self.body_text
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll_pages_api-0.1.3 lib/jekyll_pages_api/page.rb