HTML/text formatting question

D

Dr. Who

I have a tool that outputs data in either html or text output.

Currently I'm writing chucnks like:

if html:
print '<html><body bgcolor="FFFFCC">'
print '<table border="1" bgcolor="CCCCFF" width="800">'
print '<tr><td colspan="2"><h2>'
print 'Differences %s: %s' % (htypestr, lbl1)
if html:
...

This seems clunky and my next step was going to be to define generic
functions which would generate the surrounding html tags only when
passed the proper argument. I was wondering if there was a better way
to do this with a standard Python library. It looked like formatter
might but that it also might be too low-level.

Any help is appreciated,
Jeff
 
S

Steven Bethard

Dr. Who said:
I have a tool that outputs data in either html or text output.

Currently I'm writing chucnks like:

if html:
print '<html><body bgcolor="FFFFCC">'
print '<table border="1" bgcolor="CCCCFF" width="800">'
print '<tr><td colspan="2"><h2>'
print 'Differences %s: %s' % (htypestr, lbl1)
if html:
...

I'd create two Formatter classes, one for HTML and one for text. It
looks like, in your case, the HTML one should inherit from the text one.
Something like:

py> class TextFormatter(object):
.... def print_differences(self, htypestr, lbll):
.... print 'Differences %s: %s' % (htypestr, lbll)
....
py> class HTMLFormatter(TextFormatter):
.... def print_differences(self, htypestr, lbll):
.... print '<html><body bgcolor="FFFFCC">'
.... print '<table border="1" bgcolor="CCCCFF" width="800">'
.... print '<tr><td colspan="2"><h2>'
.... super(HTMLFormatter, self).print_differences(htypestr, lbll)
.... print '</h2></td></tr></table></body></html>'
....
py> formatter = TextFormatter()
py> formatter.print_differences('test', 'one')
Differences test: one
py> formatter = HTMLFormatter()
py> formatter.print_differences('test', 'one')
<html><body bgcolor="FFFFCC">
<table border="1" bgcolor="CCCCFF" width="800">
<tr><td colspan="2"><h2>
Differences test: one
</h2></td></tr></table></body></html>

Using this strategy, you would replace all your print statements with
calls to a formatter object. Which formatter you use would be
determined wherever you currently set 'html' to True or False.

HTH,

STeVe
 
E

Edvard Majakari

Dr. Who said:
This seems clunky and my next step was going to be to define generic
functions which would generate the surrounding html tags only when
passed the proper argument. I was wondering if there was a better way
to do this with a standard Python library. It looked like formatter
might but that it also might be too low-level.

You could use something like this:

class HTMLFormatter:

def __init__(self, tag, contents=None, **kwargs):

self.tag = tag
self._content = contents
self.attrs = dict()

self._set_attrs(kwargs)

def _set_attrs(self, attrs):

self.attrs = attrs

if '_class' in self.attrs:
self.attrs['class'] = self.attrs['_class']
del self.attrs['_class']

def set_content(self, contents, **kwargs):
"""
Set content of HTML element to contents.
'<br/>'
"""

self._content = contents

if kwargs:
self._set_attrs(kwargs)

def set_attribute(self, attr, val):
"""Set/update attribute 'attr' to 'val'."""

self.attrs[attr] = val

def add_content(self, contents):
"""Add content to element.
'<td>cat</td>'
"""

if self._content is None:
self._content = ''

self._content = "%s%s" % (self._content, str(contents))

def contents(self):
"""Get contents of object.
'nice doggy dog<em>called wuff</em>'

"""

return self._content

def __str__(self):
open_tag = '%s' % self.tag
if self.attrs:
attrs = self.attrs.items()
attrs.sort()
attrs_str = ' '.join(['%s="%s"' % (k, v) \
for k,v in attrs])
open_tag = '%s %s' % (self.tag, attrs_str)

if self._content is not None:
return '<%s>%s</%s>' % (open_tag, self._content, self.tag)
else:
return '<%s/>' % open_tag


Doctest strings show examples how to use it. For serious HTML building stuff
it needs fiddling with, but should be handy for tiny projects.

--
# Edvard Majakari Software Engineer
# PGP PUBLIC KEY available Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top