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 =
"_buf = [] ; _temple_pre_tags = /<pre|<textarea/ ; _buf << (\"<!DOCTYPE html><html><head>\") ; \n ; \n ; \n ; if data[:title] ; \n ; _buf << (\"<title>\#{data[:title]}\"\\\n\"\"\\\n\"</title>\") ; end ; if data[:favicon] ; \n ; _buf << (\"<link rel=\\\"shortcut icon\\\" url=\\\"\#{Temple::Utils.escape_html((data[:favicon]))}\\\">\"\\\n\"\"\\\n\"\") ; end ; if data[:description] ; \n ; _buf << (\"<meta content=\\\"\#{Temple::Utils.escape_html((data[:description]))}\\\" name=\\\"description\\\">\"\\\n\"\"\\\n\"\") ; end ; if data[:keywords].any? ; \n ; _buf << (\"<meta content=\\\"\#{Temple::Utils.escape_html((data[:keywords].flatten.join(', ')))}\\\" name=\\\"keywords\\\">\"\\\n\"\"\\\n\"\") ; end ; data[:css].flatten.each do |url| ; \n ; unless url == '' ; \n ; _buf << (\"<link href=\\\"\#{Temple::Utils.escape_html((url))}\\\" rel=\\\"stylesheet\\\" type=\\\"text/css\\\">\"\\\n\"\"\\\n\"\") ; end ; end ; data[:js].flatten.each do |url| ; \n ; unless url == '' ; \n ; _buf << (\"<script src=\\\"\#{Temple::Utils.escape_html((url))}\\\" type=\\\"text/javascript\\\"></script>\"\\\n\"\"\\\n\"\") ; end ; end ; if data[:inline_js] && !data[:inline_js].empty? ; \n ; _buf << (\"<script type=\\\"text/javascript\\\">\") ; \n ; data[:inline_js].flatten.each do |code| ; \n ; unless code == '' ; \n ; _buf << (code) ; \n ; \n ; end ; end ; _buf << (\"</script>\") ; end ; if data[:head] ; \n ; data[:head].each do |item| ; \n ; _buf << (item) ; \n ; \n ; end ; end ; _buf << (\"</head><body class=\\\"\#{Temple::Utils.escape_html((data[:body_class].flatten.join(' ')))}\\\">\"\\\n\"\#{data[:body]}\"\\\n\"\"\\\n\"\") ; if data[:ga_code] ; \n ; \n ; _buf << (\"<script type=\\\"text/javascript\\\">var _gaq = _gaq || [];\\n_gaq.push(['_setAccount', '\"\\\n\"\#{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>\"\\\n\"\"\\\n\"\"\\\n\"\"\\\n\"\"\\\n\"\"\\\n\"\"\\\n\"\") ; end ; _buf << (\"</body></html>\") ; _buf = _buf.join\n"
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 |