User avatar
HermannSW
Posts: 4829
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

C/C++/... TOhtml (vim)

Thu Aug 12, 2021 6:32 pm

At end of this posting I mentioned that I use "vi" editor since 1990:
viewtopic.php?f=33&t=317340&p=1901106#p1901106
And that "vi" is "vim" (vi improved) these days.
And that "vim" on Raspberry has 600(!) syntax files:

Code: Select all

pi@raspberrypi400:~ $ ls -l /usr/share/vim/vim81/syntax/*.vim| wc --lines
600
pi@raspberrypi400:~ $ ls -l /usr/share/vim/vim81/syntax/ | grep cpp
-rw-r--r-- 1 root root   2869 Jun 15  2019 cpp.vim
-rw-r--r-- 1 root root    600 Jun 15  2019 objcpp.vim
pi@raspberrypi400:~ $ ls -l /usr/share/vim/vim81/syntax/?.vim
-rw-r--r-- 1 root root  4902 Jun 15  2019 /usr/share/vim/vim81/syntax/b.vim
-rw-r--r-- 1 root root 26451 Jun 15  2019 /usr/share/vim/vim81/syntax/c.vim
-rw-r--r-- 1 root root 28631 Jun 15  2019 /usr/share/vim/vim81/syntax/d.vim
-rw-r--r-- 1 root root  9375 Jun 15  2019 /usr/share/vim/vim81/syntax/j.vim
-rw-r--r-- 1 root root 14730 Jun 15  2019 /usr/share/vim/vim81/syntax/r.vim
pi@raspberrypi400:~ $ 

I remembered that "vim" (after "sudo upt install vim") allows to export HTML files with syntax highlighting.
And I found an old bash script that makes using that very easy:

Code: Select all

pi@raspberrypi400:~ $ cat .local/bin/TOhtml 
#!/bin/bash
vim $1 +TOhtml +w +q +q
sed 's@\(<font face="monospace">\)@\1<table border="1" bgcolor="#000000"    '\
'style="color:#ffffff"><tr><td>@;s@\(</font></body>\)@</td></tr></table>\1@' \
$1.html -i
pi@raspberrypi400:~ $ 

This tool can be appliad to itself:

Code: Select all

pi@raspberrypi400:~ $ TOhtml .local/bin/TOhtml 
pi@raspberrypi400:~ $ 
The generated HTML file looks like this in browser:
TOhtml.html.png
TOhtml.html.png
TOhtml.html.png (16.65 KiB) Viewed 750 times

This is application for simple C file, as said 600 syntaxes are known:

Code: Select all

pi@raspberrypi400:~ $ TOhtml cgt.c
pi@raspberrypi400:~ $ 
Browser screenshot:
cgt.c.html.png
cgt.c.html.png
cgt.c.html.png (26.65 KiB) Viewed 750 times
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

GlowInTheDark
Posts: 2098
Joined: Sat Nov 09, 2019 12:14 pm

Re: C/C++/... TOhtml (vim)

Thu Aug 12, 2021 10:32 pm

I used to use this a lot back in the day to make pretty (colorful) printouts of my source code. I would use the 2html script to produced HTML, then use a browser to print that.

Nowadays, I use the :hardcopy command, which produces a pretty (colorful) PostScript file of your source code. It seems a little easier to use than 2html, but I assume the underlying engine is much the same.

Actually, I then use ps2pdf to produce a PDF as the final output of the process.
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

User avatar
HermannSW
Posts: 4829
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: C/C++/... TOhtml (vim)

Fri Aug 13, 2021 12:05 am

Interesting, I have not found "2html", but found and installed "c2html".
Output looks similar, but manpage states that only 4 languages are supported (C, Java, Pascal, perl).

TOhtml makes use of the 600 syntax files of vim and allows to syntax highlight nearly everything.
One of those syntax files is for Linux arbitrary precision calculator "bc", here syntax highlighted with TOhtml:
fac.bc.html.png
fac.bc.html.png
fac.bc.html.png (5.6 KiB) Viewed 648 times


Regarding my previous posting, the sed command did not match modern TOhtml output anymore.
I fixed that, it basically creates black background, inside a table with border=1:

Code: Select all

#!/bin/bash
vim $1 +TOhtml +w +q +q
sed "s/^\(pre.*\)color: #000000.*/\1color: #ffffff; background-color: #000000; }/;"\
"s/^<body>/<body><table border=1><tr><td>/;"\
"s/^<\/body>/<\/td><\/tr><\/table><\/body>/"\
 $1.html -i

The same cgt.c output from previous posting looks like this:
(you have to decide whether you prefer unchanged TOhtml output shown in previous posting or this version)
cgt.c.html.new.png
cgt.c.html.new.png
cgt.c.html.new.png (27.64 KiB) Viewed 648 times

I used those black background TOhtml outputs in my 268 IBM developerWorks blog postings.
They are gone since IBM deleted all developerWorks blogs and forums end of 2019.
Here is one posting I republished, with two examples of TOhtml with black background output:
https://stamm-wilbrandt.de/en/blog/Gate ... 20AES.html

They are HTML (different to the screenshots I did post here), so you can copy&past parts from them.
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 4829
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: C/C++/... TOhtml (vim)

Fri Aug 13, 2021 7:52 am

The colors are not as I want them for dark background.
I investigated, and without specifying whether background is dark or bright, vim guesses based on terminal settings.
I eliminated the guessing by providing two TOhtml versions, explicitely setting (vim) background and then doing the right stuff.

This is with bright background:
https://gist.github.com/Hermann-SW/3609 ... 18de5ee6c8

Code: Select all

pi@raspberrypi400:~ $ cat ~/.local/bin/TOhtml_
#!/bin/bash
vim -c "set background=bright" $1 +TOhtml +w +q +q
sed "s/^<body>/<body><table border=1><tr><td>/;"\
"s/^<\/body>/<\/td><\/tr><\/table><\/body>/"\
 $1.html -i
pi@raspberrypi400:~ $ 
cgt.c.bright.png
cgt.c.bright.png
cgt.c.bright.png (27.23 KiB) Viewed 592 times

And this is with dark background (my favorite), with colors for dark background:
https://gist.github.com/Hermann-SW/02d4 ... f47fa2f81d

Code: Select all

pi@raspberrypi400:~ $ cat ~/.local/bin/TOhtml
#!/bin/bash
vim -c "set background=dark" $1 +TOhtml +w +q +q
sed "s/^\(body.*\)color: #ffffff.*/\1color: #000000; background-color: #ffffff; }/;"\
"s/^<body>/<body><table border=1><tr><td>/;"\
"s/^<\/body>/<\/td><\/tr><\/table><\/body>/"\
 $1.html -i
pi@raspberrypi400:~ $ 
cgt.c.dark.png
cgt.c.dark.png
cgt.c.dark.png (27.29 KiB) Viewed 592 times
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Return to “C/C++”