nikku / kartoffeldruck

A all-in-one, opinionated, swiss army knife, hackable static site generator.
Other
6 stars 1 forks source link

Make externally defined macros available in Markdown #3

Closed nikku closed 8 years ago

nikku commented 8 years ago

It should be possible to evaluate certain template fragments in the scope of a Markdown document.

With this functionality users are able to provide boilerplate code via macros.

Page
{{ img(src='foo.gif', caption='FOO') }}
Macro
{% macro img(src='', caption='', cls='') %}
<img src="{{ src }}" class="{{ className }}" />
{% if caption %}
<span class="caption">{{ caption }}</span>
{% endif %}
{% endmacro %}
jrehwaldt commented 8 years ago

I hoped we can circumvent the addition of yet another property by rendering the combined page once with nunjucks and processing+replacing only the markdown-part in a second step.

This way, the macro could be defined as part of the layouting template.

nikku commented 8 years ago

Performing markdown processing as a post-step sounds like a good solution.

This would circumvent the fact that the Markdown parser interprets nunjucks templates and messes up with them before they are evaluated.

jrehwaldt commented 8 years ago

This would circumvent the fact that the Markdown parser interprets nunjucks templates and messes up with them before they are evaluated.

That seems already not possible as nunjucks processes in step 1.0 and markdown in 1.1.

[1] https://github.com/nikku/kartoffeldruck/blob/d9a99d91f082d0c94b4d90e306f61a4468a50ddc/lib/generator.js#L54-L59

jrehwaldt commented 8 years ago

Anyway, I would process step 1.0 or 1.2 as necessary (layout property given or not) and somehow mark the content (e.g. <page-content>...</page-content>). Next, I'd extract this content, process it with markdown, and add it back without this marker to the result.

nikku commented 8 years ago

That's what I ment.

This would circumvent the fact that the Markdown parser interprets nunjucks templates and messes up with them before they are evaluated.

In other words: The only reason why we currently have to process the page via Nunjucks twice is the fact that Markdown generation will mess up the templating otherwise.

Can you prepare a PR on this? I am happy to ditch the include feature in place of something more convenient.

jrehwaldt commented 8 years ago

I will give it a try in the evening(s).

jrehwaldt commented 8 years ago

Should I keep the include feature? Maybe no layout is desired after all?

nikku commented 8 years ago

I would ditch it. It is always possible to include a noop layout.

jrehwaldt commented 8 years ago

Why current step 1.2 is only required if markdown processing is necessary?

https://github.com/nikku/kartoffeldruck/blob/d9a99d91f082d0c94b4d90e306f61a4468a50ddc/lib/generator.js#L61-L66

jrehwaldt commented 8 years ago

Uiui... you're tests fail on Windows due to line ending issues.

nikku commented 8 years ago

An additional change (5b8b77e557e882a13d460067dbcb57c22b6f11a0) was needed to make the macro changes usable in real world scenarios.