I started thinking about Pandoc. Pandoc is this haskell library that makes miracles of text happen.
Got an org-mode file and need TeX? Done.
Got a markdown slideshow that needs to become a beamer slide show? OK, sure.
Fuck Beamer, how about markdown slides → Reveal.js slides? You bet your sweet sensual bologna.
install Pandoc…
Debian?
then add it to your path in your
.${SHELL}rc file:OSX?
Imma Use Pandoc…
Alright, so I’ve got tons of markdown files, fairly structured, with bunches of links and I need html5. I’ll create a
Makefile proof-of-concept for this:
Running
make takes my README.md and makes this:Title/Layout/CSS
So now that I’m outputting html, I still need to be able to:
- Configure a layout in which to render html
- Include a css file in said layout
- Add post metadata to my layout (e.g., title, headline, etc.)
Pandoc is able to do all of these things—easy-peasy-lemon-squeezy.
First, to establish a layout, let’s copy the default html5 layout file for Pandoc:
I’ll make some small tweaks to that file, keep the variables I need, ditch the variables I don’t need. Here is the html5 layout file I came up with:
Next, I need to figure out how to include a css stylesheet. A quick search for
css in pandoc(1) turns up the --css flag which enables you to link to a css stylesheet.
Updated
Makefile:
Finally, I need to be able to include a unique
<title> tag string for each page. Again, a search through pandoc(1) for variable yields results; using the -V flag enables you to set template variables using -V [KEY]=[val].
A bit more digging in the online docs, however, nets better solution: YAML Metadata blocks—just like jekyll!
So, for each markdown file in my repo, I’ll add a block to the top that looks like this:
$pagetitle$ is the variable I defined in my _layout.html5 that I’m now passing as a template to Pandoc.Makefile fanciness
Alright, so now that I have the basics of Pandoc down, I need to whip my
Makefile into shape.
First thing is I want to convert ALL of my markdown files to html, not just the
README.md. So howzabout I add a wildcard target for all the html files? Also, the whole point of this is to make a github pages site, so let’s add that to the Makefile too
Running
make at this point should checkout your current git repository to a subdirectory called gh-pages (which should be added to .gitignore on master).
This
Makefile first tries to checkout an existing gh-pages branch, otherwise it creates a new orphan branch for gh-pages. After that, we glob the current directory for any file name *.md and run it through pandoc, placing the result in gh-pages/[whatever].htmlNiceities
I’m a big fan of pre-processors, so the css/main.css file (which doesn’t actually exist as of yet) should be converted from
less. The easiest way to do that: add a package.json with less as a dependency.
Now running
npm install should create a new node_modules directory (which should be added to .gitignore on master). Now we need to add a lessc step to our Makefile.
Also, it’s always nice to have a
clean target in any Makefile
I’d also like to be able to preview before commiting this file by typing
make serve
Finally, speaking of commiting this file, let’s make
commit a target, too.
Now when I update my
links repo's markdown files I issue a simple series of commands: make checks-out my gh-pages branch and builds the html and css files, make serve lets me look at the output html at localhost:8000, and, finally, make commit pushes those changes live.
So here’s the result and the final
Makefile