【问题标题】:Add In-Text Tags to Jekyll Site向 Jekyll 站点添加文本内标签
【发布时间】:2017-06-23 02:06:20
【问题描述】:

我只是想知道是否可以在 Jekyll 的前端内容之外编写标签。我知道通常不是,但我真的希望能够在我写帖子时添加一些标签。我做了一些研究,认为这可能通过插件实现,但之前没有写过,有点担心沿着这条路走下去,发现这是不可能的......

这看起来像

---
layout: post
categories: Marx
--- 
In this chapter of Marx, we learn about commodity fetishism, which can be described below. 
[defs](commodity fetishism - the perception of the social relationships involved in production as economic relationships among the money and commodities exchanged in market trade)

希望在某个时候就像我现在可以写的那样

(% for categories in site.posts %)

我会写

(% for defs in site.posts %)

谢谢!

【问题讨论】:

  • 请添加一个您正在尝试的示例以及想要的效果。

标签: jekyll jekyll-extensions


【解决方案1】:

我认为使用 Jekyll 是不可能的。

您基本上是在 Jekyll 中编写定义或锚点,然后将它们放在某个地方。

但这意味着,您必须生成所有页面,然后重新生成它们并将您收集的所有定义放在正确的位置。

您需要在生成时间之前将这些 defs 放在手边,以便在生成页面时将它们放在正确的位置。

一种解决方案可能是将这些放在首位(我知道,您说过您不想这样做,但是...)。在生成开始之前,可以通过插件评估前面的内容。

例如:

--
defs_used:
   key: value
   key: value
...

在插件中:

class Generator < Jekyll::Generator
    def generate(site)
      site.data['defs'] = Array.new
      site.posts.docs.each do |p|
         p.data['defs'].each do |def|
           site.data['defs'].unshift(def)    
...

(未经测试,但我希望你明白)

完成此操作后,您可以从任何您喜欢的地方访问 defs。

没有那种插件,就很难了。据我所知,标签将在调用时创建,因此您不能将状态放入其中。而且我会犹豫从标签访问该网站 - 这将是一个可怕的维护和调试混乱。

祝你好运!

【讨论】:

    【解决方案2】:

    所以,我的目标是添加内嵌标签,然后能够在单独的页面上调用这些标签。我最终能够结合 Jekyll 和 Python 来做到这一点。我将所有原始帖子保存在“topublish”文件夹中。在每篇文章的开头,我都包含一个前端标签 defs_used: so:

    ---
    layout: post
    title:  xxxx
    date:   2017-07-11 17:50:00
    categories: ['Reading Notes']
    defs_used:
    
    ---
    

    当我记笔记并遇到定义时,我会这样做

    In this chapter of Marx, we learn about commodity fetishism, which can be described below. 
    <def>commodity fetishism: the perception of the social relationships involved 
    in production as economic relationships among the money and commodities 
    exchanged in market trade</def>
    

    完成后,我运行 python 脚本来搜索定义并将它们写入 defs_used: 类别。该脚本将更新的帖子保存在“_posts”文件夹中:

    import os
    import re
    files = [f for f in os.listdir("_topublish") if f.endswith('.markdown')]
    for posts in files:
        print(posts)
    with open("_topublish/"+posts,"r") as post1:
        post = post1.read()
        defs = re.findall("<def>(.*?)</def>",post)
        replacement = "defs_used:\n"
        for definition in defs:
            replacement = replacement+"\n    "+definition
    
        newpost = post.replace("defs_used:\n",replacement)
        newname = posts.replace('.markdown','-defs.markdown')
        print newpost
        with open("_posts/"+newname,'w') as w:
           w.write(newpost)
    

    这会收集所有定义并将它们放入标签中,如下所示:

    ---
    layout: post
    title:  xxxx
    date:   2017-07-11 17:50:00
    categories: ['Reading Notes']
    defs_used:
        commodity fetishism: the perception of the social relationships involved in production as economic relationships among the money and commodities exchanged in market trade
    ---
    

    在一个新页面上,我创建了一个定义表,如下所示:

    <table>
      <tr>
      <th>Link</th>
      <th>Title</th>
      <th>Date</th>
      <th>Term</th>
      <th>Definition</th>
      </tr>
        {% for post in site.posts %}
          {% for def in post.defs_used %}
          <tr>
          <th>Access here[{{post.url}}]</th>
          <th>{{ post.title}}</th>
          <th>{{ post.date}}</th>
          <th>{{ def[0] }}</th>
          <th>{{ def[1] }}</th>
          </tr>
    {% endfor %}
        {% endfor %}
    

    我仍然遇到的唯一问题是返回页面的链接不起作用,因为降价在 HTML 样式表中的功能不太正确。除此之外,这效果很好。我有一个来自所有阅读笔记的所有定义的运行列表,并且只需要在发布之前运行“python defs.py”!所以有我自己的问题的答案...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2020-10-18
      • 2012-04-25
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      相关资源
      最近更新 更多