Parse CSV files into HTML tables

This commit is contained in:
Thomas Miceli
2023-03-19 03:18:56 +01:00
parent 11b3eed250
commit 858ee3e70a
5 changed files with 108 additions and 13 deletions

View File

@ -3,6 +3,7 @@
{{ if .files }}
<div class="grid gap-y-4">
{{ range $file := .files }}
{{ $csv := csvFile $file }}
<div class="rounded-md border border-1 border-gray-700 overflow-auto">
<div class="border-b-1 border-gray-700 bg-gray-800 my-auto block">
<div class="ml-4 py-1.5 flex">
@ -20,21 +21,46 @@
This file has been truncated. <a href="/{{ $.gist.User.Username }}/{{ $.gist.Uuid }}/raw/{{ $.commit }}/{{$file.Filename}}">View the full file.</a>
</div>
{{ end }}
{{ if and (not $csv) (isCsv $file.Filename) }}
<div class="text-sm px-4 py-1.5 border-t-1 border-gray-700">
This file is not a valid CSV file.
</div>
{{ end }}
</div>
<div class="code overflow-auto">
{{ if isMarkdown $file.Filename }}
<div class="overflow-auto">
{{ if $csv }}
<table class="csv-table">
<thead>
<tr>
{{ range $csv.Header }}
<th>{{ . }}</th>
{{ end }}
</tr>
</thead>
<tbody>
{{ range $csv.Rows }}
<tr>
{{ range . }}
<td>{{ . }}</td>
{{ end }}
</tr>
{{ end }}
</table>
{{ else if isMarkdown $file.Filename }}
<div class="markdown markdown-body p-8">{{ $file.Content }}</div>
{{ else }}
{{ $fileslug := slug $file.Filename }}
{{ if ne $file.Content "" }}
<table class="table-code w-full whitespace-pre" data-filename-slug="{{ $fileslug }}" data-filename="{{ $file.Filename }}" style="font-size: 0.8em; border-spacing: 0; border-collapse: collapse;">
<tbody>
{{ $ii := "1" }}
{{ $i := toInt $ii }}
{{ range $line := lines $file.Content }}<tr><td id="file-{{ $fileslug }}-{{$i}}" class="select-none line-num px-4">{{$i}}</td><td class="line-code">{{ $line }}</td></tr>{{ $i = inc $i }}{{ end }}
</tbody>
</table>
{{ end }}
<div class="code">
{{ $fileslug := slug $file.Filename }}
{{ if ne $file.Content "" }}
<table class="table-code w-full whitespace-pre" data-filename-slug="{{ $fileslug }}" data-filename="{{ $file.Filename }}" style="font-size: 0.8em; border-spacing: 0; border-collapse: collapse;">
<tbody>
{{ $ii := "1" }}
{{ $i := toInt $ii }}
{{ range $line := lines $file.Content }}<tr><td id="file-{{ $fileslug }}-{{$i}}" class="select-none line-num px-4">{{$i}}</td><td class="line-code">{{ $line }}</td></tr>{{ $i = inc $i }}{{ end }}
</tbody>
</table>
{{ end }}
</div>
{{ end }}
</div>
</div>