Copy colorful terminal (emulator) text output and paste as HTML?

12

10

I want to write notes (in HTML format) when learning Linux. When I need to note colorful terminal text output, I want the color of terminal text can be remained in my notes, e.g.:

Note 1: Test

The output of xx command is

<pre>
[root@webserver ~]# ll /
total 100
drwxrwxrwt.   7 root root  4096 8月  24 12:22 <span style='background-color:green; color:blue;'>tmp</span>
drwxr-xr-x.  14 root root  4096 7月  19 21:20 <span style='color:blue;'>usr</span>
</pre>

I can manually add HTML code wrapper around the plain text, but I hope there's an automatic way to achieve that.

Currently, I'm using putty on Windows to connect to remote CentOS linux server.

LiuYan 刘研

Posted 2012-08-24T06:52:46.280

Reputation: 1 929

you want to copy data from terminal to buffer and when you paste it in notepad you expected to seen html near color words? – Ishikawa Yoshi – 2012-08-24T07:35:17.337

yes, that's what i wanted. i think this can be done in terminal emulator software (translate control characters into HTML), but i don't know if there's such emulator has such function. – LiuYan 刘研 – 2012-08-24T07:41:05.030

2http://superuser.com/questions/110902/colored-copy-of-putty-window – Ishikawa Yoshi – 2012-08-24T07:48:54.660

thanks for the link, i didn't notice that option before. however that's a slight different question, i want the paste to be HTML code, not rich text format (and my RTF paste seems malformed characters). – LiuYan 刘研 – 2012-08-24T08:27:38.263

1http://stackoverflow.com/questions/2033268/linux-shell-output-to-html maybe this link also helps you – Ishikawa Yoshi – 2012-08-24T08:38:41.277

maybe i need post a feature request to putty – LiuYan 刘研 – 2012-11-01T10:06:45.070

Answers

15

Use the script utility to capture the terminal output to a file (ansi color control characters included) and then convert it to html with ansi2html script. Eg:

inigo:tmp> script
Script started, file is typescript
inigo:tmp> ls
#
# lots of colour output
# 
inigo:tmp> exit
exit
Script done, file is typescript
inigo:tmp> cat typescript  | ansi2html.sh > typescript.html

If using PuTTY, "All session output" login option will also keep the ansi color codes.

As an alternative to ansi2html, you could use aha - Ansi HTML Adapter.

iamc

Posted 2012-08-24T06:52:46.280

Reputation: 354