Class: Frank::Output

Inherits:
Base
  • Object
show all
Includes:
Render
Defined in:
lib/frank/output.rb

Constant Summary

Constants included from Render

Render::LAYOUT_EXTS, Render::TMPL_EXTS

Instance Attribute Summary collapse

Attributes inherited from Base

#layouts_folder, #server

Instance Method Summary collapse

Methods included from Render

#ext_from_handler, #layout_ext_or_first, #layout_for, #render, #tilt, #to_file_path

Methods inherited from Base

#call, #call!

Methods included from TemplateHelpers

#lorem, #refresh, #render_partial

Methods included from Rescue

#render_404, #render_500

Constructor Details

#initialize(&block) ⇒ Output

Returns a new instance of Output.



7
8
9
# File 'lib/frank/output.rb', line 7

def initialize(&block)
  instance_eval &block
end

Instance Attribute Details

#dynamic_folderObject

Returns the value of attribute dynamic_folder.



5
6
7
# File 'lib/frank/output.rb', line 5

def dynamic_folder
  @dynamic_folder
end

#environmentObject

Returns the value of attribute environment.



5
6
7
# File 'lib/frank/output.rb', line 5

def environment
  @environment
end

#output_folderObject

Returns the value of attribute output_folder.



5
6
7
# File 'lib/frank/output.rb', line 5

def output_folder
  @output_folder
end

#proj_dirObject

Returns the value of attribute proj_dir.



5
6
7
# File 'lib/frank/output.rb', line 5

def proj_dir
  @proj_dir
end

#static_folderObject

Returns the value of attribute static_folder.



5
6
7
# File 'lib/frank/output.rb', line 5

def static_folder
  @static_folder
end

#templatesObject

Returns the value of attribute templates.



5
6
7
# File 'lib/frank/output.rb', line 5

def templates
  @templates
end

Instance Method Details

#compile_templates(production) ⇒ Object

compile the templates if production and template isn’t index and is html

name a folder based on the template and compile to index.html otherwise compile as is



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/frank/output.rb', line 15

def compile_templates(production)
  dir = File.join(@proj_dir, @dynamic_folder)
  
  Dir[File.join(dir, '**/*')].each do |path|
    if File.file?(path) && !File.basename(path).match(/^(\.|_)/)
      path    = path[ (dir.size + 1)..-1 ]
      ext     = File.extname(path)
      new_ext = ext_from_handler(ext)  
      name    = File.basename(path, ext)
      
      if production == true && "#{name}.#{new_ext}" != 'index.html' && new_ext == 'html'
        new_file = File.join(@output_folder, path.sub(/(\/?[\w-]+)\.[\w-]+$/, "\\1/index.#{new_ext}"))
      else
        new_file = File.join(@output_folder, path.sub(/\.[\w-]+$/, ".#{new_ext}"))  
      end
      
      create_dirs(new_file)
      File.open(new_file, 'w') {|f| f.write render(path) }
      puts " - \033[32mCreating\033[0m '#{new_file}'"
    end
  end
end

#copy_staticObject

copies over static content



45
46
47
48
49
# File 'lib/frank/output.rb', line 45

def copy_static
  puts " - \033[32mCopying\033[0m static content"
  static_folder = File.join(@proj_dir, @static_folder)
  FileUtils.cp_r(File.join(static_folder, '/.'), @output_folder) 
end

#create_dirs(path) ⇒ Object

use path to determine folder name and create the required folders if they don’t exist



40
41
42
# File 'lib/frank/output.rb', line 40

def create_dirs(path)
  FileUtils.makedirs path.split('/').reverse[1..-1].reverse.join('/')
end

#dump(production = false) ⇒ Object

create the dump dir, compile templates, copy over static assets



52
53
54
55
56
57
58
59
60
# File 'lib/frank/output.rb', line 52

def dump(production = false)
  FileUtils.mkdir(@output_folder)
  puts "\nFrank is..."
  puts " - \033[32mCreating\033[0m '#{@output_folder}'"
  
  compile_templates(production)
  copy_static
  puts "\n \033[32mCongratulations, project dumped to '#{@output_folder}' successfully!\033[0m"
end