lib/masterview/analyzer.rb in masterview-0.3.3 vs lib/masterview/analyzer.rb in masterview-0.3.4

- old
+ new

@@ -8,22 +8,47 @@ ExpandAlwaysElements = %w{ script textarea } module Common + ImportToNonImportDir = { + "import_render" => "gen_partial", + "import" => "generate", + } + + NonImportToImportDir = ImportToNonImportDir.invert + def calc_hash(data) #MasterView::Log.warn { 'hash= '+data.hash.to_s+' data='+data } data.gsub( /\s+/, ' ' ).hash #collapse whitespace end def src_hash(data_orig) - mv_ns = DirectiveRegistry.current.mv_namespace_prefix - data = data_orig.gsub mv_ns+'import_render', mv_ns+'gen_partial' - data = data.gsub mv_ns+'import', mv_ns+'generate' + data = convert_import_to_non_import_dirs(data_orig) calc_hash(data) end + # gsub any import directives with non-import directives and return, makes clone before gsub + def convert_import_to_non_import_dirs(text) + text_clone = text.clone + mv_ns = DirectiveRegistry.current.mv_namespace_prefix + ImportToNonImportDir.sort.reverse.each do |i,n| # reverse order to substitute longer items first + text_clone.gsub! mv_ns+i, mv_ns+n # mv:import_render to mv:gen_partial and mv:import to mv:generate + end + text_clone + end + + # gsub any non-import directives with import directives and return, makes clone before gsub + def convert_non_import_to_import_dirs(text) + text_clone = text.clone + mv_ns = DirectiveRegistry.current.mv_namespace_prefix + NonImportToImportDir.sort.reverse.each do |i,n| # reverse order to substitute longer items first + text_clone.gsub! mv_ns+i, mv_ns+n # mv:gen_partial to mv:import_render and mv:generate to mv:import + end + text_clone + end + end class ContentEntry include Common attr_accessor :hash, :data @@ -34,32 +59,34 @@ end end class StackEntry - attr_accessor :name, :buffer, :depth, :import, :parts + attr_accessor :name, :buffer, :depth, :import, :parts, :page_attributes - def initialize(name, depth, import) + def initialize(name, depth, import, page_attributes) @name = name @depth = depth @import = import @buffer = [] @parts = 0 + @page_attributes = page_attributes # directives that are unique to page and ignored during synchronization end def inc_parts @parts += 1 end end class ListEntry - attr_accessor :name, :index, :import + attr_accessor :name, :index, :import, :page_attributes attr_writer :hash_invalid - def initialize(name, index, import) + def initialize(name, index, import, page_attributes) @name = name @index = index @import = import + @page_attributes = page_attributes end def hash_invalid? @hash_invalid end @@ -162,28 +189,31 @@ @depth += 1 # expand the target specification to the actual path for import/generate directives in case of rebuilding path = nil import = false + page_attributes = { } @mv_processing_directives.each_pair { | mv_attr_name, directive_type_flags | if attributes.has_key?( mv_attr_name ) expanded_attr_value = @keyword_expander.expand_keywords(attributes[mv_attr_name]) attributes[mv_attr_name] = expanded_attr_value if directive_type_flags[:partial] partial = find_string_val_in_string_hash( expanded_attr_value, :partial ) path = render_partial_name_to_file_name(partial, @default_extname) + page_attributes[partial] = expanded_attr_value # save all the page specific values of gen_partial and import_partial by path + attributes[mv_attr_name] = ":partial => '#{partial}'" # exclude others for comparison will be put back else path = expanded_attr_value end import = directive_type_flags[:import] break end } if path store_last_buffer false - @stack << StackEntry.new(path, @depth, import) + @stack << StackEntry.new(path, @depth, import, page_attributes) end unless @stack.empty? @stack.last.buffer << "<#{qname}" sorted_attributes = attributes.sort do |a,b| # sort import and import_render like generate and gen_partial so hashs work @@ -215,15 +245,15 @@ (@stack.last.buffer << '<![CDATA[' << content << ']]>') unless @stack.empty? end def end_element(uri, localname, qname) unless @stack.empty? - if @stack.last.buffer.last == '>' && !ExpandAlwaysElements.include?(qname) #simplify empty elements + if @stack.last.buffer.last == '>' && MasterView::TemplateProcessing::Renderer::XHTMLEmptyElementNameSet.include?(qname) #collapse @stack.last.buffer.pop @stack.last.buffer << '/>' else - @stack.last.buffer << '</' << "#{qname}>" #must output </ as separate string so simplify_empty_elements can find it + @stack.last.buffer << '</' << "#{qname}>" #must output </ as separate string so simplify_empty_elements can find it, though currently not using simplify_empty_elements in rebuilding only in parser for generation end end if @stack.last && @depth == @stack.last.depth store_last_buffer true @@ -244,36 +274,38 @@ end def store_last_buffer(end_element) unless @stack.empty? - @stack.last.inc_parts - index = end_element ? -1 : @stack.last.parts-1 - data = @stack.last.buffer.join + last_stack_entry = @stack.last + last_stack_entry.inc_parts + index = end_element ? -1 : last_stack_entry.parts-1 + data = last_stack_entry.buffer.join unless @prolog.empty? data = @prolog.join + data @prolog = [] end + hash_invalid = false; - if @stack.last.import + if last_stack_entry.import if only_check_hash? hash = src_hash(data) - bdata = @builder.data(@stack.last.name, index) + bdata = @builder.data(last_stack_entry.name, index) hash_invalid = src_hash(bdata) != hash if hash_invalid Log.debug { 'hash_invalid contents'} Log.debug { 'src='+bdata } Log.debug { 'imp='+data } end - #hash_invalid = @builder.hash(@stack.last.name, index) != hash + #hash_invalid = @builder.hash(last_stack_entry.name, index) != hash end elsif !only_check_hash? - @content[@stack.last.name] ||= [] - @content[@stack.last.name] << ContentEntry.new(data) + @content[last_stack_entry.name] ||= [] + @content[last_stack_entry.name] << ContentEntry.new(data) end - @stack.last.buffer = [] - @list << ListEntry.new(@stack.last.name, index, @stack.last.import) + last_stack_entry.buffer = [] + @list << ListEntry.new(last_stack_entry.name, index, last_stack_entry.import, last_stack_entry.page_attributes) if only_check_hash? #only set these flags if doing hash_check, not accurate for first round parse @list.last.hash_invalid = hash_invalid end end