Sha256: 63476117099178de3da63c8c623c3a812dd3167fd199df95255a3568bb8fc6c2

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module Siteleaf
  class Page < Entity

    attr_accessor :title, :custom_slug, :body, :visibility, :published_at, :user_id, :site_id, :parent_id, :meta
    attr_reader :id, :slug, :url, :created_at, :updated_at, :assets
    
    def create_endpoint
      "sites/#{self.site_id}/pages"
    end
    
    def site
      Site.find(self.site_id) if self.site_id
    end
    
    def assets
      result = Client.get "pages/#{self.id}/assets"
      result.map { |r| Asset.new(r) } if result
    end
    
    def posts
      result = Client.get "pages/#{self.id}/posts"
      result.map { |r| Post.new(r) } if result
    end
    
    def pages
      result = Client.get "pages/#{self.id}?include=pages"
      result["pages"].map { |r| Page.new(r) } if result
    end
    
    def page
      Page.find(self.parent_id) if self.parent_id
    end
    
    def draft?
      visibility == 'draft'
    end
    
    def published?
      visibility == 'visible'
    end
    
    def filename
      "#{url.sub('/','')}.markdown"
    end
    
    def to_file
      assets = Dir.glob("export/_uploads/**/*").each_with_object({}) { |var, hash| hash[var.sub('export/_uploads','/assets')] = var.sub('export/_uploads','/uploads') }
      (frontmatter + "---\n\n".freeze + body.to_s).gsub(Regexp.union(assets.keys), assets)
    end
  
    protected
  
    def frontmatter
      attrs = {}
      attrs['title'] = title
      attrs['date'] = Time.parse(published_at).utc
      attrs['published'] = false if !published?
      
      meta.each{|m| attrs[m['key']] = m['value'].to_s.gsub("\r\n","\n")} unless meta.nil?
      
      if defined?(taxonomy) && !taxonomy.nil?
        taxonomy.each do |t| 
          attrs[t['key']] = t['values'].map{|v| v['value']} unless t['values'].empty?
        end 
      end
  
      attrs.empty? ? "---\n".freeze : attrs.to_yaml
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
siteleaf-1.0.0 lib/siteleaf/page.rb