slim 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/README.md +81 -0
- data/Rakefile +55 -0
- data/lib/slim.rb +14 -0
- data/lib/slim/compiler.rb +98 -0
- data/lib/slim/engine.rb +18 -0
- data/lib/slim/rails.rb +15 -0
- data/slim.gemspec +55 -0
- data/test/helper.rb +20 -0
- data/test/slim/test_compiler.rb +281 -0
- data/test/slim/test_engine.rb +81 -0
- data/test/test_slim.rb +7 -0
- metadata +78 -0
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
## Slim
|
2
|
+
|
3
|
+
Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic.
|
4
|
+
|
5
|
+
## What?
|
6
|
+
|
7
|
+
Slim is a Rails 3, Ruby 1.9.2 templating option. I do not intend on making a Rails 2.x compatible version. I don't think it would be difficult, so if you want it, I will happily accept contributions with tests.
|
8
|
+
|
9
|
+
## Why?
|
10
|
+
|
11
|
+
Simply put, I wanted to see if I could pull of a minimalistic template language that was at least matched Erb's speed. Yes, Slim is speedy.
|
12
|
+
|
13
|
+
### The syntax
|
14
|
+
|
15
|
+
I actually like the indentation and tag closing nature of Haml. I don't like the overall result of the markup though, it's a little cryptic. I'm sure, with practice, people read it like the Matrix, but it's never suited me. So why not try to improve it for me? There may be one or two other people with the same thoughts.
|
16
|
+
|
17
|
+
|
18
|
+
So here's what I came up with:
|
19
|
+
|
20
|
+
html
|
21
|
+
head
|
22
|
+
title Slim Examples
|
23
|
+
meta name="keywords" content="template language"
|
24
|
+
body
|
25
|
+
h1 Markup examples
|
26
|
+
div id="content" class="example1"
|
27
|
+
p Nest by indentation
|
28
|
+
|
29
|
+
= yield
|
30
|
+
|
31
|
+
- unless items.empty?
|
32
|
+
table
|
33
|
+
- for item in items do
|
34
|
+
tr
|
35
|
+
td
|
36
|
+
= item.name
|
37
|
+
td
|
38
|
+
= item.price
|
39
|
+
- else
|
40
|
+
p No items found
|
41
|
+
|
42
|
+
div id="footer"
|
43
|
+
` Copyright © 2010 Andrew Stone
|
44
|
+
|
45
|
+
= render partial: 'tracking_code'
|
46
|
+
|
47
|
+
script
|
48
|
+
` $(content).do_something();
|
49
|
+
|
50
|
+
|
51
|
+
### Things to know:
|
52
|
+
|
53
|
+
* Standard Ruby syntax after '-' and '='
|
54
|
+
* __end__ is not required
|
55
|
+
* Ruby code must be on it's own line (__TODO:__ allow inline)
|
56
|
+
* If you're making a method call, wrap the arguments in parenthesis (__TODO:__ make this a non requirement)
|
57
|
+
* Can put content on same line or nest it.
|
58
|
+
* If you nest content (e.g. put it on the next line), start the line with a backtick ('`')
|
59
|
+
* Indentation matters, but it's not as strict as Haml.
|
60
|
+
* If you want to indent 2 spaces, then 5. It's your choice. To nest markup you only need to indent by one space, the rest is gravy.
|
61
|
+
|
62
|
+
|
63
|
+
### Line indicators:
|
64
|
+
|
65
|
+
* `
|
66
|
+
* The backtick tells Slim to just copy the line. It essentially escapes any processing.
|
67
|
+
* -
|
68
|
+
* The dash denotes control code (similar to Haml). Examples of control code are loops and conditionals.
|
69
|
+
* =
|
70
|
+
* The equal sign tells Slim it's a Ruby call that produces output to add to the buffer (similar to Erb and Haml).
|
71
|
+
* !
|
72
|
+
* This is a directive. Most common example:
|
73
|
+
` ! doctype html renders <!doctype html> `
|
74
|
+
|
75
|
+
|
76
|
+
### Stuff I need to do (in no particular order)
|
77
|
+
|
78
|
+
* Tackle Encoding.
|
79
|
+
* Optimize the compiled code. I know it can be even faster.
|
80
|
+
* Tackle the __TODO's__ above
|
81
|
+
* ??? Suggestions...
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), "lib", "slim")
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "slim"
|
10
|
+
gem.version = Slim.version
|
11
|
+
gem.rubyforge_project = "slim"
|
12
|
+
gem.summary = "Slim is a template language."
|
13
|
+
gem.description = "Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic."
|
14
|
+
gem.email = "andy@stonean.com"
|
15
|
+
gem.homepage = "http://github.com/stonean/slim"
|
16
|
+
gem.authors = ["Andrew Stone"]
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'yard'
|
25
|
+
YARD::Rake::YardocTask.new do |t|
|
26
|
+
t.files = FileList['lib/**/*.rb']
|
27
|
+
t.options = ['-r'] # optional
|
28
|
+
end
|
29
|
+
rescue LoadError
|
30
|
+
task :yard do
|
31
|
+
abort "YARD is not available. In order to run yard, you must: sudo gem install yard"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rake/testtask'
|
36
|
+
Rake::TestTask.new(:test) do |test|
|
37
|
+
test.libs << 'lib' << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
begin
|
43
|
+
require 'rcov/rcovtask'
|
44
|
+
Rcov::RcovTask.new do |test|
|
45
|
+
test.libs << 'test'
|
46
|
+
test.pattern = 'test/**/test_*.rb'
|
47
|
+
test.verbose = true
|
48
|
+
end
|
49
|
+
rescue LoadError
|
50
|
+
task :rcov do
|
51
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
task :default => 'test'
|
data/lib/slim.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Slim
|
4
|
+
module Compiler
|
5
|
+
AUTOCLOSED = %w(meta img link br hr input area param col base)
|
6
|
+
|
7
|
+
REGEX = /^(\s*)(!?`?-?=?\w*)(\s*\w*=".+")?(.*)/
|
8
|
+
|
9
|
+
def compile
|
10
|
+
@compiled = "_buf = [];"
|
11
|
+
|
12
|
+
last_indent = -1; enders = []
|
13
|
+
|
14
|
+
@template.each_line do |line|
|
15
|
+
line.chomp!; line.rstrip!
|
16
|
+
|
17
|
+
|
18
|
+
next if line.length == 0
|
19
|
+
|
20
|
+
line =~ REGEX
|
21
|
+
|
22
|
+
indent = $1.to_s.length
|
23
|
+
marker = $2
|
24
|
+
attrs = $3
|
25
|
+
string = $4
|
26
|
+
|
27
|
+
line_type = case marker
|
28
|
+
when '`' then :text
|
29
|
+
when '-' then :control_code
|
30
|
+
when '=' then :output_code
|
31
|
+
when '!' then :declaration
|
32
|
+
else :markup
|
33
|
+
end
|
34
|
+
|
35
|
+
if attrs
|
36
|
+
attrs.gsub!('"', '\"')
|
37
|
+
end
|
38
|
+
|
39
|
+
if string
|
40
|
+
string.strip!
|
41
|
+
string = nil unless string.strip.length > 0
|
42
|
+
end
|
43
|
+
|
44
|
+
unless indent > last_indent
|
45
|
+
begin
|
46
|
+
break if enders.empty?
|
47
|
+
continue_closing = true
|
48
|
+
ender, ender_indent = enders.pop
|
49
|
+
|
50
|
+
if ender_indent >= indent
|
51
|
+
unless ender == 'end;' && line_type == :control_code
|
52
|
+
@compiled << ender
|
53
|
+
end
|
54
|
+
else
|
55
|
+
enders << [ender, ender_indent]
|
56
|
+
continue_closing = false
|
57
|
+
end
|
58
|
+
end while continue_closing == true
|
59
|
+
end
|
60
|
+
|
61
|
+
last_indent = indent
|
62
|
+
|
63
|
+
case line_type
|
64
|
+
when :markup
|
65
|
+
if AUTOCLOSED.include?(marker)
|
66
|
+
@compiled << "_buf << \"<#{marker}#{attrs || ''}/>\";"
|
67
|
+
else
|
68
|
+
enders << ["_buf << \"</#{marker}>\";", indent]
|
69
|
+
@compiled << "_buf << \"<#{marker}#{attrs || ''}>\";"
|
70
|
+
end
|
71
|
+
|
72
|
+
if string
|
73
|
+
@compiled << "_buf << \"#{string}\";"
|
74
|
+
end
|
75
|
+
when :text
|
76
|
+
@compiled << "_buf << \"#{string}\";"
|
77
|
+
when :control_code
|
78
|
+
unless enders.detect{|e| e[0] == 'end;' && e[1] == indent}
|
79
|
+
enders << ['end;', indent]
|
80
|
+
end
|
81
|
+
@compiled << "#{string};"
|
82
|
+
when :output_code
|
83
|
+
@compiled << "_buf << #{string};"
|
84
|
+
when :declaration
|
85
|
+
@compiled << "_buf << \"<!#{string}>\";"
|
86
|
+
else
|
87
|
+
raise NotImplementedError.new("Don't know how to parse line: #{line}")
|
88
|
+
end
|
89
|
+
end # template iterator
|
90
|
+
|
91
|
+
enders.reverse_each do |t|
|
92
|
+
@compiled << t[0].to_s
|
93
|
+
end
|
94
|
+
|
95
|
+
@compiled << "_buf.join;"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/lib/slim/engine.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Slim
|
2
|
+
class Engine
|
3
|
+
include Compiler
|
4
|
+
|
5
|
+
attr_reader :compiled
|
6
|
+
|
7
|
+
# @param template The .slim template to convert
|
8
|
+
# @return [Slim::Engine] instance of engine
|
9
|
+
def initialize(template)
|
10
|
+
@template = template
|
11
|
+
compile
|
12
|
+
end
|
13
|
+
|
14
|
+
def render(scope = Object.new, locals = {})
|
15
|
+
scope.instance_eval(compiled)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/slim/rails.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'slim'
|
2
|
+
|
3
|
+
module ActionView
|
4
|
+
module TemplateHandlers
|
5
|
+
class SlimHandler < TemplateHandler
|
6
|
+
include Compilable
|
7
|
+
|
8
|
+
def compile(template)
|
9
|
+
return Slim::Engine.new(template.source).compiled
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Template.register_default_template_handler :slim, TemplateHandlers::SlimHandler
|
15
|
+
end
|
data/slim.gemspec
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{slim}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Andrew Stone"]
|
12
|
+
s.date = %q{2010-09-15}
|
13
|
+
s.description = %q{Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic.}
|
14
|
+
s.email = %q{andy@stonean.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"README.md",
|
21
|
+
"Rakefile",
|
22
|
+
"lib/slim.rb",
|
23
|
+
"lib/slim/compiler.rb",
|
24
|
+
"lib/slim/engine.rb",
|
25
|
+
"lib/slim/rails.rb",
|
26
|
+
"slim.gemspec",
|
27
|
+
"test/helper.rb",
|
28
|
+
"test/slim/test_compiler.rb",
|
29
|
+
"test/slim/test_engine.rb",
|
30
|
+
"test/test_slim.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/stonean/slim}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubyforge_project = %q{slim}
|
36
|
+
s.rubygems_version = %q{1.3.7}
|
37
|
+
s.summary = %q{Slim is a template language.}
|
38
|
+
s.test_files = [
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/test_slim.rb",
|
41
|
+
"test/slim/test_engine.rb",
|
42
|
+
"test/slim/test_compiler.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
else
|
51
|
+
end
|
52
|
+
else
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'minitest/unit'
|
4
|
+
|
5
|
+
MiniTest::Unit.autorun
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
|
9
|
+
require 'slim'
|
10
|
+
|
11
|
+
|
12
|
+
class Env
|
13
|
+
def show_first?
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def hello_world
|
18
|
+
"Hello World from @env"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,281 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestEngine
|
4
|
+
include Slim::Compiler
|
5
|
+
|
6
|
+
def initialize(template)
|
7
|
+
@template = template
|
8
|
+
compile
|
9
|
+
end
|
10
|
+
|
11
|
+
def compiled
|
12
|
+
@compiled
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestSlimEngine < MiniTest::Unit::TestCase
|
17
|
+
|
18
|
+
def test_simple_html
|
19
|
+
string = <<HTML
|
20
|
+
html
|
21
|
+
head
|
22
|
+
title Simple Test Title
|
23
|
+
body
|
24
|
+
p Hello World, meet Slim.
|
25
|
+
HTML
|
26
|
+
|
27
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
28
|
+
|
29
|
+
output = TestEngine.new(string).compiled
|
30
|
+
|
31
|
+
assert_equal expected, output
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_simple_html_with_empty_lines
|
35
|
+
string = <<HTML
|
36
|
+
html
|
37
|
+
head
|
38
|
+
title Simple Test Title
|
39
|
+
body
|
40
|
+
|
41
|
+
p Hello World, meet Slim.
|
42
|
+
HTML
|
43
|
+
|
44
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
45
|
+
|
46
|
+
output = TestEngine.new(string).compiled
|
47
|
+
|
48
|
+
assert_equal expected, output
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_simple_html_with_wraparound_text
|
52
|
+
string = <<HTML
|
53
|
+
html
|
54
|
+
head
|
55
|
+
title Simple Test Title
|
56
|
+
body
|
57
|
+
p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean gravida porta neque eu tincidunt. Aenean auctor blandit est sed consectetur. Quisque feugiat massa quis diam vestibulum accumsan. Morbi pharetra accumsan augue, in imperdiet sapien viverra non. Suspendisse molestie metus in sapien hendrerit dictum sit amet et leo. Donec accumsan, justo id porttitor luctus, velit erat tincidunt lorem, eu euismod enim nisl quis justo. Aliquam orci odio, ultricies sed ultrices nec, ultrices at mi. Vestibulum vel lacus eu massa mattis venenatis. In porta, quam ut dignissim varius, neque lectus laoreet felis, eget scelerisque odio lacus sed massa. Donec fringilla laoreet mi in dignissim. Curabitur porttitor ullamcorper ultrices. Quisque hendrerit odio ut ipsum blandit quis molestie diam vehicula. Suspendisse diam nibh, sollicitudin id faucibus et, pharetra sit amet massa. Mauris molestie elit id nulla euismod tempus.
|
58
|
+
HTML
|
59
|
+
|
60
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean gravida porta neque eu tincidunt. Aenean auctor blandit est sed consectetur. Quisque feugiat massa quis diam vestibulum accumsan. Morbi pharetra accumsan augue, in imperdiet sapien viverra non. Suspendisse molestie metus in sapien hendrerit dictum sit amet et leo. Donec accumsan, justo id porttitor luctus, velit erat tincidunt lorem, eu euismod enim nisl quis justo. Aliquam orci odio, ultricies sed ultrices nec, ultrices at mi. Vestibulum vel lacus eu massa mattis venenatis. In porta, quam ut dignissim varius, neque lectus laoreet felis, eget scelerisque odio lacus sed massa. Donec fringilla laoreet mi in dignissim. Curabitur porttitor ullamcorper ultrices. Quisque hendrerit odio ut ipsum blandit quis molestie diam vehicula. Suspendisse diam nibh, sollicitudin id faucibus et, pharetra sit amet massa. Mauris molestie elit id nulla euismod tempus.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
61
|
+
|
62
|
+
output = TestEngine.new(string).compiled
|
63
|
+
|
64
|
+
assert_equal expected, output
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def test_simple_html_with_doctype
|
69
|
+
string = <<HTML
|
70
|
+
! doctype html5
|
71
|
+
html
|
72
|
+
head
|
73
|
+
title Simple Test Title
|
74
|
+
body
|
75
|
+
p Hello World, meet Slim.
|
76
|
+
HTML
|
77
|
+
|
78
|
+
expected = %q|_buf = [];_buf << "<!doctype html5>";_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
79
|
+
|
80
|
+
output = TestEngine.new(string).compiled
|
81
|
+
|
82
|
+
assert_equal expected, output
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def test_simple_html_with_params
|
87
|
+
string = <<HTML
|
88
|
+
html
|
89
|
+
head
|
90
|
+
title Simple Test Title
|
91
|
+
meta name="description" content="This is a Slim Test, that's all"
|
92
|
+
body
|
93
|
+
p Hello World, meet Slim.
|
94
|
+
HTML
|
95
|
+
|
96
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
97
|
+
|
98
|
+
output = TestEngine.new(string).compiled
|
99
|
+
|
100
|
+
assert_equal expected, output
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_simple_html_with_params_meta_first
|
104
|
+
string = <<HTML
|
105
|
+
html
|
106
|
+
head
|
107
|
+
meta name="description" content="This is a Slim Test, that's all"
|
108
|
+
title Simple Test Title
|
109
|
+
body
|
110
|
+
p Hello World, meet Slim.
|
111
|
+
HTML
|
112
|
+
|
113
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
114
|
+
|
115
|
+
output = TestEngine.new(string).compiled
|
116
|
+
|
117
|
+
assert_equal expected, output
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_nested_content
|
121
|
+
string = <<HTML
|
122
|
+
html
|
123
|
+
head
|
124
|
+
meta name="description" content="This is a Slim Test, that's all"
|
125
|
+
title Simple Test Title
|
126
|
+
body
|
127
|
+
p
|
128
|
+
` Hello World, meet Slim.
|
129
|
+
HTML
|
130
|
+
|
131
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
132
|
+
|
133
|
+
output = TestEngine.new(string).compiled
|
134
|
+
|
135
|
+
assert_equal expected, output
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_closing_tag_without_content_or_attributes
|
139
|
+
string = <<HTML
|
140
|
+
html
|
141
|
+
head
|
142
|
+
meta name="description" content="This is a Slim Test, that's all"
|
143
|
+
title Simple Test Title
|
144
|
+
body
|
145
|
+
hr
|
146
|
+
p
|
147
|
+
` Hello World, meet Slim.
|
148
|
+
HTML
|
149
|
+
|
150
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<hr/>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
151
|
+
|
152
|
+
output = TestEngine.new(string).compiled
|
153
|
+
|
154
|
+
assert_equal expected, output
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
def test_closing_tag_without_content
|
159
|
+
string = <<HTML
|
160
|
+
html
|
161
|
+
head
|
162
|
+
meta name="description" content="This is a Slim Test, that's all"
|
163
|
+
title Simple Test Title
|
164
|
+
body
|
165
|
+
img width="100" height="50" src="/images/test.jpg"
|
166
|
+
p
|
167
|
+
` Hello World, meet Slim.
|
168
|
+
HTML
|
169
|
+
|
170
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<img width=\"100\" height=\"50\" src=\"/images/test.jpg\"/>";_buf << "<p>";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
171
|
+
|
172
|
+
output = TestEngine.new(string).compiled
|
173
|
+
|
174
|
+
assert_equal expected, output
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
def test_text_that_starts_with_tag_name
|
179
|
+
string = <<HTML
|
180
|
+
html
|
181
|
+
head
|
182
|
+
meta name="description" content="This is a Slim Test, that's all"
|
183
|
+
title Simple Test Title
|
184
|
+
body
|
185
|
+
img width="100" height="50" src="/images/test.jpg"
|
186
|
+
p
|
187
|
+
` another one bites the dust
|
188
|
+
p
|
189
|
+
` i am iron man
|
190
|
+
HTML
|
191
|
+
|
192
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<img width=\"100\" height=\"50\" src=\"/images/test.jpg\"/>";_buf << "<p>";_buf << "another one bites the dust";_buf << "</p>";_buf << "<p>";_buf << "i am iron man";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
193
|
+
|
194
|
+
output = TestEngine.new(string).compiled
|
195
|
+
|
196
|
+
assert_equal expected, output
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
def test_simple_if_code_block
|
201
|
+
string = <<HTML
|
202
|
+
html
|
203
|
+
head
|
204
|
+
meta name="description" content="This is a Slim Test, that's all"
|
205
|
+
title Simple Test Title
|
206
|
+
body
|
207
|
+
- if something
|
208
|
+
p
|
209
|
+
` another one bites the dust
|
210
|
+
- else
|
211
|
+
p
|
212
|
+
` i am iron man
|
213
|
+
HTML
|
214
|
+
|
215
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";if something;_buf << "<p>";_buf << "another one bites the dust";_buf << "</p>";else;_buf << "<p>";_buf << "i am iron man";_buf << "</p>";end;_buf << "</body>";_buf << "</html>";_buf.join;|
|
216
|
+
|
217
|
+
output = TestEngine.new(string).compiled
|
218
|
+
|
219
|
+
assert_equal expected, output
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_simple_output_code
|
223
|
+
string = <<HTML
|
224
|
+
html
|
225
|
+
head
|
226
|
+
title Simple Test Title
|
227
|
+
body
|
228
|
+
p
|
229
|
+
= hello_world
|
230
|
+
HTML
|
231
|
+
|
232
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << hello_world;_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
233
|
+
|
234
|
+
output = TestEngine.new(string).compiled
|
235
|
+
|
236
|
+
assert_equal expected, output
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_simple_output_code_with_params
|
240
|
+
string = <<HTML
|
241
|
+
html
|
242
|
+
head
|
243
|
+
title Simple Test Title
|
244
|
+
body
|
245
|
+
p
|
246
|
+
= hello_world(params[:key])
|
247
|
+
HTML
|
248
|
+
|
249
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<p>";_buf << hello_world(params[:key]);_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
250
|
+
|
251
|
+
output = TestEngine.new(string).compiled
|
252
|
+
|
253
|
+
assert_equal expected, output
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_simple_html_with_params_and_content_on_same_line
|
257
|
+
string = <<TEMPLATE
|
258
|
+
html
|
259
|
+
head
|
260
|
+
title Simple Test Title
|
261
|
+
meta name="description" content="This is a Slim Test, that's all"
|
262
|
+
body
|
263
|
+
p id="first" Hello World, meet Slim.
|
264
|
+
TEMPLATE
|
265
|
+
|
266
|
+
expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "</head>";_buf << "<body>";_buf << "<p id=\"first\">";_buf << "Hello World, meet Slim.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
|
267
|
+
|
268
|
+
output = TestEngine.new(string).compiled
|
269
|
+
|
270
|
+
assert_equal expected, output
|
271
|
+
end
|
272
|
+
|
273
|
+
# Use this to do a line by line check. Much easier to see where the problem is.
|
274
|
+
def iterate_it(expected, output)
|
275
|
+
es = expected.split(';')
|
276
|
+
output.split(';').each_with_index do |text, index|
|
277
|
+
assert_equal(text, es[index])
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestSlimEngine < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@env = Env.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_simple_render
|
10
|
+
string = <<HTML
|
11
|
+
html
|
12
|
+
head
|
13
|
+
title Simple Test Title
|
14
|
+
body
|
15
|
+
p Hello World, meet Slim.
|
16
|
+
HTML
|
17
|
+
|
18
|
+
engine = Slim::Engine.new(string)
|
19
|
+
|
20
|
+
expected = "<html><head><title>Simple Test Title</title></head><body><p>Hello World, meet Slim.</p></body></html>"
|
21
|
+
|
22
|
+
assert_equal expected, engine.render
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_render_with_conditional
|
26
|
+
string = <<HTML
|
27
|
+
html
|
28
|
+
head
|
29
|
+
title Simple Test Title
|
30
|
+
body
|
31
|
+
- if show_first?
|
32
|
+
p The first paragraph
|
33
|
+
- else
|
34
|
+
p The second paragraph
|
35
|
+
HTML
|
36
|
+
|
37
|
+
engine = Slim::Engine.new(string)
|
38
|
+
|
39
|
+
expected = "<html><head><title>Simple Test Title</title></head><body><p>The second paragraph</p></body></html>"
|
40
|
+
|
41
|
+
assert_equal expected, engine.render(@env)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_render_with_call
|
45
|
+
string = <<HTML
|
46
|
+
html
|
47
|
+
head
|
48
|
+
title Simple Test Title
|
49
|
+
body
|
50
|
+
p
|
51
|
+
= hello_world
|
52
|
+
HTML
|
53
|
+
|
54
|
+
engine = Slim::Engine.new(string)
|
55
|
+
|
56
|
+
expected = "<html><head><title>Simple Test Title</title></head><body><p>Hello World from @env</p></body></html>"
|
57
|
+
|
58
|
+
assert_equal expected, engine.render(@env)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_render_with_call_and_inline_text
|
62
|
+
string = <<HTML
|
63
|
+
html
|
64
|
+
head
|
65
|
+
title Simple Test Title
|
66
|
+
body
|
67
|
+
h1 This is my title
|
68
|
+
p
|
69
|
+
= hello_world
|
70
|
+
HTML
|
71
|
+
|
72
|
+
engine = Slim::Engine.new(string)
|
73
|
+
|
74
|
+
expected = "<html><head><title>Simple Test Title</title></head><body><h1>This is my title</h1><p>Hello World from @env</p></body></html>"
|
75
|
+
|
76
|
+
assert_equal expected, engine.render(@env)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
end
|
data/test/test_slim.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slim
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Andrew Stone
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-15 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic.
|
22
|
+
email: andy@stonean.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.md
|
29
|
+
files:
|
30
|
+
- .gitignore
|
31
|
+
- README.md
|
32
|
+
- Rakefile
|
33
|
+
- lib/slim.rb
|
34
|
+
- lib/slim/compiler.rb
|
35
|
+
- lib/slim/engine.rb
|
36
|
+
- lib/slim/rails.rb
|
37
|
+
- slim.gemspec
|
38
|
+
- test/helper.rb
|
39
|
+
- test/slim/test_compiler.rb
|
40
|
+
- test/slim/test_engine.rb
|
41
|
+
- test/test_slim.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/stonean/slim
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: slim
|
70
|
+
rubygems_version: 1.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Slim is a template language.
|
74
|
+
test_files:
|
75
|
+
- test/helper.rb
|
76
|
+
- test/test_slim.rb
|
77
|
+
- test/slim/test_engine.rb
|
78
|
+
- test/slim/test_compiler.rb
|