wip

did committed Mar 31, 2014
commit d297a81a978e3470623586a4a4f8af35113686d1
Showing 5 changed files with 85 additions and 31 deletions
generators/page/template.liquid.haml.tt +8 -5
@@ @@ -1,13 +1,12 @@
---
- title: <%= config[:slug].humanize -%>
- <% if config[:translated] %>
+ title: <%= config[:title] -%>
- # unique identifier for urls, the same as a permalink
+ <% if config[:translated] -%># unique identifier for urls, the same as a permalink
slug: <%= config[:slug] -%>
- <% end %>
+ <% end -%>
# true if the page is included in the menu
- listed: true
+ listed: <% if config[:listed] -%><%= config[:listed] %><% else -%>true<% end %>
# true if the page is published
published: true
@@ @@ -19,7 +18,11 @@ published: true
# redirect_url: "<url to a page or to a remote url>"
# content type that this page is templatizing
+ <% if config[:content_type] -%>
+ content_type: <%= config[:content_type] -%>
+ <% else -%>
# content_type: "<slug of one of the content types>"
+ <% end -%>
# editable_elements:
# 'some_block/some_slug': "<text>"
generators/page/template.liquid.tt +6 -2
@@ @@ -1,5 +1,5 @@
---
- title: <%= config[:slug].humanize -%>
+ title: <%= config[:title] -%>
<% if config[:translated] %>
# unique identifier for urls, the same as a permalink
@@ @@ -7,7 +7,7 @@ slug: <%= config[:slug] -%>
<% end %>
# true if the page is included in the menu
- listed: true
+ listed: <% if config[:listed] -%><%= config[:listed] %><% else -%>true<% end -%>
# true if the page is published
published: true
@@ @@ -19,7 +19,11 @@ published: true
# redirect_url: "<url to a page or to a remote url>"
# content type that this page is templatizing
+ <% if config[:content_type] -%>
+ content_type: <%= config[:content_type] -%>
+ <% else -%>
# content_type: "<slug of one of the content types>"
+ <% end -%>
# editable_elements:
# 'some_block/some_slug': "<text>"
locomotive/wagon.rb b/lib/locomotive/wagon.rb +3 -2
@@ @@ -71,14 +71,15 @@ module Locomotive
#
# @param [ Symbol ] name The name of the generator
# @param [ Array ] *args The arguments for the generator
+ # @param [ Hash ] options The options for the generator
#
- def self.generate(name, *args)
+ def self.generate(name, args, options = {})
Bundler.require 'misc'
lib = "locomotive/wagon/generators/#{name}"
require lib
- generator = lib.camelize.constantize.new(args, {}, {})
+ generator = lib.camelize.constantize.new(args, options, {})
generator.invoke_all
end
locomotive/wagon/cli.rb b/lib/locomotive/wagon/cli.rb +8 -2
@@ @@ -74,6 +74,11 @@ module Locomotive
end
desc 'page FULLPATH', 'Create a page. No need to pass an extension to the FULLPATH arg'
+ method_option :title, aliases: '-t', type: 'string', default: nil, desc: 'Title of the page'
+ method_option :haml, aliases: '-h', type: 'boolean', default: false, desc: 'add a HAML extension to the file'
+ method_option :listed, aliases: '-l', type: 'boolean', default: false, desc: 'tell if the page is listed in the menu'
+ method_option :content_type, aliases: '-c', type: 'string', default: nil, desc: 'tell if the page is a template for a content type'
+ method_option :locales, aliases: '-lo', type: 'string', default: nil, desc: 'locales for the various translations'
long_desc <<-LONGDESC
Create a page. The generator will ask for the extension (liquid or haml) and also
if the page is localized or not.
@@ @@ -86,8 +91,8 @@ module Locomotive
LONGDESC
def page(fullpath)
if path = check_path!
- locales = self.site_config(path)['locales']
- Locomotive::Wagon.generate :page, fullpath, self.options['path'], locales
+ self.options[:default_locales] = self.site_config(path)['locales']
+ Locomotive::Wagon.generate :page, [fullpath, self.options.delete('path')], self.options
end
end
@@ @@ -102,6 +107,7 @@ module Locomotive
LONGDESC
def snippet(slug)
if path = check_path!
+ raise 'TODO'
locales = self.site_config(path)['locales']
Locomotive::Wagon.generate :snippet, slug, self.options['path'], locales
end
locomotive/wagon/generators/page.rb b/lib/locomotive/wagon/generators/page.rb +60 -20
@@ @@ -11,37 +11,42 @@ module Locomotive
argument :slug
argument :target_path # path to the site
- argument :locales
+ # argument :locales
- attr_accessor :haml
+ # options
+ # class_option :title, type: :string, default: nil, required: false, desc: 'Title?'
+ # class_option :haml, type: :boolean, default: nil, required: false, desc: 'HAML over HTML?'
+ # class_option :listed, type: :boolean, default: nil, required: false, desc: 'Listed in the nav tag?'
+ # class_option :content_type, type: :string, default: nil, required: false, desc: 'Tied to a content type?'
- def ask_for_haml
- self.haml = yes?('Do you prefer a HAML template ?')
- end
+ # attr_accessor :locales
- def apply_locales?
- self.locales.shift # remove the default locale
+ # def apply_locales?
+ # if self.options[:locales].blank?
+ # self.locales =
- unless self.locales.empty?
- unless yes?('Do you want to generate templates for each locale ?')
- self.locales = []
- end
- end
- end
+ # self.locales.shift # remove the default locale
+
+ # unless self.locales.empty?
+ # unless yes?('Do you want to generate templates for each locale ?')
+ # self.locales = []
+ # end
+ # end
+ # end
def create_page
- extension = self.haml ? 'liquid.haml' : 'liquid'
+ extension = haml? ? 'liquid.haml' : 'liquid'
segments = self.slug.split('/')
while segment = segments.pop do
- options = { slug: segment, translated: false }
- file_path = File.join(pages_path, segments, segment)
+ _options = self.page_options(slug: segment, translated: false)
+ file_path = File.join(pages_path, segments, segment)
- template "template.#{extension}.tt", "#{file_path}.#{extension}", options
+ template "template.#{extension}.tt", "#{file_path}.#{extension}", _options
- self.locales.each do |locale|
- options[:translated] = true
- template "template.#{extension}.tt", "#{file_path}.#{locale}.#{extension}", options
+ self.other_locales.each do |locale|
+ _options[:translated] = true
+ template "template.#{extension}.tt", "#{file_path}.#{locale}.#{extension}", _options
end
end
end
@@ @@ -52,10 +57,45 @@ module Locomotive
protected
+ def haml?
+ if options[:haml].nil?
+ yes?('Do you prefer a HAML template ?')
+ else
+ options[:haml]
+ end
+ end
+
def pages_path
File.join(target_path, 'app', 'views', 'pages')
end
+ def page_options(base = {})
+ base.merge({
+ title: options[:title] || base[:slug].humanize,
+ listed: options[:listed],
+ content_type: options[:content_type]
+ })
+ end
+
+ def other_locales
+ locales = options[:default_locales]
+ locales.shift
+
+ # #1 default: [fr, en, es], asked: [en, de], result => [en]
+ # #2 default: [fr, en, de], asked: [es], result => []
+ # #1 default: [fr, en, es], asked: [fr, en, es], result => [en, es]
+
+ locales & (options[:locales] || [])
+ end
+
+ # def locales
+ # self.options[:locales]
+ # end
+
+ # def locales=(values)
+ # self.options[:locales] = values
+ # end
+
end
end