Skip to content

Instantly share code, notes, and snippets.

@ynaoto
Created August 30, 2016 07:23
Show Gist options
  • Save ynaoto/fa0040adfef5172b42192a137e0a6267 to your computer and use it in GitHub Desktop.
Save ynaoto/fa0040adfef5172b42192a137e0a6267 to your computer and use it in GitHub Desktop.
#!/bin/sh
cat <<EOF
<!DOCTYPE html>
<meta charset="UTF-8">
<title>ansi to html</title>
<style>
body { font-family: menlo; }
.e1m { font-weight: bold; }
.e4m { text-decoration: underline; }
.e30m { color: black; }
.e31m { color: red; }
.e32m { color: green; }
.e33m { color: yellow; }
.e34m { color: blue; }
.e35m { color: magenta; }
.e36m { color: cyan; }
.e37m { color: white; }
.e39m { color: black; }
</style>
EOF
awk -F '
BEGIN {
inEsc = 0;
}
{
for (i = 1; i <= NF; i++) {
cmd = "";
s = $i;
if (s == "") continue;
if (match(s, /^\[[0-9]*m/)) {
cmd = substr(s, RSTART, RLENGTH);
s = substr(s, RLENGTH + 1);
}
gsub("&", "\\&amp;", s);
gsub("\"", "\\&quot;", s);
gsub("<", "\\&lt;", s);
gsub(">", "\\&gt;", s);
gsub(" ", "\\&nbsp;", s);
if (cmd == "[m" || cmd == "[0m") {
if (inEsc) printf "</span>";
inEsc = 0;
} else if (cmd != "") {
printf "<span class=\"e" substr(cmd, 2) "\">";
inEsc = 1;
}
printf s;
}
print "<br/>";
}
' $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment