Skip to content

html table builder #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions bin/table_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import sys
import argparse
import string

try:
import yaml
Expand Down Expand Up @@ -270,16 +271,63 @@ def render_table(self):

return o

###################################
#
# Outputs an HTML-table
# Currently, does NO processing of the data
# such as to handle links or to handle
# code-block directives

class HtmlTable(OutputTable):
pass
def __init__(self, imported_table):
self.tags = { 'tr': '<tr>',
'th': '<th>',
'td': '<td>',
'table': '<table>'
}

self.table = imported_table
self.output = self.render_table()

def render_table(self):
o = [self.tags['table']]

if self.table.header is not None:
o.append(self._process_html_row(self.tags['tr'], self.tags['th'], self.table.header) )

for row in self.table.rows:
o.append(self._process_html_row(self.tags['tr'], self.tags['td'], row.values()))

o.append(self._get_ending_tag(self.tags['table']))
return o

def _process_html_row(self, tag, tagchild, rowdata):
row=[]
row.append(tag)
row.append("\n")

if tagchild is None:
row.append(rowdata)
else:
for data in rowdata:
for cell in data:
row.append(self._process_html_row(tagchild, None, cell))
row.append("\n")
row.append("\n")
row.append(self._get_ending_tag(tag))

return ''.join(row)

def _get_ending_tag(self, tag):
return string.join(tag.split('<', 1), '</')

class TableBuilder(object):
def __init__(self, table):
self.output = table.output

def write(self, outputfile=None):
if outputfile is None:
outputfile = get_default_outputfile(self.inputfile)
outputfile = get_outputfile(self.inputfile)

with open(outputfile, 'w') as f:
for line in self.output:
Expand Down