I have been puzzling a little with creating a PR for this issue: https://github.com/spf13/hugo/issues/565
Basically a refactoring of when/how the short codes are rendered, and also includes the new syntax introduced in https://github.com/spf13/hugo/issues/480:
{{< >}}: Typically raw HTML. Will not be processed.
{{% %}}: Will be processed by the page's markup engine (Markdown or (in future) Asciidoctor)
Enough introduction, on to the topic of this topic:
The current parsing code for short codes is effective and compact, but kind of hard to read and understand – making changes to it (even rearranging it) seem like a scary task for me.
So, inspired by Rob Pike’s talk called “Lexical Scanning in Go” (Google it, video and slides are found online — very interesting!), I decided to try to rewrite the scanning/parsing parts related to short codes.
I thought this would be a hard sell to @spf13 (I have deleted much of his code).
But running some simple benchmarks (1000 big docs with lots of different short codes on the {{%-form (only syntax known to all versions. This is a very non-typical document set) shows significant improvements in page throughput.
And it’s easier to understand (for me, anyway), easier to test in isolation and gives fairly good error messages.
Two questions (mostly) to @spf13 (you can use this coming PR or ignore it – that’s what the “request” part in pull request means):
-
I notice the support for htmlencoded and non htmlencoded quotes. It’s not hard to implement, but I wonder if this is a real requirement or just “nice to have”?
-
The parser fixes unbalanced quotes in parameters. I see some problems with this if we want to expand on the syntax in the future.
Looking at the Go language and template syntax, I say a fairly strict parser is a good thing, if good error messages is in place. For browsers it makes sense to fix bad HTML, but Hugo users own the source, and they are smart and wants to do it to according to spec.
This flexibility comes at a cost; it has to be tested and one never knows if it will bite you in the foot in the future when wanting to add some more syntactic sugar.