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
|
#
# holds the set or properties defined for each static page.
# property sets are organized by locale.
#
# e.g.
#
# setting the property (in en.haml):
#
# - @title = 'hi'
#
# getting the property
#
# page.props.title
#
require 'i18n'
require 'time'
require 'rubygems'
require 'haml'
require 'RedCloth'
class PropertySet
DEFAULT_LOCALE = 'en'
#
# a simple class to pass through all member variables as attributes.
# when the template for a page is evaluated, all the member variabled defined in that template
# are loaded as member variables of the AttrObject instance.
#
class AttrObject
def initialize(property_set, locale)
@_ps = property_set
@_locale = locale
#code.gsub!(/^((?!^\-\ \@).)*$/, '') # remove any lines not starting with '- @'
#code.gsub!(/^- /m, '') # remove '-'
#instance_eval(code)
#instance_variables.grep(/_at$/).each do |time_variable|
# instance_variable_set(time_variable, Time.parse(instance_variable_get(time_variable)))
#end
end
def method_missing(method)
get(method)
end
def textile(str)
RedCloth.new(str).to_html
end
def get(var_name)
value = instance_variable_get("@#{var_name}")
if value.nil?
if @_locale != DEFAULT_LOCALE
# try value from default locale
@_ps.get_var(var_name)
else
# try inherited value
@_ps.get_inherited_var(var_name, @_locale)
end
else
value
end
end
def set(var_name, value)
instance_variable_set("@#{var_name}", value)
end
end
def initialize(page=nil)
@page = page
@locales = {}
end
#
# maybe something like this in the future:
#
#contr = PagesController.new()
#contr.response = ActionController::Response.new()
#scope = ActionView::Base.new(["#{RAILS_ROOT}/app/views/pages","#{RAILS_ROOT}/app/views"], {}, contr)
#scope.template_format = 'html'
#Haml::Engine.new(value, :format => :html5).render(scope)
#
#
# evaluate the template_string, and load the variables defined into an AttrObject.
#
def eval(locale, template_string)
attrs = AttrObject.new(self, locale)
body = nil
begin
body = Haml::Engine.new(template_string, :format => :html5).render(attrs) # template is evaluated in scope of attrs
rescue Exception => exc
# eat exceptions
end
attrs.instance_variable_set('@body', body)
attrs.instance_variables.grep(/_at$/).each do |time_variable|
attrs.instance_variable_set(time_variable, Time.parse(attrs.instance_variable_get(time_variable)))
end
@locales[locale] = attrs
end
#
# allows property_set.propname shortcut, assumes default locale
#
def method_missing(method)
get_var(method)
end
def locale(l)
@locales[l.to_s] || @locales[DEFAULT_LOCALE]
end
def get_var(var_name, locale=I18n.locale)
attrs = locale(locale)
if attrs
attrs.get(var_name)
else
nil
end
end
#
# tries to get the value of an inherited variable
#
def get_inherited_var(var_name, locale=I18n.locale)
if @page && @page.parent && @page.parent.props
@page.parent.props.get_var(var_name, locale)
end
end
end
#
# a simple little test.
#
if ARGV.grep('--test').any?
text_en = "
- @title = 'hi'
- @author = 'you'
- @created_at = 'Sun Aug 12 18:32:20 PDT 2012'
- ignored = 1
this is the body"
text_es = "
- @title = 'hola'
- @author = 'tu'
- @heading = textile 'h1. hi'
"
ps = PropertySet.new
ps.eval('en', text_en)
ps.eval('es', text_es)
p ps.title == 'hi'
p ps.locale(:es).title == 'hola'
p ps.locale(:es).created_at == Time.parse('Sun Aug 12 18:32:20 PDT 2012')
p ps.ignored == nil
p ps.locale(:es).heading == "<h1>hi</h1>"
p ps.body == "this is the body\n"
end
|