将文本标签添加到Jekyll站点

时间:2017-06-23 02:06:21

标签: jekyll jekyll-extensions

我只是想知道是否有可能在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 %)

谢谢!

2 个答案:

答案 0 :(得分:0)

我认为使用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。

如果没有这种插件,那将很难。据我所知,将在通话时创建标签,因此您无法将状态置于其中。我会毫不犹豫地从标签访问该网站 - 维护和调试将是一个可怕的混乱。

祝你好运!

答案 1 :(得分:0)

所以,我的目标是添加内嵌标签,然后能够在不同的页面上调用这些标签。我最终能够通过Jekyll和Python的组合来实现这一目标。我将所有原始帖子保存在&#34; topublish&#34;夹。在每篇文章的开头,我都包含一个前端标签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:类别。该脚本将更新的帖子保存在&#39; _posts&#39;文件夹:

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样式表中不能正常运行。除此之外,这很好。我有一份所有阅读笔记中所有定义的运行列表,只需要运行&#34; python defs.py&#34;出版之前!所以对我自己的问题有答案......

相关问题