Separate a HTML-tag in a hash

Hi.

Is there a simple way to separate a HTML-tag in a hash?

Initial situation:

What I need:
h[“tagName”] -> "input"
h[“type”] -> "text"
h[“name”] -> "test"
h[“size”] -> "20"
h[“style”] -> “border: 1px; color: red;”

Any ideas or hints?

greetings
Dirk Einecke

Dirk Einecke said:

Is there a simple way to separate a HTML-tag in a hash?

I think that the following page will help you.
http://www.rubygarden.org/ruby?HtmlTemplates

···


Takaaki Tateishi ttate@ttsky.net

Hi.

Takaaki Tateishi wrote:

Is there a simple way to separate a HTML-tag in a hash?

I think that the following page will help you.
http://www.rubygarden.org/ruby?HtmlTemplates

Well - I’m not searching a template system. The only example in the
given list which could be interesting for me is misen
(http://www.walrus-ruby.org/shirai/misen.html) but thats a broken link.

Any other hints, examples, code or something like that?

greetings
Dirk Einecke

Dirk Einecke said:

Well - I’m not searching a template system. The only example in the
given list which could be interesting for me is misen
(http://www.walrus-ruby.org/shirai/misen.html) but thats a broken link.

Maybe the page is not maintained by the author.
Misen is available at http://raa.ruby-lang.org/project/misen/ .

···


Takaaki Tateishi ttate@ttsky.net

Ok this is really rough around the edges and I’m sure has a bug hiding
in it, but on your sample there it works fine. It also works for
single tags (like checked and last in my example below). This is a
Q&D 2 minute job, I’m sure you or someone else can refine it.

#!/usr/local/bin/ruby

def explode_tag tag
return nil unless tag.is_a? String

hash = Hash.new
unless tag.sub!(/<([^\s]+) /, '').nil?
    hash['tagName'] = $1
    hash[$1] = $2 until tag.sub!(/([^\s]+)\s*=\s*"([^"]*)"\s*/, '').nil?
    hash[$1] = true until tag.sub!(/([^\s\/<>]+)(\s+|\/|>)/, '').nil?
end
hash

end

s = ''
h = explode_tag(s)
puts h.inspect

Also don’t be afraid to tell him this is crap! =)

Alex McHale

Hi Alex.

Thank you for the script. That’s exactly that what I need.

greetings
Dirk Einecke

No problem. Please let me know if you find any bugs. I plan on using
this function for a project I’m working on.

Alex

···

On Tue, 25 May 2004 17:13:42 +0900, Dirk Einecke dirk.einecke@gmx.de wrote:

Hi Alex.

Thank you for the script. That’s exactly that what I need.

greetings
Dirk Einecke