9d71da175a
Fix the following error seen on MacOS: /usr/bin/awk: syntax error at source line 22 source file header_pattern = >>> @ <<< /^[A-Z][[:alnum:]-]+:/ The @ character in front of regular expressions to pre-compile them seems not in the POSIX specification. Replace them with regular strings and call match() instead of the ~ operator. Also, adjust the url_pattern expression for BSD awk which explicitly states: The awk utility is compliant with the IEEE Std 1003.1-2008 (“POSIX.1”) specification, except awk does not support {n,m} pattern matching. Use [[:lower:]]+ instead of [a-z]{2,6}. Tested with: GNU Awk 5.1.1 awk version 20121220 (FreeBSD) Link: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html Fixes: https://todo.sr.ht/~rjarry/aerc/96 Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Koni Marti <koni.marti@gmail.com>
135 lines
3.2 KiB
Awk
Executable file
135 lines
3.2 KiB
Awk
Executable file
#!/usr/bin/awk -f
|
|
# Copyright (c) 2022 Robin Jarry
|
|
|
|
BEGIN {
|
|
# R;G;B colors
|
|
url = "\033[38;2;255;255;175m" # yellow
|
|
header = "\033[38;2;175;135;255m" # purple
|
|
signature = "\033[38;2;175;135;255m" # purple
|
|
diff_meta = "\033[1;38;2;255;255;255m" # bold white
|
|
diff_chunk = "\033[38;2;0;205;205m" # cyan
|
|
diff_add = "\033[38;2;0;205;0m" # green
|
|
diff_del = "\033[38;2;205;0;0m" # red
|
|
quote_1 = "\033[38;2;95;175;255m" # blue
|
|
quote_2 = "\033[38;2;255;135;0m" # orange
|
|
quote_3 = "\033[38;2;175;135;255m" # purple
|
|
quote_4 = "\033[38;2;255;95;215m" # pink
|
|
quote_x = "\033[38;2;128;128;128m" # gray
|
|
bold = "\033[1m"
|
|
reset = "\033[0m"
|
|
# state
|
|
in_diff = 0
|
|
in_signature = 0
|
|
in_headers = 0
|
|
in_body = 0
|
|
# patterns
|
|
header_pattern = "^[A-Z][[:alnum:]-]+:"
|
|
url_pattern = "[[:lower:]]+://[[:graph:]]+|(mailto:)?[[:alnum:]_\\+\\.~/-]*[[:alnum:]_]@[[:lower:]][[:alnum:]\\.-]*[[:lower:]]"
|
|
meta_pattern = "^(diff --git|(new|deleted) file|similarity index|(rename|copy) (to|from)|index|---|\\+\\+\\+) "
|
|
}
|
|
function color_quote(line) {
|
|
level = 0
|
|
quotes = ""
|
|
while (line ~ /^>/) {
|
|
level += 1
|
|
quotes = quotes ">"
|
|
line = substr(line, 2)
|
|
while (line ~ /^ /) {
|
|
quotes = quotes " "
|
|
line = substr(line, 2)
|
|
}
|
|
}
|
|
if (level == 1) {
|
|
color = quote_1
|
|
} else if (level == 2) {
|
|
color = quote_2
|
|
} else if (level == 3) {
|
|
color = quote_3
|
|
} else if (level == 4) {
|
|
color = quote_4
|
|
} else {
|
|
color = quote_x
|
|
}
|
|
if (match(line, meta_pattern)) {
|
|
return color quotes bold line reset
|
|
} else if (line ~ /^\+/) {
|
|
return color quotes diff_add line reset
|
|
} else if (line ~ /^-/) {
|
|
return color quotes diff_del line reset
|
|
}
|
|
gsub(url_pattern, url "&" color, line)
|
|
return color quotes line reset
|
|
}
|
|
{
|
|
# Strip carriage returns from line
|
|
sub(/\r$/, "")
|
|
|
|
if (in_diff) {
|
|
if ($0 ~ /^-- ?$/) {
|
|
in_diff = 0
|
|
in_signature = 1
|
|
$0 = signature $0 reset
|
|
} else if ($0 ~ /^@@ /) {
|
|
gsub(/^@@[^@]+@@/, diff_chunk "&" reset)
|
|
} else if (match($0, meta_pattern)) {
|
|
$0 = diff_meta $0 reset
|
|
} else if ($0 ~ /^\+/) {
|
|
$0 = diff_add $0 reset
|
|
} else if ($0 ~ /^-/) {
|
|
$0 = diff_del $0 reset
|
|
} else if ($0 !~ /^ / && $0 !~ /^$/) {
|
|
in_diff = 0
|
|
in_body = 1
|
|
if ($0 ~ /^>/) {
|
|
$0 = color_quote($0)
|
|
} else {
|
|
gsub(url_pattern, url "&" reset)
|
|
}
|
|
}
|
|
} else if (in_signature) {
|
|
gsub(url_pattern, url "&" signature)
|
|
$0 = signature $0 reset
|
|
} else if (in_headers) {
|
|
if ($0 ~ /^$/) {
|
|
in_headers = 0
|
|
in_body = 1
|
|
} else {
|
|
sub(header_pattern, header "&" reset)
|
|
gsub(url_pattern, url "&" reset)
|
|
}
|
|
} else if (in_body) {
|
|
if ($0 ~ /^>/) {
|
|
$0 = color_quote($0)
|
|
} else if ($0 ~ /^diff --git /) {
|
|
in_body = 0
|
|
in_diff = 1
|
|
$0 = diff_meta $0 reset
|
|
} else if ($0 ~ /^-- ?$/) {
|
|
in_body = 0
|
|
in_signature = 1
|
|
$0 = signature $0 reset
|
|
} else {
|
|
gsub(url_pattern, url "&" reset)
|
|
}
|
|
} else if ($0 ~ /^diff --git /) {
|
|
in_diff = 1
|
|
$0 = diff_meta $0 reset
|
|
} else if ($0 ~ /^-- ?$/) {
|
|
in_signature = 1
|
|
$0 = signature $0 reset
|
|
} else if (match($0, header_pattern)) {
|
|
in_headers = 1
|
|
sub(header_pattern, header "&" reset)
|
|
gsub(url_pattern, url "&" reset)
|
|
} else {
|
|
in_body = 1
|
|
if ($0 ~ /^>/) {
|
|
$0 = color_quote($0)
|
|
} else {
|
|
gsub(url_pattern, url "&" reset)
|
|
}
|
|
}
|
|
|
|
print
|
|
}
|