【问题标题】:How to add source code snippets into _data yaml in Jekyll?如何在 Jekyll 的 _data yaml 中添加源代码片段?
【发布时间】:2020-12-13 20:36:25
【问题描述】:

我正在尝试创建代码 sn-ps 作为我在 Jekyll 中创建的项目网站的示例。

我的 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)
                            ()))

问题在于与第一行缩进完全相同的行被连接成单行。我希望这与我的 pre 标签中的 yaml 完全相同。如果我将每一行缩进一个空格,那么当我添加 sn-p 时,我会在 pre 标签中获得额外的空格。

有没有办法让它完全插入我在 yaml 文件中的代码?如何在 yaml 中正确缩进以在每行之前没有空格,并在输出 html 中将每一行放在它自己的行中?

我试图在字符串周围添加引号,但它们出现在输出中,我无法在 Liquid 中删除它们。

我试过了:

  {% 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 %}

但 split 不接受正则表达式。我也试过:

{% assign len = example.code.length  %}
{% assign code = example.code | slice: 1,len-2  %}

但它只打印第一个括号。 编辑:

我已经解决了删除引号的问题,但那是死胡同,我在每行之前仍然有空格。

这是一种在 Jekyll 中从字符串中删除引号的方法:

{% assign code = example.code | remove_first: '"' | rstrip | append: "__" | remove: '"__'  %}

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    这似乎比我更简单。所需要的只是 Yaml 中的块(参考: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)
                                ()))
    

    你可以使用普通变量:

      {% for example in site.data.examples %}
      <li>
        <div class="example">
          <pre>{{ example.code }}</pre>
        </div>
        <div class="description">
          {{ example.description }}
        </div>
      </li>
      {% endfor %}
    
    

    如果以上方法不适合你,这里是旧版本的答案:

    Liquid 非常有限,但我能够创建这个 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 %}
    
    

    作为参考,这是我的示例.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)
                                ()))"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2017-10-01
      • 2018-10-20
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多