Class: Gumdrop::Generator

Inherits:
Object
  • Object
show all
Includes:
Util::SiteAccess
Defined in:
lib/gumdrop/generator.rb

Defined Under Namespace

Classes: DSL

Instance Method Summary collapse

Methods included from Util::SiteAccess

#parent, #site

Methods included from Util::Loggable

#log

Methods included from Util::Eventable

#clear_events, #event_block, #fire

Constructor Details

#initialize(content = nil, opts = {}, &block) ⇒ Generator

block?



6
7
8
9
10
11
12
# File 'lib/gumdrop/generator.rb', line 6

def initialize(content=nil, opts={}, &block) # block?
  @content= content || block
  @dsl= DSL.new self
  @pages= []
  @filename= content.nil? ? (opts[:filename] || '') : content.filename
  @base_path= content.nil? ? (opts[:base_path] || '') : content.dirname 
end

Instance Method Details

#_render_inline_content(opts) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/gumdrop/generator.rb', line 66

def _render_inline_content(opts)
  Proc.new {
    renderer= site.active_renderer || Renderer.new
    raise "Generator: page :render=>'partial' param is missing!" unless opts[:render]
    content= site.resolve(opts[:render], opts)
    opts[:inline_render]= true
    renderer.draw content, opts
  }
end

#executeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gumdrop/generator.rb', line 52

def execute
  log.debug "(Generator '#{ @filename }')"
  if @content.is_a? Proc
    if @content.arity == 1
      @content.call @dsl
    else
      @dsl.instance_eval &@content
    end
  else
    @dsl.instance_eval @content.body
  end
  log.debug "   created: #{ @pages.size } pages"
end

#gen_page(name, opts = {}, params = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gumdrop/generator.rb', line 30

def gen_page(name, opts={}, params={}, &block)
  event_block :generate_item do
    name.relative!
    opts= params.reverse_merge(opts)
    filepath= if @base_path.empty?
        site.source_path / name
      else
        site.source_path / @base_path / name
      end
    if block.nil?
      handler= _render_inline_content opts
      content= site.contents.create filepath, self, &handler          
    else
      content= site.contents.create filepath, self, &block
    end
    content.params.merge! opts
    content.params.merge! opts[:params] if opts.has_key?(:params)
    log.debug " generated: #{content.uri}"
    @pages << content
  end
end

#pagesObject



26
27
28
# File 'lib/gumdrop/generator.rb', line 26

def pages
  @pages
end

#reloadObject



21
22
23
24
# File 'lib/gumdrop/generator.rb', line 21

def reload
  unload
  execute
end

#unloadObject



14
15
16
17
18
19
# File 'lib/gumdrop/generator.rb', line 14

def unload
  pages.each do |content|
    site.contents.remove content
  end
  @dsl= DSL.new self
end