This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,28 @@
csvtxt = '''\
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!\
'''
from cgi import escape
def _row2tr(row, attr=None):
cols = escape(row).split(',')
return ('<TR>'
+ ''.join('<TD>%s</TD>' % data for data in cols)
+ '</TR>')
def csv2html(txt):
htmltxt = '<TABLE summary="csv2html program output">\n'
for rownum, row in enumerate(txt.split('\n')):
htmlrow = _row2tr(row)
htmlrow = ' <TBODY>%s</TBODY>\n' % htmlrow
htmltxt += htmlrow
htmltxt += '</TABLE>\n'
return htmltxt
htmltxt = csv2html(csvtxt)
print(htmltxt)

View file

@ -0,0 +1,8 @@
<TABLE summary="csv2html program output">
<TBODY><TR><TD>Character</TD><TD>Speech</TD></TR></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
<TBODY><TR><TD>Brians mother</TD><TD>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</TD></TR></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY>
<TBODY><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE>

View file

@ -0,0 +1,30 @@
def _row2trextra(row, attr=None):
cols = escape(row).split(',')
attr_tr = attr.get('TR', '')
attr_td = attr.get('TD', '')
return (('<TR%s>' % attr_tr)
+ ''.join('<TD%s>%s</TD>' % (attr_td, data) for data in cols)
+ '</TR>')
def csv2htmlextra(txt, header=True, attr=None):
' attr is a dictionary mapping tags to attributes to add to that tag'
attr_table = attr.get('TABLE', '')
attr_thead = attr.get('THEAD', '')
attr_tbody = attr.get('TBODY', '')
htmltxt = '<TABLE%s>\n' % attr_table
for rownum, row in enumerate(txt.split('\n')):
htmlrow = _row2trextra(row, attr)
rowclass = ('THEAD%s' % attr_thead) if (header and rownum == 0) else ('TBODY%s' % attr_tbody)
htmlrow = ' <%s>%s</%s>\n' % (rowclass, htmlrow, rowclass[:5])
htmltxt += htmlrow
htmltxt += '</TABLE>\n'
return htmltxt
htmltxt = csv2htmlextra(csvtxt, True,
dict(TABLE=' border="1" summary="csv2html extra program output"',
THEAD=' bgcolor="yellow"',
TBODY=' bgcolor="orange"'
)
)
print(htmltxt)

View file

@ -0,0 +1,8 @@
<TABLE border="1" summary="csv2html extra program output">
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE>