annotate ansi2html.sh @ 13:0a8c6b61f0f4 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/text_processing commit 4f79443517baf378fbfe1f81be361d97f2938601
author bgruening
date Wed, 03 Apr 2019 13:56:01 -0400
parents 5314e5d6f040
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
1 #!/bin/sh
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
2
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
3 # Convert ANSI (terminal) colours and attributes to HTML
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
4
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
5 # Author:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
6 # http://www.pixelbeat.org/docs/terminal_colours/
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
7 # Examples:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
8 # ls -l --color=always | ansi2html.sh > ls.html
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
9 # git show --color | ansi2html.sh > last_change.html
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
10 # Generally one can use the `script` util to capture full terminal output.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
11 # Changes:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
12 # V0.1, 24 Apr 2008, Initial release
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
13 # V0.2, 01 Jan 2009, Phil Harnish <philharnish@gmail.com>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
14 # Support `git diff --color` output by
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
15 # matching ANSI codes that specify only
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
16 # bold or background colour.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
17 # P@draigBrady.com
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
18 # Support `ls --color` output by stripping
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
19 # redundant leading 0s from ANSI codes.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
20 # Support `grep --color=always` by stripping
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
21 # unhandled ANSI codes (specifically ^[[K).
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
22 # V0.3, 20 Mar 2009, http://eexpress.blog.ubuntu.org.cn/
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
23 # Remove cat -v usage which mangled non ascii input.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
24 # Cleanup regular expressions used.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
25 # Support other attributes like reverse, ...
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
26 # P@draigBrady.com
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
27 # Correctly nest <span> tags (even across lines).
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
28 # Add a command line option to use a dark background.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
29 # Strip more terminal control codes.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
30 # V0.4, 17 Sep 2009, P@draigBrady.com
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
31 # Handle codes with combined attributes and color.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
32 # Handle isolated <bold> attributes with css.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
33 # Strip more terminal control codes.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
34 # V0.12, 12 Jul 2011
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
35 # http://github.com/pixelb/scripts/commits/master/scripts/ansi2html.sh
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
36
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
37 if [ "$1" = "--version" ]; then
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
38 echo "0.12" && exit
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
39 fi
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
40
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
41 if [ "$1" = "--help" ]; then
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
42 echo "This utility converts ANSI codes in data passed to stdin" >&2
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
43 echo "It has 2 optional parameters:" >&2
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
44 echo " --bg=dark --palette=linux|solarized|tango|xterm" >&2
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
45 echo "E.g.: ls -l --color=always | ansi2html.sh --bg=dark > ls.html" >&2
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
46 exit
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
47 fi
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
48
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
49 [ "$1" = "--bg=dark" ] && { dark_bg=yes; shift; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
50
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
51 if [ "$1" = "--palette=solarized" ]; then
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
52 # See http://ethanschoonover.com/solarized
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
53 P0=073642; P1=D30102; P2=859900; P3=B58900;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
54 P4=268BD2; P5=D33682; P6=2AA198; P7=EEE8D5;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
55 P8=002B36; P9=CB4B16; P10=586E75; P11=657B83;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
56 P12=839496; P13=6C71C4; P14=93A1A1; P15=FDF6E3;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
57 shift;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
58 elif [ "$1" = "--palette=solarized-xterm" ]; then
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
59 # Above mapped onto the xterm 256 color palette
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
60 P0=262626; P1=AF0000; P2=5F8700; P3=AF8700;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
61 P4=0087FF; P5=AF005F; P6=00AFAF; P7=E4E4E4;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
62 P8=1C1C1C; P9=D75F00; P10=585858; P11=626262;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
63 P12=808080; P13=5F5FAF; P14=8A8A8A; P15=FFFFD7;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
64 shift;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
65 elif [ "$1" = "--palette=tango" ]; then
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
66 # Gnome default
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
67 P0=000000; P1=CC0000; P2=4E9A06; P3=C4A000;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
68 P4=3465A4; P5=75507B; P6=06989A; P7=D3D7CF;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
69 P8=555753; P9=EF2929; P10=8AE234; P11=FCE94F;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
70 P12=729FCF; P13=AD7FA8; P14=34E2E2; P15=EEEEEC;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
71 shift;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
72 elif [ "$1" = "--palette=xterm" ]; then
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
73 P0=000000; P1=CD0000; P2=00CD00; P3=CDCD00;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
74 P4=0000EE; P5=CD00CD; P6=00CDCD; P7=E5E5E5;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
75 P8=7F7F7F; P9=FF0000; P10=00FF00; P11=FFFF00;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
76 P12=5C5CFF; P13=FF00FF; P14=00FFFF; P15=FFFFFF;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
77 shift;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
78 else # linux console
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
79 P0=000000; P1=AA0000; P2=00AA00; P3=AA5500;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
80 P4=0000AA; P5=AA00AA; P6=00AAAA; P7=AAAAAA;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
81 P8=555555; P9=FF5555; P10=55FF55; P11=FFFF55;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
82 P12=5555FF; P13=FF55FF; P14=55FFFF; P15=FFFFFF;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
83 [ "$1" = "--palette=linux" ] && shift
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
84 fi
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
85
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
86 [ "$1" = "--bg=dark" ] && { dark_bg=yes; shift; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
87
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
88 echo -n "<html>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
89 <head>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
90 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
91 <style type=\"text/css\">
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
92 .ef0,.f0 { color: #$P0; } .eb0,.b0 { background-color: #$P0; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
93 .ef1,.f1 { color: #$P1; } .eb1,.b1 { background-color: #$P1; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
94 .ef2,.f2 { color: #$P2; } .eb2,.b2 { background-color: #$P2; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
95 .ef3,.f3 { color: #$P3; } .eb3,.b3 { background-color: #$P3; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
96 .ef4,.f4 { color: #$P4; } .eb4,.b4 { background-color: #$P4; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
97 .ef5,.f5 { color: #$P5; } .eb5,.b5 { background-color: #$P5; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
98 .ef6,.f6 { color: #$P6; } .eb6,.b6 { background-color: #$P6; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
99 .ef7,.f7 { color: #$P7; } .eb7,.b7 { background-color: #$P7; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
100 .ef8, .f0 > .bold,.bold > .f0 { color: #$P8; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
101 .ef9, .f1 > .bold,.bold > .f1 { color: #$P9; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
102 .ef10,.f2 > .bold,.bold > .f2 { color: #$P10; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
103 .ef11,.f3 > .bold,.bold > .f3 { color: #$P11; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
104 .ef12,.f4 > .bold,.bold > .f4 { color: #$P12; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
105 .ef13,.f5 > .bold,.bold > .f5 { color: #$P13; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
106 .ef14,.f6 > .bold,.bold > .f6 { color: #$P14; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
107 .ef15,.f7 > .bold,.bold > .f7 { color: #$P15; font-weight: normal; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
108 .eb8 { background-color: #$P8; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
109 .eb9 { background-color: #$P9; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
110 .eb10 { background-color: #$P10; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
111 .eb11 { background-color: #$P11; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
112 .eb12 { background-color: #$P12; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
113 .eb13 { background-color: #$P13; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
114 .eb14 { background-color: #$P14; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
115 .eb15 { background-color: #$P15; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
116 "
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
117
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
118 # The default xterm 256 colour palette
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
119 for red in $(seq 0 5); do
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
120 for green in $(seq 0 5); do
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
121 for blue in $(seq 0 5); do
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
122 c=$((16 + ($red * 36) + ($green * 6) + $blue))
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
123 r=$((($red * 40 + 55) * ($red > 0)))
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
124 g=$((($green * 40 + 55) * ($green > 0)))
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
125 b=$((($blue * 40 + 55) * ($blue > 0)))
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
126 printf ".ef%d { color: #%2.2x%2.2x%2.2x; } " $c $r $g $b
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
127 printf ".eb%d { background-color: #%2.2x%2.2x%2.2x; }\n" $c $r $g $b
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
128 done
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
129 done
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
130 done
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
131 for gray in $(seq 0 23); do
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
132 c=$(($gray+232))
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
133 l=$(($gray*10 + 8))
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
134 printf ".ef%d { color: #%2.2x%2.2x%2.2x; } " $c $l $l $l
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
135 printf ".eb%d { background-color: #%2.2x%2.2x%2.2x; }\n" $c $l $l $l
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
136 done
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
137
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
138 echo -n '
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
139 .f9 { color: '`[ "$dark_bg" ] && echo "#$P7;" || echo "#$P0;"`' }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
140 .b9 { background-color: #'`[ "$dark_bg" ] && echo $P0 || echo $P15`'; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
141 .f9 > .bold,.bold > .f9, body.f9 > pre > .bold {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
142 /* Bold is heavy black on white, or bright white
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
143 depending on the default background */
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
144 color: '`[ "$dark_bg" ] && echo "#$P15;" || echo "#$P0;"`'
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
145 font-weight: '`[ "$dark_bg" ] && echo 'normal;' || echo 'bold;'`'
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
146 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
147 .reverse {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
148 /* CSS doesnt support swapping fg and bg colours unfortunately,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
149 so just hardcode something that will look OK on all backgrounds. */
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
150 '"color: #$P0; background-color: #$P7;"'
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
151 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
152 .underline { text-decoration: underline; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
153 .line-through { text-decoration: line-through; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
154 .blink { text-decoration: blink; }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
155
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
156 </style>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
157 </head>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
158
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
159 <body class="f9 b9">
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
160 <pre>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
161 '
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
162
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
163 p='\x1b\[' #shortcut to match escape codes
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
164 P="\(^[^°]*\)¡$p" #expression to match prepended codes below
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
165
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
166 # Handle various xterm control sequences.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
167 # See /usr/share/doc/xterm-*/ctlseqs.txt
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
168 sed "
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
169 s#\x1b[^\x1b]*\x1b\\\##g # strip anything between \e and ST
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
170 s#\x1b][0-9]*;[^\a]*\a##g # strip any OSC (xterm title etc.)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
171
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
172 #handle carriage returns
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
173 s#^.*\r\{1,\}\([^$]\)#\1#
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
174 s#\r\$## # strip trailing \r
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
175
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
176 # strip other non SGR escape sequences
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
177 s#[\x07]##g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
178 s#\x1b[]>=\][0-9;]*##g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
179 s#\x1bP+.\{5\}##g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
180 s#${p}[0-9;?]*[^0-9;?m]##g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
181
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
182 #remove backspace chars and what they're backspacing over
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
183 :rm_bs
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
184 s#[^\x08]\x08##g; t rm_bs
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
185 " |
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
186
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
187 # Normalize the input before transformation
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
188 sed "
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
189 # escape HTML
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
190 s#\&#\&amp;#g; s#>#\&gt;#g; s#<#\&lt;#g; s#\"#\&quot;#g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
191
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
192 # normalize SGR codes a little
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
193
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
194 # split 256 colors out and mark so that they're not
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
195 # recognised by the following 'split combined' line
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
196 :e
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
197 s#${p}\([0-9;]\{1,\}\);\([34]8;5;[0-9]\{1,3\}\)m#${p}\1m${p}¬\2m#g; t e
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
198 s#${p}\([34]8;5;[0-9]\{1,3\}\)m#${p}¬\1m#g;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
199
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
200 :c
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
201 s#${p}\([0-9]\{1,\}\);\([0-9;]\{1,\}\)m#${p}\1m${p}\2m#g; t c # split combined
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
202 s#${p}0\([0-7]\)#${p}\1#g #strip leading 0
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
203 s#${p}1m\(\(${p}[4579]m\)*\)#\1${p}1m#g #bold last (with clr)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
204 s#${p}m#${p}0m#g #add leading 0 to norm
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
205
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
206 # undo any 256 color marking
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
207 s#${p}¬\([34]8;5;[0-9]\{1,3\}\)m#${p}\1m#g;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
208
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
209 # map 16 color codes to color + bold
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
210 s#${p}9\([0-7]\)m#${p}3\1m${p}1m#g;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
211 s#${p}10\([0-7]\)m#${p}4\1m${p}1m#g;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
212
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
213 # change 'reset' code to a single char, and prepend a single char to
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
214 # other codes so that we can easily do negative matching, as sed
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
215 # does not support look behind expressions etc.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
216 s#°#\&deg;#g; s#${p}0m#°#g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
217 s#¡#\&iexcl;#g; s#${p}[0-9;]*m#¡&#g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
218 " |
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
219
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
220 # Convert SGR sequences to HTML
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
221 sed "
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
222 :ansi_to_span # replace ANSI codes with CSS classes
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
223 t ansi_to_span # hack so t commands below only apply to preceeding s cmd
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
224
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
225 /^[^¡]*°/ { b span_end } # replace 'reset code' if no preceeding code
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
226
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
227 # common combinations to minimise html (optional)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
228 s#${P}3\([0-7]\)m¡${p}4\([0-7]\)m#\1<span class=\"f\2 b\3\">#;t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
229 s#${P}4\([0-7]\)m¡${p}3\([0-7]\)m#\1<span class=\"f\3 b\2\">#;t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
230
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
231 s#${P}1m#\1<span class=\"bold\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
232 s#${P}4m#\1<span class=\"underline\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
233 s#${P}5m#\1<span class=\"blink\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
234 s#${P}7m#\1<span class=\"reverse\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
235 s#${P}9m#\1<span class=\"line-through\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
236 s#${P}3\([0-9]\)m#\1<span class=\"f\2\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
237 s#${P}4\([0-9]\)m#\1<span class=\"b\2\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
238
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
239 s#${P}38;5;\([0-9]\{1,3\}\)m#\1<span class=\"ef\2\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
240 s#${P}48;5;\([0-9]\{1,3\}\)m#\1<span class=\"eb\2\">#; t span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
241
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
242 s#${P}[0-9;]*m#\1#g; t ansi_to_span # strip unhandled codes
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
243
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
244 b # next line of input
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
245
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
246 # add a corresponding span end flag
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
247 :span_count
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
248 x; s/^/s/; x
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
249 b ansi_to_span
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
250
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
251 # replace 'reset code' with correct number of </span> tags
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
252 :span_end
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
253 x
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
254 /^s/ {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
255 s/^.//
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
256 x
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
257 s#°#</span>°#
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
258 b span_end
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
259 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
260 x
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
261 s#°##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
262 b ansi_to_span
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
263 " |
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
264
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
265 # Convert alternative character set
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
266 # Note we convert here, as if we do at start we have to worry about avoiding
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
267 # conversion of SGR codes etc., whereas doing here we only have to
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
268 # avoid conversions of stuff between &...; or <...>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
269 #
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
270 # Note we could use sed to do this based around:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
271 # sed 'y/abcdefghijklmnopqrstuvwxyz{}`~/▒␉␌␍␊°±␤␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥π£◆·/'
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
272 # However that would be very awkward as we need to only conv some input.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
273 # The basic scheme that we do in the python script below is:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
274 # 1. enable transliterate once ¡ char seen
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
275 # 2. disable once µ char seen (may be on diff line to ¡)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
276 # 3. never transliterate between &; or <> chars
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
277 sed "
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
278 # change 'smacs' and 'rmacs' to a single char so that we can easily do
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
279 # negative matching, as sed does not support look behind expressions etc.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
280 # Note we don't use ° like above as that's part of the alternate charset.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
281 s#\x1b(0#¡#g;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
282 s#µ#\&micro;#g; s#\x1b(B#µ#g
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
283 " |
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
284 (
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
285 python -c "
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
286 # vim:fileencoding=utf8
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
287
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
288 import sys
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
289 import locale
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
290 encoding=locale.getpreferredencoding()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
291
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
292 old='abcdefghijklmnopqrstuvwxyz{}\`~'
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
293 new='▒␉␌␍␊°±␤␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥π£◆·'
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
294 new=unicode(new, 'utf-8')
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
295 table=range(128)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
296 for o,n in zip(old, new): table[ord(o)]=n
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
297
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
298 (STANDARD, ALTERNATIVE, HTML_TAG, HTML_ENTITY) = (0, 1, 2, 3)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
299
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
300 state = STANDARD
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
301 last_mode = STANDARD
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
302 for c in unicode(sys.stdin.read(), encoding):
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
303 if state == HTML_TAG:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
304 if c == '>':
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
305 state = last_mode
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
306 elif state == HTML_ENTITY:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
307 if c == ';':
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
308 state = last_mode
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
309 else:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
310 if c == '<':
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
311 state = HTML_TAG
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
312 elif c == '&':
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
313 state = HTML_ENTITY
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
314 elif c == u'¡' and state == STANDARD:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
315 state = ALTERNATIVE
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
316 last_mode = ALTERNATIVE
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
317 continue
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
318 elif c == u'µ' and state == ALTERNATIVE:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
319 state = STANDARD
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
320 last_mode = STANDARD
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
321 continue
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
322 elif state == ALTERNATIVE:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
323 c = c.translate(table)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
324 sys.stdout.write(c.encode(encoding))
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
325 " 2>/dev/null ||
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
326 sed 's/[¡µ]//g' # just strip aternative flag chars
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
327 )
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
328
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
329 echo "</pre>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
330 </body>
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
331 </html>"