summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2013-02-19 05:35:21 +0000
committerluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2013-02-19 05:35:21 +0000
commitc77fddbd17a111edc594fd01b8861896e78c3d34 (patch)
treec785fdb9d66706dfdf7d2da320af6ab265cf2494
parentf0eee9b6738cb417c4c00c74ff7c5fc3931bdbbe (diff)
downloadpython-prettytable-c77fddbd17a111edc594fd01b8861896e78c3d34.tar.gz
Added xhtml option to control linebreak element produced by get_html_string().
git-svn-id: http://prettytable.googlecode.com/svn/trunk@122 0f58610c-415a-11de-9c03-5d6cfad8e937
-rw-r--r--prettytable.py35
-rw-r--r--prettytable_test.py2
2 files changed, 28 insertions, 9 deletions
diff --git a/prettytable.py b/prettytable.py
index f9bcbdf..756975c 100644
--- a/prettytable.py
+++ b/prettytable.py
@@ -126,7 +126,7 @@ class PrettyTable(object):
# Options
self._options = "start end fields header border sortby reversesort sort_key attributes format hrules vrules".split()
self._options.extend("int_format float_format padding_width left_padding_width right_padding_width".split())
- self._options.extend("vertical_char horizontal_char junction_char header_style valign".split())
+ self._options.extend("vertical_char horizontal_char junction_char header_style valign xhtml".split())
for option in self._options:
if option in kwargs:
self._validate_option(option, kwargs[option])
@@ -167,6 +167,7 @@ class PrettyTable(object):
self._junction_char = kwargs["junction_char"] or self._unicode("+")
self._format = kwargs["format"] or False
+ self._xhtml = kwargs["xhtml"] or False
self._attributes = kwargs["attributes"] or {}
def _unicode(self, value):
@@ -263,7 +264,7 @@ class PrettyTable(object):
self._validate_vrules(option, val)
elif option in ("fields"):
self._validate_all_field_names(option, val)
- elif option in ("header", "border", "reversesort"):
+ elif option in ("header", "border", "reversesort", "xhtml"):
self._validate_true_or_false(option, val)
elif option in ("header_style"):
self._validate_header_style(val)
@@ -993,11 +994,20 @@ class PrettyTable(object):
if not options["border"]:
return ""
lpad, rpad = self._get_padding_widths(options)
- bits = [options["junction_char"]]
+ if options['vrules'] in (ALL, FRAME):
+ bits = [options["junction_char"]]
+ else:
+ bits = [options["horizontal_char"]]
for field, width in zip(self._field_names, self._widths):
if options["fields"] and field not in options["fields"]:
continue
bits.append((width+lpad+rpad)*options["horizontal_char"])
+ if options['vrules'] == ALL:
+ bits.append(options["junction_char"])
+ else:
+ bits.append(options["horizontal_char"])
+ if options["vrules"] == FRAME:
+ bits.pop()
bits.append(options["junction_char"])
return "".join(bits)
@@ -1138,7 +1148,8 @@ class PrettyTable(object):
right_padding_width - number of spaces on right hand side of column data
sortby - name of field to sort rows by
sort_key - sorting key function, applied to data points before sorting
- attributes - dictionary of name/value pairs to include as HTML attributes in the <table> tag"""
+ attributes - dictionary of name/value pairs to include as HTML attributes in the <table> tag
+ xhtml - print <br/> tags if True, <br> tags if false"""
options = self._get_options(kwargs)
@@ -1152,6 +1163,10 @@ class PrettyTable(object):
def _get_simple_html_string(self, options):
lines = []
+ if options["xhtml"]:
+ linebreak = "<br/>"
+ else:
+ linebreak = "<br>"
open_tag = []
open_tag.append("<table")
@@ -1167,7 +1182,7 @@ class PrettyTable(object):
for field in self._field_names:
if options["fields"] and field not in options["fields"]:
continue
- lines.append(" <th>%s</th>" % escape(field).replace("\n", "<br />"))
+ lines.append(" <th>%s</th>" % escape(field).replace("\n", linebreak))
lines.append(" </tr>")
# Data
@@ -1178,7 +1193,7 @@ class PrettyTable(object):
for field, datum in zip(self._field_names, row):
if options["fields"] and field not in options["fields"]:
continue
- lines.append(" <td>%s</td>" % escape(datum).replace("\n", "<br />"))
+ lines.append(" <td>%s</td>" % escape(datum).replace("\n", linebreak))
lines.append(" </tr>")
lines.append("</table>")
@@ -1189,6 +1204,10 @@ class PrettyTable(object):
lines = []
lpad, rpad = self._get_padding_widths(options)
+ if options["xhtml"]:
+ linebreak = "<br/>"
+ else:
+ linebreak = "<br>"
open_tag = []
open_tag.append("<table")
@@ -1219,7 +1238,7 @@ class PrettyTable(object):
for field in self._field_names:
if options["fields"] and field not in options["fields"]:
continue
- lines.append(" <th style=\"padding-left: %dem; padding-right: %dem; text-align: center\">%s</th>" % (lpad, rpad, escape(field).replace("\n", "<br />")))
+ lines.append(" <th style=\"padding-left: %dem; padding-right: %dem; text-align: center\">%s</th>" % (lpad, rpad, escape(field).replace("\n", linebreak)))
lines.append(" </tr>")
# Data
@@ -1235,7 +1254,7 @@ class PrettyTable(object):
for field, datum, align, valign in zip(self._field_names, row, aligns, valigns):
if options["fields"] and field not in options["fields"]:
continue
- lines.append(" <td style=\"padding-left: %dem; padding-right: %dem; text-align: %s; vertical-align: %s\">%s</td>" % (lpad, rpad, align, valign, escape(datum).replace("\n", "<br />")))
+ lines.append(" <td style=\"padding-left: %dem; padding-right: %dem; text-align: %s; vertical-align: %s\">%s</td>" % (lpad, rpad, align, valign, escape(datum).replace("\n", linebreak)))
lines.append(" </tr>")
lines.append("</table>")
diff --git a/prettytable_test.py b/prettytable_test.py
index 1938de2..3d0daeb 100644
--- a/prettytable_test.py
+++ b/prettytable_test.py
@@ -415,7 +415,7 @@ class BreakLineTests(unittest.TestCase):
</tr>
<tr>
<td>value 1</td>
- <td>value2<br />second line</td>
+ <td>value2<br>second line</td>
</tr>
<tr>
<td>value 3</td>