psobot / keynote-parser

A packer/unpacker for Apple Keynote presentation files.
158 stars 17 forks source link

Add hyperlinks and apply syntax highlighting to code examples #12

Closed feeley closed 4 years ago

feeley commented 4 years ago

I've just discovered keynote-parser and would like to use it to automate some processing of my slides.

Some context... I teach introductory programming using the JavaScript language, the codeBoot interpreter (http://codeboot.org) and slides prepared with Keynote. The slides contain JavaScript code examples that can be run in codeBoot by simply clicking on them. Here's an example slide (.pdf and .key) :

What I would like to do with keynote-parser is

1) create the hyperlinks on the examples automatically (so they match the code)

2) apply syntax highlighting to the code

3) extract the examples and run them in nodejs to validate their correctness

This is probably not possible with keynote-parser in its current state, but could you give me some guidance on how this could be done?

Note that the hyperlinks are essentially a base64 encoding of the content of the example with a few codeBoot specific commands to create the source code file in the codeBoot environment. For the example above the hyperlink encodes the file content after the REPLAY= part:

http://codeboot.org/query.cgi?REPLAY=QENrZXlub3RlLXRlc3QuanNAMC8vIHRoaXMgaXMgYSB0ZXN0IGZvciBrZXlub3RlLXBhcnNlckBOLy8gY2xpY2sgbWUgdG8gcnVuIGluIGNvZGVCb290QE5ATnByaW50KCdiZWdpbiB0ZXN0Jyk7QE5ATnZhciBuYW1lID0gcHJvbXB0KCd5b3VyIG5hbWUgcGxlYXNlJyk7QE5ATnByaW50KCdoZWxsbyAnICsgbmFtZSk7QEU=
psobot commented 4 years ago

Hey Marc! Glad to see you're interested.

As you've probably seen in the readme, keynote-parser does simple text replacement and handles unpacking/repacking of Keynote files. What's done to those files once they're unpacked is up to the user. Most custom use cases should be handleable by running keynote-parser unpack, running your own code to edit the resulting yaml files, then running keynote-parser pack again (or doing the equivalent in your own Python script by importing keynote_parser).

To break down each of your points:

  1. While it currently doesn't support rich text replacement, it would be possible to add that functionality. Keynote uses a proprietary encoding scheme for text style, rather than something like RTF or HTML, so you'd have to write something that's aware of this encoding. I haven't tried replacing the target of a hyperlink in a presentation yet, but in theory it should be possible as the target is likely just stored as an attribute alongside the text.
  2. Syntax highlighting sounds like it'd be reasonably out of scope for this library - although if rich text replacement were added to keynote-parser, you'd be able to use any other syntax highlighting library and then could give the resulting rich text to keynote-parser to insert into a presentation.
  3. Extracting code examples from a Keynote slide is possible at the moment, although depending on how the examples are rendered (i.e.: as one text box or multiple) you may have to piece together multiple nodes from the resulting yaml file to reconstruct the code. Again, running the extracted code in nodejs would be something your code could do, after importing keynote-parser to use its unpacking functionality.

If you're interested in diving into this library to help add rich text replacement and hyperlink replacement functionality, contributions would be much appreciated!

feeley commented 4 years ago

Thanks for your quick and informative reply. I'm also exploring the possibility of creating the keynote slides for the examples using AppleScript to simplify the creation of character styles, etc. I'm not excluding the use of keynote-parser for some operations, but it seems a bit too "low-level" (sort of like patching a binary executable rather than editing the source code of a program and recompiling it). In particular the creation of new character styles by modifying the .yaml files is too complex as keynote uses its own identifiers for resources and I don't know how to generate them without corrupting the keynote format.