Class: CMD::New

Inherits:
Zarchitect show all
Defined in:
lib/zarchitect/cmd_new.rb

Constant Summary

Constants inherited from Zarchitect

Zarchitect::ASSETDIR, Zarchitect::ASSETSDIR, Zarchitect::BUILDIR, Zarchitect::CONFIGDIR, Zarchitect::DEBUGSDIR, Zarchitect::DRAFTDIR, Zarchitect::FILEDIR, Zarchitect::FILESDIR, Zarchitect::HTMLDIR, Zarchitect::LAYOUTDIR, Zarchitect::NODEDIR, Zarchitect::SHARESDIR, Zarchitect::VERSION

Instance Method Summary collapse

Methods inherited from Zarchitect

#main, #rss

Constructor Details

#initializeNew

Returns a new instance of New.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/zarchitect/cmd_new.rb', line 5

def initialize
  Zarchitect.sconf.each { |s| Zarchitect.add_section(s) }
  @section = Zarchitect.section(GPI::CLU.parameters[0])
  if @section.nil?
    GPI.print "Error: Section with key #{GPI::CLU.parameters[0]} does " +
      "not exist."
    GPI.quit
  end
  @category = nil
  if GPI::CLU.parameters.size > 2
    @category = @section.find_category(GPI::CLU.parameters[1])
    if @category.nil?
      GPI.print "Error: category with key #{GPI::CLU.parameters[1]} " +
        "not found in #{@section}."
      GPI.quit
    end
    @title = GPI::CLU.parameters[2]
    @dir = File.join(@section.key, @category.key)
  else
    @title = GPI::CLU.parameters[1]
    @dir = File.join(@section.key)
  end
end

Instance Method Details

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zarchitect/cmd_new.rb', line 29

def run
  check_key
  @id = get_id
  # write file
  a = ZERB.new(File.join(Util.path_to_data, "post.md.erb"))
  data = Hash.new
  data["title"] = @title
  data["key"] = @title
  data["date"] = Time.now
  data["author"] = Zarchitect.conf.admin
  data["id"] = @id
  data["category"] = @category
  a.handle_data(data)
  a.prepare
  a.render
  str = a.output
  @dest = File.join(@dir, "#{@id}-#{@title}.md")
  GPI.print "Writing #{@dest}"
  if File.exist?(@dest)
    GPI.print "Error: File at #{@dest} already exists!"
    GPI.quit
  end
  File.open(@dest, "w") { |f| f.write(str) }
  GPI.print "Wrote #{@dest}"
end