module MasterView # Mixin services for directive implementation classes. # # Subclasses of MasterView::DirectiveBase inherit this mixin. # module DirectiveHelpers CRLF = "\r\n" # start of ERB which is evaluated but does not contribute content to the document ERB_EVAL_START = '<% ' # end of ERB which is evaluated but does not contribute content to the document ERB_EVAL_END = ' -%>' # start of ERB whose evaluation results in content in the document ERB_CONTENT_START = '<%= ' # end of ERB whose evaluation results in content in the document ERB_CONTENT_END = ' %>' # convert render_partial_name to file_name, ex foo/bar to foo/_bar.rhtml def render_partial_name_to_file_name(render_partial_name, default_extension) pathname = Pathname.for_path(render_partial_name) dir_pathname = pathname.dirname base = pathname.basename(pathname.extname).to_s filename = '_'+base filename += default_extension if default_extension path = (dir_pathname+filename).to_s end # find the last string that fully matches exactly the # parent tags content string array # It looks for something that has been output as a unit in # the array not a substring # returns the ref to the string which you can operate on # using replace def find_last_in_parent(tag, full_string) ret = nil parent = tag.parent unless parent.nil? parent.content.reverse.each do |str| if str.kind_of? Array # if it is a nested array check inside of it str.reverse.each do |s| if s == full_string ret = s break end end break if ret else if str == full_string ret = str break end end end end ret end # set the last occurence to empty string # returns true if found and set to empty string def delete_last_in_parent(tag, full_string) str = find_last_in_parent(tag, full_string) found = !str.nil? str.replace('') if found found end # find a hash value from inside a simple str containing a hash # non-evaling, looks for a :key => 'foo/bar' returning foo/bar string def find_string_val_in_string_hash(str, key_or_sym) key = key_or_sym.to_s m = str.match( Regexp.new( Regexp.escape(key)+"\s*=>\s*'([^']*)'" ) ) #try single quote m = str.match( Regexp.new( Regexp.escape(key)+"\s*=>\s*\"([^\"]*)\"" ) ) if m.nil? #try double quote return nil if m.nil? m[1] end #parse into array of strings, containing the various arguments without evaling #looks for %q{}, %q[], hash, array, function call using (), values deliminated by commas, def parse(str) AttrStringParser.parse(str) end #add single quotes around string def quote(str, quote_char='\'') quote_char+str+quote_char end # adds single quotes around string if it is a simple # word [a-zA-Z0-9_]* otherwise return existing string # also quote if empty string def quote_if(str) (str =~ /^\w*$/) ? quote(str) : str end end end