Class: Todidnt::HTMLGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/todidnt/html_generator.rb

Constant Summary collapse

SOURCE_PATH =
File.join(
  File.dirname(File.expand_path(__FILE__)),
  '../../templates'
)
DESTINATION_PATH =
'.todidnt'

Class Method Summary collapse

Class Method Details

.destination_path(path = nil) ⇒ Object



56
57
58
# File 'lib/todidnt/html_generator.rb', line 56

def self.destination_path(path=nil)
  path ? "#{DESTINATION_PATH}/#{path}" : DESTINATION_PATH
end

.from_template(template) ⇒ Object



60
61
62
# File 'lib/todidnt/html_generator.rb', line 60

def self.from_template(template)
  Tilt::ERBTemplate.new(source_path("#{template}.erb"))
end

.generate(template, context = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/todidnt/html_generator.rb', line 35

def self.generate(template, context={})
  generate_common

  content_template = from_template(template)
  layout_template = from_template(:layout)

  inner_content = content_template.render nil, context
  result = layout_template.render { inner_content }

  file_name = destination_path("todidnt_#{template}.html")
  File.open(file_name, 'w') do |file|
    file.write(result)
  end

  File.absolute_path(file_name)
end

.generate_commonObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/todidnt/html_generator.rb', line 14

def self.generate_common
  # Create the destination folder unless it already exists.
  FileUtils.mkdir_p(DESTINATION_PATH)

  # Copy over directories (e.g. js, css) to the destination.
  common_dirs = []
  Dir.chdir(SOURCE_PATH) do
    common_dirs = Dir.glob('*').select do |dir|
      File.directory?(dir)
    end
  end

  common_dirs.each do |dir|
    FileUtils.cp_r(
      source_path(dir),
      destination_path,
      :remove_destination => true
    )
  end
end

.source_path(path = nil) ⇒ Object



52
53
54
# File 'lib/todidnt/html_generator.rb', line 52

def self.source_path(path=nil)
  path ? "#{SOURCE_PATH}/#{path}" : SOURCE_PATH
end