Search code examples
yamlmarkdowntemplate-enginepandoc

Template language for Markdown to Markdown


I'm looking for a simple templating engine that takes a YAML snipped as context and works well for reading Markdown plus some tags to produce Markdown. I tried Jekyll's Liquid and it works, except for whitespace which is really important in Markdown.

For example with this input...

---
samples:
- name: one
  samplecode: |
    function sample() {
      return 1;
    }
- name: two
  samplecode: |
    function sample() {
      return 2;
    }
---

{% for s in page.samples %}
- {{s.name}}

        {{s.samplecode}}

{% endfor %}

the output is:

- one

        function sample() {
  return 1;
}

- two

        function sample() {
  return 2;
}

instead of the following which would actually work with Pandoc:

- one

        function sample() {
          return 1;
        }

- two

        function sample() {
          return 2;
        }

This has actually been discussed before for Jekyll.

So my question: is there any simple template engine that suits my needs?


Solution

  • I think you could use the template engine built into pandoc to do what you want to do.

    t.tpl:

    $for(samples)$
    -   $samples.name$
    
        $samples.samplecode$
    
    $endfor$
    

    t.md:

    ---
    samples:
    - name: one
      samplecode: |
        ```
        function sample() {
          return 1;
        }
        ```
    - name: two
      samplecode: |
        ```
        function sample() {
          return 2;
        }
        ```
    ---
    

    Note: we put the code samples in code fences because pandoc interprets metadata values as markdown.

    Then:

    % pandoc --template t.tpl -f markdown -t markdown t.md
    -   one
    
            function sample() {
              return 1;
            }
    
    -   two
    
            function sample() {
              return 2;
            }