【发布时间】:2014-08-16 12:35:33
【问题描述】:
我想以编程方式将单行 YAML 前端数据附加到所有 source _posts 文件。
背景:我目前有一个由 Jekyll 提供支持的网站,它使用以下插件生成其 URL:
require "Date"
module Jekyll
class PermalinkRewriter < Generator
safe true
priority :low
def generate(site)
# Until Jekyll allows me to use :slug, I have to resort to this
site.posts.each do |item|
day_of_year = item.date.yday.to_s
if item.date.yday < 10
day_of_year = '00'+item.date.yday.to_s
elsif item.date.yday < 100
day_of_year = '0'+item.date.yday.to_s
end
item.data['permalink'] = '/archives/' + item.date.strftime('%g') + day_of_year + '-' + item.slug + '.html'
end
end
end
end
end
所有这一切都会生成一个类似/archives/12001-post-title.html 的 URL,它是两位数的年份(2012 年),然后是撰写帖子的年份 of(在本例中) ,1 月 1 日)。
(顺便说一句:我喜欢这个,因为它本质上为每个 Jekyll 帖子创建了一个 UID,然后可以在生成的 _site 文件夹中按名称排序,并按时间顺序结束)。
但是,现在我想更改我写的新帖子的 URL 方案,但我不希望这会在生成网站时破坏我所有现有的 URL。因此,我需要一种方法来遍历我的源 _posts 文件夹并将插件生成的 ULR 附加到每个帖子的 YAML 数据中,并使用 the URL: front matter。
我不知道如何做到这一点。我知道如何使用 Ruby 将行附加到文本文件中,但是如何为我的所有 _posts 文件执行此操作并让该行包含插件生成的 URL?
【问题讨论】:
-
不确定我能否回答您的问题,但如果在这里找不到好的答案,您可以随时将问题发布到github.com/jekyll/jekyll-help
标签: ruby jekyll github-pages