renshi 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/renshi/parser.rb +8 -3
- data/lib/renshi.rb +1 -1
- data/spec/data/example1.ren +22 -0
- data/spec/parser_spec.rb +83 -55
- metadata +2 -1
data/lib/renshi/parser.rb
CHANGED
@@ -20,7 +20,11 @@ module Renshi
|
|
20
20
|
XML_AMP = "^R_AMP^"
|
21
21
|
|
22
22
|
def self.parse(xhtml)
|
23
|
-
|
23
|
+
if xhtml.index("<head>") #better way to detect the full HTML document structure?
|
24
|
+
doc = Nokogiri::HTML::Document.parse(xhtml)
|
25
|
+
else
|
26
|
+
doc = Nokogiri::HTML.fragment(xhtml)
|
27
|
+
end
|
24
28
|
|
25
29
|
parser = self.new(doc)
|
26
30
|
out = parser.parse
|
@@ -179,8 +183,9 @@ module Renshi
|
|
179
183
|
#restore instructions in the string
|
180
184
|
bits = []
|
181
185
|
str.split(INSTRUCTION_START).each do |bit|
|
182
|
-
|
183
|
-
|
186
|
+
instr_end = bit.index(INSTRUCTION_END)
|
187
|
+
if instr_end
|
188
|
+
index = bit[0...instr_end]
|
184
189
|
instruction = @instructions[index.to_i]
|
185
190
|
|
186
191
|
bit.gsub!("#{index}#{INSTRUCTION_END}", instruction)
|
data/lib/renshi.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
|
5
|
+
<title>$Time.now</title>
|
6
|
+
${Time.now}
|
7
|
+
</head>
|
8
|
+
<body style="margin: 0; padding: 0; text-align: center;">
|
9
|
+
$Time.now
|
10
|
+
<div id="wrapper" style="width: 700px; margin: 0 auto; text-align: left; padding: 30px">
|
11
|
+
Breadcrumbs: $Time.now
|
12
|
+
Main Menu: $Time.now
|
13
|
+
<h1>$Time.now</h1>
|
14
|
+
<p>BrowserCMS has been installed successfully.</p>
|
15
|
+
|
16
|
+
<h2>Getting Started</h2>
|
17
|
+
<p>To start building your site, you can either ${foo "alter this template"} or ${foo "create a new one"}. You will be prompted to login with the credentials provided during the install process. To change which template the pages use, you can click the 'Edit Properties' button above, and choose a different template. After all pages in the site, use the new template, you can safely delete this one.</p>
|
18
|
+
|
19
|
+
$Time.now
|
20
|
+
</div>
|
21
|
+
</body>
|
22
|
+
</html>
|
data/spec/parser_spec.rb
CHANGED
@@ -6,70 +6,98 @@ def N(str)
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe Renshi::Parser do
|
9
|
-
it "should parse a $foo var in elements" do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
# it "should parse a $foo var in elements" do
|
10
|
+
# title = "hello world"
|
11
|
+
# out = interpret("data/hello_world1.ren", binding)
|
12
|
+
#
|
13
|
+
# doc = Nokogiri::XML(out)
|
14
|
+
# (doc/"title").text.should eql "hello world"
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# it "should interpret vars surrounded by whitespace" do
|
18
|
+
# foo = "in space no one can hear you scream"
|
19
|
+
# out = interpret("data/white_space.ren", binding)
|
20
|
+
# doc = N(out)
|
21
|
+
# (doc/"div[@id='content']").text.strip.should eql "in space no one can hear you scream"
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# it "should ignore $(..)" do
|
25
|
+
# doc = Nokogiri::HTML.fragment("$(foo)")
|
26
|
+
# node = doc.children.first
|
27
|
+
# eval(deliver_compiled(node), binding).should eql "$(foo)"
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# it "should parse attribute values - e.g. <div id='content$i'>" do
|
31
|
+
# i = 1
|
32
|
+
# html = interpret("data/attribute_values_parsed.ren", binding)
|
33
|
+
# html = N(html)
|
34
|
+
# (html/"div[@id='content1']").text.strip.should =~ /hello/
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
def foo(one, two = "", three = {})
|
38
|
+
"#{one}, #{two}"
|
15
39
|
end
|
40
|
+
#
|
41
|
+
# it "should understand double quotations marks within ruby code!" do
|
42
|
+
# #${link_to "alter this template", edit_cms_page_template_path(PageTemplate.find_by_file_name("default.html.erb"))}
|
43
|
+
# raw = compile_file("data/quots.ren")
|
44
|
+
# html = eval(raw, binding)
|
45
|
+
# html = N(html)
|
46
|
+
# (html/"div[@id='content']").text.strip.should =~ /1, 2/
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# it "should understand double quotations marks within ruby code!" do
|
50
|
+
# #${link_to "alter this template", edit_cms_page_template_path(PageTemplate.find_by_file_name("default.html.erb"))}
|
51
|
+
# raw = compile_file("data/quots2.ren")
|
52
|
+
# html = eval(raw, binding)
|
53
|
+
# html = N(html)
|
54
|
+
# (html/"div[@id='content']").text.strip.should =~ /1, 2/
|
55
|
+
# (html/"div[@id='content2']").text.strip.should =~ /a, 2/
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# it "should understand double quotations marks within ruby code! 2" do
|
59
|
+
# doc = Nokogiri::HTML(%Q!<p>${foo "1", foo("3", "4")}</p>!)
|
60
|
+
#
|
61
|
+
# puts doc.root
|
62
|
+
#
|
63
|
+
# body = doc.root.children.first
|
64
|
+
# node = body.children.first
|
65
|
+
# eval(deliver_compiled(node), binding).should eql "1, 3, 4"
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# it "should understand multiple statements on the same line" do
|
69
|
+
# raw = compile_file("data/multiple_statements.ren")
|
70
|
+
# html = eval(raw, binding)
|
71
|
+
# html = N(html)
|
72
|
+
# (html/"div[@id='content']").text.strip.should =~ /1, 2, 3, 4/
|
73
|
+
# (html/"div[@id='content2']").text.strip.should =~ /1, 2/
|
74
|
+
# end
|
16
75
|
|
17
|
-
it "should interpret vars surrounded by whitespace" do
|
18
|
-
foo = "in space no one can hear you scream"
|
19
|
-
out = interpret("data/white_space.ren", binding)
|
20
|
-
doc = N(out)
|
21
|
-
(doc/"div[@id='content']").text.strip.should eql "in space no one can hear you scream"
|
22
|
-
end
|
23
76
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
77
|
+
# STRING_END = "^R_END" #maybe replace this with a funky unicode char
|
78
|
+
# STRING_START = "^R_START" #maybe replace this with a funky unicode char
|
79
|
+
# BUFFER_CONCAT_OPEN = "@output_buffer.concat(\""
|
80
|
+
# BUFFER_CONCAT_CLOSE = "\");"
|
81
|
+
# NEW_LINE = "@output_buffer.concat('\n');"
|
82
|
+
# INSTRUCTION_START = "^R_INSTR_IDX_START^"
|
83
|
+
# INSTRUCTION_END = "^R_INSTR_IDX_END^"
|
29
84
|
|
30
|
-
it "should parse attribute values - e.g. <div id='content$i'>" do
|
31
|
-
i = 1
|
32
|
-
html = interpret("data/attribute_values_parsed.ren", binding)
|
33
|
-
html = N(html)
|
34
|
-
(html/"div[@id='content1']").text.strip.should =~ /hello/
|
35
|
-
end
|
36
|
-
|
37
|
-
def foo(one, two, three = {})
|
38
|
-
"#{one}, #{two}"
|
39
|
-
end
|
40
85
|
|
41
|
-
it "should
|
42
|
-
|
43
|
-
raw = compile_file("data/quots.ren")
|
86
|
+
it "should interpret all Renshi instructions and remove them from the document" do
|
87
|
+
raw = compile_file("data/example1.ren")
|
44
88
|
html = eval(raw, binding)
|
45
|
-
|
46
|
-
|
89
|
+
|
90
|
+
html.should_not =~/R_INSTR_IDX_START/
|
91
|
+
html.should_not =~/R_INSTR_IDX_END/
|
92
|
+
html.should_not =~/@output_buffer.concat/
|
93
|
+
html.should_not =~/R_START/
|
94
|
+
html.should_not =~/R_END/
|
47
95
|
end
|
48
96
|
|
49
|
-
it "should
|
50
|
-
|
51
|
-
raw = compile_file("data/quots2.ren")
|
97
|
+
it "should not remove the header of the document" do
|
98
|
+
raw = compile_file("data/example1.ren")
|
52
99
|
html = eval(raw, binding)
|
53
|
-
html = N(html)
|
54
|
-
(html/"div[@id='content']").text.strip.should =~ /1, 2/
|
55
|
-
(html/"div[@id='content2']").text.strip.should =~ /a, 2/
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should understand double quotations marks within ruby code! 2" do
|
59
|
-
doc = Nokogiri::HTML(%Q!<p>${foo "1", foo("3", "4")}</p>!)
|
60
100
|
|
61
|
-
|
62
|
-
|
63
|
-
body = doc.root.children.first
|
64
|
-
node = body.children.first
|
65
|
-
eval(deliver_compiled(node), binding).should eql "1, 3, 4"
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should understand multiple statements on the same line" do
|
69
|
-
raw = compile_file("data/multiple_statements.ren")
|
70
|
-
html = eval(raw, binding)
|
71
|
-
html = N(html)
|
72
|
-
(html/"div[@id='content']").text.strip.should =~ /1, 2, 3, 4/
|
73
|
-
(html/"div[@id='content2']").text.strip.should =~ /1, 2/
|
101
|
+
html.should =~/head/
|
74
102
|
end
|
75
103
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renshi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Faiz
|
@@ -77,6 +77,7 @@ specification_version: 3
|
|
77
77
|
summary: Renshi is a lightweight XHTML template language, inspired by Python's Genshi and build on Nokogiri.
|
78
78
|
test_files:
|
79
79
|
- spec/data/attribute_values_parsed.ren
|
80
|
+
- spec/data/example1.ren
|
80
81
|
- spec/data/for.ren
|
81
82
|
- spec/data/hello_world1.ren
|
82
83
|
- spec/data/if_1.ren
|