[SVN] Revision 1056

Chris Thomas chris at cjack.com
Mon May 23 17:24:03 UTC 2005


On May 23, 2005, at 9:15 AM, Sune Foldager wrote:

> Also made beginTag/endTag (previously tagB/tagE) maintain a tag- 
> stack so you don't need to provide the tag name
> to the endTag command. Comments are more than welcome!

In implementing the Build With Xcode command (which, tangentially,  
should probably not be in the Objective-C bundle; it's not language- 
specific), I discovered that it was useful to be able to close a  
sequence of tags based on criteria for finding the closing tag.

A generalized version might look something like this (untested) Ruby  
code:

     def start_tag( tag, attributes )
         print "<#{tag[0]}"
         attributes.each_pair {|name, value| print " #{name}=\"# 
{value}\" "}
         print ">"
         @tag_stack.push [tag, attributes]
     end

     # if find_tag and attributes are both nil, close the innermost tag
     # else, close all tags between innermost tag and the first tag  
matching find_tag and/or attributes, inclusive
     def end_tag( find_tag = nil, attributes = nil )

         if find_tag.nil? and attributes.nil?
             puts "</#{@tag_stack.pop[0]}>"
         else
             until (@tag_stack.size == 0)
                 tag = @tag_stack.pop
                 puts "</#{tag[0]}>"

                 case
                     when (find_tag != nil) and (attributes != nil)
                         break if (tag[0] == find_tag && tag[1] ==  
attributes)
                     when attributes != nil
                         break if (tag[1] == attributes)
                     else
                         break if (tag[0] == find_tag)
                 end
             end
         end
     end

With this, you could do:

     start_tag "div", :id => "enclosingHappyBox"
     #... much other output based on command output ...
     end_tag nil, :id => "enclosingHappyBox"

Chris



More information about the textmate-dev mailing list