TLWIR Special: Formatting Your Code in GNU/Linux With Highlight

by Rex Djere on February 12, 2012 · 6 comments

in TLWIR

Post image for TLWIR Special: Formatting Your Code in GNU/Linux With Highlight

Summary: As a C++ programmer, I like to program coding examples, and then paste them into tutorials on websites. I used to use web based solutions to format and highlight my code. These solutions usually have you paste the code into their web page. The web page then color-highlights the code, and allows you to download the formatted result. This can be a pretty good solution, but I really wanted a solution that I could use on my GNU/Linux PCs. Highlight is a wonderful program that provided the perfect solution. Highlight isn’t just for highlighting C++ source code; it can handle PHP, C, C#, Perl, Python, or just about any programming language that you can throw at it. In this article, I will show you how to use Highlight to format your code perfectly every time.

Why Would You Want to Format Your Code?
Raw C++ code can look somewhat unattractive when displayed on a web page. Let me give you a simple example of an unformatted C++ program.

// a simple C++ multiplication program

#include <iostream>
using namespace std;

int main ()
{
float a;
float b;

cout << “Please enter a number: “;
cin >> a;
cout << “Please enter a second number: “;
cin >> b;
float product = a * b;
cout << endl;
cout << a << ” times ” << b << ” equals “<< product << “.” << endl;
return 0;
}
Figure 1: A Simple C++ Multiplication Program – Unformatted

Figure 1 above is pretty boring. The lack of color and highlighting makes the code look pretty uninteresting. Wouldn’t it be great if we could add some pizazz to the code to make it look a little bit more appealing? That is exactly what Highlight will allow us to do. Highlight is available for most GNU/Linux distros. I will use Highlight version 3.6 on Fedora 16 for the example in this article. However, before we look at the highlighted version of the code, let us put a spotlight on RUNNING the code on a GNU/Linux system. For this, we will use my favorite GNU/Linux C++ IDE, Code::Blocks. Figure 2 below is our code pasted into a Code::Blocks 10.05 window.


Figure 2: C++ Multiplication Program in Code::Blocks

As you can see, Code::Blocks does format the C++ code and apply colors to it. Unfortunately, we can’t export that formatting to a file for display on a web page. But we can compile and run the code, as seen in Figure 3 below.

Figure 3: Running Our C++ Multiplication Program

So how do we highlight our code? The first step is to save the code in a file. By default, I like to make my C++ files have a .cpp extension, so I have called the file multp.cpp. We will then open the file in Highlight. Figure 4 below shows our code opened in Highlight.

Figure 4: C++ Multiplication Program Opened in Highlight

As you can see, Highlight has already highlighted the code. Now, I just have to hit the “Convert” button to have Highlight output the HTML that I can paste into my web page.

// a simple C++ multiplication program

#include <iostream>
using namespace std;

int main ()
{
   float a;
   float b;

   cout << "Please enter a number: ";
   cin >> a;
   cout << "Please enter a second number: ";
   cin >> b;
   float product = a * b;
   cout << endl;
   cout << a << " times " << b << " equals "<< product << "." << endl;
   return 0;
}

Figure 5: C++ Multiplication Code Highlighted and Converted to HTML by Highlight

Figure 5 above is the final result: beautifully formatted C++ code that we can paste into any web page. The nice thing is that you can paste the output into a word processor, and the formatting will be retained. I pasted it into both LibreOffice Writer, and Google Docs Writer, and both recognized the formatting. I encourage you to play with Highlight to obtain different effects. There are a lot of output color schemes, and options.

Thank you for reading this The Linux Week in Review special. I look forward to seeing you in the next edition!

{ 5 comments }

NULL February 12, 2012 at 8:25 pm

for vim users, vim also a :TOhtml command

Jens February 13, 2012 at 1:08 am

go for doxygen. It can highlight, make www and tex, do linkning sorting etc etc etc

IGnatius T Foobar February 13, 2012 at 8:03 am

Correction: you mean “the Linux Operating System.” The phrase “GNU/Linux” is intellectual snobbery from Richard Stallman and nothing more.

Rex Djere February 13, 2012 at 1:26 pm

@Ignatius No, I meant GNU/Linux. I believe in the conceptual reason behind calling it GNU/Linux, so I call it GNU/Linux. However, I respect those that choose to call it Linux because I realize that they came to calling it that through a process. I believe deeply in the ability and right of every individual to come to his or her own conclusions. The only person that I do not respect is the person that does not have the strength to stick by their convictions. Hence I will continue to call it GNU/Linux.

Rex Djere February 13, 2012 at 1:38 pm

@Jens Thanks for the info! I’ll check out doxygen.

{ 1 trackback }

Previous post:

Next post: