markus 4.0.15 → 4.0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -15
- data/lib/markus.rb +61 -99
- data/markus.gemspec +1 -1
- data/test/change.mt_ +1 -1
- data/test/change_include_common.mt_ +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: 7f0ffc073e5244986b80b5ea4387179a4f6c45a7
|
4
|
+
data.tar.gz: dea294a898cd66808c01d51aaa2117ce8ba7469f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c34f01d5632b892fd3bd60ab731249d23126f8596c316c1b99d0940e111e856b4ed395cb93fe709b0fa7008bd371762046bbf537fa22ed1e9596a563dc607b03
|
7
|
+
data.tar.gz: a9c0e966baaf7954e11f1bdfd4d9de6849db78beb5ed9cc6d7ebb32de8c44638ad1ce1151299e3aa5d2ebac2f71627b01855cbf1970db3473d6ac9488de33b1a
|
data/README.md
CHANGED
@@ -2,17 +2,23 @@
|
|
2
2
|
|
3
3
|
Copyright (C) 2004-2016 Jürgen "eTM" Mangler <juergen.mangler@gmail.com>
|
4
4
|
|
5
|
-
MarkUS is freely distributable according to the terms of the
|
6
|
-
|
5
|
+
MarkUS is freely distributable according to the terms of the GNU Lesser General
|
6
|
+
Public License 3.0 (see the file 'COPYING').
|
7
7
|
|
8
|
-
This code is distributed without any warranty. See the file
|
9
|
-
|
8
|
+
This code is distributed without any warranty. See the file 'COPYING' for
|
9
|
+
details.
|
10
10
|
|
11
11
|
## Introduction
|
12
12
|
|
13
|
-
All template libraries suck. But sometimes they are useful, for
|
14
|
-
|
15
|
-
|
13
|
+
All template libraries suck. But sometimes they are useful, for quick n' dirty
|
14
|
+
building of documents. This template library will of course suck as well. It is
|
15
|
+
inspired by _why's markaby. It supports JSON and XML.
|
16
|
+
|
17
|
+
## Possible Applications
|
18
|
+
|
19
|
+
It can be used for better handling of big elasticsearch querys (json), for
|
20
|
+
creating html documents, or for stuff that ought to return json, xml, or
|
21
|
+
switchable json<=>xml.
|
16
22
|
|
17
23
|
## Usage - Jump Start
|
18
24
|
|
@@ -75,9 +81,8 @@ main.rb:
|
|
75
81
|
puts result
|
76
82
|
```
|
77
83
|
|
78
|
-
If you add `reload` to any of the template classes, they will be reloaded if
|
79
|
-
|
80
|
-
|
84
|
+
If you add `reload` to any of the template classes, they will be reloaded if
|
85
|
+
they change (if templates are use in a long-running service).
|
81
86
|
|
82
87
|
## HTML Example Template
|
83
88
|
|
@@ -116,21 +121,24 @@ end
|
|
116
121
|
```
|
117
122
|
|
118
123
|
Why the f**k would i use a template library for JSON when i can just create a
|
119
|
-
big hash or array and create a json out of it? If you ever find yourself
|
120
|
-
lost with your big hashes, try this out. Maybe you like it,
|
121
|
-
knows.
|
124
|
+
big hash or array and create a json out of it? If you ever find yourself
|
125
|
+
feeling bad or lost with your big hashes, try this out. Maybe you like it,
|
126
|
+
maybe not. Who knows.
|
122
127
|
|
123
128
|
## Installation
|
124
129
|
|
125
130
|
* You need a least ruby 1.9.2
|
131
|
+
* gem install markus.rb
|
126
132
|
|
127
133
|
## Further Reading
|
128
134
|
|
129
|
-
View the example in the ./examples subdirectory. View the tests in the ./test
|
135
|
+
View the example in the ./examples subdirectory. View the tests in the ./test
|
136
|
+
subdirectory. From there you should be able to figure it out yourself. Tip:
|
137
|
+
neat combinations with heredocs are possible, e.g. to create script tags in
|
138
|
+
html.
|
130
139
|
|
131
140
|
```ruby
|
132
141
|
script_ <<~end
|
133
142
|
var foo = "bar";
|
134
143
|
end
|
135
144
|
```
|
136
|
-
|
data/lib/markus.rb
CHANGED
@@ -96,6 +96,10 @@ class MarkUS
|
|
96
96
|
instance_exec *args, &self.class.__markus_templates[name]
|
97
97
|
end #}}}
|
98
98
|
|
99
|
+
def __markus_indent(lvl=0) #{{{
|
100
|
+
self.class.__markus_indent ? "#{" " * (@__markus_level + lvl)}" : ""
|
101
|
+
end #}}}
|
102
|
+
|
99
103
|
def method_missing(name,*args, &blk) #{{{ # :nodoc:
|
100
104
|
if name.to_s =~ /(.*)(__)$/ || name.to_s =~ /(.*)(_)$/
|
101
105
|
__markus_method_missing $1, *args, &blk
|
@@ -104,10 +108,11 @@ class MarkUS
|
|
104
108
|
end
|
105
109
|
end #}}}
|
106
110
|
def __markus_method_missing(name,*args, &blk) #{{{ # :nodoc:
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
+
case @__markus_mode
|
112
|
+
when :json
|
113
|
+
__markus_json name, *args, &blk
|
114
|
+
else
|
115
|
+
__markus_xml name, *args, &blk
|
111
116
|
end
|
112
117
|
end #}}}
|
113
118
|
def __markus_xml(tname,*args) #{{{ # :nodoc:
|
@@ -119,54 +124,31 @@ class MarkUS
|
|
119
124
|
attrs << " " + a.collect { |key,value|
|
120
125
|
value.nil? ? nil : "#{key}=\"#{value.to_s.gsub(/"/,""")}\""
|
121
126
|
}.compact.join(" ")
|
127
|
+
attrs = '' if attrs == ' '
|
122
128
|
when String
|
123
129
|
content = EscapeUtils.escape_html(a)
|
124
130
|
when Integer
|
125
131
|
content = a
|
126
132
|
end
|
127
133
|
end
|
128
|
-
|
134
|
+
@__markus_level += 1
|
129
135
|
if block_given?
|
130
|
-
@
|
131
|
-
|
132
|
-
@__markus_buffer << "#{" " * @__markus_level}<#{tname}#{attrs}>"
|
133
|
-
else
|
134
|
-
@__markus_buffer << "<#{tname}#{attrs}>"
|
135
|
-
end
|
136
|
-
unless content.nil?
|
137
|
-
if self.class.__markus_indent
|
138
|
-
@__markus_buffer << "#{" " * (@__markus_level+1)}#{content}"
|
139
|
-
else
|
140
|
-
@__markus_buffer << "#{content}"
|
141
|
-
end
|
142
|
-
end
|
136
|
+
@__markus_buffer << __markus_indent + "<#{tname}#{attrs}>"
|
137
|
+
@__markus_buffer << __markus_indent(1) + "#{content}" unless content.nil?
|
143
138
|
res = yield
|
144
139
|
@__markus_buffer << res if String === res
|
145
|
-
|
146
|
-
@__markus_buffer << "#{" " * @__markus_level}</#{tname}>"
|
147
|
-
else
|
148
|
-
@__markus_buffer << "</#{tname}>"
|
149
|
-
end
|
150
|
-
@__markus_level -= 1
|
140
|
+
@__markus_buffer << __markus_indent + "</#{tname}>"
|
151
141
|
else
|
152
142
|
if @__markus_mode == :xml && content.nil?
|
153
|
-
|
154
|
-
@__markus_buffer << "#{" " * (@__markus_level+1)}<#{tname}#{attrs}/>"
|
155
|
-
else
|
156
|
-
@__markus_buffer << "<#{tname}#{attrs}/>"
|
157
|
-
end
|
143
|
+
@__markus_buffer << __markus_indent + "<#{tname}#{attrs}/>"
|
158
144
|
else
|
159
|
-
|
160
|
-
@__markus_buffer << "#{" " * (@__markus_level+1)}<#{tname}#{attrs}>#{content}</#{tname}>"
|
161
|
-
else
|
162
|
-
@__markus_buffer << "<#{tname}#{attrs}>#{content}</#{tname}>"
|
163
|
-
end
|
145
|
+
@__markus_buffer << __markus_indent + "<#{tname}#{attrs}>#{content}</#{tname}>"
|
164
146
|
end
|
165
147
|
end
|
148
|
+
@__markus_level -= 1
|
166
149
|
end #}}}
|
167
150
|
def __markus_json(tname,*args,&blk) #{{{ # :nodoc:
|
168
|
-
attrs = nil
|
169
|
-
content = "null"
|
151
|
+
attrs = content = nil
|
170
152
|
args.each do |a|
|
171
153
|
case a
|
172
154
|
when Array
|
@@ -197,75 +179,55 @@ class MarkUS
|
|
197
179
|
content = "\"#{a.to_s}\""
|
198
180
|
end
|
199
181
|
end
|
200
|
-
if blk
|
201
|
-
@__markus_level += 1
|
202
|
-
mpsic = @__markus_parent
|
203
|
-
if mpsic == :a && !tname.nil?
|
204
|
-
@__markus_parent = nil
|
205
|
-
if self.class.__markus_indent
|
206
|
-
@__markus_buffer << "#{" " * @__markus_level}{"
|
207
|
-
else
|
208
|
-
@__markus_buffer << "{"
|
209
|
-
end
|
210
|
-
__markus_json tname, *args, &blk
|
211
|
-
@__markus_buffer.last.chomp!(',')
|
212
|
-
if self.class.__markus_indent
|
213
|
-
@__markus_buffer << "#{" " * @__markus_level}},"
|
214
|
-
else
|
215
|
-
@__markus_buffer << "},"
|
216
|
-
end
|
217
|
-
else
|
218
|
-
@__markus_parent = type = blk.parameters.length == 1 && blk.parameters[0][1] == :array ? :a : :h
|
219
|
-
if self.class.__markus_indent
|
220
|
-
@__markus_buffer << "#{" " * @__markus_level}#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}"
|
221
|
-
else
|
222
|
-
@__markus_buffer << "#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}"
|
223
|
-
end
|
224
182
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
183
|
+
@__markus_level += 1
|
184
|
+
mpsic = @__markus_parent
|
185
|
+
|
186
|
+
if mpsic == :a
|
187
|
+
@__markus_buffer << __markus_indent + "{"
|
188
|
+
end
|
189
|
+
|
190
|
+
if [content, attrs, blk].compact.length > 1
|
191
|
+
@__markus_parent = nil
|
192
|
+
|
193
|
+
@__markus_buffer << __markus_indent + "\"#{tname}\": {"
|
194
|
+
__markus_json "attributes", attrs if attrs
|
195
|
+
__markus_json "value", content if content
|
196
|
+
__markus_json "content", &blk if blk
|
197
|
+
@__markus_buffer.last.chomp!(',')
|
198
|
+
@__markus_buffer << __markus_indent + "},"
|
199
|
+
else
|
200
|
+
if blk
|
201
|
+
if mpsic == :a && !tname.nil?
|
202
|
+
@__markus_parent = nil
|
203
|
+
__markus_json tname, *args, &blk
|
232
204
|
@__markus_buffer.last.chomp!(',')
|
233
|
-
|
234
|
-
|
205
|
+
else
|
206
|
+
@__markus_parent = type = blk.parameters.length == 1 && blk.parameters[0][1] == :array ? :a : :h
|
207
|
+
@__markus_buffer << __markus_indent + "#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}"
|
208
|
+
|
209
|
+
c1 = @__markus_buffer.length
|
210
|
+
res = blk.call
|
211
|
+
c2 = @__markus_buffer.length
|
212
|
+
if c1 == c2
|
213
|
+
@__markus_buffer.last << "#{type == :a ? ']' : '}'},"
|
235
214
|
else
|
236
|
-
@__markus_buffer <<
|
215
|
+
@__markus_buffer << res + ',' if type == :a && res.is_a?(String)
|
216
|
+
@__markus_buffer.last.chomp!(',')
|
217
|
+
@__markus_buffer << __markus_indent + "#{type == :a ? ']' : '}'},"
|
237
218
|
end
|
238
219
|
end
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
@__markus_buffer << "#{" " * @__markus_level}{"
|
247
|
-
else
|
248
|
-
@__markus_buffer << "{"
|
249
|
-
end
|
250
|
-
if self.class.__markus_indent
|
251
|
-
@__markus_buffer << "#{" " * (@__markus_level+1)}\"#{tname}\": #{attrs || content}"
|
252
|
-
else
|
253
|
-
@__markus_buffer << "\"#{tname}\": #{attrs || content}"
|
254
|
-
end
|
255
|
-
if self.class.__markus_indent
|
256
|
-
@__markus_buffer << "#{" " * @__markus_level}},"
|
257
|
-
else
|
258
|
-
@__markus_buffer << "},"
|
259
|
-
end
|
260
|
-
@__markus_level -= 1
|
261
|
-
else
|
262
|
-
if self.class.__markus_indent
|
263
|
-
@__markus_buffer << "#{" " * (@__markus_level+1)}\"#{tname}\": #{attrs || content},"
|
264
|
-
else
|
265
|
-
@__markus_buffer << "\"#{tname}\": #{attrs || content},"
|
266
|
-
end
|
267
|
-
end
|
220
|
+
else
|
221
|
+
@__markus_buffer << __markus_indent + "\"#{tname}\": #{attrs || content},"
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
if mpsic == :a
|
226
|
+
@__markus_buffer << __markus_indent + "},"
|
268
227
|
end
|
228
|
+
|
229
|
+
@__markus_level -= 1
|
230
|
+
@__markus_parent = mpsic
|
269
231
|
end #}}}
|
270
232
|
|
271
233
|
def self::inherited(subclass) #{{{ # :nodoc:
|
data/markus.gemspec
CHANGED
data/test/change.mt_
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.16
|
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-
|
11
|
+
date: 2016-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|