PowerPoint slides with code snippets

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.

##Why bother?

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.

  1. Take screen dumps from your favourite editor
  2. 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

##Solution

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)

##VBA script

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)

Sub Build()
    
  For Each sld In ActivePresentation.Slides
    For Each sh In sld.Shapes
      If sh.HasTextFrame Then
            
        Dim contents As String: contents = sh.TextFrame.TextRange.text
                
        Dim baseDir As String:
                
        If InStr(LCase(contents), "base_dir=") Then
          baseDir = Right(contents, Len(contents) - 9) 'Trim base_dir=
                    
          MacScript ("do shell script ""cd " & baseDir & " && rake"" ") 'Run Rake
        End If
                
        If InStr(LCase(contents), "src=") Then
                
          Dim source As String: source = baseDir & "rtf/" & Right(contents, Len(contents) - 4) 'Trim src=
                    
          MacScript ("do shell script ""cat " & source & " | pbcopy"" ")
        
          With sh.TextFrame.TextRange
            .Paste
            With .Font
              .Name = "monaco"
              .Size = 14
            End With
          End With
                    
        End If
                
      End If
    Next
  Next
    
  ActivePresentation.SaveAs (ActivePresentation.path & ":Processed" & ActivePresentation.Name)
    
End Sub

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.

MacScript ("do shell script ""cd " & baseDir & " && rake""")

In the above line I get myself into the right directory and then call rake.

MacScript ("do shell script ""cat " & source & " | pbcopy""")

In the above line I place the contents of a processed snippet onto the clipboard

##Rake

The rake task is heavily based on what Jim Weirich presented in his talk Power Rake. This is a great talk and I learned a lot so I would recommend you head over and give it a look.

Here’s what I came up with

SOURCE = FileList["source/**/*.[mh]"]
RTF    = SOURCE.pathmap("%{^source,rtf}d/%n%x")

RTF.zip(SOURCE).each do |target, source|
  containing_dir = target.pathmap("%d")
  directory containing_dir
  
  file target => [containing_dir, source] do
    sh "/usr/local/bin/pygmentize -f rtf -l objc -O \"style=xcode,fontface=Monaco\" #{source} > #{target}"
  end
end

task :format => RTF
task :default => :format

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

##Conclusion

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.