Method: Webby::Builder.create

Defined in:
lib/webby/builder.rb

.create(page, opts = {}) ⇒ Object

call-seq:

Builder.create( page, :from => template, :locals => {} )

This mehod is used to create a new page in the content folder based on the specified template. page is the relative path to the new page from the content/ folder. The template is the name of the template to use from the templates/ folder.

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/webby/builder.rb', line 34

def create( page, opts = {} )
  tmpl = opts[:from]
  raise Error, "template not given" unless tmpl

  name = ::Webby::Resources.basename(page)
  ext  = ::Webby::Resources.extname(page)
  dir  = ::File.dirname(page)
  dir  = '' if dir == '.'

  if tmpl.pathmap('%n') =~ %r/^_/
    page = ::File.join(::Webby.site.content_dir, dir, '_'+name)
    page << '.' << (ext.empty? ? 'txt' : ext)
  elsif ::Webby.site.create_mode == 'directory' and name != 'index'
    page = ::File.join(::Webby.site.content_dir, dir, name, 'index')
    page << '.' << (ext.empty? ? 'txt' : ext)
  else
    page = ::File.join(::Webby.site.content_dir, page)
    page << '.txt' if ext.empty?
  end
  raise Error, "#{page} already exists" if test ?e, page

  Logging::Logger[self].info "creating #{page}"
  FileUtils.mkdir_p ::File.dirname(page)

  context = scope
  opts[:locals].each do |k,v|
    Thread.current[:value] = v
    definition = "#{k} = Thread.current[:value]"
    eval(definition, context)
  end if opts.has_key?(:locals)

  str = ERB.new(::File.read(tmpl), nil, '-').result(context)
  ::File.open(page, 'w') {|fd| fd.write str}

  page
end