tags:

views:

179

answers:

2

I have an html file that I need to take any tag and put an align='left' into it.

So given the line :

  <td><img alt="" src="oooh.html_files/px" style="width: 20px; height: 1px;"/></td>

I need it to do :

  <td align='left'><img alt="" src="oooh.html_files/px" style="width: 20px; height: 1px;"/></td>

If it already specifies an alignment I need it to just leave this. So given the line :

  <tr><td width="50%">&nbsp;</td><td align="center">

I need it to do :

  <tr><td width="50%" align='left'>&nbsp;</td><td align="center">

Note it puts an align into the first td, but ignores the second one because that already specifies an alignment.

Is this possible to do it Ruby with regular expressions?

I know its not really worth using regular expressions with html.. but basically I'm just after a quick hack to get over a bug in another library. Hopefully this bug will be fixed soon and I wont need to worry about it! :)

+1  A: 

Frequently Given Answer: regular expressions are not able to parse HTML; use an HTML parsing library of which there are plenty.

martinwguy
I know! I said that in the question, the thing is its nearly 5 in the morning and Im not after a long term solution, I just want a quick hack to get past a little problem so that I can get something working so I can go to bed with a little rosy feeling of seeing something beautiful!
Mongus Pong
+3  A: 
#!/usr/bin/env ruby
require 'nokogiri'

doc = Nokogiri::XML('<tr><td width="50%">&nbsp;</td><td align="center"></tr>')

(doc / '//td[not(@align)]').each {|td| td['align'] = 'left' }

puts doc
# <?xml version="1.0"?>
# <tr>
#   <td width="50%" align="left"/>
#   <td align="center"/>
# </tr>

Look, ma! No Regexp!

It's literally a one-liner if you don't bother with Regexp.

Jörg W Mittag
Thats awesome! I have learnt something today ;) I should sleep well tonight!
Mongus Pong
I like what they say on their homepage : "XML is like violence - if it doesn’t solve your problems, you are not using enough of it."
Mongus Pong