Inside-out templates | Honza Pokorný

submited by
Style Pass
2024-11-19 00:30:05

I have been thinking about literate programming and markup languages for authoring documents. How can we write a document once and easily publish it in different formats? Markdown is user-friendly but bare-bones. XML does what we want but no one wants to write XML. I was reminded of Pollen. It promises to do what I want. Plus it’s a cool Lisp thing. But Lisp isn’t everyone’s thing. Then I had a flash of inspiration: what if we could abuse some Go templates?

Pretty standard Go stuff. Read the file, create a new template, and print the result to standard output. But what is this funcs? You can define custom functions that will be available in your template (our document). This is where we can define our title and link functions. Let’s say we want to publish our chapter as HTML:

It’s much nicer than using some custom string search and replace because the Go standard library handles all of the replacing for you: they have done the hard work of finding the edge-cases for you and handling them.

Leave a Comment