porthole 0.99.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +415 -0
  3. data/Rakefile +89 -0
  4. data/bin/porthole +94 -0
  5. data/lib/porthole.rb +304 -0
  6. data/lib/porthole/context.rb +142 -0
  7. data/lib/porthole/generator.rb +195 -0
  8. data/lib/porthole/parser.rb +263 -0
  9. data/lib/porthole/settings.rb +226 -0
  10. data/lib/porthole/sinatra.rb +205 -0
  11. data/lib/porthole/template.rb +58 -0
  12. data/lib/porthole/version.rb +3 -0
  13. data/lib/rack/bug/panels/mustache_panel.rb +81 -0
  14. data/lib/rack/bug/panels/mustache_panel/mustache_extension.rb +27 -0
  15. data/lib/rack/bug/panels/mustache_panel/view.mustache +46 -0
  16. data/man/porthole.1 +165 -0
  17. data/man/porthole.1.html +213 -0
  18. data/man/porthole.1.ron +127 -0
  19. data/man/porthole.5 +539 -0
  20. data/man/porthole.5.html +422 -0
  21. data/man/porthole.5.ron +324 -0
  22. data/test/autoloading_test.rb +56 -0
  23. data/test/fixtures/comments.porthole +1 -0
  24. data/test/fixtures/comments.rb +14 -0
  25. data/test/fixtures/complex_view.porthole +17 -0
  26. data/test/fixtures/complex_view.rb +34 -0
  27. data/test/fixtures/crazy_recursive.porthole +9 -0
  28. data/test/fixtures/crazy_recursive.rb +31 -0
  29. data/test/fixtures/delimiters.porthole +8 -0
  30. data/test/fixtures/delimiters.rb +23 -0
  31. data/test/fixtures/dot_notation.porthole +10 -0
  32. data/test/fixtures/dot_notation.rb +25 -0
  33. data/test/fixtures/double_section.porthole +7 -0
  34. data/test/fixtures/double_section.rb +14 -0
  35. data/test/fixtures/escaped.porthole +1 -0
  36. data/test/fixtures/escaped.rb +14 -0
  37. data/test/fixtures/inner_partial.porthole +1 -0
  38. data/test/fixtures/inner_partial.txt +1 -0
  39. data/test/fixtures/inverted_section.porthole +7 -0
  40. data/test/fixtures/inverted_section.rb +14 -0
  41. data/test/fixtures/lambda.porthole +7 -0
  42. data/test/fixtures/lambda.rb +31 -0
  43. data/test/fixtures/method_missing.rb +19 -0
  44. data/test/fixtures/namespaced.porthole +1 -0
  45. data/test/fixtures/namespaced.rb +25 -0
  46. data/test/fixtures/nested_objects.porthole +17 -0
  47. data/test/fixtures/nested_objects.rb +35 -0
  48. data/test/fixtures/node.porthole +8 -0
  49. data/test/fixtures/partial_with_module.porthole +4 -0
  50. data/test/fixtures/partial_with_module.rb +37 -0
  51. data/test/fixtures/passenger.conf +5 -0
  52. data/test/fixtures/passenger.rb +27 -0
  53. data/test/fixtures/recursive.porthole +4 -0
  54. data/test/fixtures/recursive.rb +14 -0
  55. data/test/fixtures/simple.porthole +5 -0
  56. data/test/fixtures/simple.rb +26 -0
  57. data/test/fixtures/template_partial.porthole +2 -0
  58. data/test/fixtures/template_partial.rb +18 -0
  59. data/test/fixtures/template_partial.txt +4 -0
  60. data/test/fixtures/unescaped.porthole +1 -0
  61. data/test/fixtures/unescaped.rb +14 -0
  62. data/test/fixtures/utf8.porthole +3 -0
  63. data/test/fixtures/utf8_partial.porthole +1 -0
  64. data/test/helper.rb +7 -0
  65. data/test/parser_test.rb +78 -0
  66. data/test/partial_test.rb +168 -0
  67. data/test/porthole_test.rb +677 -0
  68. data/test/spec_test.rb +68 -0
  69. data/test/template_test.rb +20 -0
  70. metadata +127 -0
@@ -0,0 +1,127 @@
1
+ porthole(1) -- Porthole processor
2
+ =================================
3
+
4
+ ## SYNOPSIS
5
+
6
+ porthole <YAML> <FILE>
7
+ porthole --compile <FILE>
8
+ porthole --tokens <FILE>
9
+
10
+
11
+ ## DESCRIPTION
12
+
13
+ Porthole is a logic-less templating system for HTML, config files,
14
+ anything.
15
+
16
+ The `porthole` command processes a Porthole template preceded by YAML
17
+ frontmatter from standard input and prints one or more documents to
18
+ standard output.
19
+
20
+ YAML frontmatter beings with `---` on a single line, followed by YAML,
21
+ ending with another `---` on a single line, e.g.
22
+
23
+ ---
24
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
25
+ ---
26
+
27
+ If you are unfamiliar with YAML, it is a superset of JSON. Valid JSON
28
+ should work fine.
29
+
30
+ After the frontmatter should come any valid Porthole template. See
31
+ porthole(5) for an overview of Porthole templates.
32
+
33
+ For example:
34
+
35
+ {{#names}}
36
+ Hi {{name}}!
37
+ {{/names}}
38
+
39
+ Now let's combine them.
40
+
41
+ $ cat data.yml
42
+ ---
43
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
44
+ ---
45
+
46
+ $ cat template.porthole
47
+ {{#names}}
48
+ Hi {{name}}!
49
+ {{/names}}
50
+
51
+ $ cat data.yml template.porthole | porthole
52
+ Hi chris!
53
+ Hi mark!
54
+ Hi scott!
55
+
56
+ If you provide multiple YAML documents (as delimited by `---`), your
57
+ template will be rendered multiple times. Like a mail merge.
58
+
59
+ For example:
60
+
61
+ $ cat data.yml
62
+ ---
63
+ name: chris
64
+ ---
65
+ name: mark
66
+ ---
67
+ name: scott
68
+ ---
69
+
70
+ $ cat template.porthole
71
+ Hi {{name}}!
72
+
73
+ $ cat data.yml template.porthole | porthole
74
+ Hi chris!
75
+ Hi mark!
76
+ Hi scott!
77
+
78
+ ## OPTIONS
79
+
80
+ By default `porthole` will try to render a Porthole template using the
81
+ YAML frontmatter you provide. It can do a few other things, however.
82
+
83
+ * `-c`, `--compile`:
84
+ Print the compiled Ruby version of a given template. This is the
85
+ code that is actually used when rendering a template into a
86
+ string. Useful for debugging but only if you are familiar with
87
+ Porthole's internals.
88
+
89
+ * `-t`, `--tokens`:
90
+ Print the tokenized form of a given Porthole template. This can be
91
+ used to understand how Porthole parses a template. The tokens are
92
+ handed to a generator which compiles them into a Ruby
93
+ string. Syntax errors and confused tags, therefor, can probably be
94
+ identified by examining the tokens produced.
95
+
96
+
97
+ ## INSTALLATION
98
+
99
+ If you have RubyGems installed:
100
+
101
+ gem install porthole
102
+
103
+
104
+ ## EXAMPLES
105
+
106
+ $ porthole data.yml template.porthole
107
+ $ cat data.yml | porthole - template.porthole
108
+ $ porthole -c template.porthole
109
+ $ cat <<data | ruby porthole - template.porthole
110
+ ---
111
+ name: Bob
112
+ age: 30
113
+ ---
114
+ data
115
+
116
+
117
+ ## COPYRIGHT
118
+
119
+ Porthole is Copyright (C) 2009 Chris Wanstrath
120
+
121
+ Original CTemplate by Google
122
+
123
+
124
+ ## SEE ALSO
125
+
126
+ porthole(5), gem(1),
127
+ <http://porthole.github.com/>
@@ -0,0 +1,539 @@
1
+ .\" generated with Ronn/v0.7.3
2
+ .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
+ .
4
+ .TH "MUSTACHE" "5" "August 2011" "DEFUNKT" "Porthole Manual"
5
+ .
6
+ .SH "NAME"
7
+ \fBporthole\fR \- Logic\-less templates\.
8
+ .
9
+ .SH "SYNOPSIS"
10
+ A typical Porthole template:
11
+ .
12
+ .IP "" 4
13
+ .
14
+ .nf
15
+
16
+ Hello {{name}}
17
+ You have just won {{value}} dollars!
18
+ {{#in_ca}}
19
+ Well, {{taxed_value}} dollars, after taxes\.
20
+ {{/in_ca}}
21
+ .
22
+ .fi
23
+ .
24
+ .IP "" 0
25
+ .
26
+ .P
27
+ Given the following hash:
28
+ .
29
+ .IP "" 4
30
+ .
31
+ .nf
32
+
33
+ {
34
+ "name": "Chris",
35
+ "value": 10000,
36
+ "taxed_value": 10000 \- (10000 * 0\.4),
37
+ "in_ca": true
38
+ }
39
+ .
40
+ .fi
41
+ .
42
+ .IP "" 0
43
+ .
44
+ .P
45
+ Will produce the following:
46
+ .
47
+ .IP "" 4
48
+ .
49
+ .nf
50
+
51
+ Hello Chris
52
+ You have just won 10000 dollars!
53
+ Well, 6000\.0 dollars, after taxes\.
54
+ .
55
+ .fi
56
+ .
57
+ .IP "" 0
58
+ .
59
+ .SH "DESCRIPTION"
60
+ Porthole can be used for HTML, config files, source code \- anything\. It works by expanding tags in a template using values provided in a hash or object\.
61
+ .
62
+ .P
63
+ We call it "logic\-less" because there are no if statements, else clauses, or for loops\. Instead there are only tags\. Some tags are replaced with a value, some nothing, and others a series of values\. This document explains the different types of Porthole tags\.
64
+ .
65
+ .SH "TAG TYPES"
66
+ Tags are indicated by the double portholes\. \fB{{person}}\fR is a tag, as is \fB{{#person}}\fR\. In both examples, we\'d refer to \fBperson\fR as the key or tag key\. Let\'s talk about the different types of tags\.
67
+ .
68
+ .SS "Variables"
69
+ The most basic tag type is the variable\. A \fB{{name}}\fR tag in a basic template will try to find the \fBname\fR key in the current context\. If there is no \fBname\fR key, nothing will be rendered\.
70
+ .
71
+ .P
72
+ All variables are HTML escaped by default\. If you want to return unescaped HTML, use the triple porthole: \fB{{{name}}}\fR\.
73
+ .
74
+ .P
75
+ You can also use \fB&\fR to unescape a variable: \fB{{& name}}\fR\. This may be useful when changing delimiters (see "Set Delimiter" below)\.
76
+ .
77
+ .P
78
+ By default a variable "miss" returns an empty string\. This can usually be configured in your Porthole library\. The Ruby version of Porthole supports raising an exception in this situation, for instance\.
79
+ .
80
+ .P
81
+ Template:
82
+ .
83
+ .IP "" 4
84
+ .
85
+ .nf
86
+
87
+ * {{name}}
88
+ * {{age}}
89
+ * {{company}}
90
+ * {{{company}}}
91
+ .
92
+ .fi
93
+ .
94
+ .IP "" 0
95
+ .
96
+ .P
97
+ Hash:
98
+ .
99
+ .IP "" 4
100
+ .
101
+ .nf
102
+
103
+ {
104
+ "name": "Chris",
105
+ "company": "<b>GitHub</b>"
106
+ }
107
+ .
108
+ .fi
109
+ .
110
+ .IP "" 0
111
+ .
112
+ .P
113
+ Output:
114
+ .
115
+ .IP "" 4
116
+ .
117
+ .nf
118
+
119
+ * Chris
120
+ *
121
+ * &lt;b&gt;GitHub&lt;/b&gt;
122
+ * <b>GitHub</b>
123
+ .
124
+ .fi
125
+ .
126
+ .IP "" 0
127
+ .
128
+ .SS "Sections"
129
+ Sections render blocks of text one or more times, depending on the value of the key in the current context\.
130
+ .
131
+ .P
132
+ A section begins with a pound and ends with a slash\. That is, \fB{{#person}}\fR begins a "person" section while \fB{{/person}}\fR ends it\.
133
+ .
134
+ .P
135
+ The behavior of the section is determined by the value of the key\.
136
+ .
137
+ .P
138
+ \fBFalse Values or Empty Lists\fR
139
+ .
140
+ .P
141
+ If the \fBperson\fR key exists and has a value of false or an empty list, the HTML between the pound and slash will not be displayed\.
142
+ .
143
+ .P
144
+ Template:
145
+ .
146
+ .IP "" 4
147
+ .
148
+ .nf
149
+
150
+ Shown\.
151
+ {{#nothin}}
152
+ Never shown!
153
+ {{/nothin}}
154
+ .
155
+ .fi
156
+ .
157
+ .IP "" 0
158
+ .
159
+ .P
160
+ Hash:
161
+ .
162
+ .IP "" 4
163
+ .
164
+ .nf
165
+
166
+ {
167
+ "person": true,
168
+ }
169
+ .
170
+ .fi
171
+ .
172
+ .IP "" 0
173
+ .
174
+ .P
175
+ Output:
176
+ .
177
+ .IP "" 4
178
+ .
179
+ .nf
180
+
181
+ Shown\.
182
+ .
183
+ .fi
184
+ .
185
+ .IP "" 0
186
+ .
187
+ .P
188
+ \fBNon\-Empty Lists\fR
189
+ .
190
+ .P
191
+ If the \fBperson\fR key exists and has a non\-false value, the HTML between the pound and slash will be rendered and displayed one or more times\.
192
+ .
193
+ .P
194
+ When the value is a non\-empty list, the text in the block will be displayed once for each item in the list\. The context of the block will be set to the current item for each iteration\. In this way we can loop over collections\.
195
+ .
196
+ .P
197
+ Template:
198
+ .
199
+ .IP "" 4
200
+ .
201
+ .nf
202
+
203
+ {{#repo}}
204
+ <b>{{name}}</b>
205
+ {{/repo}}
206
+ .
207
+ .fi
208
+ .
209
+ .IP "" 0
210
+ .
211
+ .P
212
+ Hash:
213
+ .
214
+ .IP "" 4
215
+ .
216
+ .nf
217
+
218
+ {
219
+ "repo": [
220
+ { "name": "resque" },
221
+ { "name": "hub" },
222
+ { "name": "rip" },
223
+ ]
224
+ }
225
+ .
226
+ .fi
227
+ .
228
+ .IP "" 0
229
+ .
230
+ .P
231
+ Output:
232
+ .
233
+ .IP "" 4
234
+ .
235
+ .nf
236
+
237
+ <b>resque</b>
238
+ <b>hub</b>
239
+ <b>rip</b>
240
+ .
241
+ .fi
242
+ .
243
+ .IP "" 0
244
+ .
245
+ .P
246
+ \fBLambdas\fR
247
+ .
248
+ .P
249
+ When the value is a callable object, such as a function or lambda, the object will be invoked and passed the block of text\. The text passed is the literal block, unrendered\. \fB{{tags}}\fR will not have been expanded \- the lambda should do that on its own\. In this way you can implement filters or caching\.
250
+ .
251
+ .P
252
+ Template:
253
+ .
254
+ .IP "" 4
255
+ .
256
+ .nf
257
+
258
+ {{#wrapped}}
259
+ {{name}} is awesome\.
260
+ {{/wrapped}}
261
+ .
262
+ .fi
263
+ .
264
+ .IP "" 0
265
+ .
266
+ .P
267
+ Hash:
268
+ .
269
+ .IP "" 4
270
+ .
271
+ .nf
272
+
273
+ {
274
+ "name": "Willy",
275
+ "wrapped": function() {
276
+ return function(text) {
277
+ return "<b>" + render(text) + "</b>"
278
+ }
279
+ }
280
+ }
281
+ .
282
+ .fi
283
+ .
284
+ .IP "" 0
285
+ .
286
+ .P
287
+ Output:
288
+ .
289
+ .IP "" 4
290
+ .
291
+ .nf
292
+
293
+ <b>Willy is awesome\.</b>
294
+ .
295
+ .fi
296
+ .
297
+ .IP "" 0
298
+ .
299
+ .P
300
+ \fBNon\-False Values\fR
301
+ .
302
+ .P
303
+ When the value is non\-false but not a list, it will be used as the context for a single rendering of the block\.
304
+ .
305
+ .P
306
+ Template:
307
+ .
308
+ .IP "" 4
309
+ .
310
+ .nf
311
+
312
+ {{#person?}}
313
+ Hi {{name}}!
314
+ {{/person?}}
315
+ .
316
+ .fi
317
+ .
318
+ .IP "" 0
319
+ .
320
+ .P
321
+ Hash:
322
+ .
323
+ .IP "" 4
324
+ .
325
+ .nf
326
+
327
+ {
328
+ "person?": { "name": "Jon" }
329
+ }
330
+ .
331
+ .fi
332
+ .
333
+ .IP "" 0
334
+ .
335
+ .P
336
+ Output:
337
+ .
338
+ .IP "" 4
339
+ .
340
+ .nf
341
+
342
+ Hi Jon!
343
+ .
344
+ .fi
345
+ .
346
+ .IP "" 0
347
+ .
348
+ .SS "Inverted Sections"
349
+ An inverted section begins with a caret (hat) and ends with a slash\. That is \fB{{^person}}\fR begins a "person" inverted section while \fB{{/person}}\fR ends it\.
350
+ .
351
+ .P
352
+ While sections can be used to render text one or more times based on the value of the key, inverted sections may render text once based on the inverse value of the key\. That is, they will be rendered if the key doesn\'t exist, is false, or is an empty list\.
353
+ .
354
+ .P
355
+ Template:
356
+ .
357
+ .IP "" 4
358
+ .
359
+ .nf
360
+
361
+ {{#repo}}
362
+ <b>{{name}}</b>
363
+ {{/repo}}
364
+ {{^repo}}
365
+ No repos :(
366
+ {{/repo}}
367
+ .
368
+ .fi
369
+ .
370
+ .IP "" 0
371
+ .
372
+ .P
373
+ Hash:
374
+ .
375
+ .IP "" 4
376
+ .
377
+ .nf
378
+
379
+ {
380
+ "repo": []
381
+ }
382
+ .
383
+ .fi
384
+ .
385
+ .IP "" 0
386
+ .
387
+ .P
388
+ Output:
389
+ .
390
+ .IP "" 4
391
+ .
392
+ .nf
393
+
394
+ No repos :(
395
+ .
396
+ .fi
397
+ .
398
+ .IP "" 0
399
+ .
400
+ .SS "Comments"
401
+ Comments begin with a bang and are ignored\. The following template:
402
+ .
403
+ .IP "" 4
404
+ .
405
+ .nf
406
+
407
+ <h1>Today{{! ignore me }}\.</h1>
408
+ .
409
+ .fi
410
+ .
411
+ .IP "" 0
412
+ .
413
+ .P
414
+ Will render as follows:
415
+ .
416
+ .IP "" 4
417
+ .
418
+ .nf
419
+
420
+ <h1>Today\.</h1>
421
+ .
422
+ .fi
423
+ .
424
+ .IP "" 0
425
+ .
426
+ .P
427
+ Comments may contain newlines\.
428
+ .
429
+ .SS "Partials"
430
+ Partials begin with a greater than sign, like \fB{{> box}}\fR\.
431
+ .
432
+ .P
433
+ Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible\. Just avoid infinite loops\.
434
+ .
435
+ .P
436
+ They also inherit the calling context\. Whereas in ERB you may have this:
437
+ .
438
+ .IP "" 4
439
+ .
440
+ .nf
441
+
442
+ <%= partial :next_more, :start => start, :size => size %>
443
+ .
444
+ .fi
445
+ .
446
+ .IP "" 0
447
+ .
448
+ .P
449
+ Porthole requires only this:
450
+ .
451
+ .IP "" 4
452
+ .
453
+ .nf
454
+
455
+ {{> next_more}}
456
+ .
457
+ .fi
458
+ .
459
+ .IP "" 0
460
+ .
461
+ .P
462
+ Why? Because the \fBnext_more\.porthole\fR file will inherit the \fBsize\fR and \fBstart\fR methods from the calling context\.
463
+ .
464
+ .P
465
+ In this way you may want to think of partials as includes, or template expansion, even though it\'s not literally true\.
466
+ .
467
+ .P
468
+ For example, this template and partial:
469
+ .
470
+ .IP "" 4
471
+ .
472
+ .nf
473
+
474
+ base\.porthole:
475
+ <h2>Names</h2>
476
+ {{#names}}
477
+ {{> user}}
478
+ {{/names}}
479
+
480
+ user\.porthole:
481
+ <strong>{{name}}</strong>
482
+ .
483
+ .fi
484
+ .
485
+ .IP "" 0
486
+ .
487
+ .P
488
+ Can be thought of as a single, expanded template:
489
+ .
490
+ .IP "" 4
491
+ .
492
+ .nf
493
+
494
+ <h2>Names</h2>
495
+ {{#names}}
496
+ <strong>{{name}}</strong>
497
+ {{/names}}
498
+ .
499
+ .fi
500
+ .
501
+ .IP "" 0
502
+ .
503
+ .SS "Set Delimiter"
504
+ Set Delimiter tags start with an equal sign and change the tag delimiters from \fB{{\fR and \fB}}\fR to custom strings\.
505
+ .
506
+ .P
507
+ Consider the following contrived example:
508
+ .
509
+ .IP "" 4
510
+ .
511
+ .nf
512
+
513
+ * {{default_tags}}
514
+ {{=<% %>=}}
515
+ * <% erb_style_tags %>
516
+ <%={{ }}=%>
517
+ * {{ default_tags_again }}
518
+ .
519
+ .fi
520
+ .
521
+ .IP "" 0
522
+ .
523
+ .P
524
+ Here we have a list with three items\. The first item uses the default tag style, the second uses erb style as defined by the Set Delimiter tag, and the third returns to the default style after yet another Set Delimiter declaration\.
525
+ .
526
+ .P
527
+ According to ctemplates \fIhttp://google\-ctemplate\.googlecode\.com/svn/trunk/doc/howto\.html\fR, this "is useful for languages like TeX, where double\-braces may occur in the text and are awkward to use for markup\."
528
+ .
529
+ .P
530
+ Custom delimiters may not contain whitespace or the equals sign\.
531
+ .
532
+ .SH "COPYRIGHT"
533
+ Porthole is Copyright (C) 2009 Chris Wanstrath
534
+ .
535
+ .P
536
+ Original CTemplate by Google
537
+ .
538
+ .SH "SEE ALSO"
539
+ porthole(1), \fIhttp://porthole\.github\.com/\fR