lokar 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +24 -0
- data/lokar.rb +139 -0
- metadata +56 -0
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Copyright (c) 2009-2010, Zoxc
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
* Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
* Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
* Neither the name of Zoxc nor the
|
12
|
+
names of its contributors may be used to endorse or promote products
|
13
|
+
derived from this software without specific prior written permission.
|
14
|
+
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
16
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
17
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18
|
+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
19
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
20
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
21
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
22
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
24
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/lokar.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
module Lokar
|
4
|
+
def self.render(string, filename = '<Lokar>', binding = nil)
|
5
|
+
output = ["__output__ = []", *parse(string, filename), "\n__output__"]
|
6
|
+
|
7
|
+
eval(output.join, binding, filename).join
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.compile(string, filename = '<Lokar>', binding = nil)
|
11
|
+
output = ["Proc.new do __output__ = []", *parse(string, filename), "\n__output__ end"]
|
12
|
+
|
13
|
+
eval output.join, binding, filename
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.parse(string, filename)
|
17
|
+
scanner = StringScanner.new(string)
|
18
|
+
output = []
|
19
|
+
prev_text = false
|
20
|
+
prev_output = true
|
21
|
+
|
22
|
+
while true
|
23
|
+
match = scanner.scan_until(/(?=<\?r[ \t]|\#{|^[ \t]*%)/m)
|
24
|
+
if match
|
25
|
+
# Add the text before the match
|
26
|
+
if prev_text
|
27
|
+
output.insert(-1, match.inspect)
|
28
|
+
else
|
29
|
+
if prev_output
|
30
|
+
output << " << "
|
31
|
+
else
|
32
|
+
output << "\n__output__ << "
|
33
|
+
prev_output = true
|
34
|
+
end
|
35
|
+
output << match.inspect
|
36
|
+
prev_text = true
|
37
|
+
end
|
38
|
+
|
39
|
+
case # Find out what of the regular expression matched
|
40
|
+
when scanner.match?(/</) # Parse <?r ?> tags
|
41
|
+
scanner.pos += 3
|
42
|
+
result = scanner.scan_until(/(?=\?>)/m)
|
43
|
+
raise "#{filename}: Unterminated <?r ?> tag" unless result
|
44
|
+
|
45
|
+
output << "\n"
|
46
|
+
output << result
|
47
|
+
|
48
|
+
prev_text = false
|
49
|
+
prev_output = false
|
50
|
+
|
51
|
+
scanner.pos += 2
|
52
|
+
|
53
|
+
when scanner.match?(/\#/) # Parse #{ } tags
|
54
|
+
index = 1
|
55
|
+
scanner.pos += 2
|
56
|
+
|
57
|
+
if prev_output
|
58
|
+
output << " << ("
|
59
|
+
else
|
60
|
+
output << "\n__output__ << ("
|
61
|
+
prev_output = true
|
62
|
+
end
|
63
|
+
|
64
|
+
while true
|
65
|
+
result = scanner.scan_until(/(?=}|{)/m)
|
66
|
+
raise "#{filename}: Unterminated \#{ } tag" unless result
|
67
|
+
output << result
|
68
|
+
case
|
69
|
+
when scanner.scan(/{/)
|
70
|
+
index += 1
|
71
|
+
output << '{'
|
72
|
+
|
73
|
+
when scanner.scan(/}/)
|
74
|
+
index -= 1
|
75
|
+
break if index == 0
|
76
|
+
output << '}'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
output << ")"
|
81
|
+
|
82
|
+
prev_output = true
|
83
|
+
prev_text = false
|
84
|
+
|
85
|
+
else # Parse %, %% and %= lines
|
86
|
+
result = scanner.scan(/[ \t]*%/)
|
87
|
+
if scanner.scan(/%/)
|
88
|
+
result = result.inspect
|
89
|
+
if prev_text
|
90
|
+
output << result
|
91
|
+
else
|
92
|
+
if prev_output
|
93
|
+
output << " << "
|
94
|
+
else
|
95
|
+
output << "\n__output__ << "
|
96
|
+
prev_output = true
|
97
|
+
end
|
98
|
+
output << result
|
99
|
+
prev_text = true
|
100
|
+
end
|
101
|
+
else
|
102
|
+
if scanner.scan(/=/)
|
103
|
+
if prev_output
|
104
|
+
output << " << ("
|
105
|
+
else
|
106
|
+
output << "\n__output__ << ("
|
107
|
+
prev_output = true
|
108
|
+
end
|
109
|
+
output << scanner.scan_until(/(\r\n|\n|\Z)/)
|
110
|
+
output << ")"
|
111
|
+
else
|
112
|
+
output << "\n"
|
113
|
+
output << scanner.scan_until(/(\r\n|\n|\Z)/)
|
114
|
+
prev_output = false
|
115
|
+
end
|
116
|
+
prev_text = false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
else # End of file
|
120
|
+
unless scanner.eos?
|
121
|
+
# Add the pending text
|
122
|
+
if prev_text
|
123
|
+
output << scanner.rest.inspect
|
124
|
+
else
|
125
|
+
if prev_output
|
126
|
+
output << " << "
|
127
|
+
else
|
128
|
+
output << "\n__output__ << "
|
129
|
+
end
|
130
|
+
output << scanner.rest.inspect
|
131
|
+
end
|
132
|
+
end
|
133
|
+
break
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
output
|
138
|
+
end
|
139
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lokar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zoxc
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-05 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- LICENSE
|
26
|
+
- lokar.rb
|
27
|
+
has_rdoc: true
|
28
|
+
homepage:
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- .
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.9.0
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Lokar is a simple fast template engine.
|
55
|
+
test_files: []
|
56
|
+
|