2010-09-23 23:00:13 +00:00
|
|
|
module Locomotive
|
|
|
|
module Import
|
|
|
|
module Pages
|
|
|
|
|
|
|
|
def self.process(context)
|
|
|
|
site, pages, theme_path = context[:site], context[:database]['pages'], context[:theme_path]
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
context[:done] = {} # initialize the hash storing pages already processed
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
self.add_index_and_404(context)
|
|
|
|
|
|
|
|
Dir[File.join(theme_path, 'templates', '**/*')].each do |template_path|
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
fullpath = template_path.gsub(File.join(theme_path, 'templates'), '').gsub('.liquid', '').gsub(/^\//, '')
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2010-09-28 22:08:11 +00:00
|
|
|
# puts "=========== #{fullpath} ================="
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
next if %w(index 404).include?(fullpath)
|
|
|
|
|
|
|
|
self.add_page(fullpath, context)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
def self.add_page(fullpath, context)
|
|
|
|
puts "....adding #{fullpath}"
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
page = context[:done][fullpath]
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
return page if page # already added, so skip it
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
site, pages, theme_path = context[:site], context[:database]['site']['pages'], context[:theme_path]
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
template = File.read(File.join(theme_path, 'templates', "#{fullpath}.liquid"))
|
|
|
|
|
|
|
|
self.build_parent_template(template, context)
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
parent = self.find_parent(fullpath, context)
|
|
|
|
|
2010-09-28 22:08:11 +00:00
|
|
|
# puts "updating..... #{fullpath} / #{template}"
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
page = site.pages.where(:fullpath => fullpath).first || site.pages.build
|
2010-09-28 22:08:11 +00:00
|
|
|
|
|
|
|
attributes = {
|
2010-09-26 22:31:50 +00:00
|
|
|
:title => fullpath.split('/').last.humanize,
|
2010-09-28 22:08:11 +00:00
|
|
|
:slug => fullpath.split('/').last,
|
|
|
|
:parent => parent,
|
|
|
|
:raw_template => template
|
2010-09-26 22:31:50 +00:00
|
|
|
}.merge(pages[fullpath] || {}).symbolize_keys
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
# templatized ?
|
|
|
|
if content_type_slug = attributes.delete(:content_type)
|
|
|
|
attributes[:content_type] = site.content_types.where(:slug => content_type_slug).first
|
|
|
|
end
|
|
|
|
|
|
|
|
page.attributes = attributes
|
|
|
|
|
|
|
|
# do not parse liquid templates now
|
2010-09-26 22:31:50 +00:00
|
|
|
# page.instance_variable_set(:@template_changed, false)
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
page.save!
|
|
|
|
|
|
|
|
site.reload
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
context[:done][fullpath] = page
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
page
|
|
|
|
end
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
def self.build_parent_template(template, context)
|
2010-09-28 22:08:11 +00:00
|
|
|
# puts "building parent_template #{template.blank?}"
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
# just check if the template contains the extends keyword
|
|
|
|
# template
|
|
|
|
fullpath = template.scan(/\{% extends (\w+) %\}/).flatten.first
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
if fullpath # inheritance detected
|
|
|
|
fullpath.gsub!("'", '')
|
2010-09-28 22:08:11 +00:00
|
|
|
|
|
|
|
# puts "found parent_template #{fullpath}"
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
return if fullpath == 'parent'
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
self.add_page(fullpath, context)
|
|
|
|
else
|
2010-09-28 22:08:11 +00:00
|
|
|
# puts "no parent_template found #{fullpath}"
|
2010-09-26 22:31:50 +00:00
|
|
|
end
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
def self.find_parent(fullpath, context)
|
2010-09-28 22:08:11 +00:00
|
|
|
# puts "finding parent for #{fullpath}"
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
site = context[:site]
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
segments = fullpath.split('/')
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
return site.pages.index.first if segments.size == 1
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
(segments.last == 'index' ? 2 : 1).times { segments.pop }
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
parent_fullpath = File.join(segments.join('/'), 'index').gsub(/^\//, '')
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
# look for a local index page in db
|
|
|
|
parent = site.pages.where(:fullpath => parent_fullpath).first
|
|
|
|
|
|
|
|
parent || self.add_page(parent_fullpath, context)
|
|
|
|
end
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
def self.add_index_and_404(context)
|
|
|
|
site, pages, theme_path = context[:site], context[:database]['site']['pages'], context[:theme_path]
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
%w(index 404).each do |slug|
|
|
|
|
page = site.pages.where({ :slug => slug, :depth => 0 }).first
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
# puts "building system page (#{slug}) => #{page.inspect}"
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
page ||= sites.pages.build(:slug => slug, :parent => nil)
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
template = File.read(File.join(theme_path, 'templates', "#{slug}.liquid"))
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
page.attributes = { :raw_template => template }.merge(pages[slug] || {})
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
page.save! rescue nil # TODO better error handling
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
site.reload
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
context[:done][slug] = page
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|