Create syntax highlighted PDF and HTML files from C or perl

2009/10/05 by Girish Venkatachalam

Girish Venkatachalam is a UNIX hacker with more than a decade of networking and crypto programming experience. His hobbies include yoga,cycling, cooking and he runs his own business. Details here:

People who have a necessity to publish source code for public view like myself and Tony will benefit by this article.

It is rather funny since I have published a lot of code in this very site without syntax highlighting. Perhaps things will change with this newfound wisdom.

Let me quickly cut to the chase. This article comes close on the heels of my Vim article and you shall see in a moment why.

Both my tips involve Vim and its genius is doing a perfect job of this. Let us see for ourselves. We are going to use a C file sqlite2.c for this purpose.

$ vim sqlite2.c
:hardcopy >

gets us the file which is a postscript file. If you open it with gv you find that it is neatly syntax highlighted. This will only happen if you have it enabled in your vimrc like this:

sy on
se nu
se textwidth=72
se spell spelllang=en_us
noremap  :se nospell

Now that is an interesting vimrc. But let us focus on the first line alone and get on with the article. The other way to do it is by opening the file in vim and then highlighting the syntax manually with the colon command ":sy on".

But we are not very happy with this for two reasons. It is in ps format which is fine for academic papers and not for Internet publishing. And the second problem with it is that it not suitable for batch conversions. Hackers are incredibly lazy individuals who want the computer to work for them. They don't want to sit and do the job.

What is the way out?

It turns out that there are two ways. One way is interesting. The other a bit boring but both work well.

$ vim -me -e -c ":hardcopy >" -c ":q" sqlite2.c

works but this is a better way.

$ vim -e -s sqlite2.c < hardcopy.vim

$ cat hardcopy.vim
:hardcopy >

It is obvious why we prefer the second approach. The first approach takes a bunch of vim commands. You are limited to 10 and it is clumsy since you start vim and kill it!

The second approach is filled with UNIX genius. It has redirection and it is infinitely scalable since your one line vim script can do a lot more than this job alone.

Please spend an hour studying the tutorial for writing vim scripts by typing this colon command ":help script"

Vim scripting has object oriented programming, dictionaries arrays and of course loops.

But can't we do this with the shell?

for file in "foo.c foo.perl bar.c"
	vim -e -s $file < process.vim

Good. Now there is one itch remaining. How to convert the resulting ps file into PDF?

$ ps2pdf14
$ xpdf sqlite2.c.pdf

Happy? No? Then I have another trick for you.

Does looking at this PDF help?

There is a canned plugin that comes with Vim for html conversion. It is called TOhtml. Invoke it inside a vim session with ":TOhtml"

Now put two and two together and come up with your own way to convert C files into syntax highlighted html.

For those of you who hate white backgrounds there is a solution. You can set this in vim.

:se background=dark


Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Create syntax highlighted PDF and HTML files from C or perl


Increase ad revenue 50-250% with Ezoic

More Articles by © Girish Venkatachalam

Mon Oct 5 14:09:17 2009: 7073   TonyLawrence

It's so obvious how much easier syntax highlighting can make reading, writing, avoiding silly typo's - and yet I've never taken advantage of that.

In fact, when I've happened to be working somewhere where it is turned on by default, I have actually shut it off!

Mon Oct 5 14:24:32 2009: 7074   TonyLawrence

Also: I used that :TOhtml many years back.

It's pretty awful html :-)

Mon Oct 5 14:31:43 2009: 7075   MikeHostetler

GNU Enscript is what I usually use for this task:

Tony: I think once you get with syntax highlighting, you will never go back. But, yes, it's hard to get use to at first. What do you think of colored ls listings?

Mon Oct 5 14:52:00 2009: 7076   TonyLawrence

Don't get me started on Linux colorizing. I hate it, hate it, HATE IT.

Seriously: I do not like it and get very, very annoyed by it.

Mon Oct 5 16:07:40 2009: 7077   TonyLawrence

And most of all, I hate Vim coloring: (link)

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

The successful construction of all machinery depends on the perfection of the tools employed; and whoever is a master in the arts of tool-making possesses the key to the construction of all machines... The contrivance and construction of tools must therefore ever stand at the head of the industrial arts. (Charles Babbage)

This post tagged: