Skip to content

Commit

Permalink
csv2table: add --html
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrargyrum committed Sep 17, 2024
1 parent 9593bda commit 6397db3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions csv2table/_csv2table
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ _arguments \
'(-b --box)-b[Use Unicode pretty characters instead of plain ASCII table]' \
'(-b --box)--box[Use Unicode pretty characters instead of plain ASCII table]' \
'--markdown[Output markdown table]' \
'--html[Output HTML table]' \
--sniff \
::FILE:_files
10 changes: 9 additions & 1 deletion csv2table/csv2table.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def main():
'--markdown', action='store_true',
help='Output markdown table',
)
parser.add_argument(
"--html", action="store_true",
help="Output HTML table",
)
parser.add_argument('--sniff', action='store_true')
parser.add_argument('file', nargs='?', default='-')
args = parser.parse_args()
Expand All @@ -62,7 +66,11 @@ def main():

for row in data:
table.add_row([row.get(col) for col in table.field_names])
print(table)

if args.html:
print(table.get_html_string())
else:
print(table.get_string())


if __name__ == "__main__":
Expand Down
38 changes: 38 additions & 0 deletions csv2table/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,41 @@ check <<- EOF
| banana | yellow |
| orange | orange |
EOF

# html
init --header --html <<- EOF
name,color
zucchini,green
tomato,red
banana,yellow
orange,orange
EOF

check <<- EOF
<table>
<thead>
<tr>
<th>name</th>
<th>color</th>
</tr>
</thead>
<tbody>
<tr>
<td>zucchini</td>
<td>green</td>
</tr>
<tr>
<td>tomato</td>
<td>red</td>
</tr>
<tr>
<td>banana</td>
<td>yellow</td>
</tr>
<tr>
<td>orange</td>
<td>orange</td>
</tr>
</tbody>
</table>
EOF

0 comments on commit 6397db3

Please sign in to comment.