markus 4.0.5 → 4.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -19
- data/lib/markus.rb +17 -10
- data/markus.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d48cbb12a240bc989879271a8497265c3223cec
|
4
|
+
data.tar.gz: 352bcd5138109702e4a7868823fa61c8306b1892
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72a6ea71891f5a72908cfbaab751ca7d27352565ea0c684d0d662128052bd5a187faae2a67b1e4cfb64830152b9368939bed9ee3b27387ed331bccd5a88fa6aa
|
7
|
+
data.tar.gz: f59e224c2bdac2839a5229e605b42816ed7eeb03701194cb5dfc240599f2a4cd070f75831a5d839dd0862b5c77a4ac4da2634ee1637f40430679937bf7b61827
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ This code is distributed without any warranty. See the file
|
|
10
10
|
|
11
11
|
## Introduction
|
12
12
|
|
13
|
-
All template libraries suck. But sometimes they are useful, for
|
13
|
+
All template libraries suck. But sometimes they are useful, for
|
14
14
|
quick n' dirty building of documents. This template library will of course suck
|
15
15
|
as well. It is inspired by _why's markaby. It supports JSON and XML.
|
16
16
|
|
@@ -18,38 +18,44 @@ as well. It is inspired by _why's markaby. It supports JSON and XML.
|
|
18
18
|
|
19
19
|
Its fairly simple:
|
20
20
|
|
21
|
-
1. Create classes that inherit from MarkUS, and add templates with a name to them (see below).
|
22
|
-
2. In the templates use arbitrary code, mixed with functions that have an _ at the end.
|
21
|
+
1. Create classes that inherit from MarkUS, and add templates with a name to them (see below).
|
22
|
+
2. In the templates use arbitrary code, mixed with functions that have an _ at the end.
|
23
23
|
3. Everything with an _ at the end is added to the result buffer
|
24
24
|
- If the first parameter is a String or Integer it will be used as content of the element
|
25
25
|
- If any parameter is a Hash it will be used as attributes in XML, or you-know-what in JSON
|
26
26
|
- If any parameter is an Array it will be used as you-know-what in JSON
|
27
27
|
- If it has a block, a nested data structure is implied (see template examples below)
|
28
|
-
- JSON only: by default a Hash is assumed, if you pass a paramter
|
29
|
-
-
|
30
|
-
-
|
31
|
-
4. Get the result by instantiating the class and calling one of
|
32
|
-
-
|
28
|
+
- JSON only: by default a Hash is assumed, if you pass a paramter `array`, e.g. `value_ do |array| ... end`, the result is `"value": [ ... ]`
|
29
|
+
- `#template_!` is a special method to include other templates
|
30
|
+
- `#element_!` allows you to include stuff in your result that is not a valid ruby function name (e.g. with a dot in the name - see below)
|
31
|
+
4. Get the result by instantiating the class and calling one of `#json_!`, `#xml_!`, `#html_!`
|
32
|
+
- `#xml_!` and `#html_!` differ in the way elements with no content are printed. XML uses short-handed tags, HTML doesn't.
|
33
|
+
|
34
|
+
`#json_!`, `#xml_!` and `#html_!` need the name of the template as
|
35
|
+
the first parameter, optional you can pass a hash. All pairs in the hash are
|
36
|
+
available as instance variables. Of course you can also handle it yourself through a
|
37
|
+
constructor in the template class.
|
33
38
|
|
34
39
|
## Usage - Example
|
35
40
|
|
36
41
|
template1.rb:
|
37
|
-
```
|
42
|
+
```ruby
|
38
43
|
class Common < MarkUS
|
39
44
|
template :test1 do
|
40
|
-
|
45
|
+
query1_ [2, 3, @w]
|
41
46
|
end
|
42
47
|
template :test2 do
|
43
|
-
|
48
|
+
query2_ :a => 2, :b => @h
|
44
49
|
end
|
45
50
|
end
|
46
51
|
```
|
47
52
|
|
48
53
|
template2.rb:
|
49
|
-
```
|
54
|
+
```ruby
|
50
55
|
require File.expand_path(File.dirname(__FILE__) + '/template1')
|
56
|
+
|
51
57
|
class Something < MarkUS
|
52
|
-
templates Common
|
58
|
+
templates Common
|
53
59
|
|
54
60
|
indent
|
55
61
|
|
@@ -61,17 +67,21 @@ end
|
|
61
67
|
```
|
62
68
|
|
63
69
|
main.rb:
|
64
|
-
```
|
70
|
+
```ruby
|
71
|
+
require 'markus'
|
65
72
|
require File.expand_path(File.dirname(__FILE__) + '/template2')
|
66
73
|
s = Something.new
|
67
|
-
result = s.json_! :main
|
74
|
+
result = s.json_! :main, :h => 'hello', :w => 'world'
|
75
|
+
puts result
|
68
76
|
```
|
69
77
|
|
70
|
-
If you add
|
78
|
+
If you add `reload` to any of the template classes, they will be reloaded if they change (if templates are use in a long-running service).
|
79
|
+
|
80
|
+
|
71
81
|
|
72
82
|
## HTML Example Template
|
73
83
|
|
74
|
-
```
|
84
|
+
```ruby
|
75
85
|
html_ do
|
76
86
|
body_ :class => 'test' do
|
77
87
|
a_ 'Ruby', :href => 'https://ruby-lang.org'
|
@@ -84,7 +94,7 @@ end
|
|
84
94
|
|
85
95
|
## JSON Example Template
|
86
96
|
|
87
|
-
```
|
97
|
+
```ruby
|
88
98
|
query_ do
|
89
99
|
filtered_ do
|
90
100
|
filter_ do
|
@@ -118,7 +128,7 @@ knows.
|
|
118
128
|
|
119
129
|
View the example in the ./examples subdirectory. View the tests in the ./test subdirectory. From there you should be able to figure it out yourself. Tip: neat combinations with heredocs are possible, e.g. to create script tags in html.
|
120
130
|
|
121
|
-
```
|
131
|
+
```ruby
|
122
132
|
script_ <<~end
|
123
133
|
var foo = "bar";
|
124
134
|
end
|
data/lib/markus.rb
CHANGED
@@ -64,7 +64,7 @@ class MarkUS
|
|
64
64
|
@__markus_mode = type
|
65
65
|
|
66
66
|
template_!(name)
|
67
|
-
@__markus_buffer.last.
|
67
|
+
@__markus_buffer.last.chomp!(',') if @__markus_mode == :json
|
68
68
|
self.class.__markus_indent ? @__markus_buffer.join("\n") : @__markus_buffer.join
|
69
69
|
end
|
70
70
|
#}}}
|
@@ -156,7 +156,7 @@ class MarkUS
|
|
156
156
|
value.nil? ? nil : "\"#{value.to_s.gsub(/"/,'\\\"')}\""
|
157
157
|
end
|
158
158
|
}.compact.join(", ").strip + " ]"
|
159
|
-
attrs =
|
159
|
+
attrs = '[]' if attrs == "[ ]"
|
160
160
|
when Hash
|
161
161
|
attrs = "{ " + a.collect { |key,value|
|
162
162
|
case value
|
@@ -166,7 +166,7 @@ class MarkUS
|
|
166
166
|
value.nil? ? nil : "\"#{key}\": \"#{value.to_s.gsub(/"/,'\\\"')}\""
|
167
167
|
end
|
168
168
|
}.compact.join(", ").strip + " }"
|
169
|
-
attrs =
|
169
|
+
attrs = '{}' if attrs == "{ }"
|
170
170
|
when String
|
171
171
|
content = "\"#{a.gsub(/"/,'\\\"')}\""
|
172
172
|
when Integer, Float
|
@@ -186,7 +186,7 @@ class MarkUS
|
|
186
186
|
@__markus_buffer << "{"
|
187
187
|
end
|
188
188
|
__markus_json tname, *args, &blk
|
189
|
-
@__markus_buffer.last.
|
189
|
+
@__markus_buffer.last.chomp!(',')
|
190
190
|
if self.class.__markus_indent
|
191
191
|
@__markus_buffer << "#{" " * @__markus_level}},"
|
192
192
|
else
|
@@ -199,13 +199,20 @@ class MarkUS
|
|
199
199
|
else
|
200
200
|
@__markus_buffer << "#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}"
|
201
201
|
end
|
202
|
+
|
203
|
+
c1 = @__markus_buffer.length
|
202
204
|
res = blk.call
|
203
|
-
@__markus_buffer
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
@__markus_buffer
|
205
|
+
c2 = @__markus_buffer.length
|
206
|
+
if c1 == c2
|
207
|
+
@__markus_buffer.last << "#{type == :a ? ']' : '}'},"
|
208
|
+
else
|
209
|
+
@__markus_buffer << res + ',' if type == :a && res.is_a?(String)
|
210
|
+
@__markus_buffer.last.chomp!(',')
|
211
|
+
if self.class.__markus_indent
|
212
|
+
@__markus_buffer << "#{" " * @__markus_level}#{type == :a ? ']' : '}'},"
|
213
|
+
else
|
214
|
+
@__markus_buffer << "#{type == :a ? ']' : '}'},"
|
215
|
+
end
|
209
216
|
end
|
210
217
|
end
|
211
218
|
@__markus_level -= 1
|
data/markus.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.0.
|
4
|
+
version: 4.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juergen eTM Mangler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|