laydown 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress
3
+ spec
@@ -0,0 +1,45 @@
1
+ #
2
+ # «=»
3
+ # ^
4
+ # $ «« ^¨^ »» $
5
+ # '' ''
6
+ # .
7
+ # «=»
8
+ # ^
9
+ # «« ^¨^ »»
10
+ # LAYDOWN
11
+ # ''|'|''
12
+ # | | |>
13
+ # | | ______|_
14
+ # | | | oo |
15
+ #'''''''''''''''''''''''''''''******
16
+
17
+ # $ gem install laydown
18
+
19
+ Laydown is a simple template language for defining quick HTML5 layouts in Ruby. Never write the same old boilerplate again.
20
+
21
+ require 'laydown'
22
+
23
+ layout = Laydown.new do |_|
24
+ _.charset 'some crazy charset'
25
+ _.title 'A man in a cave'
26
+ _.description 'A man is sitting in a cave.'
27
+ _.keywords 'man, cave, mystery', @keywords
28
+ _.favicon '/maninacave.png'
29
+ _.css '/maninacave/style.css'
30
+ _.css @css, '/site.css'
31
+ _.js @js
32
+ _.inline_js 'alert("Grrr");'
33
+ _.head '<meta generator="FlushFlox Super Deluxe">'
34
+ _.ga_code 'UA-8079526-5' # google analytics
35
+ _.body_class @body_class
36
+ end
37
+
38
+ @css = 'somesheet.css'
39
+ @body_class = 'front'
40
+
41
+ layout.render(self) { '<p>this comes in the body</p>' }
42
+ # => your html5 layout
43
+
44
+ The block given to `Laydown.new` will be evaluated in the context passed to the `render` method.
45
+
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  gem.email = "post@jostein.be"
11
11
  gem.homepage = "http://github.com/jbe/laydown"
12
12
  gem.authors = ["jbe"]
13
- gem.add_dependency "instant_dsl", ">= 0"
13
+ gem.add_dependency "backports" #, ">= 0"
14
14
  gem.add_development_dependency "tenjin", ">= 0"
15
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
16
  end
@@ -21,7 +21,24 @@ end
21
21
 
22
22
  namespace :build do
23
23
  desc 'Builds Tenjin templates'
24
- task :templates do
25
- sh 'rbtenjin -s templates/default_layout.tenjin.html > lib/templates/default_layout.rb'
24
+ task :template do
25
+
26
+ require 'haml'
27
+ src = File.read('template/default.haml')
28
+ compiled = Haml::Engine.new(src).precompiled
29
+ File.open('lib/laydown/template.rb', 'w') do |f|
30
+ f.write <<-RUBY
31
+ require 'haml'
32
+ #require 'haml/helpers'
33
+ #require 'haml/util'
34
+ #require 'haml/buffer'
35
+ extend Haml::Helpers
36
+ _hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer)
37
+ RUBY
38
+ f.write(compiled)
39
+ f.write "@haml_buffer = @haml_buffer.upper"
40
+ end
41
+
42
+ #sh 'rbtenjin -s templates/default_layout.tenjin.html > lib/templates/default_layout.rb'
26
43
  end
27
44
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -5,35 +5,34 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{laydown}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jbe"]
12
- s.date = %q{2011-02-01}
12
+ s.date = %q{2011-02-03}
13
13
  s.description = %q{Provides a simple Ruby DSL for defining HTML5 layouts for web apps. For those of us who has written basically the same html head 200 times and feels like minimalism.}
14
14
  s.email = %q{post@jostein.be}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ ".rspec",
21
22
  "LICENSE",
22
- "README",
23
+ "README.md",
23
24
  "Rakefile",
24
25
  "VERSION",
25
26
  "laydown.gemspec",
26
27
  "lib/laydown.rb",
27
- "lib/templates/default_layout.rb",
28
- "templates/default_layout.tenjin.html",
29
- "test/rough.rb"
28
+ "spec/laydown_spec.rb"
30
29
  ]
31
30
  s.homepage = %q{http://github.com/jbe/laydown}
32
31
  s.require_paths = ["lib"]
33
32
  s.rubygems_version = %q{1.3.7}
34
33
  s.summary = %q{Pure Ruby HTML5 layout DSL for microframeworks.}
35
34
  s.test_files = [
36
- "test/rough.rb"
35
+ "spec/laydown_spec.rb"
37
36
  ]
38
37
 
39
38
  if s.respond_to? :specification_version then
@@ -41,14 +40,14 @@ Gem::Specification.new do |s|
41
40
  s.specification_version = 3
42
41
 
43
42
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
- s.add_runtime_dependency(%q<instant_dsl>, [">= 0"])
43
+ s.add_runtime_dependency(%q<backports>, [">= 0"])
45
44
  s.add_development_dependency(%q<tenjin>, [">= 0"])
46
45
  else
47
- s.add_dependency(%q<instant_dsl>, [">= 0"])
46
+ s.add_dependency(%q<backports>, [">= 0"])
48
47
  s.add_dependency(%q<tenjin>, [">= 0"])
49
48
  end
50
49
  else
51
- s.add_dependency(%q<instant_dsl>, [">= 0"])
50
+ s.add_dependency(%q<backports>, [">= 0"])
52
51
  s.add_dependency(%q<tenjin>, [">= 0"])
53
52
  end
54
53
  end
@@ -1,51 +1,131 @@
1
1
 
2
- require 'instant_dsl'
2
+ require 'backports'
3
+
3
4
 
4
5
  module Laydown
6
+ TOPOBJECT = defined?(BasicObject) ? BasicObject : Object
5
7
 
6
- DSL_WORDS = [:title, :description, :favicon, :keywords, :css, :js, :head, :body]
7
- DSL = InstantDSL[*DSL_WORDS]
8
+ def self.new(&blk)
9
+ Template.new(&blk)
10
+ end
8
11
 
9
- class NoScope < Object; end
12
+ class Template
13
+ def initialize(&blk)
14
+ @layout = blk
15
+ end
10
16
 
11
- class TemplateScope
12
- def initialize(v)
13
- @title = v[:title].join
14
- @description = v[:description].join
15
- @favicon = v[:favicon].first
16
- @keywords = v[:keywords].join(', ')
17
- @stylesheets = v[:css].compact
18
- @javascripts = v[:js].compact
19
- @head = v[:head].join("\n")
20
- @body = v[:body].join("\n")
17
+ def render(scope=Object.new, &blk)
18
+ dsl = ::Laydown::DSL.new
19
+ scope.instance_exec(dsl, &@layout)
20
+ Renderer.new(dsl._captured_values, &blk).render
21
21
  end
22
22
  end
23
23
 
24
- def self.layout(&conf)
25
- Template.new(&conf)
26
- end
24
+ class DSL
25
+ TARGETS = %w{
26
+ charset lang title description favicon keywords css js
27
+ inline_js head body ga_code body_class
28
+ }
27
29
 
28
- class Template
29
- def initialize(&conf)
30
- @conf = conf
31
- @tpath = File.expand_path('../templates/default_layout.rb', __FILE__)
32
- @template = IO.read(@tpath)
30
+ attr_reader :_captured_values
31
+
32
+ def initialize
33
+ @_captured_values = Hash.new { [] }
34
+ end
35
+
36
+ def _laydown(name, *values)
37
+ @_captured_values[name] += values
33
38
  end
34
39
 
35
- def render(source_scope=::Laydown::NoScope.new)
36
- dsl_scope = ::Laydown::DSL.new(source_scope)
40
+ TARGETS.each do |name|
41
+ class_eval <<-RUBY
42
+ def #{name}(*a); _laydown('#{name}', *a); end
43
+ RUBY
44
+ end
45
+ end
37
46
 
38
- dsl_scope.send :instance_eval, &@conf
47
+ class Renderer
39
48
 
40
- scope = ::Laydown::TemplateScope.new(dsl_scope.dsl_values)
41
- scope.send :instance_eval, @template, @tpath
49
+ def initialize(v)
50
+
51
+ @charset = v['charset'].first || 'utf-8'
52
+ @lang = v['lang'].first
53
+ @title = v['title'].compact.join(' &ndash; ')
54
+ @favicon = v['favicon'].first
55
+ @description = v['description'].compact.join(' ') if v['description']
56
+ @keywords = v['keywords'].compact.join(', ') if v['keywords']
57
+ @css = v['css'].compact
58
+ @js = v['js'].compact
59
+ @inline_js = v['inline_js'].compact
60
+ @head = v['head'].compact.join("\n")
61
+ @ga_code = v['ga_code'].first
62
+ @body_class = v['body_class'].compact.join(' ') if v['body_class']
63
+ @body = [block_given? ? yield : nil, v['body']].
64
+ flatten.compact.join("\n")
42
65
  end
43
66
 
44
- end
67
+ def render
68
+ "<!DOCTYPE html>\n" +
69
+ _(:html, {:lang => @lang},
70
+ _(:head, {},
71
+ _(:meta, {:charset => @charset}) +
72
+ _(:title, {}, @title) +
73
+ (@favicon ?
74
+ _(:link, {:rel => 'shortcut icon', :href => @favicon}) : '') +
75
+ (@description ?
76
+ _(:meta, {:description => @description}) : '') +
77
+ (@keywords ?
78
+ _(:meta, {:keywords => @keywords}) : '') +
79
+ @css.map do |url|
80
+ _(:link, {:rel => :stylesheet, :type => 'text/css', :href => url})
81
+ end.join +
82
+ @js.map do |url|
83
+ _(:script, {:type => 'text/javascript', :src => url}, '')
84
+ end.join +
85
+ @inline_js.map do |code|
86
+ _(:script, {:type => 'text/javascript'}, code)
87
+ end.join +
88
+ (@ga_code ? google_analytics_js : '') +
89
+ @head
90
+ ) +
91
+ _(:body, {:class => @body_class},
92
+ @body
93
+ )
94
+ )
95
+ end
45
96
 
46
- end
47
97
 
98
+ # html helpers
48
99
 
100
+ def _(name, props={}, contents=nil)
101
+ tag_str = name.to_s + prop_str(props)
102
+ if contents
103
+ "<#{tag_str}>\n#{contents}\n</#{name}>\n"
104
+ else
105
+ "<#{tag_str}/>\n"
106
+ end
107
+ end
49
108
 
109
+ def prop_str(hsh)
110
+ return '' if hsh.values.compact.empty?
111
+ ' ' + hsh.map do |k,v|
112
+ "#{k.to_s}='#{v.to_s}'" if v
113
+ end.compact.join(' ')
114
+ end
115
+
116
+ def google_analytics_js
117
+ <<-JS
118
+ var _gaq = _gaq || [];
119
+ _gaq.push(['_setAccount', '#{@ga_code}']);
120
+ _gaq.push(['_trackPageview']);
121
+ (function() {
122
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
123
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
124
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
125
+ })();
126
+ JS
127
+ end
50
128
 
129
+ end
130
+ end
51
131
 
@@ -0,0 +1,36 @@
1
+
2
+ require 'laydown'
3
+
4
+
5
+ describe Laydown do
6
+
7
+ subject do
8
+ Laydown.new do |_|
9
+ _.charset 'iso-piso'
10
+ _.title 'Apple sucks'
11
+ _.description 'Yes, it does. And so do their fans.'
12
+ _.favicon 'applesuck.png'
13
+ _.keywords 'apple suck lol'
14
+ _.css 'applesuck.css'
15
+ _.js 'applesuck.js'
16
+ _.inline_js "alert('#{@msg}');"
17
+ _.head '<meta generator="Ubuntu">'
18
+ _.body @body
19
+ _.ga_code 'GA-UGGABUGGA'
20
+ end
21
+ end
22
+
23
+ it 'generates properly' do
24
+ @msg = 'lol @ iPhone'
25
+ @body = '<p>Seriously.</p>'
26
+ str = subject.render(self)
27
+ str.should == nil
28
+
29
+ #str.match(/lang="en"/).should
30
+ #str.match(/charset="iso-piso"/)
31
+ #str.match(/GA-UGGABUGGA/)
32
+ end
33
+
34
+ end
35
+
36
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - jbe
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-01 00:00:00 +01:00
17
+ date: 2011-02-03 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: instant_dsl
21
+ name: backports
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -51,18 +51,17 @@ extensions: []
51
51
 
52
52
  extra_rdoc_files:
53
53
  - LICENSE
54
- - README
54
+ - README.md
55
55
  files:
56
56
  - .document
57
+ - .rspec
57
58
  - LICENSE
58
- - README
59
+ - README.md
59
60
  - Rakefile
60
61
  - VERSION
61
62
  - laydown.gemspec
62
63
  - lib/laydown.rb
63
- - lib/templates/default_layout.rb
64
- - templates/default_layout.tenjin.html
65
- - test/rough.rb
64
+ - spec/laydown_spec.rb
66
65
  has_rdoc: true
67
66
  homepage: http://github.com/jbe/laydown
68
67
  licenses: []
@@ -96,4 +95,4 @@ signing_key:
96
95
  specification_version: 3
97
96
  summary: Pure Ruby HTML5 layout DSL for microframeworks.
98
97
  test_files:
99
- - test/rough.rb
98
+ - spec/laydown_spec.rb
data/README DELETED
@@ -1,48 +0,0 @@
1
- #
2
- # .
3
- # «=» ☺
4
- # ^
5
- # «« ^¨^ »»
6
- # LAYDOWN
7
- # ''|'|''
8
- # | |
9
- # | |
10
- # | |
11
- #'''''''''''''''''''''''''''''
12
- # $ gem install laydown
13
- # «=»
14
- # ^
15
- # $ «« ^¨^ »» $
16
- # http://jostein.be
17
- # '' ''
18
- #
19
- # Provides a simple Ruby DSL for defining HTML5 layouts
20
- # for web apps. For those of us who has written basically
21
- # the same html head 200 times and want to streamline it.
22
-
23
- require 'laydown'
24
-
25
- layout = Laydown.layout do
26
- title 'A man in a cave'
27
- description 'A man is sitting in a cave. What will he do?'
28
- keywords 'man, cave, mystery', @keywords
29
- favicon '/maninacave.png'
30
- css '/maninacave/style.css'
31
- css @css
32
- js '/maninacave/site.js'
33
- js @js
34
-
35
- body @body || '<p>Empty</p>'
36
- end
37
-
38
-
39
- @css = 'somesheet.css'
40
- @body = '<h1>A man in a cave</h1>'
41
-
42
- puts layout.render(self) # => beautiful html5
43
-
44
- # Note the binding passed to render (in this case self)
45
- # -- its instance variables are made accessible to the
46
- # layout block.
47
-
48
-
@@ -1,30 +0,0 @@
1
- _buf = ''; _buf << %Q`<!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8">
5
- <title>#{@title}</title>\n`
6
- if @description
7
- _buf << %Q` <meta name="description" content="#{@description}">\n`
8
- end
9
- if @keywords
10
- _buf << %Q` <meta name="keywords" content="#{@keywords}">\n`
11
- end
12
- if @favicon
13
- _buf << %Q` <link rel="shortcun icon" href="#{@favicon}">\n`
14
- end
15
- _buf << %Q`\n`
16
- @stylesheets.each do |sheet|
17
- _buf << %Q` <link rel="stylesheet" type="text/css" href="#{sheet}">\n`
18
- end
19
- _buf << %Q`\n`
20
- @javascripts.each do |script|
21
- _buf << %Q` <script src="#{script}" ></script>\n`
22
- end
23
- _buf << %Q` #{ @head }
24
- </head>
25
-
26
- <body>
27
- #{ @body }
28
- </body>
29
- </html>\n`
30
- _buf.to_s
@@ -1,29 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8">
5
- <title>#{@title}</title>
6
- <?rb if @description ?>
7
- <meta name="description" content="#{@description}">
8
- <?rb end ?>
9
- <?rb if @keywords ?>
10
- <meta name="keywords" content="#{@keywords}">
11
- <?rb end ?>
12
- <?rb if @favicon ?>
13
- <link rel="shortcun icon" href="#{@favicon}">
14
- <?rb end ?>
15
-
16
- <?rb @stylesheets.each do |sheet| ?>
17
- <link rel="stylesheet" type="text/css" href="#{sheet}">
18
- <?rb end ?>
19
-
20
- <?rb @javascripts.each do |script| ?>
21
- <script src="#{script}" ></script>
22
- <?rb end ?>
23
- #{ @head }
24
- </head>
25
-
26
- <body>
27
- #{ @body }
28
- </body>
29
- </html>
@@ -1,19 +0,0 @@
1
- require './lib/laydown'
2
-
3
-
4
- Layout = Laydown.layout do
5
- title 'A man in a cave'
6
- description 'A man is sitting in a cave. What will he do?'
7
- keywords 'man, cave, mystery'
8
- favicon 'maninacave.png'
9
- css '/maninacave/style.css'
10
- css @css
11
- js '/maninacave/site.js'
12
- js @js
13
-
14
- body "<h1>A man in\na cave</h1>"
15
- end
16
-
17
- @css = 'somesheet.css'
18
-
19
- puts Layout.render(self)