Code:
class TsvToHTML
@@styleBlock = <<-ENDMARK
said:
print TsvToHTML.makePage(STDIN)
Given that no "instances" are created, there's no real need to use a
class (in Python, at least -- I don't know if Ruby is like Java, where
everything is embedded in a class). A simple module (file) is
sufficient.
I took a few liberties -- like splitting out the table generation
from the rest of the page, and adding argument parsing for input files
(so this version will create multiple tables if multiple files were
supplied). Be careful, one or two lines were wrapped by the news client.
-=-=-=-=-=-=-=-
# tsv2html.py
# function module
import sys
# define CSS style definition
STYLEBLOCK = """
<style type="text/css">
td {
border-left:1px solid #000000;
padding-right:4px;
padding-left:4px;
white-space: nowrap; }
..cellTitle {
border-bottom:1px solid #000000;
background:#ffffe0;
font-weight: bold;
text-align: center; }
..cell0 { background:#3ff1f1; }
..cell1 { background:#f8f8f8; }
</style>
"""
# utility function to wrap "data" within
# <tag modifier> data </tag>
def wrapTag(data, tag, modifier = ""):
if type(tag) != type(""): #check for complex (tag, modifier) tuple
tag, modifier = tag
return "<%s %s>%s</%s>\n" % (tag, modifier, data, tag)
# utility function to produce an HTML table
# from tab-separated data read from
# iterable source material
def makeTable(source):
tableParts = []
rowNum = 0
# get each line of source
for record in source:
rowParts = []
# get each field of source; splitting on tabs
for field in record.strip().split("\t"):
# convert empty fields to a non-breaking space
if not field: field = " "
if rowNum:
# past the first row, alternate cell style
tagged = wrapTag(field, "td",
'class="cell%s"' % (rowNum % 2))
else:
# first row, use "title" style
tagged = wrapTag(field, "td", #I'd use "th"
'class="cellTitle"')
# collect the tagged field as a list of row parts
rowParts.append(tagged)
rowNum += 1
# join the row parts, and wrap as a row, collecting rows in
list
tableParts.append(wrapTag("".join(rowParts), "tr"))
# join the rows with a new-line separator
return wrapTag("\n".join(tableParts),
("table",
'align="center" cellpadding="0" cellspacing="0"
border="0"'))
def makePage(data):
# wrap the tables in rest of HTML tags: table, body, html
for tag in ["body", "html"]:
# if current tag is the <html>, insert a <head> block with
# the CSS style definition
if tag == "html":
data = wrapTag(STYLEBLOCK, "head") + data
data = wrapTag(data, tag)
return data
if __name__ == "__main__":
# if command line arguments supplied, treat as file names
if len(sys.argv) > 1:
fout = open("TSV2HTML.html", "w")
tables = []
# for each file supplied
for fid in sys.argv[1:]:
# open for read, and open a <filename>.html for output
fin = open(fid, "r")
# generate page from file data, write new file
tables.append(makeTable(fin))
fin.close()
fout.write(makePage("\n".join(tables)))
fout.close()
else:
# no arguments, read stdin, write stdout
sys.stdout.write(makePage(makeTable(sys.stdin))) #could use
print
NOTE: no HTML escaping is done, and my test data sometimes caused
problems.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/