Reputation: 66478
I'm trying to create code snippets as examples for my project website created in Jekyll.
I have yaml that look like this:
-
description: "Scheme hygienic macro that creates assoc list, with macroexpand."
code: >
(define-syntax alist
(syntax-rules ()
((_) ())
((_ x y z ...)
(cons (cons x y) (alist z ...)))))
(print (alist "foo" 10 "bar" 20 "baz" 30))
;; ==> ((foo . 10) (bar . 20) (baz . 30))
(macroexpand (alist "foo" 10 "bar" 20))
;; ==> (#:cons (#:cons "foo" 10)
(#:cons (#:cons "bar" 20)
()))
the problem is that lines that are exactly the same indent as first line are concatenated into single line. I want this to be exactly like in yaml in my pre tag. If I indent every line that be one more space I get extra space in pre tag when I'm adding the snippet.
Is there any way to make it insert exactly the code I have in yaml file? how to properly indent in yaml to not have then spaces before each line and have each line in it's own line in output html?
I was trying to add quotes around the string but they are present in output and I was not able to removed them in Liquid.
I've tried this:
{% for example in site.data.examples %}
{% assign code = example.code | split: '^"|"$' %}
<li>
<div class="example">
<pre>{{ code[1] }}</pre>
</div>
<div class="description">
{{ example.description }}
</div>
</li>
{% endfor %}
but split don't accept regular expressions. I've also tried:
{% assign len = example.code.length %}
{% assign code = example.code | slice: 1,len-2 %}
but it only prints first parenthesis. EDIT:
I've solved issue with removing quotes but that was dead end, I still got spaces before each line.
This is a way to remove quotes from string in Jekyll:
{% assign code = example.code | remove_first: '"' | rstrip | append: "__" | remove: '"__' %}
Upvotes: 2
Views: 825
Reputation: 66478
It seems that this is simpler than I though. All that is required is block in Yaml (ref: https://idratherbewriting.com/documentation-theme-jekyll/mydoc_yaml_tutorial.html)
-
description: "Scheme hygienic macro that creates assoc list, with macroexpand."
code: |
(define-syntax alist
(syntax-rules ()
((_) ())
((_ x y z ...)
(cons (cons x y) (alist z ...)))))
(print (alist "foo" 10 "bar" 20 "baz" 30))
;; ==> ((foo . 10) (bar . 20) (baz . 30))
(macroexpand (alist "foo" 10 "bar" 20))
;; ==> (#:cons (#:cons "foo" 10)
(#:cons (#:cons "bar" 20)
()))
And you can use normal variable:
{% for example in site.data.examples %}
<li>
<div class="example">
<pre>{{ example.code }}</pre>
</div>
<div class="description">
{{ example.description }}
</div>
</li>
{% endfor %}
If above don't work for you, here is old version of the answer:
Liquid is very limited but I was able to create this hack:
{% for example in site.data.examples %}
{% assign code = example.code | remove_first: '"' | rstrip | append: "__" | remove: '"__' | newline_to_br | split: "<br />" %}
<li>
<div class="example">
<pre>{% for line in code %}{{ line | remove_first: " " }}{% endfor %}</pre>
</div>
<div class="description">
{{ example.description }}
</div>
</li>
{% endfor %}
and for reference this is my examples.yaml
-
description: "Scheme hygienic macro that creates assoc list, with macroexpand."
code: >
" (define-syntax alist
(syntax-rules ()
((_) ())
((_ x y z ...)
(cons (cons x y) (alist z ...)))))
(print (alist "foo" 10 "bar" 20 "baz" 30))
;; ==> ((foo . 10) (bar . 20) (baz . 30))
(macroexpand (alist "foo" 10 "bar" 20))
;; ==> (#:cons (#:cons "foo" 10)
(#:cons (#:cons "bar" 20)
()))"
Upvotes: 1