-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdtable2csv.awk
More file actions
executable file
·31 lines (26 loc) · 904 Bytes
/
mdtable2csv.awk
File metadata and controls
executable file
·31 lines (26 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/awk -f
# Convert Github-Flavoured Markdown tables to RFC 4180 CSV
# FIXME: check support with non-gnu awk
BEGIN { FS="\1E" }
function trim(str) { sub(/^\s+/, "", str); sub(/\s+$/, "", str); return str }
function escape(str) { gsub(/"/, "\"\"", str); return str }
function quote(str) { return sprintf("\"%s\"", str) }
/^\s*\|(\s*-+\s*\|)+\s*$/ {
gsub(/\|/, "\1E")
for (i = 2; i < NF; i++)
printf(i == NF - 1 ? "" : ",")
printf("\r\n")
}
# FIXME handle uneven rows https://github.github.com/gfm/#example-204
# FIXME handle colon alignment indicator in delimeter row
/^\s*\|.*\|\s*$/ {
# Replace non-escaped pipes with the ASCII record separator
gsub(/(^|[^\\])\|/, "\1E")
# Un-escape escaped pipes
gsub(/\\\|/, "|")
# Strip leading record separators
gsub(/^\s*\1E/, "")
for (i = 1; i < NF; i++)
printf("%s%s", quote(escape(trim($i))), i == NF - 1 ? "" : ",")
printf("\r\n")
}