Module: Stack::Template
- Included in:
- Stack::Templates::Layout, Stack::Templates::Page
- Defined in:
- lib/stack/template.rb
Instance Attribute Summary collapse
-
#basename ⇒ Object
Returns the value of attribute basename.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#generator ⇒ Object
Returns the value of attribute generator.
-
#original_extension ⇒ Object
Returns the value of attribute original_extension.
-
#path ⇒ Object
Returns the value of attribute path.
-
#raw ⇒ Object
Returns the value of attribute raw.
Instance Method Summary collapse
- #initialize(path, generator = nil) ⇒ Object
- #payload ⇒ Object
- #read ⇒ Object
- #render(_payload = { }, do_layout = true) ⇒ Object
- #template_payload ⇒ Object
- #to_hash ⇒ Object
- #transform(content = self.raw) ⇒ Object
- #write!(content = self.render) ⇒ Object
- #write_basename ⇒ Object
- #write_filename ⇒ Object
- #write_path ⇒ Object
Instance Attribute Details
#basename ⇒ Object
Returns the value of attribute basename.
5 6 7 |
# File 'lib/stack/template.rb', line 5 def basename @basename end |
#extension ⇒ Object
Returns the value of attribute extension.
8 9 10 |
# File 'lib/stack/template.rb', line 8 def extension @extension end |
#generator ⇒ Object
Returns the value of attribute generator.
3 4 5 |
# File 'lib/stack/template.rb', line 3 def generator @generator end |
#original_extension ⇒ Object
Returns the value of attribute original_extension.
7 8 9 |
# File 'lib/stack/template.rb', line 7 def original_extension @original_extension end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/stack/template.rb', line 6 def path @path end |
#raw ⇒ Object
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 |
#payload ⇒ Object
79 80 81 |
# File 'lib/stack/template.rb', line 79 def payload raise InterfaceNotProvided end |
#read ⇒ Object
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_payload ⇒ Object
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_hash ⇒ Object
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_basename ⇒ Object
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_filename ⇒ Object
127 128 129 |
# File 'lib/stack/template.rb', line 127 def write_filename "#{write_basename}.#{self.extension}" end |
#write_path ⇒ Object
115 116 117 |
# File 'lib/stack/template.rb', line 115 def write_path File.join(self.generator.target, self.write_filename) end |