Changesets can be listed by changeset number.
The Git repository is here.
Changeset 53
Added <r:parent> tag in line with a Radiant mailing list post. The
implementation may change in future in relation to an alternative
approach suggested in a follow-up, but the implementation used in
this revision is good enough for now.
- Comitted by: adh
- Date: Friday August 04 16:26:41 2006 (over 18 years ago)
Affected files:
rool/rails/radiant/trunk/app/models/page_context.rb:
prev. | current | |
class PageContext < Radius::Context | ||
class TagError < StandardError; end | ||
3 | | |
3 | ||
attr_reader :page | ||
5 | | |
5 | ||
def initialize(page) | ||
super() | ||
8 | | |
8 | ||
@page = page | ||
globals.page = @page | ||
... | ... | |
tag.locals.page = tag.globals.page | ||
tag.expand | ||
end | ||
20 | | |
20 | ||
# | ||
# <r:url /> | ||
# <r:title /> | ||
... | ... | |
tag.locals.children = tag.locals.page.children | ||
tag.expand | ||
end | ||
41 | | |
41 | ||
# | ||
# <r:children:count /> | ||
# | ||
... | ... | |
define_tag 'children:count' do |tag| | ||
tag.locals.children.count | ||
end | ||
50 | | |
50 | ||
# | ||
# <r:children:first>...<r:children:first> | ||
# | ||
... | ... | |
tag.expand | ||
end | ||
end | ||
64 | | |
64 | ||
# | ||
# <r:children:last>...</r:children:last> | ||
# | ||
... | ... | |
tag.expand | ||
end | ||
end | ||
78 | | |
78 | ||
# | ||
80 | | |
80 | # <r:children:each [offset="number"] [limit="number"] [by="attribute"] [order="asc|desc"] | |
# [status="draft|reviewed|published|hidden|all"]> | ||
# ... | ||
# </r:children:each> | ||
... | ... | |
# | ||
define_tag "children:each" do |tag| | ||
attr = tag.attr.symbolize_keys | ||
90 | | |
90 | ||
options = {} | ||
92 | | |
92 | ||
[:limit, :offset].each do |symbol| | ||
if number = attr[symbol] | ||
if number =~ /^\d{1,4}$/ | ||
... | ... | |
end | ||
end | ||
end | ||
102 | | |
102 | ||
by = (attr[:by] || 'published_at').strip | ||
order = (attr[:order] || 'asc').strip | ||
order_string = '' | ||
... | ... | |
raise TagError.new(%{`order' attribute of `each' tag must be set to either "asc" or "desc"}) | ||
end | ||
options[:order] = order_string | ||
117 | | |
117 | ||
status = (attr[:status] || 'published').downcase | ||
unless status == 'all' | ||
stat = Status[status] | ||
... | ... | |
else | ||
options[:conditions] = ["virtual = ?", false] | ||
end | ||
129 | | |
129 | ||
result = [] | ||
children = tag.locals.children | ||
tag.locals.previous_headers = {} | ||
... | ... | |
tag.locals.child = item | ||
tag.locals.page = item | ||
result << tag.expand | ||
137 | | |
137 | end | |
result | ||
end | ||
140 | | |
140 | ||
# | ||
# <r:child>...</r:child> | ||
# | ||
... | ... | |
end | ||
# | ||
153 | | |
153 | # <r:parent>...</r:parent> | |
# | ||
155 | # Page attribute tags inside of this tag refer to the parent of the current page. | |
156 | # Included as a Radiant patch according to Jay Levitt's mailing list post held at | |
157 | # "http://www.ruby-forum.com/topic/75229". | |
158 | # | |
159 | define_tag 'parent' do |tag| | |
160 | tag.locals.page = page.parent || page | |
161 | tag.expand | |
162 | end | |
163 | ||
164 | # | |
165 | # <r:header [name="header_name"] [restart="name1[;name2;...]"]>...</r:header> | |
166 | # | |
# 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. | ||
# | ||
... | ... | |
part_page.behavior.render_page_part(part_name) | ||
end | ||
end | ||
216 | | |
228 | ||
# | ||
# <r:if_content [part="part_name"]>...</r:if_content> | ||
# | ||
... | ... | |
tag.expand | ||
end | ||
end | ||
230 | | |
242 | ||
# | ||
# <r:unless_content [part="part_name"]>...</r:unless_content> | ||
# | ||
... | ... | |
tag.expand | ||
end | ||
end | ||
243 | | |
255 | ||
# | ||
# <r:if_url matches="regexp" [ignore_case="true|false"]>...</if_url> | ||
# | ||
... | ... | |
tag.expand | ||
end | ||
end | ||
258 | | |
270 | ||
# | ||
# <r:unless_url matches="regexp" [ignore_case="true|false"]>...</unless_url> | ||
# | ||
# The opposite of the 'if_url' tag. | ||
263 | | |
275 | # | |
define_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') | ||
... | ... | |
tag.expand | ||
end | ||
end | ||
271 | | |
283 | ||
# | ||
# <r:author /> | ||
# | ||
... | ... | |
author.name | ||
end | ||
end | ||
283 | | |
295 | ||
# | ||
# <r:date [format="format_string"] /> | ||
# | ||
... | ... | |
date.strftime(format) | ||
end | ||
end | ||
299 | | |
311 | ||
# | ||
# <r:link [anchor="name"] [other attributes...] /> | ||
# <r:link>...</r:link> | ||
... | ... | |
raise TagError.new("`snippet' tag must contain `name' attribute") | ||
end | ||
end | ||
355 | | |
367 | ||
# | ||
# <r:find url="value_to_find">...</r:find> | ||
# | ||
... | ... | |
raise TagError.new("`find' tag must contain `url' attribute") | ||
end | ||
end | ||
371 | | |
383 | ||
# | ||
# <r:random> | ||
# <r:option>...</r:option> | ||
# <r:option>...</r:option> | ||
# ... | ||
377 | | |
389 | # <r:random> | |
# | ||
# Randomly renders one of the options specified by the 'option' tags. | ||
# | ||
... | ... | |
items = tag.locals.random | ||
items << tag.block | ||
end | ||
392 | | |
404 | ||
# | ||
# <r:comment>...</r:comment> | ||
# | ||
... | ... | |
# | ||
define_tag 'comment' do |tag| | ||
end | ||
400 | | |
412 | ||
# | ||
# <r:escape_html>...</r:escape_html> | ||
# | ||
... | ... | |
define_tag "escape_html" do |tag| | ||
CGI.escapeHTML(tag.expand) | ||
end | ||
409 | | |
421 | ||
# | ||
# <r:rfc1123_date /> | ||
# | ||
... | ... | |
CGI.rfc1123_date(date.to_time) | ||
end | ||
end | ||
421 | | |
433 | ||
# | ||
# <r:navigation urls="[Title: url; Title: url; ...]"> | ||
# <r:normal><a href="<r:url />"><r:title /></a></r:normal> | ||
... | ... | |
hash[symbol] | ||
end | ||
end | ||
481 | | |
493 | ||
end | ||
483 | | |
495 | ||
def render_tag(name, attributes = {}, &block) | ||
super | ||
rescue Exception => e | ||
render_error_message(e.message) | ||
end | ||
489 | | |
501 | ||
def tag_missing(name, attributes = {}, &block) | ||
super | ||
rescue Radius::UndefinedTagError => e | ||
raise TagError.new(e.message) | ||
end | ||
495 | | |
507 | ||
private | ||
497 | | |
509 | ||
def render_error_message(message) | ||
"<div><strong>#{message}</strong></div>" | ||
end | ||
501 | | |
513 | ||
def remove_trailing_slash(string) | ||
(string =~ %r{^(.*?)/$}) ? $1 : string | ||
end | ||
505 | | |
517 | ||
def tag_part_name(tag) | ||
tag.attr['part'] || 'body' | ||
end | ||
509 | | |
521 | ||
def tag_time_format(tag) | ||
(tag.attr['format'] || '%A, %B %d, %Y') | ||
end | ||
513 | | |
525 | ||
def postgres? | ||
ActiveRecord::Base.connection.adapter_name =~ /postgres/i | ||
end |