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
|
#
# 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
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
#
# evaluate the template_string, and load the variables defined into an AttrObject.
#
def eval(locale, template_string)
# render to the template to get the instance variables
attrs = AttrObject.new(self, locale)
begin
# template is evaluated with binding of attrs object
Haml::Engine.new(template_string, :format => :html5).render(attrs)
rescue Exception => exc
# eat exceptions
end
# convert date/time variables to objects of class Time
attrs.instance_variables.grep(/_at$/).each do |time_variable|
attrs.instance_variable_set(time_variable, Time.parse(attrs.instance_variable_get(time_variable)))
end
# save the AttrObject
@locales[locale] = attrs
end
#
# ALTERNATE EVAL
#
# body = nil
# controller = ApplicationController.new()
# controller.response = ActionController::Response.new()
# view = ActionView::Base.new(["#{Rails.root}/app/views/pages","#{Rails.root}/app/views"], {}, controller)
# view.extend ApplicationHelper # TODO: figure out how to extend all helpers
# view.extend BlogHelper
# view.extend HamlHelper
# view.extend NavigationHelper
# begin
# body = Haml::Engine.new(template_string, :format => :html5).render(view) # template is evaluated with binding of view object
# rescue Exception => exc
# # eat exceptions
# end
#
# this doesn't work because view doesn't get instance variables set
#
# copy new instance variables
# new_variables = view.instance_variables
# new_variables = original_instance_variables - view.instance_variables
# new_variables.each do |variable|
# #attrs.instance_variable_set(variable, view.instance_variable_get(variable))
# end
#
# clean up attrs
# attrs.instance_variable_set('@body', body)
#
#
# 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
|