将文本标签添加到 Jekyll 站点

Add In-Text Tags to Jekyll Site

我只是想知道是否可以在 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 中编写定义或锚点,然后将它们放在某个地方。

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

您需要在生成之前准备好这些定义,以便在生成页面时将它们放在正确的位置。

一个解决方案可能是,将这些放在最前面(我知道,你说过你不想,但是......)。在生成开始之前,可以通过插件评估前端内容。

例如:

--
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。

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

祝你好运!

所以,我的目标是添加内联标签,然后能够在单独的页面上调用这些标签。我最终能够通过 Jekyll 和 Python 的组合来做到这一点。我将所有原始 post 保存在 "topublish" 文件夹中。在每个 post 的开头,我都包含一个 front matter 标签 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 保存在“_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 定义,如下所示:

<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 风格 table 中不能正常工作。除此之外,效果很好。我有一个 运行ning 列表,其中包含我所有阅读笔记中的所有定义,只需 运行 "python defs.py" 即可发布!所以我自己的问题有了答案...