slim 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  *.swp
2
2
  pkg
3
3
  readme.html
4
+ .yardoc/
5
+ doc/
data/lib/slim.rb CHANGED
@@ -8,7 +8,7 @@ require 'slim/engine'
8
8
  module Slim
9
9
  class << self
10
10
  def version
11
- '0.2.0'
11
+ '0.3.0'
12
12
  end
13
13
  end
14
14
  end
data/lib/slim/compiler.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'slim/optimizer'
4
+
3
5
  module Slim
4
6
  module Compiler
7
+ include Optimizer
5
8
  AUTOCLOSED = %w(meta img link br hr input area param col base)
6
9
 
7
10
  REGEX = /^(\s*)(!?`?-?=?\w*)(\s*\w*=".+")?(.*)/
8
11
 
9
12
  def compile
10
- @compiled = "_buf = [];"
13
+ @_buffer = ["_buf = [];"]
11
14
 
12
15
  last_indent = -1; enders = []
13
16
 
@@ -49,7 +52,7 @@ module Slim
49
52
 
50
53
  if ender_indent >= indent
51
54
  unless ender == 'end;' && line_type == :control_code
52
- @compiled << ender
55
+ @_buffer << ender
53
56
  end
54
57
  else
55
58
  enders << [ender, ender_indent]
@@ -63,41 +66,47 @@ module Slim
63
66
  case line_type
64
67
  when :markup
65
68
  if AUTOCLOSED.include?(marker)
66
- @compiled << "_buf << \"<#{marker}#{attrs || ''}/>\";"
69
+ @_buffer << "_buf << \"<#{marker}#{attrs || ''}/>\";"
67
70
  else
68
71
  enders << ["_buf << \"</#{marker}>\";", indent]
69
- @compiled << "_buf << \"<#{marker}#{attrs || ''}>\";"
72
+ @_buffer << "_buf << \"<#{marker}#{attrs || ''}>\";"
70
73
  end
71
74
 
72
75
  if string
73
76
  string.lstrip!
74
77
  if string =~ /^=(.*)/
75
- @compiled << "_buf << #{$1.strip};"
78
+ @_buffer << "_buf << #{$1.strip};"
76
79
  else
77
- @compiled << "_buf << \"#{string}\";"
80
+ @_buffer << "_buf << \"#{string}\";"
78
81
  end
79
82
  end
80
83
  when :text
81
- @compiled << "_buf << \"#{string}\";"
84
+ @_buffer << "_buf << \"#{string}\";"
82
85
  when :control_code
83
86
  unless enders.detect{|e| e[0] == 'end;' && e[1] == indent}
84
87
  enders << ['end;', indent]
85
88
  end
86
- @compiled << "#{string};"
89
+ @_buffer << "#{string};"
87
90
  when :output_code
88
- @compiled << "_buf << #{string};"
91
+ @_buffer << "_buf << #{string};"
89
92
  when :declaration
90
- @compiled << "_buf << \"<!#{string}>\";"
93
+ @_buffer << "_buf << \"<!#{string}>\";"
91
94
  else
92
95
  raise NotImplementedError.new("Don't know how to parse line: #{line}")
93
96
  end
94
97
  end # template iterator
95
98
 
96
99
  enders.reverse_each do |t|
97
- @compiled << t[0].to_s
100
+ @_buffer << t[0].to_s
98
101
  end
99
102
 
100
- @compiled << "_buf.join;"
103
+ @_buffer << "_buf.join;"
104
+
105
+ @compiled = @_buffer.join
106
+
107
+ optimize
108
+
109
+ return nil
101
110
  end
102
111
  end
103
112
  end
data/lib/slim/engine.rb CHANGED
@@ -3,6 +3,7 @@ module Slim
3
3
  include Compiler
4
4
 
5
5
  attr_reader :compiled
6
+ attr_reader :optimized
6
7
 
7
8
  # @param template The .slim template to convert
8
9
  # @return [Slim::Engine] instance of engine
@@ -12,7 +13,7 @@ module Slim
12
13
  end
13
14
 
14
15
  def render(scope = Object.new, locals = {})
15
- scope.instance_eval(compiled)
16
+ scope.instance_eval(optimized)
16
17
  end
17
18
  end
18
19
  end
@@ -0,0 +1,70 @@
1
+ module Slim
2
+ # Given the following example:
3
+ # html
4
+ # head
5
+ # meta name="description" content="This is a Slim Test, that's all"
6
+ # title Simple Test Title
7
+ # body
8
+ # - if logged_in?
9
+ # p
10
+ # ` Welcome!
11
+ # - else
12
+ # p
13
+ # ` Please sign in.
14
+ #
15
+ # When compiling the above code to be eval'd, Slim produces a
16
+ # compiled string that looks like:
17
+ #
18
+ # buf = [];
19
+ # _buf << "<html>";
20
+ # _buf << "<head>";
21
+ # _buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";
22
+ # _buf << "<title>";
23
+ # _buf << "Simple Test Title";
24
+ # _buf << "</title>";
25
+ # _buf << "</head>";
26
+ # _buf << "<body>";
27
+ # if logged_in?;
28
+ # _buf << "<p>";
29
+ # _buf << "Welcome!";
30
+ # _buf << "</p>";
31
+ # else;
32
+ # _buf << "<p>";
33
+ # _buf << "Please sign in.";
34
+ # _buf << "</p>";
35
+ # end;
36
+ # _buf << "</body>";
37
+ # _buf << "</html>";
38
+ # _buf.join;
39
+ #
40
+ # The optimized string after:
41
+ #
42
+ # buf = [];
43
+ # _buf << "<html><head><meta name=\"description\" content=\"This is a Slim Test, that's all\"/><title>Simple Test Title</title></head><body>";
44
+ # if logged_in?;
45
+ # _buf << "<p>Welcome!</p>";
46
+ # else;
47
+ # _buf << "<p>Please sign in.</p>";
48
+ # end;
49
+ # _buf << "</body></html>";
50
+ # _buf.join;
51
+ module Optimizer
52
+ def optimize
53
+ @optimized = ""
54
+ string = nil
55
+ @_buffer.each do |line|
56
+ if line =~ /^_buf << "(.+)"/
57
+ string ||= ""
58
+ string << $1
59
+ else
60
+ if string
61
+ @optimized << "_buf << \"#{string}\";"
62
+ end
63
+ @optimized << line
64
+ string = nil
65
+ end
66
+ end
67
+ return nil
68
+ end
69
+ end # Optimizer
70
+ end # Slim
data/lib/slim/rails.rb CHANGED
@@ -6,10 +6,12 @@ module ActionView
6
6
  include Compilable
7
7
 
8
8
  def compile(template)
9
- return Slim::Engine.new(template.source).compiled
9
+ return Slim::Engine.new(template.source).optimized
10
10
  end
11
11
  end
12
12
  end
13
13
 
14
14
  Template.register_default_template_handler :slim, TemplateHandlers::SlimHandler
15
15
  end
16
+
17
+ puts ">> Slim (v#{Slim.version})"
data/slim.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{slim}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Stone"]
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  "lib/slim.rb",
23
23
  "lib/slim/compiler.rb",
24
24
  "lib/slim/engine.rb",
25
+ "lib/slim/optimizer.rb",
25
26
  "lib/slim/rails.rb",
26
27
  "slim.gemspec",
27
28
  "test/helper.rb",
data/test/test_slim.rb CHANGED
@@ -1,7 +1,4 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestSlim < MiniTest::Unit::TestCase
4
- def test_version_is_current
5
- assert_equal '0.1.0', Slim.version
6
- end
7
4
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Stone
@@ -33,6 +33,7 @@ files:
33
33
  - lib/slim.rb
34
34
  - lib/slim/compiler.rb
35
35
  - lib/slim/engine.rb
36
+ - lib/slim/optimizer.rb
36
37
  - lib/slim/rails.rb
37
38
  - slim.gemspec
38
39
  - test/helper.rb