import csv from StringIO import StringIO from trac.wiki.macros import WikiMacroBase from trac.wiki.formatter import format_to_html from trac.util import escape class CsvToTableMacro(WikiMacroBase): """CsvToTable Display CSV data in a table. Simply copy and paste the CSV data into the macro body. Example: {{{ {{{ #!CSV '''header1''',header2,header3 123,#123,[wiki:WikiStart 123] ''32'',{{{32}}} 45,'''21''',12 $,,© }}} }}} """ def expand_macro(self, formatter, name, txt): txt = txt.encode('ascii', 'replace') # Parse string assuming a ',' delimiter so we can use spaces in the values # ref: http://docs.python.org/library/csv.html#csv-fmt-params reader = csv.reader(StringIO(txt), delimiter=',', dialect='excel') formatter.out.write('
%s | ' % escape(format_to_html(self.env, formatter.context, col or ''))) formatter.out.write('