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.
Files changed (105) hide show
  1. data/CHANGELOG +17 -0
  2. data/README +36 -504
  3. data/RELEASE_NOTES +126 -45
  4. data/Rakefile +215 -86
  5. data/TODO +8 -3
  6. data/doc/configuration.html +485 -0
  7. data/doc/directives.html +1085 -0
  8. data/doc/guide.html +243 -0
  9. data/doc/index.html +287 -0
  10. data/doc/installation.html +376 -0
  11. data/doc/stylesheets/masterview.css +206 -0
  12. data/doc/stylesheets/mv-config.css +23 -0
  13. data/doc/stylesheets/mv-directives.css +18 -0
  14. data/doc/stylesheets/mv-installation.css +10 -0
  15. data/doc/troubleshooting.html +18 -0
  16. data/examples/product.html +256 -0
  17. data/examples/product.html.old +107 -0
  18. data/examples/rails_app_config/masterview/environment/development.rb +22 -0
  19. data/examples/rails_app_config/masterview/environment/production.rb +9 -0
  20. data/examples/rails_app_config/masterview/settings.rb +59 -0
  21. data/examples/test.import +80 -0
  22. data/init.rb +26 -12
  23. data/lib/masterview/analyzer.rb +25 -15
  24. data/lib/masterview/directive_base.rb +4 -0
  25. data/lib/masterview/directive_helpers.rb +7 -5
  26. data/lib/masterview/directives/import_render.rb +6 -0
  27. data/lib/masterview/directives/insert_generated_comment.rb +8 -8
  28. data/lib/masterview/extras/app/controllers/masterview_controller.rb +154 -2
  29. data/lib/masterview/extras/app/views/masterview/admin/create.rhtml +4 -4
  30. data/lib/masterview/extras/app/views/masterview/admin/empty.rhtml +1 -1
  31. data/lib/masterview/extras/app/views/masterview/admin/list.rhtml +14 -9
  32. data/lib/masterview/extras/app/views/masterview/admin/view_rhtml.rhtml +70 -0
  33. data/lib/masterview/extras/init_logger.rb +102 -0
  34. data/lib/masterview/extras/init_rails_erb_mv_direct.rb +117 -0
  35. data/lib/masterview/extras/init_rails_reparse_checking.rb +59 -0
  36. data/lib/masterview/extras/watcher.rb +17 -23
  37. data/lib/masterview/filter_helpers.rb +26 -0
  38. data/lib/masterview/initializer.rb +912 -0
  39. data/lib/masterview/io.rb +352 -0
  40. data/lib/masterview/keyword_expander.rb +116 -0
  41. data/lib/masterview/masterview_version.rb +2 -2
  42. data/lib/masterview/mtime_tracking_hash.rb +44 -0
  43. data/lib/masterview/parser.rb +64 -92
  44. data/lib/masterview/pathname_extensions.rb +33 -0
  45. data/lib/masterview/template_spec.rb +49 -56
  46. data/lib/masterview.rb +40 -85
  47. data/test/fixtures/configs/fake_rails_app_with_config/config/masterview/environments/development.rb +12 -0
  48. data/test/fixtures/configs/fake_rails_app_with_config/config/masterview/environments/production.rb +11 -0
  49. data/test/fixtures/configs/fake_rails_app_with_config/config/masterview/settings.rb +23 -0
  50. data/test/fixtures/templates/product.html +256 -0
  51. data/test/fixtures/templates/test.import +80 -0
  52. data/test/test_helper.rb +5 -3
  53. data/test/tmp/template/foo.txt +1 -0
  54. data/test/tmp/templates_src/product.html +256 -0
  55. data/test/tmp/views/layouts/product.rhtml +35 -0
  56. data/test/tmp/views/product/_form.rhtml +30 -0
  57. data/test/tmp/views/product/_product.rhtml +14 -0
  58. data/test/tmp/views/product/_show.rhtml +27 -0
  59. data/test/tmp/views/product/destroy.rhtml +27 -0
  60. data/test/tmp/views/product/edit.rhtml +26 -0
  61. data/test/tmp/views/product/list.rhtml +31 -0
  62. data/test/tmp/views/product/new.rhtml +29 -0
  63. data/test/tmp/views/product/show.rhtml +16 -0
  64. data/test/unit/config_settings_test.rb +172 -0
  65. data/test/{attr_test.rb → unit/directive_attr_test.rb} +2 -2
  66. data/test/{block_test.rb → unit/directive_block_test.rb} +2 -2
  67. data/test/{content_test.rb → unit/directive_content_test.rb} +2 -2
  68. data/test/{else_test.rb → unit/directive_else_test.rb} +2 -2
  69. data/test/{elsif_test.rb → unit/directive_elsif_test.rb} +2 -2
  70. data/test/{form_test.rb → unit/directive_form_test.rb} +2 -2
  71. data/test/{global_inline_erb_test.rb → unit/directive_global_inline_erb_test.rb} +2 -2
  72. data/test/{hidden_field_test.rb → unit/directive_hidden_field_test.rb} +2 -2
  73. data/test/{if_test.rb → unit/directive_if_test.rb} +2 -2
  74. data/test/unit/directive_import_render_test.rb +62 -0
  75. data/test/{import_test.rb → unit/directive_import_test.rb} +2 -2
  76. data/test/{javascript_include_test.rb → unit/directive_javascript_include_test.rb} +2 -2
  77. data/test/{link_to_if_test.rb → unit/directive_link_to_if_test.rb} +2 -2
  78. data/test/{link_to_test.rb → unit/directive_link_to_test.rb} +2 -2
  79. data/test/{omit_tag_test.rb → unit/directive_omit_tag_test.rb} +2 -2
  80. data/test/{password_field_test.rb → unit/directive_password_field_test.rb} +2 -2
  81. data/test/{replace_test.rb → unit/directive_replace_test.rb} +2 -2
  82. data/test/{stylesheet_link_test.rb → unit/directive_stylesheet_link_test.rb} +2 -2
  83. data/test/{submit_test.rb → unit/directive_submit_test.rb} +2 -2
  84. data/test/{text_area_test.rb → unit/directive_text_area_test.rb} +2 -2
  85. data/test/{text_field_test.rb → unit/directive_text_field_test.rb} +2 -2
  86. data/test/{example_test.rb → unit/example_test.rb} +1 -1
  87. data/test/unit/file_mio_test.rb +368 -0
  88. data/test/{filter_helpers_test.rb → unit/filter_helpers_test.rb} +1 -1
  89. data/test/unit/keyword_expander_test.rb +95 -0
  90. data/test/unit/mio_test.rb +110 -0
  91. data/test/unit/mtime_string_hash_mio_tree_test.rb +289 -0
  92. data/test/unit/mtime_tracking_hash_test.rb +38 -0
  93. data/test/{parser_test.rb → unit/parser_test.rb} +19 -1
  94. data/test/unit/pathname_extensions_test.rb +46 -0
  95. data/test/{run_parser_test.rb → unit/run_parser_test.rb} +7 -3
  96. data/test/unit/string_hash_mio_test.rb +320 -0
  97. data/test/unit/template_file_watcher_test.rb +107 -0
  98. data/test/{template_spec_test.rb → unit/template_spec_test.rb} +57 -21
  99. data/test/{template_test.rb → unit/template_test.rb} +123 -22
  100. data/test/xtras/config-mv-logger_config.rb +109 -0
  101. data/test/xtras/config_initialize_standalone.rb +53 -0
  102. metadata +111 -38
  103. data/lib/masterview/extras/rails_init.rb +0 -72
  104. data/test/import_render_test.rb +0 -30
  105. data/test/template_file_watcher_test.rb +0 -50
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright (c) 2006 Jeff Barczewski
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #++
25
+ #
26
+ # Setup hook to intercept template_exist? calls and read_template_file
27
+ # so that rails can find masterview templates. MasterView cannot use the
28
+ # provided hooks because rails always assumes everything is file based
29
+ # and that there will be a file for each thing to render. If the
30
+ # template dispatching code is enriched in the future we may be able
31
+ # to rewrite this code to use the provided hooks
32
+ #
33
+ #
34
+
35
+ MasterView::Log.info { 'Adding hooks to enable Rails to read erb directly from MasterView' }
36
+
37
+ module ActionView #:nodoc:
38
+ class Base #:nodoc:
39
+
40
+ # checks for template existence, uses standard rails file based check first and if not found
41
+ # check MasterView paths because might not exist on file system
42
+ alias :template_exists_pre_mv? :template_exists? #:nodoc:
43
+ def template_exists?(template_path, extension) #:nodoc:
44
+ short_path = template_path+'.'+extension.to_s
45
+ template_exists_pre_mv?(template_path, extension) || MasterView::IOMgr.erb.path(short_path).exist?
46
+ end
47
+
48
+ # checks for the existence of the template in MasterView first before using the
49
+ # rails file system based read_template_file
50
+ alias :read_template_file_pre_mv :read_template_file #:nodoc:
51
+ def read_template_file(template_path, extension) #:nodoc:
52
+ short_path_mio = MasterView::IOMgr.erb.path(short_relative_path_mv(template_path))
53
+ if short_path_mio.exist?
54
+ short_path_mio.read
55
+ else # use original rails read_template_file
56
+ read_template_file_pre_mv(template_path, extension)
57
+ end
58
+ end
59
+
60
+ # Check whether compilation is necessary.
61
+ # Since rails compile_templat? is file based there was no easy way to hook into
62
+ # this, so much of this method had to be duplicated.
63
+ # If file_name exists in MasterView then check mtime from it otherwise defer to
64
+ # original code.
65
+ alias :compile_template_pre_mv? :compile_template? #:nodoc:
66
+ def compile_template?(template, file_name, local_assigns) #:nodoc:
67
+ short_path_mio = (file_name && !file_name.empty?) ? MasterView::IOMgr.erb.path(short_relative_path_mv(file_name)) : nil
68
+ if short_path_mio && short_path_mio.exist? # mio template
69
+ method_key = file_name || template
70
+ render_symbol = @@method_names[method_key]
71
+
72
+ if @@compile_time[render_symbol] && supports_local_assigns?(render_symbol, local_assigns)
73
+ if file_name && !@@cache_template_loading
74
+ need_to_compile = (@@compile_time[render_symbol] < short_path_mio.mtime) # use mio.mtime instead of File.mtime
75
+ MasterView::Log.debug{ 'compile_template? template_short_path = '+short_path_mio.pathname.to_s+' compile?='+need_to_compile.to_s }
76
+ need_to_compile
77
+ end
78
+ else
79
+ true
80
+ end
81
+ else # use original file based mtime checking
82
+ compile_template_pre_mv?(template, file_name, local_assigns)
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ # returns the short path relative to view_base since MasterView uses relative paths exclusively
89
+ def short_relative_path_mv(template_path) #:nodoc:
90
+ (template_path.starts_with?(self.base_path)) ? template_path[self.base_path.length+1..-1] : template_path
91
+ end
92
+
93
+ end
94
+ end
95
+
96
+
97
+
98
+ module ActionController #:nodoc:
99
+ module Layout #:nodoc:
100
+ module ClassMethods
101
+
102
+ # get list of layout template paths, original rails implementation assumes everything is file based
103
+ # so this method is enhanced to pull layouts out of MasterView and concatenate them to the list
104
+ # from rails
105
+ alias :layout_list_pre_mv :layout_list
106
+ def layout_list
107
+ mv_layouts = []
108
+ MasterView::IOMgr.erb.find do |mio|
109
+ mv_layouts << "#{template_root}/"+mio.pathname.to_s if mio.pathname.to_s.starts_with?('layouts/')
110
+ end
111
+ layout_list_pre_mv.concat( mv_layouts )
112
+ end
113
+
114
+ end
115
+ end
116
+ end
117
+
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright (c) 2006 Jeff Barczewski
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #++
25
+ #
26
+ # Setup hook to insert MasterView changed-template reparsing
27
+ # into the Rails request dispatching handler in ActionController::Base.
28
+ #
29
+ # Ensures that masterview output files are always current while running a
30
+ # Rails application. Should generally be enabled/disabled in conjunction
31
+ # with Rails class load caching, which is normally off in a development
32
+ # environment configuration. Requires that masterview templates be
33
+ # parsed at application startup to properly initialize the mechanism
34
+ # to detect template changes after application startup.
35
+ #
36
+
37
+ #--
38
+ # DBC-style notation per DbC - rubydbc-0.1 (Andy Hunt's Design by Contract)
39
+ #pre( MasterView::ParseMasterViewTemplatesAtStartup )
40
+ #pre( MasterView::ReparseChangedMasterViewTemplates )
41
+ #pre( defined?(ActionController) && ActionController::Base.perform_caching )
42
+ #++
43
+ #
44
+
45
+ MasterView::Log.info { 'Adding hook to allow MasterView to check for templates that have changed when processing a request' }
46
+
47
+ module ActionController #:nodoc:
48
+ class Base #:nodoc
49
+ # Install hook to enable MasterView to detect and automatically reparse
50
+ # changed templates during Rails request dispatching.
51
+ alias :process_pre_mv :process #:nodoc:
52
+ def process(request, response, method = :perform_action, *arguments) #:nodoc:
53
+ MasterView::TemplateWatcher.check_updated(MasterView::IOMgr.template, MasterView::TemplateFilenamePattern ) do |mio|
54
+ MasterView::Parser.parse_mio( mio, MasterView::IOMgr.erb )
55
+ end
56
+ process_pre_mv(request, response, method, *arguments)
57
+ end
58
+ end
59
+ end
@@ -1,31 +1,25 @@
1
- require 'find'
2
-
3
1
  module MasterView
4
- class TemplateFileWatcher
5
- # Check path for filenames matching filename_pattern and if found
6
- # exec block passing in the full path name.
7
- # filename_pattern is a wildcard pattern similar to those used in shells
8
- # If check_subdirectories is true then the system will recurse into
9
- # subdirectories looking for filename matches as well.
10
- # returns an array of files that were modified and the block was run for
11
- def self.check(path, filename_pattern, check_subdirectories, &block)
12
- files_run = []
13
- return files_run unless File.exist?(path)
2
+ class TemplateWatcher
14
3
 
15
- full_path = File.expand_path(path)
16
- Find.find(full_path) do |f|
17
- if !File.directory?(f) && File.fnmatch?(filename_pattern, f)
18
- mtime = File.mtime(f)
19
- @@file_mtimes ||= {}
20
- unless @@file_mtimes[f] == mtime
21
- yield f
22
- @@file_mtimes[f] = File.mtime(f)
23
- files_run << f
24
- end
4
+ # check all the templates in the MIOTree to see if their mtimes
5
+ # is newer than what we have saved in @@template_mtimes.
6
+ # If newer, then run block and add path to array to be returned
7
+ # If filename_pattern is specified then it will filter the results
8
+ # returns array of paths that were newer
9
+ def self.check_updated(template_mio_tree, filename_pattern, &block)
10
+ template_paths_run = []
11
+ template_mio_tree.find(:pattern => filename_pattern) do |mio|
12
+ mtime = mio.mtime
13
+ @@template_mtimes ||= {}
14
+ unless @@template_mtimes[mio.pathname.to_s] == mtime
15
+ yield mio
16
+ @@template_mtimes[mio.pathname.to_s] = mtime
17
+ template_paths_run << mio.pathname.to_s
25
18
  end
26
19
  end
27
- files_run
20
+ template_paths_run
28
21
  end
22
+
29
23
  end
30
24
  end
31
25
 
@@ -0,0 +1,26 @@
1
+ module MasterView
2
+ module TidyHelper
3
+ def self.tidy(html)
4
+ Tidy.path = ::MasterView::TidyPath unless Tidy.path
5
+ xml = Tidy.open do |tidy|
6
+ tidy.options.output_xml = true
7
+ tidy.options.indent = true
8
+ tidy.options.wrap = 0
9
+ xml = tidy.clean(html)
10
+ end
11
+ xml = ::MasterView::EscapeErbHelper.escape_erb(xml)
12
+ ::MasterView::Log.debug { 'tidy corrected xml='+xml }
13
+ xml
14
+ end
15
+
16
+
17
+ end
18
+
19
+ module EscapeErbHelper
20
+ def self.escape_erb(html)
21
+ html = html.gsub(/<%/, InlineErbStart)
22
+ html.gsub!(/%>/, InlineErbEnd)
23
+ html
24
+ end
25
+ end
26
+ end