Module: Laydown
- Defined in:
- lib/laydown.rb
Defined Under Namespace
Classes: Template
Constant Summary collapse
- DEFAULTS =
{ :charset => 'utf-8', :title => '#{@title}', :description => nil, :favicon => nil, :keywords => [:@keywords], :css => [:@css, :@stylesheets], :js => [:@javascripts, :@js], :inline_js => [:@inline_js], :head => [], :body_class => [], :body => :yield, :ga_code => :@ga_code }
- TEMPLATE =
<<'TEMPLATE' _buf = [] ; _temple_pre_tags = /<pre|<textarea/ ; _buf << ("<!DOCTYPE html><html><head>") ; ; ; ; if data[:title] ; ; _buf << ("<title>#{data[:title]}"\ ""\ "</title>") ; end ; if data[:favicon] ; ; _buf << ("<link rel=\"shortcut icon\" url=\"#{Temple::Utils.escape_html((data[:favicon]))}\">"\ ""\ "") ; end ; if data[:description] ; ; _buf << ("<meta content=\"#{Temple::Utils.escape_html((data[:description]))}\" name=\"description\">"\ ""\ "") ; end ; if data[:keywords].any? ; ; _buf << ("<meta content=\"#{Temple::Utils.escape_html((data[:keywords].flatten.join(', ')))}\" name=\"keywords\">"\ ""\ "") ; end ; data[:css].flatten.each do |url| ; ; unless url == '' ; ; _buf << ("<link href=\"#{Temple::Utils.escape_html((url))}\" rel=\"stylesheet\" type=\"text/css\">"\ ""\ "") ; end ; end ; data[:js].flatten.each do |url| ; ; unless url == '' ; ; _buf << ("<script src=\"#{Temple::Utils.escape_html((url))}\" type=\"text/javascript\"></script>"\ ""\ "") ; end ; end ; if data[:inline_js] && !data[:inline_js].empty? ; ; _buf << ("<script type=\"text/javascript\">") ; ; data[:inline_js].flatten.each do |code| ; ; unless code == '' ; ; _buf << (code) ; ; ; end ; end ; _buf << ("</script>") ; end ; if data[:head] ; ; data[:head].each do |item| ; ; _buf << (item) ; ; ; end ; end ; _buf << ("</head><body class=\"#{Temple::Utils.escape_html((data[:body_class].flatten.join(' ')))}\">"\ "#{data[:body]}"\ ""\ "") ; if data[:ga_code] ; ; ; _buf << ("<script type=\"text/javascript\">var _gaq = _gaq || [];\n_gaq.push(['_setAccount', '"\ "#{Temple::Utils.escape_html((data[:ga_code]))}']);\n_gaq.push(['_trackPageview']);\n(function() {\nvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\nga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\nvar s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n})();</script>"\ ""\ ""\ ""\ ""\ ""\ ""\ "") ; end ; _buf << ("</body></html>") ; _buf = _buf.join TEMPLATE
Class Method Summary collapse
- .compile(values = {}) ⇒ Object
- .literalize(obj) ⇒ Object
- .make_template_from(values) ⇒ Object
- .new(hsh = {}) ⇒ Object
- .plural_properties ⇒ Object
- .process(values) ⇒ Object
- .properties ⇒ Object
- .validate(values) ⇒ Object
Class Method Details
.compile(values = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/laydown.rb', line 35 def compile(values={}) process(values) validate(values) make_template_from(values) end |
.literalize(obj) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/laydown.rb', line 62 def literalize(obj) case obj when String then obj.inspect.gsub(/\\#\{/, '#{') + "+''" when Symbol then obj.to_s when nil then 'nil' when Array '[' + obj.map {|o| literalize(o) }.join(', ') + ']' else obj.to_s.inspect end end |
.make_template_from(values) ⇒ Object
56 57 58 59 60 |
# File 'lib/laydown.rb', line 56 def make_template_from(values) TEMPLATE.gsub(/data\[:([a-zA-Z0-9_]+)\]/) do |m| literalize values[:"#{$1}"] end end |
.new(hsh = {}) ⇒ Object
90 91 92 |
# File 'lib/laydown.rb', line 90 def self.new(hsh={}) Template.new(hsh) end |
.plural_properties ⇒ Object
31 32 33 |
# File 'lib/laydown.rb', line 31 def plural_properties properties.select {|name| DEFAULTS[name].is_a?(Array) } end |
.process(values) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/laydown.rb', line 41 def process(values) DEFAULTS.each {|k,v| values[k] ||= v } plural_properties.each do |k| values[k] = Array(values[k]).flatten.compact end end |
.properties ⇒ Object
27 28 29 |
# File 'lib/laydown.rb', line 27 def properties DEFAULTS.keys end |
.validate(values) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/laydown.rb', line 48 def validate(values) values.keys.each do |value| unless DEFAULTS.keys.include?(value) raise "no Laydown property called #{value.inspect}" end end end |