forked from radiant/radiant
/
standard_tags.rb
1015 lines (845 loc) · 30.8 KB
/
standard_tags.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module StandardTags
include Radiant::Taggable
include LocalTime
class TagError < StandardError; end
desc %{
Causes the tags referring to a page's attributes to refer to the current page.
*Usage:*
<pre><code><r:page>...</r:page></code></pre>
}
tag 'page' do |tag|
tag.locals.page = tag.globals.page
tag.expand
end
[:breadcrumb, :slug, :title].each do |method|
desc %{
Renders the @#{method}@ attribute of the current page.
}
tag method.to_s do |tag|
tag.locals.page.send(method)
end
end
desc %{
Renders the @url@ attribute of the current page.
}
tag 'url' do |tag|
relative_url_for(tag.locals.page.url, tag.globals.page.request)
end
desc %{
Gives access to a page's children.
By default, all child pages with status="published" are returned in
ascending order of publishing date. The order can be reversed by including
the attribute @order="desc"@. The pages may also be sorted by any other
field using the @by@ attribute (e.g. @by="title"@). If you want to fetch
pages whose status is not published, you can use the @status@ attribute.
The @limit@ and @offset@ attributes can also be used to modify the result
set.
*Usage:*
<pre><code><r:children [by="attribute"] [order="asc|desc"]
[offset="number"] [limit="number"]
[status="draft|reviewed|published|hidden|all"]>
...
</r:children></code></pre>
Note that all of the same options are available in the @<r:each/>@,
@<r:first/>@ and @<r:last/>@ tags. So the following examples are equivalent:
<pre><code><r:children by="title">
<r:each>
<r:title/>
</r:each>
</r:children>
<r:children:each by="title">
<r:title/>
</r:children:each></code></pre>
}
tag 'children' do |tag|
tag.locals.filter_attributes = tag.attr
tag.locals.options = children_find_options(tag)
tag.locals.children = tag.locals.page.children
tag.locals.children_filtered = tag.locals.page.children.find(:all, tag.locals.options.clone)
tag.expand
end
desc %{
Renders the total number of children.
}
tag 'children:count' do |tag|
tag.locals.children.count
end
desc %{
Returns the first child. Inside this tag all page attribute tags are mapped to
the first child. Takes the same ordering options as @<r:children>@.
*Usage:*
<pre><code><r:children:first>...</r:children:first></code></pre>
}
tag 'children:first' do |tag|
inherit_filter_attributes(tag)
options = children_find_options(tag)
children = children_filtered(tag,options)
if first = children.first
tag.locals.page = first
tag.expand
end
end
desc %{
Returns the last child. Inside this tag all page attribute tags are mapped to
the last child. Takes the same ordering options as @<r:children>@.
*Usage:*
<pre><code><r:children:last>...</r:children:last></code></pre>
}
tag 'children:last' do |tag|
inherit_filter_attributes(tag)
options = children_find_options(tag)
children = children_filtered(tag,options)
if last = children.last
tag.locals.page = last
tag.expand
end
end
desc %{
Cycles through each of the children. Inside this tag all page attribute tags
are mapped to the current child page. Takes the same ordering options as
@<r:children>@.
*Usage:*
<pre><code><r:children:each [offset="number"] [limit="number"] [by="attribute"] [order="asc|desc"]
[status="draft|reviewed|published|hidden|all"]>
...
</r:children:each>
</code></pre>
}
tag 'children:each' do |tag|
inherit_filter_attributes(tag)
options = children_find_options(tag)
children = children_filtered(tag,options)
result = []
tag.locals.previous_headers = {}
children.each do |item|
tag.locals.child = item
tag.locals.page = item
result << tag.expand
end
result
end
desc %{
When cycling through @<children:each/>@, the contents of this tag will be output
only for the first child.
}
tag 'children:each:if_first' do |tag|
children = tag.locals.children_filtered
if children.first == tag.locals.child
tag.expand
end
end
desc %{
When cycling through @<children:each/>@, the contents of this tag will be output
only for the last child.
}
tag 'children:each:if_last' do |tag|
children = tag.locals.children_filtered
if children.last == tag.locals.child
tag.expand
end
end
desc %{
When cycling through @<children:each/>@, the contents of this tag will be output
only for the first and the last children.
}
tag 'children:each:if_first_or_last' do |tag|
children = tag.locals.children_filtered
if children.first == tag.locals.child or children.last == tag.locals.child
tag.expand
end
end
desc %{
Page attribute tags inside of this tag refer to the current child. This is occasionally
useful if you are inside of another tag (like <r:find>) and need to refer back to the
current child.
*Usage:*
<pre><code><r:children:each>
<r:child>...</r:child>
</r:children:each>
</code></pre>
}
tag 'children:each:child' do |tag|
tag.locals.page = tag.locals.child
tag.expand
end
desc %{
Renders the tag contents only if the contents do not match the previous header. This
is extremely useful for rendering date headers for a list of child pages.
If you would like to use several header blocks you may use the @name@ attribute to
name the header. When a header is named it will not restart until another header of
the same name is different.
Using the @restart@ attribute you can cause other named headers to restart when the
present header changes. Simply specify the names of the other headers in a semicolon
separated list.
*Usage:*
<pre><code><r:children:each>
<r:header [name="header_name"] [restart="name1[;name2;...]"]>
...
</r:header>
</r:children:each>
</code></pre>
}
tag 'children:each:header' do |tag|
previous_headers = tag.locals.previous_headers
name = tag.attr['name'] || :unnamed
restart = (tag.attr['restart'] || '').split(';')
header = tag.expand
unless header == previous_headers[name]
previous_headers[name] = header
unless restart.empty?
restart.each do |n|
previous_headers[n] = nil
end
end
header
end
end
desc %{
Page attribute tags inside this tag refer to the parent of the current page.
*Usage:*
<pre><code><r:parent>...</r:parent></code></pre>
}
tag "parent" do |tag|
parent = tag.locals.page.parent
tag.locals.page = parent
tag.expand if parent
end
desc %{
Renders the contained elements only if the current contextual page has a parent, i.e.
is not the root page.
*Usage:*
<pre><code><r:if_parent>...</r:if_parent></code></pre>
}
tag "if_parent" do |tag|
parent = tag.locals.page.parent
tag.expand if parent
end
desc %{
Renders the contained elements only if the current contextual page has no parent, i.e.
is the root page.
*Usage:*
<pre><code><r:unless_parent>...</r:unless_parent></code></pre>
}
tag "unless_parent" do |tag|
parent = tag.locals.page.parent
tag.expand unless parent
end
desc %{
Renders the contained elements only if the current contextual page has one or
more child pages. The @status@ attribute limits the status of found child pages
to the given status, the default is @"published"@. @status="all"@ includes all
non-virtual pages regardless of status.
*Usage:*
<pre><code><r:if_children [status="published"]>...</r:if_children></code></pre>
}
tag "if_children" do |tag|
children = tag.locals.page.children.count(:conditions => children_find_options(tag)[:conditions])
tag.expand if children > 0
end
desc %{
Renders the contained elements only if the current contextual page has no children.
The @status@ attribute limits the status of found child pages to the given status,
the default is @"published"@. @status="all"@ includes all non-virtual pages
regardless of status.
*Usage:*
<pre><code><r:unless_children [status="published"]>...</r:unless_children></code></pre>
}
tag "unless_children" do |tag|
children = tag.locals.page.children.count(:conditions => children_find_options(tag)[:conditions])
tag.expand unless children > 0
end
desc %{
Renders one of the passed values based on a global cycle counter. Use the @reset@
attribute to reset the cycle to the beginning. Use the @name@ attribute to track
multiple cycles; the default is @cycle@.
*Usage:*
<pre><code><r:cycle values="first, second, third" [reset="true|false"] [name="cycle"] /></code></pre>
}
tag 'cycle' do |tag|
raise TagError, "`cycle' tag must contain a `values' attribute." unless tag.attr['values']
cycle = (tag.globals.cycle ||= {})
values = tag.attr['values'].split(",").collect(&:strip)
cycle_name = tag.attr['name'] || 'cycle'
current_index = (cycle[cycle_name] ||= 0)
current_index = 0 if tag.attr['reset'] == 'true'
cycle[cycle_name] = (current_index + 1) % values.size
values[current_index]
end
desc %{
Renders the main content of a page. Use the @part@ attribute to select a specific
page part. By default the @part@ attribute is set to body. Use the @inherit@
attribute to specify that if a page does not have a content part by that name that
the tag should render the parent's content part. By default @inherit@ is set to
@false@. Use the @contextual@ attribute to force a part inherited from a parent
part to be evaluated in the context of the child page. By default 'contextual'
is set to true.
*Usage:*
<pre><code><r:content [part="part_name"] [inherit="true|false"] [contextual="true|false"] /></code></pre>
}
tag 'content' do |tag|
page = tag.locals.page
part_name = tag_part_name(tag)
boolean_attr = proc do |attribute_name, default|
attribute = (tag.attr[attribute_name] || default).to_s
raise TagError.new(%{`#{attribute_name}' attribute of `content' tag must be set to either "true" or "false"}) unless attribute =~ /true|false/i
(attribute.downcase == 'true') ? true : false
end
inherit = boolean_attr['inherit', false]
part_page = page
if inherit
while (part_page.part(part_name).nil? and (not part_page.parent.nil?)) do
part_page = part_page.parent
end
end
contextual = boolean_attr['contextual', true]
part = part_page.part(part_name)
tag.locals.page = part_page unless contextual
tag.globals.page.render_snippet(part) unless part.nil?
end
desc %{
Renders the containing elements if all of the listed parts exist on a page.
By default the @part@ attribute is set to @body@, but you may list more than one
part by separating them with a comma. Setting the optional @inherit@ to true will
search ancestors independently for each part. By default @inherit@ is set to @false@.
When listing more than one part, you may optionally set the @find@ attribute to @any@
so that it will render the containing elements if any of the listed parts are found.
By default the @find@ attribute is set to @all@.
*Usage:*
<pre><code><r:if_content [part="part_name, other_part"] [inherit="true"] [find="any"]>...</r:if_content></code></pre>
}
tag 'if_content' do |tag|
page = tag.locals.page
part_name = tag_part_name(tag)
parts_arr = part_name.split(',')
inherit = boolean_attr_or_error(tag, 'inherit', 'false')
find = attr_or_error(tag, :attribute_name => 'find', :default => 'all', :values => 'any, all')
expandable = true
one_found = false
part_page = page
parts_arr.each do |name|
name.strip!
if inherit
while (part_page.part(name).nil? and (not part_page.parent.nil?)) do
part_page = part_page.parent
end
end
expandable = false if part_page.part(name).nil?
one_found ||= true if !part_page.part(name).nil?
end
expandable = true if (find == 'any' and one_found)
tag.expand if expandable
end
desc %{
The opposite of the @if_content@ tag. It renders the contained elements if all of the
specified parts do not exist. Setting the optional @inherit@ to true will search
ancestors independently for each part. By default @inherit@ is set to @false@.
When listing more than one part, you may optionally set the @find@ attribute to @any@
so that it will not render the containing elements if any of the listed parts are found.
By default the @find@ attribute is set to @all@.
*Usage:*
<pre><code><r:unless_content [part="part_name, other_part"] [inherit="false"] [find="any"]>...</r:unless_content></code></pre>
}
tag 'unless_content' do |tag|
page = tag.locals.page
part_name = tag_part_name(tag)
parts_arr = part_name.split(',')
inherit = boolean_attr_or_error(tag, 'inherit', false)
find = attr_or_error(tag, :attribute_name => 'find', :default => 'all', :values => 'any, all')
expandable, all_found = true, true
part_page = page
parts_arr.each do |name|
name.strip!
if inherit
while (part_page.part(name).nil? and (not part_page.parent.nil?)) do
part_page = part_page.parent
end
end
expandable = false if !part_page.part(name).nil?
all_found = false if part_page.part(name).nil?
end
if all_found == false and find == 'all'
expandable = true
end
tag.expand if expandable
end
desc %{
Renders the containing elements only if the page's url matches the regular expression
given in the @matches@ attribute. If the @ignore_case@ attribute is set to false, the
match is case sensitive. By default, @ignore_case@ is set to true.
*Usage:*
<pre><code><r:if_url matches="regexp" [ignore_case="true|false"]>...</r:if_url></code></pre>
}
tag 'if_url' do |tag|
raise TagError.new("`if_url' tag must contain a `matches' attribute.") unless tag.attr.has_key?('matches')
regexp = build_regexp_for(tag, 'matches')
unless tag.locals.page.url.match(regexp).nil?
tag.expand
end
end
desc %{
The opposite of the @if_url@ tag.
*Usage:*
<pre><code><r:unless_url matches="regexp" [ignore_case="true|false"]>...</r:unless_url></code></pre>
}
tag 'unless_url' do |tag|
raise TagError.new("`unless_url' tag must contain a `matches' attribute.") unless tag.attr.has_key?('matches')
regexp = build_regexp_for(tag, 'matches')
if tag.locals.page.url.match(regexp).nil?
tag.expand
end
end
desc %{
Renders the contained elements if the current contextual page is either the actual page or one of its parents.
This is typically used inside another tag (like <r:children:each>) to add conditional mark-up if the child element is or descends from the current page.
*Usage:*
<pre><code><r:if_ancestor_or_self>...</r:if_ancestor_or_self></code></pre>
}
tag "if_ancestor_or_self" do |tag|
tag.expand if (tag.globals.page.ancestors + [tag.globals.page]).include?(tag.locals.page)
end
desc %{
Renders the contained elements unless the current contextual page is either the actual page or one of its parents.
This is typically used inside another tag (like <r:children:each>) to add conditional mark-up unless the child element is or descends from the current page.
*Usage:*
<pre><code><r:unless_ancestor_or_self>...</r:unless_ancestor_or_self></code></pre>
}
tag "unless_ancestor_or_self" do |tag|
tag.expand unless (tag.globals.page.ancestors + [tag.globals.page]).include?(tag.locals.page)
end
desc %{
Renders the contained elements if the current contextual page is also the actual page.
This is typically used inside another tag (like <r:children:each>) to add conditional mark-up if the child element is the current page.
*Usage:*
<pre><code><r:if_self>...</r:if_self></code></pre>
}
tag "if_self" do |tag|
tag.expand if tag.locals.page == tag.globals.page
end
desc %{
Renders the contained elements unless the current contextual page is also the actual page.
This is typically used inside another tag (like <r:children:each>) to add conditional mark-up unless the child element is the current page.
*Usage:*
<pre><code><r:unless_self>...</r:unless_self></code></pre>
}
tag "unless_self" do |tag|
tag.expand unless tag.locals.page == tag.globals.page
end
desc %{
Renders the name of the author of the current page.
}
tag 'author' do |tag|
page = tag.locals.page
if author = page.created_by
author.name
end
end
desc %{
Renders the date based on the current page (by default when it was published or created).
The format attribute uses the same formating codes used by the Ruby @strftime@ function. By
default it's set to @%A, %B %d, %Y@. The @for@ attribute selects which date to render. Valid
options are @published_at@, @created_at@, @updated_at@, and @now@. @now@ will render the
current date/time, regardless of the page.
*Usage:*
<pre><code><r:date [format="%A, %B %d, %Y"] [for="published_at"]/></code></pre>
}
tag 'date' do |tag|
page = tag.locals.page
format = (tag.attr['format'] || '%A, %B %d, %Y')
time_attr = tag.attr['for']
date = if time_attr
case
when time_attr == 'now'
Time.now
when ['published_at', 'created_at', 'updated_at'].include?(time_attr)
page[time_attr]
else
raise TagError, "Invalid value for 'for' attribute."
end
else
page.published_at || page.created_at
end
adjust_time(date).strftime(format)
end
desc %{
Renders a link to the page. When used as a single tag it uses the page's title
for the link name. When used as a double tag the part in between both tags will
be used as the link text. The link tag passes all attributes over to the HTML
@a@ tag. This is very useful for passing attributes like the @class@ attribute
or @id@ attribute. If the @anchor@ attribute is passed to the tag it will
append a pound sign (<code>#</code>) followed by the value of the attribute to
the @href@ attribute of the HTML @a@ tag--effectively making an HTML anchor.
*Usage:*
<pre><code><r:link [anchor="name"] [other attributes...] /></code></pre>
or
<pre><code><r:link [anchor="name"] [other attributes...]>link text here</r:link></code></pre>
}
tag 'link' do |tag|
options = tag.attr.dup
anchor = options['anchor'] ? "##{options.delete('anchor')}" : ''
attributes = options.inject('') { |s, (k, v)| s << %{#{k.downcase}="#{v}" } }.strip
attributes = " #{attributes}" unless attributes.empty?
text = tag.double? ? tag.expand : tag.render('title')
%{<a href="#{tag.render('url')}#{anchor}"#{attributes}>#{text}</a>}
end
desc %{
Renders a trail of breadcrumbs to the current page. The separator attribute
specifies the HTML fragment that is inserted between each of the breadcrumbs. By
default it is set to @>@. The boolean nolinks attribute can be specified to render
breadcrumbs in plain text, without any links (useful when generating title tag).
*Usage:*
<pre><code><r:breadcrumbs [separator="separator_string"] [nolinks="true"] /></code></pre>
}
tag 'breadcrumbs' do |tag|
page = tag.locals.page
breadcrumbs = [page.breadcrumb]
nolinks = (tag.attr['nolinks'] == 'true')
page.ancestors.each do |ancestor|
tag.locals.page = ancestor
if nolinks
breadcrumbs.unshift tag.render('breadcrumb')
else
breadcrumbs.unshift %{<a href="#{tag.render('url')}">#{tag.render('breadcrumb')}</a>}
end
end
separator = tag.attr['separator'] || ' > '
breadcrumbs.join(separator)
end
desc %{
Renders the snippet specified in the @name@ attribute within the context of a page.
*Usage:*
<pre><code><r:snippet name="snippet_name" /></code></pre>
When used as a double tag, the part in between both tags may be used within the
snippet itself, being substituted in place of @<r:yield/>@.
*Usage:*
<pre><code><r:snippet name="snippet_name">Lorem ipsum dolor...</r:snippet></code></pre>
}
tag 'snippet' do |tag|
if name = tag.attr['name']
if snippet = Snippet.find_by_name(name.strip)
tag.locals.yield = tag.expand if tag.double?
tag.globals.page.render_snippet(snippet)
else
raise TagError.new('snippet not found')
end
else
raise TagError.new("`snippet' tag must contain `name' attribute")
end
end
desc %{
Used within a snippet as a placeholder for substitution of child content, when
the snippet is called as a double tag.
*Usage (within a snippet):*
<pre><code>
<div id="outer">
<p>before</p>
<r:yield/>
<p>after</p>
</div>
</code></pre>
If the above snippet was named "yielding", you could call it from any Page,
Layout or Snippet as follows:
<pre><code><r:snippet name="yielding">Content within</r:snippet></code></pre>
Which would output the following:
<pre><code>
<div id="outer">
<p>before</p>
Content within
<p>after</p>
</div>
</code></pre>
When called in the context of a Page or a Layout, @<r:yield/>@ outputs nothing.
}
tag 'yield' do |tag|
tag.locals.yield
end
desc %{
Inside this tag all page related tags refer to the page found at the @url@ attribute.
@url@s may be relative or absolute paths.
*Usage:*
<pre><code><r:find url="value_to_find">...</r:find></code></pre>
}
tag 'find' do |tag|
url = tag.attr['url']
raise TagError.new("`find' tag must contain `url' attribute") unless url
found = Page.find_by_url(absolute_path_for(tag.locals.page.url, url))
if page_found?(found)
tag.locals.page = found
tag.expand
end
end
desc %{
Randomly renders one of the options specified by the @option@ tags.
*Usage:*
<pre><code><r:random>
<r:option>...</r:option>
<r:option>...</r:option>
...
<r:random>
</code></pre>
}
tag 'random' do |tag|
tag.locals.random = []
tag.expand
options = tag.locals.random
option = options[rand(options.size)]
option if option
end
tag 'random:option' do |tag|
items = tag.locals.random
items << tag.expand
end
desc %{
Nothing inside a set of comment tags is rendered.
*Usage:*
<pre><code><r:comment>...</r:comment></code></pre>
}
tag 'comment' do |tag|
end
desc %{
Escapes angle brackets, etc. for rendering in an HTML document.
*Usage:*
<pre><code><r:escape_html>...</r:escape_html></code></pre>
}
tag "escape_html" do |tag|
CGI.escapeHTML(tag.expand)
end
desc %{
Outputs the published date using the format mandated by RFC 1123. (Ideal for RSS feeds.)
*Usage:*
<pre><code><r:rfc1123_date /></code></pre>
}
tag "rfc1123_date" do |tag|
page = tag.locals.page
if date = page.published_at || page.created_at
CGI.rfc1123_date(date.to_time)
end
end
desc %{
Renders a list of links specified in the @urls@ attribute according to three
states:
* @normal@ specifies the normal state for the link
* @here@ specifies the state of the link when the url matches the current
page's URL
* @selected@ specifies the state of the link when the current page matches
is a child of the specified url
The @between@ tag specifies what should be inserted in between each of the links.
*Usage:*
<pre><code><r:navigation urls="[Title: url | Title: url | ...]">
<r:normal><a href="<r:url />"><r:title /></a></r:normal>
<r:here><strong><r:title /></strong></r:here>
<r:selected><strong><a href="<r:url />"><r:title /></a></strong></r:selected>
<r:between> | </r:between>
</r:navigation>
</code></pre>
}
tag 'navigation' do |tag|
hash = tag.locals.navigation = {}
tag.expand
raise TagError.new("`navigation' tag must include a `normal' tag") unless hash.has_key? :normal
result = []
pairs = tag.attr['urls'].to_s.split('|').map do |pair|
parts = pair.split(':')
value = parts.pop
key = parts.join(':')
[key.strip, value.strip]
end
pairs.each do |title, url|
compare_url = remove_trailing_slash(url)
page_url = remove_trailing_slash(self.url)
hash[:title] = title
hash[:url] = url
case page_url
when compare_url
result << (hash[:here] || hash[:selected] || hash[:normal]).call
when Regexp.compile( '^' + Regexp.quote(url))
result << (hash[:selected] || hash[:normal]).call
else
result << hash[:normal].call
end
end
between = hash.has_key?(:between) ? hash[:between].call : ' '
result.reject { |i| i.blank? }.join(between)
end
[:normal, :here, :selected, :between].each do |symbol|
tag "navigation:#{symbol}" do |tag|
hash = tag.locals.navigation
hash[symbol] = tag.block
end
end
[:title, :url].each do |symbol|
tag "navigation:#{symbol}" do |tag|
hash = tag.locals.navigation
hash[symbol]
end
end
desc %{
Renders the containing elements only if Radiant in is development mode.
*Usage:*
<pre><code><r:if_dev>...</r:if_dev></code></pre>
}
tag 'if_dev' do |tag|
tag.expand if dev?(tag.globals.page.request)
end
desc %{
The opposite of the @if_dev@ tag.
*Usage:*
<pre><code><r:unless_dev>...</r:unless_dev></code></pre>
}
tag 'unless_dev' do |tag|
tag.expand unless dev?(tag.globals.page.request)
end
desc %{
Prints the page's status as a string. Optional attribute 'downcase'
will cause the status to be all lowercase.
*Usage:*
<pre><code><r:status [downcase='true'] /></code></pre>
}
tag 'status' do |tag|
status = tag.globals.page.status.name
return status.downcase if tag.attr['downcase']
status
end
desc %{
The namespace for 'meta' attributes. If used as a singleton tag, both the description
and keywords fields will be output as <meta /> tags unless the attribute 'tag' is set to 'false'.
*Usage*:
<pre><code> <r:meta [tag="false"] />
<r:meta>
<r:description [tag="false"] />
<r:keywords [tag="false"] />
</r:meta>
</code></pre>
}
tag 'meta' do |tag|
if tag.double?
tag.expand
else
tag.render('description', tag.attr) +
tag.render('keywords', tag.attr)
end
end
desc %{
Emits the page description field in a meta tag, unless attribute
'tag' is set to 'false'.
*Usage*:
<pre><code> <r:meta:description [tag="false"] /> </code></pre>
}
tag 'meta:description' do |tag|
show_tag = tag.attr['tag'] != 'false' || false
description = CGI.escapeHTML(tag.locals.page.description)
if show_tag
"<meta name=\"description\" content=\"#{description}\" />"
else
description
end
end
desc %{
Emits the page keywords field in a meta tag, unless attribute
'tag' is set to 'false'.
*Usage*:
<pre><code> <r:meta:keywords [tag="false"] /> </code></pre>
}
tag 'meta:keywords' do |tag|
show_tag = tag.attr['tag'] != 'false' || false
keywords = CGI.escapeHTML(tag.locals.page.keywords)
if show_tag
"<meta name=\"keywords\" content=\"#{keywords}\" />"
else
keywords
end
end
private
def children_filtered(tag,options)
if tag.attr.empty? or options == tag.locals.options
tag.locals.children_filtered
else
tag.locals.children_filtered = tag.locals.children.find(:all, options)
end
end
def inherit_filter_attributes(tag)
tag.attr.reverse_merge!(tag.locals.filter_attributes)
end
def children_find_options(tag)
attr = tag.attr.symbolize_keys
options = {}
[:limit, :offset].each do |symbol|
if number = attr[symbol]
if number =~ /^\d{1,4}$/
options[symbol] = number.to_i
else
raise TagError.new("`#{symbol}' attribute of `each' tag must be a positive number between 1 and 4 digits")
end
end
end
by = (attr[:by] || 'published_at').strip
order = (attr[:order] || 'asc').strip
order_string = ''
if self.attributes.keys.include?(by)
order_string << by
else
raise TagError.new("`by' attribute of `each' tag must be set to a valid field name")
end
if order =~ /^(asc|desc)$/i
order_string << " #{$1.upcase}"
else
raise TagError.new(%{`order' attribute of `each' tag must be set to either "asc" or "desc"})
end
options[:order] = order_string
status = (attr[:status] || ( dev?(tag.globals.page.request) ? 'all' : 'published')).downcase
unless status == 'all'
stat = Status[status]
unless stat.nil?
options[:conditions] = ["(virtual = ?) and (status_id = ?)", false, stat.id]
else
raise TagError.new(%{`status' attribute of `each' tag must be set to a valid status})
end
else
options[:conditions] = ["virtual = ?", false]
end
options
end
def remove_trailing_slash(string)
(string =~ %r{^(.*?)/$}) ? $1 : string
end
def tag_part_name(tag)
tag.attr['part'] || 'body'
end
def build_regexp_for(tag, attribute_name)
ignore_case = tag.attr.has_key?('ignore_case') && tag.attr['ignore_case']=='false' ? nil : true
begin
regexp = Regexp.new(tag.attr['matches'], ignore_case)
rescue RegexpError => e
raise TagError.new("Malformed regular expression in `#{attribute_name}' argument of `#{tag.name}' tag: #{e.message}")
end
regexp
end
def relative_url_for(url, request)
File.join(request.relative_url_root, url)
end
def absolute_path_for(base_path, new_path)
if new_path.first == '/'
new_path
else
File.expand_path(File.join(base_path, new_path))
end
end
def page_found?(page)
page && !(FileNotFoundPage === page)
end
def boolean_attr_or_error(tag, attribute_name, default)
attribute = attr_or_error(tag, :attribute_name => attribute_name, :default => default.to_s, :values => 'true, false')
(attribute.to_s.downcase == 'true') ? true : false
end