stonean-ruhl 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +51 -14
- data/VERSION +1 -1
- data/lib/ruhl.rb +50 -14
- data/lib/ruhl/errors.rb +1 -0
- data/lib/ruhl/sinatra.rb +1 -1
- data/ruhl.gemspec +56 -0
- data/spec/html/fragment.html +2 -0
- data/spec/html/layout.html +8 -0
- data/spec/ruhl_spec.rb +16 -0
- data/spec/spec_helper.rb +3 -2
- metadata +5 -2
data/README
CHANGED
@@ -7,7 +7,27 @@ At no time in the dev process would the view be unviewable in a browser. The vi
|
|
7
7
|
|
8
8
|
Notes (use cases) for me to remember:
|
9
9
|
|
10
|
-
|
10
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
11
|
+
:: Basic Use ::
|
12
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
13
|
+
|
14
|
+
<h1 ruby="page_header">
|
15
|
+
|
16
|
+
Method :page_header would know how to represent itself in the context of the h1 element.
|
17
|
+
|
18
|
+
The ruby executed would replace the content of the element it was being called on.
|
19
|
+
|
20
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
21
|
+
:: Replacing attribute values ::
|
22
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
23
|
+
|
24
|
+
<meta ruby="content: meta_description" content='This is a description template' id='metaDescription' name='description' />
|
25
|
+
|
26
|
+
content: meta_description is telling the parser to replace attribute 'content' with results from meta_description method.
|
27
|
+
|
28
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
29
|
+
:: Don't use iterators in views ::
|
30
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
11
31
|
|
12
32
|
<ul id="aab" ruby="present_results">
|
13
33
|
<li>Lorem ipsum dolor sit amet</li>
|
@@ -16,32 +36,49 @@ Notes (use cases) for me to remember:
|
|
16
36
|
|
17
37
|
Method :present_results would know how to represent itself in the context of the ul element. In other words, it would know how to produce <li> elements.
|
18
38
|
|
39
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
40
|
+
:: Using a Layout ::
|
41
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
19
42
|
|
20
|
-
|
43
|
+
Layout:
|
44
|
+
<html>
|
45
|
+
<head>
|
46
|
+
<title>This is a title template</title>
|
47
|
+
</head>
|
48
|
+
<body>
|
49
|
+
<div ruby="_render_"></div>
|
50
|
+
</body>
|
51
|
+
</html>
|
21
52
|
|
22
|
-
|
53
|
+
Fragment:
|
54
|
+
<h1 ruby="generate_h1">I am a templated headline</h1>
|
55
|
+
<p ruby="my_content">Lorem ipsum dolor sit amet</p>
|
23
56
|
|
24
|
-
|
57
|
+
To use:
|
25
58
|
|
26
|
-
|
59
|
+
Ruhl::Engine.new(File.read(fragment), :layout => path_to_layout).render(self)
|
27
60
|
|
61
|
+
Returns the expected result of parsed Layout w/ parsed Fragment. Note the use of the _render_ method. This is a 'special' method that Ruhl uses to inject the results of the parsed fragment into the layout.
|
28
62
|
|
29
|
-
##############################################################################
|
30
63
|
|
31
|
-
|
64
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
65
|
+
:: Notes ::
|
66
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
32
67
|
|
33
|
-
|
68
|
+
* No eval (I don't think eval is evil, it's just not the way this works)
|
34
69
|
|
70
|
+
* The ruby attribute is always removed from the output.
|
35
71
|
|
36
|
-
|
72
|
+
* Each method called must accept a tag parameter.
|
73
|
+
e.g def page_header(tag)
|
37
74
|
|
38
|
-
|
75
|
+
* Since it's just HTML, syntax highlighting is built-in. For vim, just add this to your ~/.vimrc:
|
76
|
+
au BufNewFile,BufRead *.ruhl set filetype=html
|
39
77
|
|
40
|
-
1) The ruby attribute is always removed from the output.
|
41
|
-
2) Each method called must accept a tag parameter.
|
42
|
-
e.g def page_header(tag)
|
43
78
|
|
44
|
-
|
79
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
80
|
+
:: TODO ::
|
81
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
45
82
|
|
46
83
|
1) Work on supporting templates (shouldn't be hard)
|
47
84
|
2) Work on supporting partials (shouldn't be hard)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/ruhl.rb
CHANGED
@@ -5,28 +5,36 @@ require 'ruhl/errors'
|
|
5
5
|
|
6
6
|
module Ruhl
|
7
7
|
class Engine
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :document, :scope, :layout
|
9
9
|
|
10
|
-
def initialize(html)
|
11
|
-
@
|
10
|
+
def initialize(html, options = {})
|
11
|
+
if @layout = options[:layout]
|
12
|
+
raise LayoutNotFoundError.new(@layout) unless File.exists?(@layout)
|
13
|
+
|
14
|
+
@document = Nokogiri::HTML.fragment(html)
|
15
|
+
else
|
16
|
+
@document = Nokogiri::HTML(html)
|
17
|
+
end
|
12
18
|
end
|
13
19
|
|
14
20
|
def render(current_scope)
|
15
21
|
set_scope(current_scope)
|
16
22
|
|
17
|
-
|
18
|
-
code = tag['ruby']
|
19
|
-
|
20
|
-
if code =~ /^\w+:/
|
21
|
-
process_attribute(tag,code)
|
22
|
-
else
|
23
|
-
tag.inner_html = execute_ruby(tag,code)
|
24
|
-
end
|
23
|
+
parse_doc(@document)
|
25
24
|
|
26
|
-
|
25
|
+
if @layout
|
26
|
+
render_with_layout
|
27
|
+
else
|
28
|
+
document.to_s
|
27
29
|
end
|
30
|
+
end
|
28
31
|
|
29
|
-
|
32
|
+
# The _render_ method is used within a layout to inject
|
33
|
+
# the results of the template render.
|
34
|
+
#
|
35
|
+
# Ruhl::Engine.new(html, :layout => path_to_layout).render(self)
|
36
|
+
def _render_
|
37
|
+
document.to_s
|
30
38
|
end
|
31
39
|
|
32
40
|
private
|
@@ -40,13 +48,41 @@ module Ruhl
|
|
40
48
|
end
|
41
49
|
end
|
42
50
|
|
51
|
+
def render_with_layout
|
52
|
+
doc = Nokogiri::HTML( File.read(@layout) )
|
53
|
+
parse_doc(doc)
|
54
|
+
doc.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_doc(doc)
|
58
|
+
if (nodes = doc.xpath('//*[@ruby]')).empty?
|
59
|
+
nodes = doc.xpath('*[@ruby]')
|
60
|
+
end
|
61
|
+
|
62
|
+
nodes.each do |tag|
|
63
|
+
code = tag['ruby']
|
64
|
+
|
65
|
+
if code =~ /^\w+:/
|
66
|
+
process_attribute(tag,code)
|
67
|
+
else
|
68
|
+
tag.inner_html = execute_ruby(tag,code)
|
69
|
+
end
|
70
|
+
|
71
|
+
tag.remove_attribute('ruby')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
43
75
|
def set_scope(current_scope)
|
44
76
|
raise Ruhl::NoScopeError unless current_scope
|
45
77
|
@scope = current_scope
|
46
78
|
end
|
47
79
|
|
48
80
|
def execute_ruby(tag, code)
|
49
|
-
|
81
|
+
unless code == '_render_'
|
82
|
+
scope.send(code, tag)
|
83
|
+
else
|
84
|
+
_render_
|
85
|
+
end
|
50
86
|
end
|
51
87
|
end
|
52
88
|
end
|
data/lib/ruhl/errors.rb
CHANGED
data/lib/ruhl/sinatra.rb
CHANGED
data/ruhl.gemspec
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{ruhl}
|
8
|
+
s.version = "0.2.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{2009-09-17}
|
13
|
+
s.description = %q{Make your HTML dynamic with the addition of a ruby attribute.}
|
14
|
+
s.email = %q{andy@stonean.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"README",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/ruhl.rb",
|
24
|
+
"lib/ruhl/errors.rb",
|
25
|
+
"lib/ruhl/sinatra.rb",
|
26
|
+
"ruhl.gemspec",
|
27
|
+
"spec/html/basic.html",
|
28
|
+
"spec/html/fragment.html",
|
29
|
+
"spec/html/layout.html",
|
30
|
+
"spec/html/medium.html",
|
31
|
+
"spec/html/seo.html",
|
32
|
+
"spec/rcov.opts",
|
33
|
+
"spec/ruhl_spec.rb",
|
34
|
+
"spec/spec.opts",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/stonean/ruhl}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.5}
|
41
|
+
s.summary = %q{Ruby Hypertext Language}
|
42
|
+
s.test_files = [
|
43
|
+
"spec/ruhl_spec.rb",
|
44
|
+
"spec/spec_helper.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
+
else
|
53
|
+
end
|
54
|
+
else
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,2 @@
|
|
1
|
+
<h1 ruby="generate_h1">I am a templated headline</h1>
|
2
|
+
<p ruby="my_content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet lacinia metus. Nam sed dui est. Sed pellentesque aliquet massa, vel cursus arcu faucibus tincidunt. Nunc aliquet ultricies tellus sit amet elementum. Integer porttitor lorem dolor. Proin leo nunc, sollicitudin sed ullamcorper non, tempus non erat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed commodo sodales pharetra. Vivamus pharetra, augue a volutpat scelerisque, elit nisl auctor elit, quis mollis dolor urna in ligula. Aliquam nisi augue, adipiscing quis mollis ut, aliquam nec urna. Morbi faucibus semper ante ut dignissim. Maecenas et dui eros, eget tristique odio. Sed vehicula erat nec dui porttitor laoreet.</p>
|
data/spec/ruhl_spec.rb
CHANGED
@@ -20,6 +20,10 @@ def present_results(tag = nil)
|
|
20
20
|
"<li>line item 1</li><li>line item 2</li>"
|
21
21
|
end
|
22
22
|
|
23
|
+
def my_content(tag = nil)
|
24
|
+
"hello from my content."
|
25
|
+
end
|
26
|
+
|
23
27
|
describe Ruhl do
|
24
28
|
|
25
29
|
describe "basic.html" do
|
@@ -62,6 +66,18 @@ describe Ruhl do
|
|
62
66
|
ul.inner_html.should == "<li>line item 1</li>\n<li>line item 2</li>\n"
|
63
67
|
end
|
64
68
|
end
|
69
|
+
|
70
|
+
describe "fragment.html" do
|
71
|
+
before do
|
72
|
+
@html = File.read html(:fragment)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "will be injected into layout.html" do
|
76
|
+
doc = create_doc( html(:layout) )
|
77
|
+
puts '*'*40
|
78
|
+
puts doc.to_s
|
79
|
+
end
|
80
|
+
end
|
65
81
|
end
|
66
82
|
|
67
83
|
|
data/spec/spec_helper.rb
CHANGED
@@ -8,8 +8,9 @@ def do_parse(html)
|
|
8
8
|
Nokogiri::HTML(html)
|
9
9
|
end
|
10
10
|
|
11
|
-
def create_doc
|
12
|
-
|
11
|
+
def create_doc(layout = nil)
|
12
|
+
options = {:layout => layout}
|
13
|
+
html = Ruhl::Engine.new(@html, :layout => layout).render(self)
|
13
14
|
do_parse(html)
|
14
15
|
end
|
15
16
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stonean-ruhl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Stone
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-17 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -29,7 +29,10 @@ files:
|
|
29
29
|
- lib/ruhl.rb
|
30
30
|
- lib/ruhl/errors.rb
|
31
31
|
- lib/ruhl/sinatra.rb
|
32
|
+
- ruhl.gemspec
|
32
33
|
- spec/html/basic.html
|
34
|
+
- spec/html/fragment.html
|
35
|
+
- spec/html/layout.html
|
33
36
|
- spec/html/medium.html
|
34
37
|
- spec/html/seo.html
|
35
38
|
- spec/rcov.opts
|