lib/siteleaf/site.rb in siteleaf-0.9.23 vs lib/siteleaf/site.rb in siteleaf-1.0.0

- old
+ new

@@ -1,9 +1,9 @@ module Siteleaf class Site < Entity - attr_accessor :title, :domain, :timezone, :meta + attr_accessor :title, :domain, :timezone, :meta, :posts_path attr_reader :id, :user_id, :created_at, :updated_at def self.find_by_domain(domain) result = Client.get self.endpoint, {"domain" => domain} self.new(result.first) if result and result.size >= 1 @@ -25,10 +25,15 @@ def pages result = Client.get "sites/#{self.id}/pages" result.map { |r| Page.new(r) } if result end + def posts + result = Client.get "sites/#{self.id}/posts" + result.map { |r| Post.new(r) } if result + end + def resolve(url = '/') Client.get "sites/#{self.id}/resolve", {"url" => url} end def preview(url = '/', template = nil) @@ -36,9 +41,72 @@ end def publish result = Client.post "sites/#{self.id}/publish", {} Job.new(id: result.parsed_response["job_id"]) if result + end + + def posts_path + @posts_path || 'posts' + end + + def filename + "_config.yml" + 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') } + config.gsub(Regexp.union(assets.keys), assets) + end + + protected + + def config + attrs = {} + attrs['title'] = title + attrs['url'] = "http://#{domain}" + + meta.each{|m| attrs[m['key']] = m['value'].to_s.gsub("\r\n","\n")} unless meta.nil? + + attrs['timezone'] = timezone + attrs['permalink'] = 'pretty' + + # output uploads using v1 /assets path + attrs['collections'] = { + 'uploads' => { + 'title' => 'Uploads', + 'output' => true + } + } + + # use collections for any set of posts not called "posts" + pages.each do |page| + path = page.url.sub('/','').gsub('/','_') + if path != posts_path && page.posts.size > 0 + attrs['collections'][path] = {'output' => true} + # output permalink for non-standard urls (e.g. posts inside sub-pages) + attrs['collections'][path]['permalink'] = "#{page.url}/:path" if path != page.slug + end + end + + # set permalink style for posts + attrs['defaults'] = { + 'scope' => { + 'path' => '', + 'type' => 'posts' + }, + 'values' => { + 'permalink' => "/#{posts_path}/:title/" + } + } + + # markdown defaults to match v1 rendering + attrs['markdown'] = 'redcarpet' + attrs['redcarpet'] = { + 'extensions' => ['autolink', 'fenced_code_blocks', 'lax_spacing', 'strikethrough', 'superscript', 'tables', 'footnotes', 'highlight'] + } + + attrs.empty? ? "---\n".freeze : attrs.to_yaml end end end \ No newline at end of file