2010-09-23 23:00:13 +00:00
|
|
|
module Locomotive
|
|
|
|
module Import
|
2010-10-28 23:36:45 +00:00
|
|
|
class Pages < Base
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def process
|
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
|
|
|
|
2011-04-28 15:04:18 +00:00
|
|
|
self.add_page('404')
|
|
|
|
|
2011-02-03 10:22:19 +00:00
|
|
|
self.add_page('index')
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
Dir[File.join(theme_path, 'app', 'views', 'pages', '**/*')].each do |template_path|
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
fullpath = template_path.gsub(File.join(theme_path, 'app', 'views', 'pages'), '').gsub('.liquid', '').gsub(/^\//, '')
|
2010-09-23 23:00:13 +00:00
|
|
|
|
|
|
|
next if %w(index 404).include?(fullpath)
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
self.add_page(fullpath)
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
2011-01-10 13:57:44 +00:00
|
|
|
|
|
|
|
# make sure all the pages were processed (redirection pages without template for instance)
|
|
|
|
self.pages.each { |fullpath, attributes| self.add_page_without_template(fullpath.to_s) }
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
protected
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2011-01-10 13:57:44 +00:00
|
|
|
def add_page_without_template(fullpath)
|
|
|
|
page = context[:done][fullpath]
|
|
|
|
|
|
|
|
return page if page # already added, so skip it
|
|
|
|
|
|
|
|
self._save_page!(fullpath, nil)
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def add_page(fullpath)
|
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
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
template = File.read(File.join(theme_path, 'app', 'views', 'pages', "#{fullpath}.liquid")) rescue "Unable to find #{fullpath}.liquid"
|
2010-09-26 22:31:50 +00:00
|
|
|
|
2010-10-30 22:30:30 +00:00
|
|
|
self.replace_images!(template)
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
self.build_parent_template(template)
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2011-01-10 13:57:44 +00:00
|
|
|
self._save_page!(fullpath, template)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _save_page!(fullpath, template)
|
2010-10-28 23:36:45 +00:00
|
|
|
parent = self.find_parent(fullpath)
|
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,
|
2011-01-03 10:50:09 +00:00
|
|
|
:raw_template => template,
|
|
|
|
:published => true
|
2010-10-28 23:36:45 +00:00
|
|
|
}.merge(self.pages[fullpath] || {}).symbolize_keys
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2011-02-12 14:32:35 +00:00
|
|
|
if %w(index 404).include?(fullpath)
|
|
|
|
attributes[:position] = fullpath == 'index' ? 0 : 1
|
|
|
|
end
|
|
|
|
|
2011-02-12 23:01:42 +00:00
|
|
|
attributes[:position] = attributes[:position].to_i
|
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
# templatized ?
|
|
|
|
if content_type_slug = attributes.delete(:content_type)
|
2010-10-28 23:36:45 +00:00
|
|
|
attributes.merge!({
|
|
|
|
:templatized => true,
|
|
|
|
:content_type => site.content_types.where(:slug => content_type_slug).first
|
|
|
|
})
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
|
|
|
|
2011-01-10 13:57:44 +00:00
|
|
|
# redirection page ?
|
|
|
|
attributes[:redirect] = true if attributes[:redirect_url].present?
|
|
|
|
|
2011-07-19 13:51:29 +00:00
|
|
|
# Don't want the editable elements to be imported: they will be regenerated
|
|
|
|
attributes.delete(:editable_elements)
|
|
|
|
|
2011-01-10 13:57:44 +00:00
|
|
|
page = site.pages.where(:fullpath => self.sanitize_fullpath(fullpath)).first || site.pages.build
|
2010-10-28 23:36:45 +00:00
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
page.attributes = attributes
|
|
|
|
|
|
|
|
page.save!
|
|
|
|
|
2011-01-10 13:57:44 +00:00
|
|
|
self.log "adding #{page.fullpath} (#{template.blank? ? 'without' : 'with'} template) / #{page.position}"
|
2010-10-28 23:36:45 +00:00
|
|
|
|
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][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-10-28 23:36:45 +00:00
|
|
|
def build_parent_template(template)
|
2010-09-26 22:31:50 +00:00
|
|
|
# just check if the template contains the extends keyword
|
2011-03-04 09:53:20 +00:00
|
|
|
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
|
|
|
|
return if fullpath == 'parent'
|
2010-10-28 23:36:45 +00:00
|
|
|
self.add_page(fullpath)
|
2010-09-26 22:31:50 +00:00
|
|
|
end
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def find_parent(fullpath)
|
2011-02-12 23:01:42 +00:00
|
|
|
return nil if %w(index 404).include?(fullpath) # avoid cyclic issue with the index page
|
2011-02-03 10:22:19 +00:00
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
segments = fullpath.split('/')
|
|
|
|
|
2011-01-27 09:49:42 +00:00
|
|
|
return site.pages.root.first if segments.size == 1
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
segments.pop
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
parent_fullpath = segments.join('/').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
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
parent || self.add_page(parent_fullpath)
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
|
|
|
|
2010-10-30 22:30:30 +00:00
|
|
|
def replace_images!(template)
|
|
|
|
return if template.blank?
|
|
|
|
|
|
|
|
template.gsub!(/\/samples\/(.*\.[a-zA-Z0-9]{3})/) do |match|
|
|
|
|
name = $1
|
|
|
|
|
2011-07-04 21:11:35 +00:00
|
|
|
if asset = site.assets.where(:source_filename => name).first
|
2010-10-30 22:30:30 +00:00
|
|
|
asset.source.url
|
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def pages
|
2010-10-30 22:30:30 +00:00
|
|
|
@pages ||= self.retrieve_pages
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_pages
|
|
|
|
pages = context[:database]['site']['pages']
|
|
|
|
|
|
|
|
if pages.is_a?(Array) # ordered list of pages
|
2011-01-03 10:50:09 +00:00
|
|
|
tmp, positions = {}, Hash.new(0)
|
|
|
|
pages.each do |data|
|
|
|
|
position = nil
|
|
|
|
fullpath = data.keys.first.to_s
|
|
|
|
|
2011-02-12 14:32:35 +00:00
|
|
|
unless %w(index 404).include?(fullpath)
|
2011-01-03 10:50:09 +00:00
|
|
|
(segments = fullpath.split('/')).pop
|
|
|
|
position_key = segments.empty? ? 'index' : segments.join('/')
|
|
|
|
|
|
|
|
position = positions[position_key]
|
|
|
|
|
|
|
|
positions[position_key] += 1
|
|
|
|
end
|
|
|
|
|
2010-10-30 22:30:30 +00:00
|
|
|
attributes = (data.values.first || {}).merge(:position => position)
|
2011-01-03 10:50:09 +00:00
|
|
|
|
|
|
|
tmp[fullpath] = attributes
|
2010-10-30 22:30:30 +00:00
|
|
|
end
|
|
|
|
pages = tmp
|
|
|
|
end
|
|
|
|
|
|
|
|
pages
|
2010-10-28 23:36:45 +00:00
|
|
|
end
|
|
|
|
|
2011-01-10 13:57:44 +00:00
|
|
|
def sanitize_fullpath(fullpath)
|
|
|
|
fullpath.gsub(/\/template$/, '/content_type_template')
|
|
|
|
end
|
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|