PowerPoint slides with code snippets24 Jul 2012
I've been in the process of creating some slides for teaching resources. This presented me with a couple of small issues with regards to displaying code snippets.
A well written code snippet can be worth a thousand words. Well written code communicates it's purpose much better than dull prose and gives the audience something to hang their hat on.
So what's the issue?
There are two main ways of getting code into a presentation.
- Take screen dumps from your favourite editor
- Write/insert the text into the slide
Of the two options I prefer #2 as #1 sounds like a lot of hard work (I won't even discuss it further). So let's concentrate on the issues with #2
- PowerPoint is terrible for code editing
- PowerPoint does not have syntax highlighting (nor should I expect it to have)
- I want to keep things DRY which means no copy and paste
- If the presentation is handed out I want the snippet to be selectable/copyable
Warning I consider this a fairly ad hoc solution but it seems to be doing the job for me for the time being until I rework it.
The general flow goes like this (50,000ft view)
- A VBA Macro loops through all the textfields in the whole presentation
- The very first text box has text like "base_url=..."
- This initiates a Rake task which processes all the code snippets
- Other text boxes may have text like "src=..."
- This causes the .rtf file to be copied to the paste board and then pasted into the textfield
Ok so I must admit this is most likely terrible VBA code but then again VBA was terrible to use (on a mac at least). The editor did not work like other editors. There was no help system and god forbid you try and look anything up on line - I didn't realise how spoiled I have been with Apple's documentation and documentation for Ruby.
Anyway rant over here is what I came up with (please don't judge me)
Now I will be the first to admit that this is butt ugly code, but I feel like I came out of the fight laughing as VBA finally did what I wanted it to do.
The work horse lines simply shell out.
In the above line I get myself into the right directory and then call rake.
In the above line I place the contents of a processed snippet onto the clipboard
Here's what I came up with
The end result of running this is that the whole file structure of one directory is duplicated with all the .h/.m in the original being process into rtf files. The source files are run through pygments which takes care of the syntax highlighting and output to rtf
The important switches for getting pygments to produce the right result are
-l objc # Give it some warning that it should use the appropriate lexer for Objective-C -f rtf # Tell pygments to give me rtf output
So with all this monkeying around we end up with a solution where I can hit play on a macro and all of my well placed
src=... tags will be substituted for code snippets stored nicely under git and easily amendable with my favourite editor. The experience of using VBA was not a pleasant one but once that was out of the way it was plain sailing - now I just need to write the content for the presentation.