From: William James Date: 2006-03-10T17:33:48+09:00 Subject: Re: Is there link extractor or similar html processing libs for Ruby James Edward Gray II wrote: > On Mar 7, 2006, at 7:48 PM, William James wrote: > > > class String > > def xtag(s) > > result = [] > > scan( %r! > > < #{s} (?: \s+ ( [^>]* ) )? / > > > | > > < #{s} (?: \s+ ( [^>]* ) )? > > > ( .*? ) > > !mix ) \ > > { |unpaired, attr, data| h = { } > > ( unpaired || attr || "" ). > > scan( %r{ ( \w+ ) \s* = \s* > > (?: ( ["'] ) ( .*? ) \2 | ( \S+ ) ) > > }x ) { |k,q,v,v2| > > h[k.downcase] = (v || v2) } > > block_given? ? ( yield [ h, data ] ) : result << [ h, data ] > > } > > result > > end > > end > > > > DATA.read.xtag('a'){|atr,txt| puts atr['href'], txt } > > > > __END__ > > > href = "alert('Junior broke it!')" >foo bar > > > href = www.foo.bar >foo bar > > > > upcoming HTML 3.2 reference. All the > > is A, with the attribute HREF. > > Help | > > Javascript Link class String def xtag(str) result = [] ; re = %r{ < #{str} (?: \s+ ( (?> [^>"/]* (?> "[^"]*" )? )* ) )? }xi scan( %r{ #{re} / > | #{re} > ( .*? ) }mix ) \ { |unpaired, attr, data| h = { } ( unpaired || attr || "" ). scan( %r{ ( \w+ ) \s* = \s* (?: ( ["'] ) ( .*? ) \2 | ( \S+ ) ) }mx ) { |k,q,v,v2| h[k.downcase] = (v || v2) } block_given? ? ( yield [ h, data ] ) : result << [ h, data ] } result end end DATA.read.xtag('a'){|atr,txt| puts "-"*9; p atr['href']; puts txt } __END__ foo bar foo bar upcoming HTML 3.2 reference. All the is A, with the attribute HREF. Help | Javascript Link Javascript "circumlocutory" Link