masterview 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +17 -0
- data/README +36 -504
- data/RELEASE_NOTES +126 -45
- data/Rakefile +215 -86
- data/TODO +8 -3
- data/doc/configuration.html +485 -0
- data/doc/directives.html +1085 -0
- data/doc/guide.html +243 -0
- data/doc/index.html +287 -0
- data/doc/installation.html +376 -0
- data/doc/stylesheets/masterview.css +206 -0
- data/doc/stylesheets/mv-config.css +23 -0
- data/doc/stylesheets/mv-directives.css +18 -0
- data/doc/stylesheets/mv-installation.css +10 -0
- data/doc/troubleshooting.html +18 -0
- data/examples/product.html +256 -0
- data/examples/product.html.old +107 -0
- data/examples/rails_app_config/masterview/environment/development.rb +22 -0
- data/examples/rails_app_config/masterview/environment/production.rb +9 -0
- data/examples/rails_app_config/masterview/settings.rb +59 -0
- data/examples/test.import +80 -0
- data/init.rb +26 -12
- data/lib/masterview/analyzer.rb +25 -15
- data/lib/masterview/directive_base.rb +4 -0
- data/lib/masterview/directive_helpers.rb +7 -5
- data/lib/masterview/directives/import_render.rb +6 -0
- data/lib/masterview/directives/insert_generated_comment.rb +8 -8
- data/lib/masterview/extras/app/controllers/masterview_controller.rb +154 -2
- data/lib/masterview/extras/app/views/masterview/admin/create.rhtml +4 -4
- data/lib/masterview/extras/app/views/masterview/admin/empty.rhtml +1 -1
- data/lib/masterview/extras/app/views/masterview/admin/list.rhtml +14 -9
- data/lib/masterview/extras/app/views/masterview/admin/view_rhtml.rhtml +70 -0
- data/lib/masterview/extras/init_logger.rb +102 -0
- data/lib/masterview/extras/init_rails_erb_mv_direct.rb +117 -0
- data/lib/masterview/extras/init_rails_reparse_checking.rb +59 -0
- data/lib/masterview/extras/watcher.rb +17 -23
- data/lib/masterview/filter_helpers.rb +26 -0
- data/lib/masterview/initializer.rb +912 -0
- data/lib/masterview/io.rb +352 -0
- data/lib/masterview/keyword_expander.rb +116 -0
- data/lib/masterview/masterview_version.rb +2 -2
- data/lib/masterview/mtime_tracking_hash.rb +44 -0
- data/lib/masterview/parser.rb +64 -92
- data/lib/masterview/pathname_extensions.rb +33 -0
- data/lib/masterview/template_spec.rb +49 -56
- data/lib/masterview.rb +40 -85
- data/test/fixtures/configs/fake_rails_app_with_config/config/masterview/environments/development.rb +12 -0
- data/test/fixtures/configs/fake_rails_app_with_config/config/masterview/environments/production.rb +11 -0
- data/test/fixtures/configs/fake_rails_app_with_config/config/masterview/settings.rb +23 -0
- data/test/fixtures/templates/product.html +256 -0
- data/test/fixtures/templates/test.import +80 -0
- data/test/test_helper.rb +5 -3
- data/test/tmp/template/foo.txt +1 -0
- data/test/tmp/templates_src/product.html +256 -0
- data/test/tmp/views/layouts/product.rhtml +35 -0
- data/test/tmp/views/product/_form.rhtml +30 -0
- data/test/tmp/views/product/_product.rhtml +14 -0
- data/test/tmp/views/product/_show.rhtml +27 -0
- data/test/tmp/views/product/destroy.rhtml +27 -0
- data/test/tmp/views/product/edit.rhtml +26 -0
- data/test/tmp/views/product/list.rhtml +31 -0
- data/test/tmp/views/product/new.rhtml +29 -0
- data/test/tmp/views/product/show.rhtml +16 -0
- data/test/unit/config_settings_test.rb +172 -0
- data/test/{attr_test.rb → unit/directive_attr_test.rb} +2 -2
- data/test/{block_test.rb → unit/directive_block_test.rb} +2 -2
- data/test/{content_test.rb → unit/directive_content_test.rb} +2 -2
- data/test/{else_test.rb → unit/directive_else_test.rb} +2 -2
- data/test/{elsif_test.rb → unit/directive_elsif_test.rb} +2 -2
- data/test/{form_test.rb → unit/directive_form_test.rb} +2 -2
- data/test/{global_inline_erb_test.rb → unit/directive_global_inline_erb_test.rb} +2 -2
- data/test/{hidden_field_test.rb → unit/directive_hidden_field_test.rb} +2 -2
- data/test/{if_test.rb → unit/directive_if_test.rb} +2 -2
- data/test/unit/directive_import_render_test.rb +62 -0
- data/test/{import_test.rb → unit/directive_import_test.rb} +2 -2
- data/test/{javascript_include_test.rb → unit/directive_javascript_include_test.rb} +2 -2
- data/test/{link_to_if_test.rb → unit/directive_link_to_if_test.rb} +2 -2
- data/test/{link_to_test.rb → unit/directive_link_to_test.rb} +2 -2
- data/test/{omit_tag_test.rb → unit/directive_omit_tag_test.rb} +2 -2
- data/test/{password_field_test.rb → unit/directive_password_field_test.rb} +2 -2
- data/test/{replace_test.rb → unit/directive_replace_test.rb} +2 -2
- data/test/{stylesheet_link_test.rb → unit/directive_stylesheet_link_test.rb} +2 -2
- data/test/{submit_test.rb → unit/directive_submit_test.rb} +2 -2
- data/test/{text_area_test.rb → unit/directive_text_area_test.rb} +2 -2
- data/test/{text_field_test.rb → unit/directive_text_field_test.rb} +2 -2
- data/test/{example_test.rb → unit/example_test.rb} +1 -1
- data/test/unit/file_mio_test.rb +368 -0
- data/test/{filter_helpers_test.rb → unit/filter_helpers_test.rb} +1 -1
- data/test/unit/keyword_expander_test.rb +95 -0
- data/test/unit/mio_test.rb +110 -0
- data/test/unit/mtime_string_hash_mio_tree_test.rb +289 -0
- data/test/unit/mtime_tracking_hash_test.rb +38 -0
- data/test/{parser_test.rb → unit/parser_test.rb} +19 -1
- data/test/unit/pathname_extensions_test.rb +46 -0
- data/test/{run_parser_test.rb → unit/run_parser_test.rb} +7 -3
- data/test/unit/string_hash_mio_test.rb +320 -0
- data/test/unit/template_file_watcher_test.rb +107 -0
- data/test/{template_spec_test.rb → unit/template_spec_test.rb} +57 -21
- data/test/{template_test.rb → unit/template_test.rb} +123 -22
- data/test/xtras/config-mv-logger_config.rb +109 -0
- data/test/xtras/config_initialize_standalone.rb +53 -0
- metadata +111 -38
- data/lib/masterview/extras/rails_init.rb +0 -72
- data/test/import_render_test.rb +0 -30
- data/test/template_file_watcher_test.rb +0 -50
@@ -0,0 +1,912 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Jeff Barczewski and Deborah Lewis
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
#
|
24
|
+
# == MasterView Configuration and Initialization
|
25
|
+
#
|
26
|
+
# The MasterView template engine supports a number of configuration settings
|
27
|
+
# which allow a client to easily customize its operation.
|
28
|
+
#
|
29
|
+
# A MasterView::Configuration specification is used to allow the application to
|
30
|
+
# specify custom configuration options during application startup. The configuration options
|
31
|
+
# are installed in MasterView when its Initializer is run, which handles
|
32
|
+
# loading and configuring MasterView for operation.
|
33
|
+
#
|
34
|
+
#--
|
35
|
+
# This style of configuration specification + initializer follows the technique
|
36
|
+
# used by Rails itself. A configuration specification can be customized by the
|
37
|
+
# application in its startup and is used by the Initialzer to configure
|
38
|
+
# the MasterView template engine implementation at load time.
|
39
|
+
# [D. Lewis 31-May-2006]
|
40
|
+
#++
|
41
|
+
#
|
42
|
+
#--
|
43
|
+
# todo - change configuration to accomodate non-file based storage, use MIOTree objects
|
44
|
+
# [JJB 13-Jun-2006]
|
45
|
+
#++
|
46
|
+
#
|
47
|
+
module MasterView
|
48
|
+
|
49
|
+
# A Configuration holds all the configuration settings used the
|
50
|
+
# MasterView::Initializer to configure the template engine.
|
51
|
+
#
|
52
|
+
# Configuration provides defaults that suit most Rails applications.
|
53
|
+
# You can change the default settings in the configuration to customize
|
54
|
+
# MasterView with options suitable for your application.
|
55
|
+
#
|
56
|
+
# In the most common usage pattern, a Configuration instance initialized
|
57
|
+
# with the standard default settings is created implicitly when the
|
58
|
+
# Initializer is run, which happens automatically
|
59
|
+
# when Masterview is installed as a plugin in a Rails application.
|
60
|
+
# Application-specific settings are then loaded from the application's
|
61
|
+
# +config+ directory and used by the Initializer to configure
|
62
|
+
# the MasterView template engine for operation.
|
63
|
+
#
|
64
|
+
# When using MasterView in a standalone application (i.e., not within rails),
|
65
|
+
# it is also possible to create the Configuration instance in advance and
|
66
|
+
# pass it in explicitly to the Initializer. This technique can be useful
|
67
|
+
# when it is necessary to establish the application root directory location
|
68
|
+
# that MasterView uses during its operation or to specify to location of the
|
69
|
+
# application's configuration files.
|
70
|
+
#
|
71
|
+
# require 'masterview/initializer'
|
72
|
+
# config = MasterView::Configuration.new( :app_root_path => '/path/to/my/app' )
|
73
|
+
# config.template_src_dir_path = "masterview/templates"
|
74
|
+
# config.template_dst_dir_path = "masterview/output"
|
75
|
+
# #...set other config options...
|
76
|
+
# MasterView::Initializer.run( :process, config )
|
77
|
+
#
|
78
|
+
class Configuration
|
79
|
+
|
80
|
+
# directory in which the MasterView plugin or gem is installed
|
81
|
+
attr_reader :mv_installation_dir #:nodoc:
|
82
|
+
|
83
|
+
# root directory of MasterView itself and the built-in directives
|
84
|
+
# This is lib/masterview w/in the plugin or gem installation directory.
|
85
|
+
attr_reader :mv_code_base_dir #:nodoc:
|
86
|
+
|
87
|
+
# root directory when operating context is a rails app
|
88
|
+
attr_reader :rails_root_path #:nodoc:
|
89
|
+
|
90
|
+
# directory in a rails app for views (RAILS_ROOT/app/views)
|
91
|
+
attr_reader :rails_views_dir_path #:nodoc:
|
92
|
+
|
93
|
+
# RE spec for scripts which are used to launch rails
|
94
|
+
attr_accessor :rails_runner_scripts_pattern #:nodoc:
|
95
|
+
|
96
|
+
# is RAILS_ROOT defined?
|
97
|
+
attr_reader :has_rails_context #:nodoc:
|
98
|
+
|
99
|
+
# is the root a rails app?
|
100
|
+
def rails_app? #:nodoc:
|
101
|
+
@rails_root_path != nil
|
102
|
+
end
|
103
|
+
|
104
|
+
# Set to indicate that we're actually running the rails app (server or console...)
|
105
|
+
attr_accessor :running_rails #:nodoc:
|
106
|
+
|
107
|
+
# are we actually running rails when our root is a rails app?
|
108
|
+
def on_rails? #:nodoc:
|
109
|
+
@running_rails
|
110
|
+
end
|
111
|
+
|
112
|
+
# === General Options
|
113
|
+
|
114
|
+
# Path to the root location for the application.
|
115
|
+
# Used for resolving relative references.
|
116
|
+
#
|
117
|
+
# For a rails application, this is +RAILS_ROOT+.
|
118
|
+
#
|
119
|
+
attr_accessor :root_path
|
120
|
+
|
121
|
+
# Path to the directory containing the application's MasterView
|
122
|
+
# configuration settings files (if any). If no configuration
|
123
|
+
# directory or settings files are defined, MasterView runs
|
124
|
+
# with the standard default configuration options.
|
125
|
+
#
|
126
|
+
# Specified as a relative path from the application's +root_path+.
|
127
|
+
#
|
128
|
+
# Default: for a Rails application, this is <tt>RAILS_ROOT/config/masterview</tt>.
|
129
|
+
#
|
130
|
+
attr_accessor :config_dir_path
|
131
|
+
# Specify the directory where masterview templates are located
|
132
|
+
# as a relative path from the application's +root_path+.
|
133
|
+
def config_dir_path= rel_path #:nodoc:
|
134
|
+
# bolts down abs path, thanks to our firm roots
|
135
|
+
@config_dir_path = rel_path.nil? ? nil : ( rel_path.empty? ? root_path : File.join( root_path, rel_path ))
|
136
|
+
end
|
137
|
+
|
138
|
+
# The current execution environment, if applicable
|
139
|
+
# (e.g., 'development', 'test', 'production').
|
140
|
+
#
|
141
|
+
# Used for loading application settings from config directory, if available.
|
142
|
+
#
|
143
|
+
# For a Rails application, this is +RAILS_ENV+.
|
144
|
+
#
|
145
|
+
attr_accessor :environment
|
146
|
+
|
147
|
+
# Paths of directories to load directives from.
|
148
|
+
#
|
149
|
+
# The directory containing the built-in MasterView directives
|
150
|
+
# is installed by default. If the application provides
|
151
|
+
# a <tt>masterview/directives</tt> directory in its +root_path+,
|
152
|
+
# that directory is also automatically added to the directives
|
153
|
+
# load path.
|
154
|
+
#
|
155
|
+
# Append additional path(s) to load custom directives from specific
|
156
|
+
# locations.
|
157
|
+
attr_accessor :directive_paths
|
158
|
+
|
159
|
+
# Relative path from +root_path+ of the temp directory used for creating
|
160
|
+
# backup files before rebuilding/updating a template file.
|
161
|
+
#
|
162
|
+
# Set to nil to suppress backups.
|
163
|
+
#
|
164
|
+
# Default: <tt>RAILS_ROOT/tmp/masterview/rebuild/backups</tt>
|
165
|
+
#--
|
166
|
+
# ISSUE: change this to use IOMgr mapping, how best to do this config??
|
167
|
+
# Leave existing mechanism here, but drop out of documentation and examples
|
168
|
+
# until resolved
|
169
|
+
#++
|
170
|
+
attr_accessor :rebuild_backups_tmp_dir_path #:nodoc:
|
171
|
+
|
172
|
+
# Logger which will be used to record debug, warning, and error messages.
|
173
|
+
#
|
174
|
+
# Supported loggers:
|
175
|
+
# 'log4r' - the Log4r logging library
|
176
|
+
# 'logger' - Logger in the ruby standard class library
|
177
|
+
#
|
178
|
+
# Default: uses <code>Log4r</code> if available, otherwise a standard ruby +Logger+.
|
179
|
+
attr_accessor :logger
|
180
|
+
|
181
|
+
# Logging severity threshold for the logger.
|
182
|
+
#
|
183
|
+
# Specify the name of the logging level to use with the configured logger.
|
184
|
+
# Standard logging levels in both Log4r and ruby Logger are
|
185
|
+
# +DEBUG+, +INFO+, +WARN+, +ERROR+, +FATAL+.
|
186
|
+
# Other level names depend on the configured logger.
|
187
|
+
#
|
188
|
+
# Default: +INFO+ for development, +WARN+ otherwise
|
189
|
+
attr_accessor :log_level
|
190
|
+
|
191
|
+
# Sets a block which will be executed after MasterView has been fully initialized.
|
192
|
+
# Useful for per-environment configuration which depends on the plugin being
|
193
|
+
# fully initialized.
|
194
|
+
def after_initialize(&after_initialize_block)
|
195
|
+
@after_initialize_block = after_initialize_block
|
196
|
+
end
|
197
|
+
|
198
|
+
# Returns the block set in Configuration#after_initialize
|
199
|
+
def after_initialize_block
|
200
|
+
@after_initialize_block
|
201
|
+
end
|
202
|
+
|
203
|
+
# === Template Source Options
|
204
|
+
|
205
|
+
# Path to the directory where masterview templates are located.
|
206
|
+
#
|
207
|
+
# Assign the value as a relative path from the application's +root_path+
|
208
|
+
# or use the +template_src_dir_abs_path+ method to specify an absolute path.
|
209
|
+
#
|
210
|
+
# Default: <tt>RAILS_ROOT/app/views</tt> for a rails application
|
211
|
+
# or <tt>{root_path}/masterview/templates</tt> for a non-rails application.
|
212
|
+
attr_accessor :template_src_dir_path
|
213
|
+
# Specify the directory where masterview templates are located
|
214
|
+
# as a relative path from the application's +root_path+.
|
215
|
+
def template_src_dir_path= rel_path #:nodoc:
|
216
|
+
# bolts down abs path, thanks to our firm roots
|
217
|
+
@template_src_dir_path = rel_path.nil? ? nil : ( rel_path.empty? ? root_path : File.join( root_path, rel_path ))
|
218
|
+
end
|
219
|
+
# Specify the absolute path to the directory where masterview templates are located.
|
220
|
+
def template_src_dir_abs_path(abs_path)
|
221
|
+
@template_src_dir_path = abs_path
|
222
|
+
end
|
223
|
+
|
224
|
+
# Filename pattern for masterview template files
|
225
|
+
# within the template source directory.
|
226
|
+
#
|
227
|
+
# Default: <tt>'*.html'</tt>
|
228
|
+
attr_accessor :template_filename_pattern
|
229
|
+
|
230
|
+
# === Template Generation Options
|
231
|
+
|
232
|
+
# Path to the directory where Masterview template output (rhtml)
|
233
|
+
# will be generated.
|
234
|
+
#
|
235
|
+
# Assign the value as a relative path from the application's +root_path+
|
236
|
+
# or use the +template_dst_dir_abs_path+ method to specify an absolute path.
|
237
|
+
#
|
238
|
+
# Default: <tt>RAILS_ROOT/app/views</tt>
|
239
|
+
attr_accessor :template_dst_dir_path
|
240
|
+
# Specify the directory where masterview template output is generated
|
241
|
+
# as a relative path from the application's +root_path+.
|
242
|
+
def template_dst_dir_path= rel_path #:nodoc:
|
243
|
+
# bolts down abs path, thanks to our firm roots
|
244
|
+
@template_dst_dir_path = rel_path.nil? ? nil : ( rel_path.empty? ? root_path : File.join( root_path, rel_path ))
|
245
|
+
end
|
246
|
+
# Specify the absolute path to the directory where masterview template output is generated.
|
247
|
+
def template_dst_dir_abs_path(abs_path)
|
248
|
+
@template_dst_dir_path = abs_path
|
249
|
+
end
|
250
|
+
|
251
|
+
# Filename extension to use for generated output files if not
|
252
|
+
# explicitly specified in the +mv:generate+ attribute value.
|
253
|
+
#
|
254
|
+
# Default: <tt>'.rhtml'</tt>
|
255
|
+
attr_accessor :output_filename_extension
|
256
|
+
|
257
|
+
# Filename extension to use for generated files if not explicitly specified.
|
258
|
+
#
|
259
|
+
# Default: <tt>'.rhtml'</tt>
|
260
|
+
attr_accessor :generated_file_default_extension #FIXME
|
261
|
+
|
262
|
+
# Boolean which controls whether to include a comment in template
|
263
|
+
# output files indicating that the file was generated by MasterView.
|
264
|
+
#
|
265
|
+
# By default, a warning comment is generated in template output files
|
266
|
+
# indicating that the file was generated by MasterView from a template
|
267
|
+
# and should not be directly edited, as any changes would be lost when the
|
268
|
+
# output file is regenerated.
|
269
|
+
#
|
270
|
+
# Default: +true+
|
271
|
+
attr_accessor :include_generated_file_comment
|
272
|
+
|
273
|
+
# Text for generated-file comment inserted in rhtml template output files
|
274
|
+
# generated by MasterView.
|
275
|
+
#
|
276
|
+
# Standard comment includes the name of the master template file which contains
|
277
|
+
# the source to edit.
|
278
|
+
#
|
279
|
+
# Variable substitution on the comment text will replace a reference
|
280
|
+
# to <tt>{template_path}</tt> with the pathname of the source template file.
|
281
|
+
#
|
282
|
+
attr_accessor :generated_file_comment
|
283
|
+
|
284
|
+
# === Template Parsing Options
|
285
|
+
|
286
|
+
# Specify whether MasterView should handle exceptions when
|
287
|
+
# parsing template files.
|
288
|
+
#
|
289
|
+
# Exceptions that occur during template parsing are always
|
290
|
+
# recorded in the debug log. These are generally problems
|
291
|
+
# caused by invalid (x)html in the template.
|
292
|
+
#
|
293
|
+
# Set to +true+ to have the template parser catch exceptions and
|
294
|
+
# continue processing after logging a problem report.
|
295
|
+
#
|
296
|
+
# Set to +false+ to have exceptions raised to the application.
|
297
|
+
#
|
298
|
+
# Default: +true+
|
299
|
+
#
|
300
|
+
attr_accessor :handle_parse_exceptions
|
301
|
+
|
302
|
+
# Default option settings for template parsing (a hash)
|
303
|
+
# :tidy => false - run tidy before parsing (tidy_path must be set if enabled)
|
304
|
+
# :escape_erb => true - escapes <% %> before parsing
|
305
|
+
attr_accessor :default_parser_options
|
306
|
+
|
307
|
+
# Path on this system to tidy library if <tt>:tidy</tt> parser option is enabled
|
308
|
+
# so that masterview templates will be run through tidy before being parsed.
|
309
|
+
# Allows invalid xhmtl to be corrected before masterview template parsing is performed.
|
310
|
+
attr_accessor :tidy_path
|
311
|
+
|
312
|
+
# XML name space prefix for MasterView directive attributes in template html.
|
313
|
+
# e.g. mv:generate="target.rhtml".
|
314
|
+
#
|
315
|
+
# Default: <tt>'mv:'</tt>
|
316
|
+
attr_accessor :namespace_prefix
|
317
|
+
|
318
|
+
# Xhtml-safe substitution for '<%' in a masterview template
|
319
|
+
# NOTE: you must also update inline_erb_substitution_regex if this is changed.
|
320
|
+
#
|
321
|
+
# Default: <tt>'{{{'</tt>
|
322
|
+
attr_accessor :inline_erb_start
|
323
|
+
|
324
|
+
# Xhtml safe substitution for '%>'in a masterview template
|
325
|
+
# NOTE: you must also update inline_erb_substitution_regex if this is changed.
|
326
|
+
#
|
327
|
+
# Default: <tt>'}}}'</tt>
|
328
|
+
attr_accessor :inline_erb_end
|
329
|
+
|
330
|
+
# regex used to find escaped inline_erb markup.
|
331
|
+
# Needs to match +inline_erb_start+ and +inline_erb_end+.
|
332
|
+
attr_accessor :inline_erb_substitution_regex
|
333
|
+
|
334
|
+
# === Rails Application Options
|
335
|
+
|
336
|
+
# Boolean which specifies whether masterview templates are parsed
|
337
|
+
# during initial application startup loading.
|
338
|
+
#
|
339
|
+
# Set to +false+ to disable all load-time parsing.
|
340
|
+
# Template parsing must be manually triggered when the auto-parse option is disabled.
|
341
|
+
#
|
342
|
+
# Default: +true+
|
343
|
+
attr_accessor :parse_masterview_templates_at_startup
|
344
|
+
|
345
|
+
# Boolean which determines whether masterview templates are automatically
|
346
|
+
# reparsed if the template changes after initial application loading.
|
347
|
+
#
|
348
|
+
# Set to +true+ to monitor masterview templates for file system changes
|
349
|
+
# during Rails request dispatching.
|
350
|
+
#
|
351
|
+
# Set to +false+ to disable changed-template monitoring.
|
352
|
+
# Template parsing must be manually triggered when the auto-parse option is disabled.
|
353
|
+
#
|
354
|
+
# This option is only supported when running as part of a Rails application.
|
355
|
+
# It is enabled by default in the standard Rails development configuration
|
356
|
+
# (+parse_masterview_templates_at_startup+ is enabled and
|
357
|
+
# ActionController::Base.perform_caching is false so that changed classes
|
358
|
+
# are dynamically reloaded during request dispatching).
|
359
|
+
#
|
360
|
+
# Automatic change-detection and reparsing of masterview templates is disabled
|
361
|
+
# by default in the usual Rails configuration for production mode
|
362
|
+
# (ActionController::Base.perform_caching is on to enable loaded class caching).
|
363
|
+
#
|
364
|
+
# Default: +true+ if parse_masterview_templates_at_startup is enabled
|
365
|
+
# and ActionController is reloading changed classes (no caching)
|
366
|
+
attr_accessor :reparse_changed_masterview_templates
|
367
|
+
|
368
|
+
# Enable MasterView admin pages in the rails application.
|
369
|
+
#
|
370
|
+
# Enables the masterview admin controller at <tt>http://yourappdomain/masterview</tt>.
|
371
|
+
#
|
372
|
+
# Default: +false+
|
373
|
+
attr_accessor :enable_admin_pages
|
374
|
+
|
375
|
+
# Enable MasterView admin view rhtml feature
|
376
|
+
#
|
377
|
+
# If MasterView admin pages are enabled, then you may set this feature to true and it
|
378
|
+
# will allow you to get to the generated rhtml from the MasterView admin page which is
|
379
|
+
# especially useful for debugging or understanding how MasterView is interpretting the
|
380
|
+
# your templates. When enabled you may click on any of the generated rhtml parts from
|
381
|
+
# the list view and it will show you the rhtml that is generated. For security purposes
|
382
|
+
# you will probably want this to disable this feature in production (or even better
|
383
|
+
# disable admin pages itself). Alternatively there is a rake task called mv:view_rhtml
|
384
|
+
# which allows you to see the rhtml from the command line (regardless of this setting).
|
385
|
+
#
|
386
|
+
# Default +false+
|
387
|
+
attr_accessor :enable_view_rhtml
|
388
|
+
|
389
|
+
# Generate rhtml files if true, rails will load them from there. Otherwise when this
|
390
|
+
# setting is false, enable rails app to read rhtml(erb) directly from MasterView cache
|
391
|
+
# bypassing the serialization to the file system.
|
392
|
+
# Default: +false+
|
393
|
+
attr_accessor :generate_rhtml_files
|
394
|
+
|
395
|
+
# Create a new Configuration instance, initialized with the default
|
396
|
+
# values.
|
397
|
+
#
|
398
|
+
# Optional arguments to the constructor allow for initializing defaults
|
399
|
+
# for a rails app when not actually running rails or for initializing
|
400
|
+
# defaults for a standalone app with context anchored at some well-defined
|
401
|
+
# point in space (other than where we're actually running).
|
402
|
+
#
|
403
|
+
# :app_root_path => path to the root directory of the application
|
404
|
+
# :rails_app_root_path => path to the root directory of a Rails app
|
405
|
+
# :environment => current environment for loading settings
|
406
|
+
#
|
407
|
+
# The +app_root_path+ and +rails_app_root_path+ arguments are mutually exclusive.
|
408
|
+
# Use +rails_app_root_path+ when operating on a Rails application which
|
409
|
+
# isn't actually running; use +app_root_path+ for a non-rails application.
|
410
|
+
#
|
411
|
+
def initialize( params={} )
|
412
|
+
|
413
|
+
# unpack the supported keyword args
|
414
|
+
app_root_path = params[:app_root_path]
|
415
|
+
rails_app_root_path = params[:rails_app_root_path]
|
416
|
+
env = params[:environment]
|
417
|
+
|
418
|
+
program_root_path = File.expand_path( '.' )
|
419
|
+
#assert program_root_path == Dir.pwd
|
420
|
+
|
421
|
+
debug_TRACE_HACK = false #temp dev debug tracking for rails vs. non-rails init
|
422
|
+
|
423
|
+
@mv_code_base_dir = File.expand_path( File.dirname(__FILE__) )
|
424
|
+
builtin_directives_path = File.join( mv_code_base_dir, 'directives')
|
425
|
+
@mv_installation_dir = File.expand_path( "#{File.dirname(__FILE__)}/../.." )
|
426
|
+
|
427
|
+
#ISSUE: should probably also detect std console or breakpointer launch scripts [DJL 10-Jun-2006]
|
428
|
+
@rails_runner_scripts_pattern = /server|dispatch/
|
429
|
+
|
430
|
+
@has_rails_context = (defined?(::RAILS_ROOT) != nil)
|
431
|
+
decide_if_running_rails
|
432
|
+
|
433
|
+
# only run if auto parsing and when launching server, check if need to update mv files
|
434
|
+
# MasterView::ParseMasterViewTemplatesAtStartup
|
435
|
+
#ISSUE: what about console and breakpointer scripts?
|
436
|
+
|
437
|
+
if debug_TRACE_HACK
|
438
|
+
STDOUT.puts "\n####Initializing MasterView config (default settings)"
|
439
|
+
STDOUT.puts "...RAILS_ROOT=#{defined?(::RAILS_ROOT) ? ::RAILS_ROOT : '(undefined)'}"
|
440
|
+
STDOUT.puts "...has_rails_context=#{has_rails_context.nil? ? 'nil' : has_rails_context}"
|
441
|
+
end
|
442
|
+
|
443
|
+
# establish the fundamental roots for anchoring relative references
|
444
|
+
# Scenarios: running a rails app, operating on a rails app but not running it, standalone
|
445
|
+
#assert at most one of the two args is supplied, they're mutually exclusive
|
446
|
+
if has_rails_context
|
447
|
+
@rails_root_path = File.expand_path( ::RAILS_ROOT )
|
448
|
+
elsif rails_app_root_path
|
449
|
+
@rails_root_path = File.expand_path( rails_app_root_path )
|
450
|
+
elsif app_root_path.nil? && looks_like_rails_app?
|
451
|
+
# if client hasn't specifically provided an app or rails root anchor parm
|
452
|
+
# and we aren't actually launching rails, sniff around and make a good guess
|
453
|
+
# at whether this app has the rails structure so we can set proper defaults
|
454
|
+
# (e.g., running rake tasks on a rails app)
|
455
|
+
# ISSUE: Is this just too clever? Maybe the rakefile should establish this?
|
456
|
+
# [DJL 15-Ajun-2006]
|
457
|
+
@rails_root_path = program_root_path
|
458
|
+
else
|
459
|
+
@rails_root_path = nil
|
460
|
+
end
|
461
|
+
@rails_views_dir_path = rails_app? ? File.join( rails_root_path, 'app/views' ) : nil
|
462
|
+
#if on_rails?
|
463
|
+
# #ISSUE: ActionController::Base.template_root vs. ActionController::Base.view_root???? significant diff??
|
464
|
+
# #assert rails_views_path ==ActionController::Base.template_root
|
465
|
+
#end
|
466
|
+
|
467
|
+
if debug_TRACE_HACK
|
468
|
+
STDOUT.puts "...rails_app?=#{rails_app? ? 'yes' : 'nope'}"
|
469
|
+
STDOUT.puts "...on_rails?=#{on_rails? ? 'yes' : 'nope'}"
|
470
|
+
end
|
471
|
+
if debug_TRACE_HACK and rails_app?
|
472
|
+
STDOUT.puts "...rails_root_path=#{self.rails_root_path}"
|
473
|
+
STDOUT.puts "...rails_views_dir_path=#{rails_views_dir_path}"
|
474
|
+
end
|
475
|
+
|
476
|
+
# general options - establish the roots to anchor MV in an operating context
|
477
|
+
if rails_app?
|
478
|
+
self.root_path = self.rails_root_path
|
479
|
+
elsif app_root_path
|
480
|
+
self.root_path = File.expand_path( app_root_path )
|
481
|
+
else
|
482
|
+
#ISSUE: add hook here to check for MASTERVIEW_ROOT_PATH or ENV[xxx]???
|
483
|
+
self.root_path = program_root_path
|
484
|
+
end
|
485
|
+
# For a rails app, we have a point of view on where to find config files.
|
486
|
+
# A standalone client needs to proactively tell us where to find their settings.
|
487
|
+
self.config_dir_path = rails_app? ? "config/masterview" : nil
|
488
|
+
self.environment = on_rails? ? ::RAILS_ENV : env
|
489
|
+
self.directive_paths = [ builtin_directives_path ]
|
490
|
+
|
491
|
+
#TODO: if rails_app? && File.exist?( "#{rails_root_path}/app/masterview/directives" ) THEN append it as well
|
492
|
+
self.rebuild_backups_tmp_dir_path = rails_app? ? File.join( rails_root_path, 'tmp/masterview/rebuild/backups') : nil
|
493
|
+
# use Log4r by default if available, otherwise Logger from standard ruby library
|
494
|
+
# find out if Kernel#require will succeed if we try to load Log4R
|
495
|
+
log4r_dir = $:.detect { |dir| File.exists?("#{dir}/log4r.rb") } #? path_contains? predicate somewhere??
|
496
|
+
self.logger = log4r_dir ? 'log4r' : 'logger'
|
497
|
+
#self.log_level = nil # use the default level of the logger
|
498
|
+
self.log_level = (self.environment == 'development') ? 'INFO' : 'WARN'
|
499
|
+
|
500
|
+
if debug_TRACE_HACK
|
501
|
+
STDOUT.puts "...root_path=#{root_path}"
|
502
|
+
STDOUT.puts "...config_dir_path=#{config_dir_path || 'nil'}"
|
503
|
+
STDOUT.puts "...environment=#{environment || 'nil'}"
|
504
|
+
STDOUT.puts "...directive_paths=[ #{directive_paths.join(', ')} ]"
|
505
|
+
end
|
506
|
+
|
507
|
+
# template source options
|
508
|
+
self.template_src_dir_path = rails_app? ? 'app/views' : 'masterview/templates' # bolts down abs ref
|
509
|
+
self.template_filename_pattern = '*.html'
|
510
|
+
|
511
|
+
STDOUT.puts "...template_src_dir_path=#{template_src_dir_path || 'nil'}" if debug_TRACE_HACK
|
512
|
+
|
513
|
+
# template generation options
|
514
|
+
self.template_dst_dir_path = rails_app? ? 'app/views' : 'masterview/output' # bolts down abs ref
|
515
|
+
self.output_filename_extension = '.rhtml'
|
516
|
+
self.generated_file_default_extension = '.rhtml'
|
517
|
+
self.include_generated_file_comment = true
|
518
|
+
self.generated_file_comment = <<-END
|
519
|
+
# WARNING - This is a generated file created by MasterView.
|
520
|
+
# Do not edit - changes will be lost when this file is re-generated.
|
521
|
+
#
|
522
|
+
# To make changes, edit the MasterView source file located at:
|
523
|
+
END
|
524
|
+
self.generated_file_comment << '# #{template_path}' # carefully avoid premature subst eval
|
525
|
+
|
526
|
+
STDOUT.puts "...template_dst_dir_path=#{template_dst_dir_path || 'nil'}" if debug_TRACE_HACK
|
527
|
+
|
528
|
+
# template parsing options
|
529
|
+
self.handle_parse_exceptions = true
|
530
|
+
self.default_parser_options = { :tidy => false, :escape_erb => true }
|
531
|
+
# default locations where tidy likely to be found; assume on user's PATH if on Windows
|
532
|
+
self.tidy_path = RUBY_PLATFORM =~ /mswin32/ ? 'tidy.exe' : '/usr/lib/libtidy.so'
|
533
|
+
self.namespace_prefix = 'mv:'
|
534
|
+
self.inline_erb_start = '{{{'
|
535
|
+
self.inline_erb_end = '}}}'
|
536
|
+
self.inline_erb_substitution_regex = /\{\{\{(([^}]|\}[^}]|\}\}[^}])*)\}\}\}/
|
537
|
+
|
538
|
+
# Rails application options
|
539
|
+
self.parse_masterview_templates_at_startup = true
|
540
|
+
self.reparse_changed_masterview_templates = on_rails? ? (not ActionController::Base.perform_caching) : false
|
541
|
+
self.enable_admin_pages = false
|
542
|
+
self.enable_view_rhtml = false
|
543
|
+
self.generate_rhtml_files = false
|
544
|
+
|
545
|
+
STDOUT.puts "...mv config initialized with default settings\n" if debug_TRACE_HACK
|
546
|
+
|
547
|
+
end
|
548
|
+
|
549
|
+
def decide_if_running_rails #:nodoc:
|
550
|
+
@running_rails = has_rails_context && ($PROGRAM_NAME =~ rails_runner_scripts_pattern) != nil
|
551
|
+
end
|
552
|
+
|
553
|
+
# see if this app has the std file structure that indicates a rails application
|
554
|
+
def looks_like_rails_app? #:nodoc:
|
555
|
+
std_rails_directories = [ 'app', 'config', 'public' ]
|
556
|
+
std_rails_directories.each { | dir_path |
|
557
|
+
return if ! File.directory?(dir_path)
|
558
|
+
}
|
559
|
+
true
|
560
|
+
end
|
561
|
+
|
562
|
+
# The path to the application's config settings file.
|
563
|
+
# By default the file is at <tt>config/masterview/settings.rb</tt>.
|
564
|
+
def app_settings_path
|
565
|
+
config_dir_path ? "#{config_dir_path}/settings.rb" : nil
|
566
|
+
end
|
567
|
+
|
568
|
+
# The path to the current environment's settings file (development.rb, etc.).
|
569
|
+
# By default the file is at <tt>config/masterview/environments/{environment}.rb</tt>.
|
570
|
+
def environment_settings_path
|
571
|
+
(config_dir_path && environment) ? "#{config_dir_path}/environments/#{environment}.rb" : nil
|
572
|
+
end
|
573
|
+
|
574
|
+
end
|
575
|
+
|
576
|
+
# The Initializer is responsible for processing the MasterView configuration.
|
577
|
+
#
|
578
|
+
# In a Rails application using the MasterView facilities,
|
579
|
+
# the Initializer is run automatically during rails startup
|
580
|
+
# when the plugin is loaded. No special action is required
|
581
|
+
# by the application.
|
582
|
+
#
|
583
|
+
# To customize the MasterView configuration, provide config settings files
|
584
|
+
# in your application's config directory, setting standard settings
|
585
|
+
# in <tt>config/masterview/settings.rb</tt> and optionally setting
|
586
|
+
# environment-specific values in <tt>config/masterview/environments/{environment}.rb</tt>.
|
587
|
+
# The settings files are executed with the +config+ variable initialized to
|
588
|
+
# the current configuration. This is the standard technique for a Rails application.
|
589
|
+
#
|
590
|
+
# An application using the MasterView facilities outside the Rails context
|
591
|
+
# must run the MasterView::Initializer during its application startup.
|
592
|
+
#
|
593
|
+
# It can be run as a simple command that uses the default configuration:
|
594
|
+
#
|
595
|
+
# require 'masterview/initializer'
|
596
|
+
# MasterView::Initializer.run
|
597
|
+
#
|
598
|
+
# or more simply just load MasterView:
|
599
|
+
#
|
600
|
+
# require 'masterview'
|
601
|
+
#
|
602
|
+
# In order to customize the MasterView configuration outside the Rails context,
|
603
|
+
# you must explicitly provide a basic Configuration to the Initializer
|
604
|
+
# which specifies the root location of the application and the paths
|
605
|
+
# to the template directories if the default locations are not appropriate.
|
606
|
+
# Configuration settings can be set directly or provided through config
|
607
|
+
# settings file by specifying the +config_dir_path+ to the application's
|
608
|
+
# setttings files.
|
609
|
+
#
|
610
|
+
# require 'masterview/initializer'
|
611
|
+
# config = MasterView::Configuration( :app_root_path = '/path/to/my/app' )
|
612
|
+
# config.environment = 'production'
|
613
|
+
# config.config_dir_path = 'masterview/config'
|
614
|
+
# #... additional settings will be loaded from the config dir...
|
615
|
+
# Masterview::Initializer( :process, config )
|
616
|
+
#
|
617
|
+
class Initializer
|
618
|
+
|
619
|
+
# The Configuration instance used by this Initializer instance.
|
620
|
+
attr_reader :configuration
|
621
|
+
|
622
|
+
# Runs the initializer. By default, this will invoke the #process method,
|
623
|
+
# which simply executes all of the initialization routines. Alternately,
|
624
|
+
# you can specify explicitly which initialization routine you want:
|
625
|
+
#
|
626
|
+
# MasterView::Initializer.run(:initialize_configuration)
|
627
|
+
#
|
628
|
+
# This is useful if you only want the config settings initialized, without
|
629
|
+
# incurring the overhead of completely loading the entire component.
|
630
|
+
#
|
631
|
+
def self.run(command = :process, configuration = Configuration.new)
|
632
|
+
if block_given?
|
633
|
+
yield configuration
|
634
|
+
end
|
635
|
+
initializer = new(configuration)
|
636
|
+
initializer.send(command)
|
637
|
+
initializer
|
638
|
+
end
|
639
|
+
|
640
|
+
# Create a new Initializer instance that references the given Configuration
|
641
|
+
# instance.
|
642
|
+
def initialize(configuration) #:nodoc:
|
643
|
+
@configuration = configuration
|
644
|
+
end
|
645
|
+
|
646
|
+
# Load the MasterView configuration settings
|
647
|
+
# and initialize the template engine.
|
648
|
+
def process
|
649
|
+
initialize_configuration
|
650
|
+
load_plugin
|
651
|
+
complete_plugin_installation
|
652
|
+
end
|
653
|
+
|
654
|
+
# Load the Masterview configuration settings.
|
655
|
+
# Does *not* load and configure the template engine.
|
656
|
+
#
|
657
|
+
# Intended for use in testing.
|
658
|
+
def initialize_configuration
|
659
|
+
#?? return if MasterView.const_defined?(:ConfigSettings) ??
|
660
|
+
load_config_settings
|
661
|
+
ensure_valid_settings
|
662
|
+
discover_standard_directive_path_additions
|
663
|
+
install_config_settings
|
664
|
+
# make a final check for running_rails? (in case config settings changed scripts spec)
|
665
|
+
configuration.decide_if_running_rails
|
666
|
+
# keep a permananent record of how we got started
|
667
|
+
configuration.freeze
|
668
|
+
configuration.directive_paths.freeze
|
669
|
+
MasterView.const_set('ConfigSettings', configuration)
|
670
|
+
end
|
671
|
+
|
672
|
+
# Load configuration settings from <tt>{config.config_dir_path}/settings.rb</tt>
|
673
|
+
# and <tt>{config.config_dir_path}/environments/{config.environment}.rb</tt>.
|
674
|
+
def load_config_settings #:nodoc:
|
675
|
+
load_app_settings
|
676
|
+
load_environment_settings
|
677
|
+
end
|
678
|
+
|
679
|
+
# Loads application config settings.
|
680
|
+
def load_app_settings #:nodoc:
|
681
|
+
load_settings_file(configuration.app_settings_path)
|
682
|
+
end
|
683
|
+
|
684
|
+
# Loads config settings for the environment specified by
|
685
|
+
# Configuration#environment_path, which
|
686
|
+
# is typically one of development, testing, or production.
|
687
|
+
def load_environment_settings #:nodoc:
|
688
|
+
load_settings_file(configuration.environment_settings_path)
|
689
|
+
end
|
690
|
+
|
691
|
+
# Loads MasterView config settings from the specified config file.
|
692
|
+
def load_settings_file(config_file_path) #:nodoc:
|
693
|
+
return if not (config_file_path && File.file?(config_file_path))
|
694
|
+
config = configuration # define config var in the binding context of the settings eval
|
695
|
+
eval(IO.read(config_file_path), binding)
|
696
|
+
end
|
697
|
+
|
698
|
+
def discover_standard_directive_path_additions #:nodoc:
|
699
|
+
#TODO: check for config/masterview/directives and automatically
|
700
|
+
# append to the configuration.directive_paths if found.
|
701
|
+
# We have to wait until all the config settings are loaded so that
|
702
|
+
# all references to root locations and client's own prefs for
|
703
|
+
# locating directives.
|
704
|
+
# [DJL 01-Jun-2006]
|
705
|
+
#... something like:
|
706
|
+
#return if not config_dir_path
|
707
|
+
#config_directives_path = File.join(configuration.config_dir_path, 'directives')
|
708
|
+
#if File.directory?(config_directives_path)
|
709
|
+
# configuration.directivePaths << File.expand_path(config_directives_path unless it's already there
|
710
|
+
#end
|
711
|
+
end
|
712
|
+
|
713
|
+
# ensure that the requested configuration settings are consistent and valid
|
714
|
+
# before we actually install anything. Normalize representations as needed.
|
715
|
+
def ensure_valid_settings #:nodoc:
|
716
|
+
|
717
|
+
config = configuration
|
718
|
+
|
719
|
+
#??config.root_path = File.expand_path(config.root_path) if config.root_path #?? ensure bolted down firmly?
|
720
|
+
|
721
|
+
|
722
|
+
####if config.directive_paths.length > 1 then expand_path on user-appended entries??
|
723
|
+
|
724
|
+
# template source and generation options
|
725
|
+
if config.on_rails?
|
726
|
+
#TODO: ensure that the config.template_dst_dir_path is
|
727
|
+
# in the RAILS_ROOT/app/views directory
|
728
|
+
# (ActionController::Base.view_root)
|
729
|
+
# Otherwise all the fine rails view template stuff doesn't work
|
730
|
+
end
|
731
|
+
|
732
|
+
# Rails application options
|
733
|
+
if config.on_rails?
|
734
|
+
# ensure we don't activate runtime reparsing if we didn't autoparse at startup
|
735
|
+
# (?overzealous? But this what we're currently asserting as the intended behavior in the docs)
|
736
|
+
if not config.parse_masterview_templates_at_startup
|
737
|
+
config.reparse_changed_masterview_templates = false
|
738
|
+
end
|
739
|
+
#else
|
740
|
+
# self.reparse_changed_masterview_templates = false # not supported
|
741
|
+
end
|
742
|
+
|
743
|
+
end
|
744
|
+
|
745
|
+
# Install the configuration settings
|
746
|
+
def install_config_settings #:nodoc:
|
747
|
+
set_module_constants
|
748
|
+
end
|
749
|
+
|
750
|
+
def set_module_constants #:nodoc:
|
751
|
+
|
752
|
+
config = configuration
|
753
|
+
|
754
|
+
# we don't record root_path or config_dir_path - their purpose is satisfied
|
755
|
+
# by the time we're done processing this installation configuration
|
756
|
+
|
757
|
+
MasterView.const_set('DefaultDirectiveLoadPaths', config.directive_paths.clone) # clone before freezing
|
758
|
+
|
759
|
+
# template source options
|
760
|
+
MasterView.const_set('TemplateFilenamePattern', config.template_filename_pattern)
|
761
|
+
|
762
|
+
# template generation options
|
763
|
+
MasterView.const_set('OutputExtension', config.output_filename_extension) ###??IS THIS NEEDED???
|
764
|
+
MasterView.const_set('OmitGeneratedComments', (not config.include_generated_file_comment))
|
765
|
+
MasterView.const_set('GeneratedCommentText', config.generated_file_comment)
|
766
|
+
|
767
|
+
# template parsing options
|
768
|
+
MasterView.const_set('RescueExceptions', config.handle_parse_exceptions)
|
769
|
+
MasterView.const_set('DefaultParserOptions', config.default_parser_options)
|
770
|
+
MasterView.const_set('TidyPath', config.tidy_path)
|
771
|
+
MasterView.const_set('NamespacePrefix', config.namespace_prefix)
|
772
|
+
MasterView.const_set('InlineErbStart', config.inline_erb_start)
|
773
|
+
MasterView.const_set('InlineErbEnd', config.inline_erb_end)
|
774
|
+
MasterView.const_set('InlineErbSubstitutionRegex', config.inline_erb_substitution_regex)
|
775
|
+
|
776
|
+
# Rails application options
|
777
|
+
MasterView.const_set('ParseMasterViewTemplatesAtStartup', config.parse_masterview_templates_at_startup)
|
778
|
+
MasterView.const_set('ReparseChangedMasterViewTemplates', config.reparse_changed_masterview_templates)
|
779
|
+
MasterView.const_set('EnableMasterViewAdminPages', config.enable_admin_pages)
|
780
|
+
MasterView.const_set('EnableMasterViewAdminViewRHTML', config.enable_view_rhtml)
|
781
|
+
|
782
|
+
# convenience constants for MV attributes involving output generation and imports
|
783
|
+
MasterView.const_set('GenerateAttribute', config.namespace_prefix + 'generate')
|
784
|
+
MasterView.const_set('ImportAttribute', config.namespace_prefix + 'import')
|
785
|
+
MasterView.const_set('GenRenderAttribute', config.namespace_prefix + 'gen_partial')
|
786
|
+
MasterView.const_set('ImportRenderAttribute', config.namespace_prefix + 'import_render')
|
787
|
+
|
788
|
+
end
|
789
|
+
|
790
|
+
# Load the masterview code
|
791
|
+
def load_plugin #:nodoc:
|
792
|
+
require 'masterview' #:nodoc:
|
793
|
+
end
|
794
|
+
|
795
|
+
# Complete installation of masterview after its code has been loaded
|
796
|
+
def complete_plugin_installation #:nodoc:
|
797
|
+
#?? return if MasterView.const_defined?(:Initialized) ??
|
798
|
+
initialize_mio
|
799
|
+
initialize_logger
|
800
|
+
install_in_rails
|
801
|
+
# mark the module as fully loaded and configured
|
802
|
+
MasterView.const_set('Initialized', true)
|
803
|
+
after_initialize # callback to client's afer_initialize block
|
804
|
+
end
|
805
|
+
|
806
|
+
# Initialize the MasterView I/O subsystem
|
807
|
+
def initialize_mio
|
808
|
+
config = configuration
|
809
|
+
MasterView.const_set('DefaultSerializer', MIOSerializer)
|
810
|
+
# all root_path directory anchor points for I/O are expanded absolute paths
|
811
|
+
io_mgr = MIOTrees.new
|
812
|
+
template_extension = File.extname( config.template_filename_pattern )
|
813
|
+
io_mgr.template = FileMIOTree.new( config.template_src_dir_path, template_extension,
|
814
|
+
:escape_erb => config.default_parser_options[:escape_erb],
|
815
|
+
:tidy => config.default_parser_options[:tidy],
|
816
|
+
#TODO: expose the following in Configuration.default_parser_options and document
|
817
|
+
:caching => false,
|
818
|
+
:logging => true )
|
819
|
+
if config.generate_rhtml_files
|
820
|
+
io_mgr.erb = FileMIOTree.new( config.template_dst_dir_path, config.generated_file_default_extension, :logging => true)
|
821
|
+
else
|
822
|
+
io_mgr.erb = RailsErbCacheMIOTree.new( config.generated_file_default_extension, :logging => true)
|
823
|
+
end
|
824
|
+
io_mgr.backup = FileMIOTree.new( config.rebuild_backups_tmp_dir_path ) if config.rebuild_backups_tmp_dir_path
|
825
|
+
MasterView.const_set('IOMgr', io_mgr)
|
826
|
+
end
|
827
|
+
|
828
|
+
# Initialize MasterView::Log with a logger which emits to std output, default DEBUG level
|
829
|
+
def initialize_logger #:nodoc:
|
830
|
+
#?return if defined?(Log)
|
831
|
+
require 'masterview/extras/init_logger'
|
832
|
+
end
|
833
|
+
|
834
|
+
def install_in_rails #:nodoc:
|
835
|
+
return if ! configuration.on_rails?
|
836
|
+
enable_mv_admin_pages
|
837
|
+
parse_templates_at_startup
|
838
|
+
enable_reparse_changed_templates
|
839
|
+
enable_rails_erb_direct
|
840
|
+
end
|
841
|
+
|
842
|
+
# install the MasterviewController to support masterview admin pages in the site
|
843
|
+
def enable_mv_admin_pages #:nodoc:
|
844
|
+
return if ! configuration.enable_admin_pages #MasterView::EnableMasterViewAdminPages
|
845
|
+
MasterView::Log.info{ 'MasterView Admin pages enabled' }
|
846
|
+
|
847
|
+
# verify that stylesheets we use are available and if not copy them to public/stylesheets
|
848
|
+
mv_generator_templates_dir = "#{configuration.mv_installation_dir}/generators/masterview/templates"
|
849
|
+
unless File.exist?(mv_generator_templates_dir)
|
850
|
+
# we are in a gem so things are in different directories
|
851
|
+
MasterView::Log.debug{ 'MasterView appears to be installed as a gem...' }
|
852
|
+
mv_generator_dir = configuration.mv_installation_dir.gsub('\\','/').gsub( %r{/masterview-([^/]+)$}, '/masterview_generator-\1' )
|
853
|
+
mv_generator_templates_dir = "#{mv_generator_dir}/templates"
|
854
|
+
end
|
855
|
+
MasterView::Log.debug{ 'MasterView gem admin stylesheet src dir='+mv_generator_templates_dir }
|
856
|
+
|
857
|
+
if File.directory?(mv_generator_templates_dir)
|
858
|
+
rails_app_stylesheets_dir = Pathname.for_path(RAILS_ROOT) + 'public/stylesheets/masterview'
|
859
|
+
stylesheet_specs = [
|
860
|
+
# from/to spec: [ <src filename in templates dir>, <target filename in app stylesheets dir> ]
|
861
|
+
[ 'style.css', 'style.css' ],
|
862
|
+
[ 'sidebox.css', 'sidebox.css' ],
|
863
|
+
[ 'color-scheme.css', 'color-scheme.css' ]
|
864
|
+
]
|
865
|
+
src_dir_accessor = MasterView::FileMIOTree.new( mv_generator_templates_dir )
|
866
|
+
dst_dir_accessor = MasterView::FileMIOTree.new( rails_app_stylesheets_dir, '.css', :logging => true)
|
867
|
+
stylesheet_specs.each { | from, to |
|
868
|
+
src_file = src_dir_accessor.path(from)
|
869
|
+
dst_file = dst_dir_accessor.path(to)
|
870
|
+
dst_file.write( src_file.read ) unless dst_file.exist?
|
871
|
+
}
|
872
|
+
end
|
873
|
+
# add our app directories with the masterview_controller to the load path
|
874
|
+
mv_controller_code_dirs = Dir[File.join(configuration.mv_code_base_dir, '/extras/app/**/*')].select { |dir|
|
875
|
+
File.directory?(dir) }
|
876
|
+
mv_controller_code_dirs.each { |dir| $LOAD_PATH.push dir }
|
877
|
+
end
|
878
|
+
|
879
|
+
def parse_templates_at_startup #:nodoc:
|
880
|
+
if configuration.parse_masterview_templates_at_startup
|
881
|
+
require 'masterview/extras/watcher'
|
882
|
+
MasterView::Log.debug { 'Parsing MasterView templates...' }
|
883
|
+
MasterView::IOMgr.template.find(:pattern => MasterView::TemplateFilenamePattern) do |mio|
|
884
|
+
MasterView::Parser.parse_mio(mio, MasterView::IOMgr.erb)
|
885
|
+
end
|
886
|
+
end
|
887
|
+
end
|
888
|
+
|
889
|
+
def enable_reparse_changed_templates #:nodoc:
|
890
|
+
if configuration.reparse_changed_masterview_templates #MasterView::ReparseChangedMasterViewTemplates
|
891
|
+
# if not caching then check for masterview updates on every request
|
892
|
+
# assert not ActionController::Base.perform_caching
|
893
|
+
require 'masterview/extras/watcher' #:nodoc:
|
894
|
+
require 'masterview/extras/init_rails_reparse_checking'
|
895
|
+
end
|
896
|
+
end
|
897
|
+
|
898
|
+
def enable_rails_erb_direct #:nodoc:
|
899
|
+
unless configuration.generate_rhtml_files
|
900
|
+
# if not generating rhtml the read erb directly from masterview
|
901
|
+
require 'masterview/extras/init_rails_erb_mv_direct'
|
902
|
+
end
|
903
|
+
end
|
904
|
+
|
905
|
+
# Fires the user-supplied after_initialize block (Configuration#after_initialize)
|
906
|
+
def after_initialize #:nodoc:
|
907
|
+
configuration.after_initialize_block.call if configuration.after_initialize_block
|
908
|
+
end
|
909
|
+
|
910
|
+
end
|
911
|
+
|
912
|
+
end
|