« just asking | Main | rendering html in firefox from irb »

rendering html from irb

for the last few weeks I've been doing some fairly tedious documentation stuff, e.g. pulling together 'requirements' from out of different word docs into a single list, categorising them (e.g. into 'functional', 'non-functional', 'future') and then creating a new table for each group that lists each requirement with an ID, a description, and a link back to the original source document.

I'm not going to explain why I need to do all that, it's just The Way Things Are Done Around Here.

So I've been using word automation to extract the source docs as html, like this:

def get_html_from_doc(docname)
 	winword = WIN32OLE.new('word.application')
doc=winword.Documents.Open(docname,nil,true)
tempfile=Tempfile.new("tmp")
tempfile.close #word reopens it
doc.SaveAs(tempfile.path,10)
doc.Close
winword.Quit unless winword.nil?
tempfile.open
html=tempfile.read
tempfile.close
html
end
then Rubyful Soup to pull the data from html tables into some linked hashes, which were saved as yaml. Once the data was a collection of ruby objects, I could modify it interactively with irb, then turn it into html. However while irb would happily print out the html source, if I wanted to see how that html would display I needed to first save it to a file, then load that file into IE. That got boring pretty quickly, so I put together this little routine to pipe a html string straight into a MSHTML control.
require 'win32ole'

class ShowHTML

private
def ShowHTML.create_new_window
@@ie=WIN32OLE.new('InternetExplorer.Application')
@@ie.menubar=0
@@ie.toolbar=0
@@ie.statusbar=0
end
ShowHTML.create_new_window
public
def ShowHTML.show(html)
#check our window is still around
begin
@@ie.navigate('about:blank')
rescue WIN32OLERuntimeError
create_new_window
@@ie.navigate('about:blank')
end
@@ie.document.open
@@ie.document.write(html.to_s)
@@ie.document.close
@@ie.visible=true
end

def ShowHTML.show_hash(hash)
html="<html><table>"<<hash.map{|v|"<tr><td>#{v[0]}</td><td>#{v[1]}</td></tr>\n"}.to_s<<"</table>"
ShowHTML.show(html)
end
end

Here's a screenshot of how this looks:
displaying html from irb
irb (foreground) displaying the ENV hash in a html window (background)