comparison ansi2html.sh @ 0:d2dd051f072b draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weather_app commit daa27cb2061f0d1cfd0f9bdc55e7073c2543f719
author iuc
date Tue, 19 Jul 2016 11:55:14 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d2dd051f072b
1 #!/bin/sh
2
3 # Convert ANSI (terminal) colours and attributes to HTML
4
5 # Licence: LGPLv2
6 # Author:
7 # http://www.pixelbeat.org/docs/terminal_colours/
8 # Examples:
9 # ls -l --color=always | ansi2html.sh > ls.html
10 # git show --color | ansi2html.sh > last_change.html
11 # Generally one can use the `script` util to capture full terminal output.
12 # Changes:
13 # V0.1, 24 Apr 2008, Initial release
14 # V0.2, 01 Jan 2009, Phil Harnish <philharnish@gmail.com>
15 # Support `git diff --color` output by
16 # matching ANSI codes that specify only
17 # bold or background colour.
18 # P@draigBrady.com
19 # Support `ls --color` output by stripping
20 # redundant leading 0s from ANSI codes.
21 # Support `grep --color=always` by stripping
22 # unhandled ANSI codes (specifically ^[[K).
23 # V0.3, 20 Mar 2009, http://eexpress.blog.ubuntu.org.cn/
24 # Remove cat -v usage which mangled non ascii input.
25 # Cleanup regular expressions used.
26 # Support other attributes like reverse, ...
27 # P@draigBrady.com
28 # Correctly nest <span> tags (even across lines).
29 # Add a command line option to use a dark background.
30 # Strip more terminal control codes.
31 # V0.4, 17 Sep 2009, P@draigBrady.com
32 # Handle codes with combined attributes and color.
33 # Handle isolated <bold> attributes with css.
34 # Strip more terminal control codes.
35 # V0.23, 28 Feb 2016
36 # http://github.com/pixelb/scripts/commits/master/scripts/ansi2html.sh
37
38 gawk --version >/dev/null || exit 1
39
40 if [ "$1" = "--version" ]; then
41 printf '0.23\n' && exit
42 fi
43
44 usage()
45 {
46 printf '%s\n' \
47 'This utility converts ANSI codes in data passed to stdin
48 It has 4 optional parameters:
49 --bg=dark --palette=linux|solarized|tango|xterm --css-only|--body-only
50 E.g.: ls -l --color=always | ansi2html.sh --bg=dark > ls.html' >&2
51 exit
52 }
53
54 if [ "$1" = "--help" ]; then
55 usage
56 fi
57
58 processArg()
59 {
60 [ "$1" = "--bg=dark" ] && { dark_bg=yes; return; }
61 [ "$1" = "--css-only" ] && { css_only=yes; return; }
62 [ "$1" = "--body-only" ] && { body_only=yes; return; }
63 if [ "$1" = "--palette=solarized" ]; then
64 # See http://ethanschoonover.com/solarized
65 P0=073642; P1=D30102; P2=859900; P3=B58900;
66 P4=268BD2; P5=D33682; P6=2AA198; P7=EEE8D5;
67 P8=002B36; P9=CB4B16; P10=586E75; P11=657B83;
68 P12=839496; P13=6C71C4; P14=93A1A1; P15=FDF6E3;
69 return;
70 elif [ "$1" = "--palette=solarized-xterm" ]; then
71 # Above mapped onto the xterm 256 color palette
72 P0=262626; P1=AF0000; P2=5F8700; P3=AF8700;
73 P4=0087FF; P5=AF005F; P6=00AFAF; P7=E4E4E4;
74 P8=1C1C1C; P9=D75F00; P10=585858; P11=626262;
75 P12=808080; P13=5F5FAF; P14=8A8A8A; P15=FFFFD7;
76 return;
77 elif [ "$1" = "--palette=tango" ]; then
78 # Gnome default
79 P0=000000; P1=CC0000; P2=4E9A06; P3=C4A000;
80 P4=3465A4; P5=75507B; P6=06989A; P7=D3D7CF;
81 P8=555753; P9=EF2929; P10=8AE234; P11=FCE94F;
82 P12=729FCF; P13=AD7FA8; P14=34E2E2; P15=EEEEEC;
83 return;
84 elif [ "$1" = "--palette=xterm" ]; then
85 P0=000000; P1=CD0000; P2=00CD00; P3=CDCD00;
86 P4=0000EE; P5=CD00CD; P6=00CDCD; P7=E5E5E5;
87 P8=7F7F7F; P9=FF0000; P10=00FF00; P11=FFFF00;
88 P12=5C5CFF; P13=FF00FF; P14=00FFFF; P15=FFFFFF;
89 return;
90 else # linux console
91 P0=000000; P1=AA0000; P2=00AA00; P3=AA5500;
92 P4=0000AA; P5=AA00AA; P6=00AAAA; P7=AAAAAA;
93 P8=555555; P9=FF5555; P10=55FF55; P11=FFFF55;
94 P12=5555FF; P13=FF55FF; P14=55FFFF; P15=FFFFFF;
95 [ "$1" = "--palette=linux" ] && return;
96 fi
97 }
98
99 processArg #defaults
100 for var in "$@"; do processArg $var; done
101 [ "$css_only" ] && [ "$body_only" ] && usage
102
103 # Mac OSX's GNU sed is installed as gsed
104 # use e.g. homebrew 'gnu-sed' to get it
105 if ! sed --version >/dev/null 2>&1; then
106 if gsed --version >/dev/null 2>&1; then
107 alias sed=gsed
108 else
109 echo "Error, can't find an acceptable GNU sed." >&2
110 exit 1
111 fi
112 fi
113
114 [ "$css_only" ] || [ "$body_only" ] || printf '%s' "<html>
115 <head>
116 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
117 <style type=\"text/css\">
118 "
119 [ "$body_only" ] || printf ".ef0,.f0 { color: #$P0; } .eb0,.b0 { background-color: #$P0; }
120 .ef1,.f1 { color: #$P1; } .eb1,.b1 { background-color: #$P1; }
121 .ef2,.f2 { color: #$P2; } .eb2,.b2 { background-color: #$P2; }
122 .ef3,.f3 { color: #$P3; } .eb3,.b3 { background-color: #$P3; }
123 .ef4,.f4 { color: #$P4; } .eb4,.b4 { background-color: #$P4; }
124 .ef5,.f5 { color: #$P5; } .eb5,.b5 { background-color: #$P5; }
125 .ef6,.f6 { color: #$P6; } .eb6,.b6 { background-color: #$P6; }
126 .ef7,.f7 { color: #$P7; } .eb7,.b7 { background-color: #$P7; }
127 .ef8, .f0 > .bold,.bold > .f0 { color: #$P8; font-weight: normal; }
128 .ef9, .f1 > .bold,.bold > .f1 { color: #$P9; font-weight: normal; }
129 .ef10,.f2 > .bold,.bold > .f2 { color: #$P10; font-weight: normal; }
130 .ef11,.f3 > .bold,.bold > .f3 { color: #$P11; font-weight: normal; }
131 .ef12,.f4 > .bold,.bold > .f4 { color: #$P12; font-weight: normal; }
132 .ef13,.f5 > .bold,.bold > .f5 { color: #$P13; font-weight: normal; }
133 .ef14,.f6 > .bold,.bold > .f6 { color: #$P14; font-weight: normal; }
134 .ef15,.f7 > .bold,.bold > .f7 { color: #$P15; font-weight: normal; }
135 .eb8 { background-color: #$P8; }
136 .eb9 { background-color: #$P9; }
137 .eb10 { background-color: #$P10; }
138 .eb11 { background-color: #$P11; }
139 .eb12 { background-color: #$P12; }
140 .eb13 { background-color: #$P13; }
141 .eb14 { background-color: #$P14; }
142 .eb15 { background-color: #$P15; }
143 "
144 # The default xterm 256 colour palette
145 for red in 0 1 2 3 4 5 ; do
146 for green in 0 1 2 3 4 5 ; do
147 for blue in 0 1 2 3 4 5 ; do
148 c=$((16 + ($red * 36) + ($green * 6) + $blue))
149 r=$((($red * 40 + 55) * ($red > 0)))
150 g=$((($green * 40 + 55) * ($green > 0)))
151 b=$((($blue * 40 + 55) * ($blue > 0)))
152 [ "$body_only" ] || printf ".ef%d { color: #%2.2x%2.2x%2.2x; } " $c $r $g $b
153 [ "$body_only" ] || printf ".eb%d { background-color: #%2.2x%2.2x%2.2x; }\n" $c $r $g $b
154 done
155 done
156 done
157 for gray in $(seq 0 23); do
158 c=$(($gray+232))
159 l=$(($gray*10 + 8))
160 [ "$body_only" ] || printf ".ef%d { color: #%2.2x%2.2x%2.2x; } " $c $l $l $l
161 [ "$body_only" ] || printf ".eb%d { background-color: #%2.2x%2.2x%2.2x; }\n" $c $l $l $l
162 done
163
164 [ "$body_only" ] || printf '%s' '
165 .f9 { color: '`[ "$dark_bg" ] && printf "#$P7;" || printf "#$P0;"`' }
166 .b9 { background-color: #'`[ "$dark_bg" ] && printf $P0 || printf $P15`'; }
167 .f9 > .bold,.bold > .f9, body.f9 > pre > .bold {
168 /* Bold is heavy black on white, or bright white
169 depending on the default background */
170 color: '`[ "$dark_bg" ] && printf "#$P15;" || printf "#$P0;"`'
171 font-weight: '`[ "$dark_bg" ] && printf 'normal;' || printf 'bold;'`'
172 }
173 .reverse {
174 /* CSS does not support swapping fg and bg colours unfortunately,
175 so just hardcode something that will look OK on all backgrounds. */
176 '"color: #$P0; background-color: #$P7;"'
177 }
178 .underline { text-decoration: underline; }
179 .line-through { text-decoration: line-through; }
180 .blink { text-decoration: blink; }
181
182 /* Avoid pixels between adjacent span elements.
183 Note this only works for lines less than 80 chars
184 where we close span elements on the same line.
185 span { display: inline-block; }
186 */
187 '
188 [ "$body_only" ] || [ "$css_only" ] && printf '%s\n' \
189 'To use the css generated from --css-only, do: '\
190 '<head><link rel="stylesheet" type="text/css" href="style.css"></head>' >&2
191 [ "$css_only" ] && exit
192 [ "$body_only" ] || printf '%s' '</style>
193 </head>
194
195 <body class="f9 b9">
196 <pre>
197 '
198 [ "$body_only" ] && printf '%s\n' 'Be sure to use <body class="f9 b9"> and <pre>' >&2
199
200 p='\x1b\[' #shortcut to match escape codes
201
202 # Handle various xterm control sequences.
203 # See /usr/share/doc/xterm-*/ctlseqs.txt
204 sed "
205 # escape ampersand and quote
206 s#&#\&amp;#g; s#\"#\&quot;#g;
207 s#\x1b[^\x1b]*\x1b\\\##g # strip anything between \e and ST
208 s#\x1b][0-9]*;[^\a]*\a##g # strip any OSC (xterm title etc.)
209
210 s#\r\$## # strip trailing \r
211
212 # strip other non SGR escape sequences
213 s#[\x07]##g
214 s#\x1b[]>=\][0-9;]*##g
215 s#\x1bP+.\{5\}##g
216 # Mark cursor positioning codes \"Jr;c;
217 s#${p}\([0-9]\{1,2\}\)G#\"J;\1;#g
218 s#${p}\([0-9]\{1,2\}\);\([0-9]\{1,2\}\)H#\"J\1;\2;#g
219
220 # Mark clear as \"Cn where n=1 is screen and n=0 is to end-of-line
221 s#${p}H#\"C1;#g
222 s#${p}K#\"C0;#g
223 # Mark Cursor move columns as \"Mn where n is +ve for right, -ve for left
224 s#${p}C#\"M1;#g
225 s#${p}\([0-9]\{1,\}\)C#\"M\1;#g
226 s#${p}\([0-9]\{1,\}\)D#\"M-\1;#g
227 s#${p}\([0-9]\{1,\}\)P#\"X\1;#g
228
229 s#${p}[0-9;?]*[^0-9;?m]##g
230
231 " |
232
233 # Normalize the input before transformation
234 sed "
235 # escape HTML (ampersand and quote done above)
236 s#>#\&gt;#g; s#<#\&lt;#g;
237
238 # normalize SGR codes a little
239
240 # split 256 colors out and mark so that they're not
241 # recognised by the following 'split combined' line
242 :e
243 s#${p}\([0-9;]\{1,\}\);\([34]8;5;[0-9]\{1,3\}\)m#${p}\1m${p}¬\2m#g; t e
244 s#${p}\([34]8;5;[0-9]\{1,3\}\)m#${p}¬\1m#g;
245
246 :c
247 s#${p}\([0-9]\{1,\}\);\([0-9;]\{1,\}\)m#${p}\1m${p}\2m#g; t c # split combined
248 s#${p}0\([0-7]\)#${p}\1#g #strip leading 0
249 s#${p}1m\(\(${p}[4579]m\)*\)#\1${p}1m#g #bold last (with clr)
250 s#${p}m#${p}0m#g #add leading 0 to norm
251
252 # undo any 256 color marking
253 s#${p}¬\([34]8;5;[0-9]\{1,3\}\)m#${p}\1m#g;
254
255 # map 16 color codes to color + bold
256 s#${p}9\([0-7]\)m#${p}3\1m${p}1m#g;
257 s#${p}10\([0-7]\)m#${p}4\1m${p}1m#g;
258
259 # change 'reset' code to \"R
260 s#${p}0m#\"R;#g
261 " |
262
263 # Convert SGR sequences to HTML
264 sed "
265 # common combinations to minimise html (optional)
266 :f
267 s#${p}3[0-7]m${p}3\([0-7]\)m#${p}3\1m#g; t f
268 :b
269 s#${p}4[0-7]m${p}4\([0-7]\)m#${p}4\1m#g; t b
270 s#${p}3\([0-7]\)m${p}4\([0-7]\)m#<span class=\"f\1 b\2\">#g
271 s#${p}4\([0-7]\)m${p}3\([0-7]\)m#<span class=\"f\2 b\1\">#g
272
273 s#${p}1m#<span class=\"bold\">#g
274 s#${p}4m#<span class=\"underline\">#g
275 s#${p}5m#<span class=\"blink\">#g
276 s#${p}7m#<span class=\"reverse\">#g
277 s#${p}9m#<span class=\"line-through\">#g
278 s#${p}3\([0-9]\)m#<span class=\"f\1\">#g
279 s#${p}4\([0-9]\)m#<span class=\"b\1\">#g
280
281 s#${p}38;5;\([0-9]\{1,3\}\)m#<span class=\"ef\1\">#g
282 s#${p}48;5;\([0-9]\{1,3\}\)m#<span class=\"eb\1\">#g
283
284 s#${p}[0-9;]*m##g # strip unhandled codes
285 " |
286
287 # Convert alternative character set and handle cursor movement codes
288 # Note we convert here, as if we do at start we have to worry about avoiding
289 # conversion of SGR codes etc., whereas doing here we only have to
290 # avoid conversions of stuff between &...; or <...>
291 #
292 # Note we could use sed to do this based around:
293 # sed 'y/abcdefghijklmnopqrstuvwxyz{}`~/▒␉␌␍␊°±␤␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥π£◆·/'
294 # However that would be very awkward as we need to only conv some input.
295 # The basic scheme that we do in the awk script below is:
296 # 1. enable transliterate once "T1; is seen
297 # 2. disable once "T0; is seen (may be on diff line)
298 # 3. never transliterate between &; or <> chars
299 # 4. track x,y movements and active display mode at each position
300 # 5. buffer line/screen and dump when required
301 sed "
302 # change 'smacs' and 'rmacs' to \"T1 and \"T0 to simplify matching.
303 s#\x1b(0#\"T1;#g;
304 s#\x0E#\"T1;#g;
305
306 s#\x1b(B#\"T0;#g
307 s#\x0F#\"T0;#g
308 " |
309 (
310 gawk '
311 function dump_line(l,del,c,blanks,ret) {
312 for(c=1;c<maxX;c++) {
313 if ((c SUBSEP l) in attr || length(cur)) {
314 ret = ret blanks fixas(cur,attr[c,l])
315 if(del) delete attr[c,l]
316 blanks=""
317 }
318 if ((c SUBSEP l) in dump) {
319 ret=ret blanks dump[c,l]
320 if(del) delete dump[c,l]
321 blanks=""
322 } else blanks=blanks " "
323 }
324 if(length(cur)) ret=ret blanks
325 return ret
326 }
327
328 function dump_screen(l,ret) {
329 for(l=1;l<=maxY;l++)
330 ret=ret dump_line(l,0) "\n"
331 return ret fixas(cur, "")
332 }
333
334 function atos(a,i,ret) {
335 for(i=1;i<=length(a);i++) if(i in a) ret=ret a[i]
336 return ret
337 }
338
339 function fixas(a,s,spc,i,attr,rm,ret) {
340 spc=length(a)
341 l=split(s,attr,">")
342 for(i=1;i<=spc;i++) {
343 rm=rm?rm:(a[i]!=attr[i]">")
344 if(rm) {
345 ret=ret "</span>"
346 delete a[i];
347 }
348 }
349 for(i=1;i<l;i++) {
350 attr[i]=attr[i]">"
351 if(a[i]!=attr[i]) {
352 a[i]=attr[i]
353 ret = ret attr[i]
354 }
355 }
356 return ret
357 }
358
359 function encode(string,start,end,i,ret,pos,sc,buf) {
360 if(!end) end=length(string);
361 if(!start) start=1;
362 state=3
363 for(i=1;i<=length(string);i++) {
364 c=substr(string,i,1)
365 if(state==2) {
366 sc=sc c
367 if(c==";") {
368 c=sc
369 state=last_mode
370 } else continue
371 } else {
372 if(c=="\r") { x=1; continue }
373 if(c=="<") {
374 # Change attributes - store current active
375 # attributes in span array
376 split(substr(string,i),cord,">");
377 i+=length(cord[1])
378 span[++spc]=cord[1] ">"
379 continue
380 }
381 else if(c=="&") {
382 # All goes to single position till we see a semicolon
383 sc=c
384 state=2
385 continue
386 }
387 else if(c=="\b") {
388 # backspace move insertion point back 1
389 if(spc) attr[x,y]=atos(span)
390 x=x>1?x-1:1
391 continue
392 }
393 else if(c=="\"") {
394 split(substr(string,i+2),cord,";")
395 cc=substr(string,i+1,1);
396 if(cc=="T") {
397 # Transliterate on/off
398 if(cord[1]==1&&state==3) last_mode=state=4
399 if(cord[1]==0&&state==4) last_mode=state=3
400 }
401 else if(cc=="C") {
402 # Clear
403 if(cord[1]+0) {
404 # Screen - if Recording dump screen
405 if(dumpStatus==dsActive) ret=ret dump_screen()
406 dumpStatus=dsActive
407 delete dump
408 delete attr
409 x=y=1
410 } else {
411 # To end of line
412 for(pos=x;pos<maxX;pos++) {
413 dump[pos,y]=" "
414 if (!spc) delete attr[pos,y]
415 else attr[pos,y]=atos(span)
416 }
417 }
418 }
419 else if(cc=="J") {
420 # Jump to x,y
421 i+=length(cord[2])+1
422 # If line is higher - dump previous screen
423 if(dumpStatus==dsActive&&cord[1]<y) {
424 ret=ret dump_screen();
425 dumpStatus=dsNew;
426 }
427 x=cord[2]
428 if(length(cord[1]) && y!=cord[1]){
429 y=cord[1]
430 if(y>maxY) maxY=y
431 # Change y - start recording
432 dumpStatus=dumpStatus?dumpStatus:dsReset
433 }
434 }
435 else if(cc=="M") {
436 # Move left/right on current line
437 x+=cord[1]
438 }
439 else if(cc=="X") {
440 # delete on right
441 for(pos=x;pos<=maxX;pos++) {
442 nx=pos+cord[1]
443 if(nx<maxX) {
444 if((nx SUBSEP y) in attr) attr[pos,y] = attr[nx,y]
445 else delete attr[pos,y]
446 if((nx SUBSEP y) in dump) dump[pos,y] = dump[nx,y]
447 else delete dump[pos,y]
448 } else if(spc) {
449 attr[pos,y]=atos(span)
450 dump[pos,y]=" "
451 }
452 }
453 }
454 else if(cc=="R") {
455 # Reset attributes
456 while(spc) delete span[spc--]
457 }
458 i+=length(cord[1])+2
459 continue
460 }
461 else if(state==4&&i>=start&&i<=end&&c in Trans) c=Trans[c]
462 }
463 if(dumpStatus==dsReset) {
464 delete dump
465 delete attr
466 ret=ret"\n"
467 dumpStatus=dsActive
468 }
469 if(dumpStatus==dsNew) {
470 # After moving/clearing we are now ready to write
471 # somthing to the screen so start recording now
472 ret=ret"\n"
473 dumpStatus=dsActive
474 }
475 if(dumpStatus==dsActive||dumpStatus==dsOff) {
476 dump[x,y] = c
477 if(!spc) delete attr[x,y]
478 else attr[x,y] = atos(span)
479 if(++x>maxX) maxX=x;
480 }
481 }
482 # End of line if dumping increment y and set x back to first col
483 x=1
484 if(!dumpStatus) return ret dump_line(y,1);
485 else if(++y>maxY) maxY=y;
486 return ret
487 }
488 BEGIN{
489 OFS=FS
490 # dump screen status
491 dsOff=0 # Not dumping screen contents just write output direct
492 dsNew=1 # Just after move/clear waiting for activity to start recording
493 dsReset=2 # Screen cleared build new empty buffer and record
494 dsActive=3 # Currently recording
495 F="abcdefghijklmnopqrstuvwxyz{}`~"
496 T="▒␉␌␍␊°±␤␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥π£◆·"
497 maxX=80
498 delete cur;
499 x=y=1
500 for(i=1;i<=length(F);i++)Trans[substr(F,i,1)]=substr(T,i,1);
501 }
502
503 { $0=encode($0) }
504 1
505 END {
506 if(dumpStatus) {
507 print dump_screen();
508 }
509 }'
510 )
511
512 [ "$body_only" ] || printf '</pre>
513 </body>
514 </html>\n'