Module: Stack::Template

Included in:
Stack::Templates::Layout, Stack::Templates::Page
Defined in:
lib/stack/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



5
6
7
# File 'lib/stack/template.rb', line 5

def basename
  @basename
end

#extensionObject

Returns the value of attribute extension.



8
9
10
# File 'lib/stack/template.rb', line 8

def extension
  @extension
end

#generatorObject

Returns the value of attribute generator.



3
4
5
# File 'lib/stack/template.rb', line 3

def generator
  @generator
end

#original_extensionObject

Returns the value of attribute original_extension.



7
8
9
# File 'lib/stack/template.rb', line 7

def original_extension
  @original_extension
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/stack/template.rb', line 6

def path
  @path
end

#rawObject

Returns the value of attribute raw.



10
11
12
# File 'lib/stack/template.rb', line 10

def raw
  @raw
end

Instance Method Details

#initialize(path, generator = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stack/template.rb', line 12

def initialize(path, generator = nil)
  self.generator = generator
  self.path = path
  self.original_extension = "html"
  
  if File.file?(path)
    self.basename = File.basename(path)
    ext = File.extname(path)

    self.original_extension = ext[1, ext.length]
    self.extension = self.original_extension
    
    read
  end
end

#payloadObject

Raises:

  • (InterfaceNotProvided)


79
80
81
# File 'lib/stack/template.rb', line 79

def payload
  raise InterfaceNotProvided
end

#readObject



28
29
30
31
32
# File 'lib/stack/template.rb', line 28

def read
  f = File.open(self.path)
  self.raw = f.read
  f.close
end

#render(_payload = { }, do_layout = true) ⇒ Object



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
70
# File 'lib/stack/template.rb', line 34

def render(_payload = { }, do_layout = true)
  _payload = template_payload.deep_merge(_payload)
  
  layout_name = _payload[:layout]
  
  if (layout_name and do_layout)
    # get layout 
    _tpl_payload = self.generator.layouts[layout_name].template_payload
    _tpl_payload.delete(:layout)
    _tpl_payload.delete(:template)
    _tpl_payload.delete(:generator)
    
    #puts _tpl_payload.inspect
    #puts "\n"
    
    _payload = _payload.merge(_tpl_payload)
  end
  
  #puts _payload.inspect
  #puts "\n\n"
  
  content = Liquid::Template.parse(self.raw).render(Mash.new(_payload), Stack::Filters::Register.extensions)
  content = self.transform(content)
  
  if (layout_name and do_layout)
    _payload.delete(:layout)
    begin
      _payload = _payload.merge({ :content => content })

      content = self.generator.layouts[layout_name].render(_payload)
    rescue => e
      STDERR.puts e.to_str
    end
  end
  
  return content
end

#template_payloadObject



72
73
74
75
76
77
# File 'lib/stack/template.rb', line 72

def template_payload
  {
    :template => self.to_hash,
    :generator => self.generator.to_hash
  }.merge(self.payload)
end

#to_hashObject



83
84
85
86
87
# File 'lib/stack/template.rb', line 83

def to_hash
  {
    
  }.merge(self.payload)
end

#transform(content = self.raw) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/stack/template.rb', line 89

def transform(content = self.raw)
  case self.basename
  when /\.textile/
    self.extension = "html"
    RedCloth.new(content).to_html
  when /\.(mdown|markdown|mkdn|md)/
    self.extension = "html"
    Maruku.new(content).to_html
  when /\.(less)/
    self.extension = "css"
    Less::Engine.new(content).to_css
  when /\.(liquid|liq)/
    self.extension = "html"
  else
    content
  end
end

#write!(content = self.render) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/stack/template.rb', line 107

def write!(content = self.render)
  FileUtils.mkdir_p(File.dirname(write_path))
  out = File.open(write_path, "w+")
  out.rewind
  out.write(content)
  out.close
end

#write_basenameObject



119
120
121
122
123
124
125
# File 'lib/stack/template.rb', line 119

def write_basename
  if self.basename
    self.basename.gsub(".#{self.original_extension}", "")
  else
    @write_basename ||= Digest::MD5.hexdigest(self.raw)
  end
end

#write_filenameObject



127
128
129
# File 'lib/stack/template.rb', line 127

def write_filename
  "#{write_basename}.#{self.extension}"
end

#write_pathObject



115
116
117
# File 'lib/stack/template.rb', line 115

def write_path
  File.join(self.generator.target, self.write_filename)
end