Server Side Cheaters

Page content

Brett Terpstra rolled out Cheaters awhile ago. If you are unfamiliar, it is a system for viewing your own custom cheat sheets on the Mac. His version is very attractive and highly functional but requires a Mac to view and a method of creating HTML tables.1

Of course, I like to make things difficult. I want my cheat sheets available to me when I’m away from my Mac, so that means Cheaters will not work for me. I could upload HTML files to a web host but I want an automated system that allows me to create cheat sheets in MultiMarkdown tables and have the results automatically converted to static HTML on my web host.

Crib Sheets

The first half of the system is a Python script that operates on a MultiMarkdown file and converts it to HTML. Along the way, it injects some CSS styling to make something a bit more attractive than a standard HTML table. This is not impressive stuff.

The Python script is pretty long since it includes a bunch of default CSS.2 You can download the file here or view the code here.

The core functional piece of the script is one single line:

:::python
marktable = markdown.markdown(h, ['footnotes', 'fenced_code', 'tables'])

It uses the Python Markdown module with some of the extensions for things I might use in a cheat sheet. Crucially, the tables extension is not on in the module by default.

The script uses the Python argparse module to accept various parameters for generating the HTML. I went all out with this part so that I could adapt the system for use on my Mac, on my web host, and with an external CSS file. Here are the parameters (run python tableFromMMD.py -h to see the help).

:::text
-o <file_path>    Writes file to specified path. Must include file name
-b Opens the output HTML file in the default browser
-css <file_path>  Uses a css file from specified path instead of built-in. Must include file name

The “-browser” flag should only be used on a Mac.

Automated Builds

So now I have a way to generate a static HTML page from a MultiMarkdown table. Next, I needed a way to automate it server-side. Yay for bash and cron!

I created a short shell script for cron to execute every hour:

:::bash
#!/bin/bash
export LANG=en_US.UTF-8
cd /home/macdrifter/virt_env/pelican3.2/
source bin/activate
cd /home/macdrifter/cribsheet_RAW/
for f in *.txt
do 
    python tableFromMMD.py "$f" -css "sardineStyle.css" -o "/home/macdrifter/webapps/pelican/pages/cribsheets/$f"
done

It’s a dumb little script. It uses the same Python Virtual Environment that generates this site with Pelican. This environment has all of the dependencies like argparse and markdown already installed.

The directory /home/macdrifter/cribsheet_RAW/ contains my tableFromMMD.py script as well as the external CSS file. I’m also storing all of the cheat sheet markdown files in the same directory as .txt files.

The shell script looks for all of the .txt files in the directory and then converts each one to HTML using the Python script from above. Each HTML file is stored in a directory accessible through the apache service that is hosting Macdrifter.

Conclusion

The whole process turns this:

:::text
### FastMail ###

| **Keyboard Shortcut** |***Function*** |  
| --------- ||
| j/k   |select messages    |  
| o | Open  |  
| x |select checkbox    |  
| Enter | Open  |  
| n/p   |next/prev in conversation  |  
| d |delete |  
| c |compose    |  
| e |expand |  
| g |find mailbox folder and open   |  
| m |move messages in conversation  |  
| r |reply  |  
| space | scroll down   |  
| shift-so  |scroll up  |  
| / |message search |  
| ctrl-enter    | send message draft    | 
| u | Refresh Mailbox View | 


[FastMail Reference](http://fastmail.wikia.com/wiki/KeyboardShortcuts)

Into a page like this that is viewable from my Mac, Windows, iPhone and iPad. If I want to add or change a sheet, I can work in MultiMarkdown through SFTP on the server.

There’s still some tweaking to do, but so far this is close to what I wanted. It’s not as pretty or densely packed with information as Brett’s version, but if I want to change the table styling, I just need to change my external CSS file and wait for the cron job to run regenerate the HTML file.


  1. Brett’s Marked.app is a great way to turn MultiMarkdown into beautiful stand-alone HTML files for use with Cheaters. ↩︎

  2. I’m using the CSS found here. It’s simple, compact and attractive. ↩︎