Class: Reacco::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/reacco/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(readme, dir, template = nil, css = nil) ⇒ Generator

Returns a new instance of Generator.



3
4
5
6
7
8
# File 'lib/reacco/generator.rb', line 3

def initialize(readme, dir, template=nil, css=nil)
  @readme   = readme
  @dir      = dir
  @css      = css
  @template = template || Reacco.root('data')
end

Instance Method Details

#append_cssObject

Adds the CSS file to the style.css file.



32
33
34
35
# File 'lib/reacco/generator.rb', line 32

def append_css
  contents = File.read(@css)
  File.open("#{@dir}/style.css", 'a+') { |f| f.write "\n/* Custom */\n#{contents}" }
end

#copy_files(&blk) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reacco/generator.rb', line 37

def copy_files(&blk)
  files = Dir.chdir(@template) { Dir['**/*'] }

  # For each of the template files...
  files.each do |f|
    next  if File.basename(f)[0] == '_'
    next  if File.fnmatch('index.*', f)
    ext = File.extname(f)[1..-1]

    fullpath = File.join @template, f

    # Try to render it with Tilt if possible.
    if Tilt.mappings.keys.include?(ext)
      contents = Tilt.new(fullpath).render
      outfile  = f.match(/^(.*)(\.[^\.]+)$/) && $1
    else
      contents = File.read(fullpath)
      outfile  = f
    end

    yield "#{@dir}/#{outfile}"
    write_to "#{@dir}/#{outfile}", contents
  end
end

#htmlObject

Returns the HTML contents.



11
12
13
14
15
16
17
# File 'lib/reacco/generator.rb', line 11

def html
  file = Dir["#{@template}/index.*"].first
  raise "No index file found in template path."  unless file

  tpl  = Tilt.new(file)
  out  = tpl.render({}, @readme.locals) { @readme.html }
end

#template?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/reacco/generator.rb', line 27

def template?
  File.directory?(@template) && Dir["#{@template}/index.*"].first
end

#write! {|"#{@dir}/index.html"| ... } ⇒ Object

Writes to the output directory.

Yields:

  • ("#{@dir}/index.html")


20
21
22
23
24
25
# File 'lib/reacco/generator.rb', line 20

def write!(&blk)
  yield "#{@dir}/index.html"
  write_to "#{@dir}/index.html", html
  copy_files &blk
  append_css   if @css
end