[TxMt] [ANN] Select Balanced HTML Tag!!!1!

Thomas Aylott - subtleGradient textmate at subtleGradient.com
Fri Nov 16 12:29:58 UTC 2007


This runs into the problem I'd been having for 3 years.
How do you get it to work when you have a tag nested inside the same  
kind of tag?
Keeping it from matching the first close tag it finds, or the very  
last one.

<div>
	<div>
		<div>
			TEXT
		</div>
	</div>
</div>

I've tried some rather complex Regex, but it just turns out that  
oniguruma isn't capable of doing if tests in regex. That might be a  
solution to the problem, but I forget the specifics of how that might  
work now.

This is one of the paths I'd tried a while ago:

(?xm)
<(?<tag>\w+)  # match the initial opening parenthesis

# Now make a named group 'balanced' which matches
# a balanced substring.
(?<balanced>

	# Look for a single character that isn't the start of an html tag
	[^<]*(?=<)
	
	| # Then look for everything until something that could be and ending  
tag or nested tag
	.*?(?=</?\k<tag>)
	
	| # or is a full this tag with opening and end:
	<\k<tag> # A parenthesised string begins with an opening this tag
		\g<balanced>* # followed by a sequence of balanced substrings
	</\k<tag>> # and ends with a closing this tag

)*  # Look for a sequence of balanced substrings

</\k<tag>>  # Finally, the outer closing parenthesis


I think the problem with this was that it was crashing TextMate. I  
don't remember now.

—Thomas Aylott – subtleGradient—

On Nov 16, 2007, at 6:38 AM, Hans-Jörg Bibiko wrote:

> On 16.11.2007, at 12:23, Thomas Aylott - subtleGradient wrote:
>> Thanks!
>>
>> Yeah, I've already come across most of this stuff too.
>> I just finally found something that half-worked and wanted to get  
>> it out in the world before I got bored or too busy to finish it.
>>
>> Some sort of native functionality is what I would prefer.
>> 	It's almost shameful that TextMate doesn't offer this natively.
>>
>> For the updated version, I'll probly have it escape all brackets in  
>> the doc first, and then do another pass to put back only those  
>> brackets that are most likely to give the best balancing results.  
>> And then do all the business with selections and whatever. That  
>> should make it much more robust and capable of handling rhtml and  
>> embedded source and stuff.
>>
>> The current version is only really useful on REALLY clean pages,  
>> and then only sometimes ;)
>>
>> I'd also like to build up some nice testcases to check against  
>> anytime I make any changes.
>> 	I bet I could use TMTOOLS to automate testing since it can move  
>> the caret around and run macros.
> I attached a very FIRST approach using TMTOOLS for that.
>
> Mainly I took the ruby script 'Insert close Tag' := $TAG
> [It has some tiny hooks but at the first glance it works quite well]
>
> Then I created a SIMPLE regexp:
> REGEXP="(?m)<$TAG\b.*?>\\/$TAG>"
> [One has to enhance it later!!]
>
> Then I took that regexp for two macro:
> -findNext regexp
> -findPrevious regexp
>
> Maybe you find some hints in it ;)
>
> Of course, that command has no 'go backwards' function.
>
>
> Cheers,
>
> --Hans
>
> <Select Balanced XHTML Tag FIRST APPROACH.tmCommand>
> ______________________________________________________________________
> For new threads USE THIS: textmate at lists.macromates.com
> (threading gets destroyed and the universe will collapse if you don't)
> http://lists.macromates.com/mailman/listinfo/textmate




More information about the textmate mailing list