oscartheduckin’ around

May 9, 2008

code -> pdf

Filed under: how to — Tags: , , — oscartheduck @ 2:59 pm

I’m on my way out right now, but I’m going to write up a script tonight called code2pdf, and wanted to write down the history of it before it leaves my head.

I have been taking a CS class this semester, and have been writing code as a consequence. The requirements for submitting this code are twofold:

1. pop all code and a scriptfile proving the code works on a USB stick

2. print out copies of all code and the scriptfile and submit with above.

Now, most of the folks are using some kind of windows kludge to write their code, so they were taking screenshots of command windows and handing that in as a script file. I just used script, because I figured that’s what it’s there for. The problems were twofold:

1. I don’t have a printer in my house.

2. The printers at school are all connected to windows boxes.

So when I come in with a nice raw text file and attempt to print it, it looks _awful_. Same goes for code; my text files looked horrible.

But my instructor is a unix geek, so she didn’t mind at first. However, the driver being used to run the printer has a quirk in it; it has its own definition of what a tab is. I couldn’t believe it; my nice sensible “two spaces to a tab” code was running and running and running over line after line of blank space. Which didn’t worry me, but it made trying to read the code a horrible task for the instructor. True to her form, she mildly pointed this out to me and asked me to work on the issue, no fuss just a pleasant request. Which is the best way to get results.

So, I started thinking about it. First things first, I stopped using tabs and started using spaces. The printouts looked a lot better from that alone.

But I decided not to stop there; my instructor had been polite, and I have the power of FreeBSD available. I installed OpenOffice from the port (which took ages), after which my hard drive killed itself. So I installed anew and installed OpenOffice again, which took forever again. And I copied my code into OpenOffice and output it as a .pdf.

.pdf rules. I format things nicely, go through the file and make sure everything’s pretty and I’m happy as a clam at high tide. The script file was more difficult than the code, funnily enough, as openoffice is smart enough to read the .txt extension, scan the interior and then kindly offer to format everything as a spreadsheet.

Remove the extension and open the file again, and a different preprocessor asks for advice. I told it to use luxi mono as the font, and everything looks pretty. From there to .pdf is as simple as using the filter.

But why stop there? With the ports system at my fingertips, I can do anything! And I don’t want to start up openoffice just to spew out a .pdf file, more importantly. After a couple of creative searches, I foundĀ  a beautiful program called highlight, which takes code in and spews out one of several forms, HTML, rtf, TeX, LaTeX, XML, a few others.

I export everything to .rtf and then open it in openoffice, just to take a look. It looks great; colour code highlighting, the works. Export to pdf, to make sure I have it in time for my deadline, and then get back to work.

I export everything as TeX, then use pdftex and it comes out as a gorgeous looking pdf, but only in greyscale. I wanted to hand in colour, so I didn’t worry about this and instead used openoffice, but tonight I’ll be looking for a .rtf to .pdf filter on the command line so I can deal with everything and get colour output.

Whether I find it or not, I’ll write up a quick script to take in code and output a greyscale pdf file. Then I’ll investigate script files, which I imagine will be as simple as txt2pdf or something named similarly. Then I’ll submit it as a port; it’ll be a front end to other tools, but I think the ability to ensure my code looked gorgeous from any operating system and printed out correctly was sufficiently useful that other students would like the same thing. And lo! It will be called something witty.

I’m thinking it should be a simple thing that offers flags to output colour or greyscale pdfs (eventually), and also can handle script files. Pretty code all day long.


Blog at WordPress.com.