Class: Moka::Generators::MokaPageGenerator
- Includes:
- SiteTree, Thor::Actions
- Defined in:
- lib/commands/page/page_generator.rb
Constant Summary
Constants included from SiteTree
Class Method Summary collapse
Instance Method Summary collapse
- #create_page_dir_and_files ⇒ Object
- #delete_variables_using_reserved_name ⇒ Object
- #dump_variables ⇒ Object
- #load_manifest ⇒ Object
- #notify_about_creation ⇒ Object
- #parse_page_group_and_name ⇒ Object
- #update_manifest ⇒ Object
- #verify_if_page_exists ⇒ Object
Class Method Details
.source_root ⇒ Object
20 21 22 |
# File 'lib/commands/page/page_generator.rb', line 20 def self.source_root File.('template/', File.dirname(__FILE__)) end |
Instance Method Details
#create_page_dir_and_files ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/commands/page/page_generator.rb', line 44 def create_page_dir_and_files unless [:template].nil? @template_name, @template_group = Moka::Utilities.parse_grouppage [:template] if @site.find_group(@template_group).nil? say_status "ERROR:", "cannot find group '#{@template_group}'", :red exit elsif @site.find_group(@template_group).find_page(@template_name).nil? say_status "ERROR:", "cannot find page '#{@template_name}' in group '#{@template_group}'", :red exit end directory(File.("project/site/#{@template_group}/#{@template_name}", MOKA_ROOT), File.("project/site/#{@group}/#{@page_name}", MOKA_ROOT)) else directory("pagedir", File.("project/site/#{@group}/#{@page_name}", MOKA_ROOT)) end end |
#delete_variables_using_reserved_name ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/commands/page/page_generator.rb', line 82 def delete_variables_using_reserved_name unless [:vars].nil? [:vars].each do |var_name, var_value| if Moka::SiteTree::RESERVED_NAMES.include? var_name.to_s say_status "WARNING:", "variable '#{var_name}' is a reserved word, and will not be set as a variable", :yellow end end [:vars].delete_if {|var_name, var_value| Moka::SiteTree::RESERVED_NAMES.include? var_name.to_s } end end |
#dump_variables ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/commands/page/page_generator.rb', line 93 def dump_variables if File.exists? File.("project/site/#{@group}/#{@page_name}/variables.yml", MOKA_ROOT) @page_variables = YAML.load_file(File.("project/site/#{@group}/#{@page_name}/variables.yml", MOKA_ROOT)) else @page_variables = {} end unless [:vars].nil? @page_variables.merge! [:vars] f = File.open( File.("project/site/#{@group}/#{@page_name}/variables.yml", MOKA_ROOT), 'w' ) do |out| YAML.dump( @page_variables, out ) end say_status "update", "project/site/#{@group}/#{@page_name}/variables.yml", :green end end |
#load_manifest ⇒ Object
32 33 34 35 |
# File 'lib/commands/page/page_generator.rb', line 32 def load_manifest @manifest = YAML.load_file(File.("manifest.yml", MOKA_ROOT)) @site = SiteNode.new("site", @manifest["site"]) end |
#notify_about_creation ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/commands/page/page_generator.rb', line 108 def notify_about_creation say "" if [:template].nil? say "Generated page #{@group}:#{@page_name}" else say "Generated page #{@group}:#{@page_name} using page #{@template_group}:#{@template_name} as a template" end say "" say " Page Parameters:" @page_params.each do |param_name, param_value| say " #{param_name} = #{param_value}" end say "" say " Page Variables:" @page_variables.each do |var_name, var_value| puts " #{var_name} = #{var_value}" end say "" end |
#parse_page_group_and_name ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/commands/page/page_generator.rb', line 24 def parse_page_group_and_name if name.nil? say_status "ERROR:", "you need to specify a page name.", :red exit end @page_name, @group = Moka::Utilities.parse_grouppage name end |
#update_manifest ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/commands/page/page_generator.rb', line 60 def update_manifest @page_params = {"order" => Time.new.utc.to_i} unless [:template].nil? or @manifest["site"][@template_group][@template_name]["layout"].nil? @page_params["layout"] = @manifest["site"][@template_group][@template_name]["layout"] end unless @group == "root" @page_params["path"] = File.join [@group, @page_name + "." + (@site.respond_to?(:default_extension) ? @site.default_extension : "html")] else @page_params["path"] = @page_name + "." + (@site.respond_to?(:default_extension) ? @site.default_extension : "html") end unless @site.find_group(@group).nil? @manifest["site"][@group][@page_name] = @page_params else @manifest["site"][@group] = {"order" => Time.new.utc.to_i, @page_name => @page_params} end # dump manifest f = File.open( File.('manifest.yml', MOKA_ROOT), 'w' ) do |out| YAML.dump( @manifest, out ) end say_status "update", "manifest.yml", :green end |
#verify_if_page_exists ⇒ Object
37 38 39 40 41 42 |
# File 'lib/commands/page/page_generator.rb', line 37 def verify_if_page_exists unless @site.find_group(@group).nil? or @site.find_group(@group).find_page(@page_name).nil? say_status "ERROR:", "page '#{@page_name}' already exists in group '#{@group}'!", :red exit end end |