implemented the generator to create snippets
did
committed Jan 16, 2013
commit 72f30be71433276c8455968466e53d81555857fb
Showing 4
changed files with
79 additions
and 5 deletions
generators/snippet/template.liquid.haml.tt
+4
-0
| @@ | @@ -0,0 +1,4 @@ |
| + | {% comment %} |
| + | / To use your snippet, just add the following code in your page template |
| + | / {% include <%= config[:slug] -%> %} |
| + | {% endcomment %} |
| \ No newline at end of file | |
generators/snippet/template.liquid.tt
+4
-0
| @@ | @@ -0,0 +1,4 @@ |
| + | {% comment %} |
| + | <!-- To use your snippet, just add the following code in your page template--> |
| + | <!-- {% include <%= config[:slug] -%> %} --> |
| + | {% endcomment %} |
| \ No newline at end of file | |
locomotive/builder/cli.rb b/lib/locomotive/builder/cli.rb
+17
-5
| @@ | @@ -25,7 +25,7 @@ module Locomotive |
| def content_type(name, *fields) | |
| say('The fields are missing', :red) and return false if fields.empty? | |
| - | if path = check_path! |
| + | if check_path! |
| Locomotive::Builder.generate :content_type, name, self.options['path'], fields | |
| else | |
| say 'The path does not point to a LocomotiveCMS site', :red | |
| @@ | @@ -44,16 +44,28 @@ module Locomotive |
| * builder generate page about_us/me | |
| LONGDESC | |
| def page(fullpath) | |
| - | if path = check_path! |
| + | if check_path! |
| Locomotive::Builder.generate :page, fullpath, self.options['path'] | |
| else | |
| say 'The path does not point to a LocomotiveCMS site', :red | |
| end | |
| end | |
| - | desc 'snippet NAME', 'Create a snippet' |
| - | def snippet(name) |
| - | puts "TODO [snippet] #{name}" |
| + | desc 'snippet SLUG', 'Create a snippet' |
| + | long_desc <<-LONGDESC |
| + | Create a snippet. The generator will ask for the extension (liquid or haml) and also |
| + | if the snippet is localized or not. |
| + | |
| + | Example: |
| + | |
| + | * builder generate snippet footer |
| + | LONGDESC |
| + | def snippet(slug) |
| + | if check_path! |
| + | Locomotive::Builder.generate :snippet, slug, self.options['path'] |
| + | else |
| + | say 'The path does not point to a LocomotiveCMS site', :red |
| + | end |
| end | |
| protected | |
locomotive/builder/generators/snippet.rb b/lib/locomotive/builder/generators/snippet.rb
+54
-0
| @@ | @@ -0,0 +1,54 @@ |
| + | require 'thor/group' |
| + | require 'active_support' |
| + | require 'active_support/core_ext' |
| + | |
| + | module Locomotive |
| + | module Builder |
| + | module Generators |
| + | class Snippet < Thor::Group |
| + | |
| + | include Thor::Actions |
| + | |
| + | argument :slug |
| + | argument :target_path # path to the site |
| + | |
| + | attr_accessor :haml, :locales |
| + | |
| + | def ask_for_haml_and_locales |
| + | self.locales = [] |
| + | self.haml = yes?('Do you prefer a HAML template ?') |
| + | |
| + | if yes?('Is your snippet localized ?') |
| + | self.locales = ask('What are the locales other than the default one (comma separated) ?').split(',').map(&:strip) |
| + | end |
| + | end |
| + | |
| + | def create_snippet |
| + | extension = self.haml ? 'liquid.haml' : 'liquid' |
| + | |
| + | options = { slug: slug, translated: false } |
| + | file_path = File.join(pages_path, slug) |
| + | |
| + | 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 |
| + | end |
| + | end |
| + | |
| + | def self.source_root |
| + | File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'generators', 'snippet') |
| + | end |
| + | |
| + | protected |
| + | |
| + | def pages_path |
| + | File.join(target_path, 'app', 'views', 'snippets') |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| \ No newline at end of file | |