Jekyll中每个博客文章的两个版本

在一个非常简单的Jekyll设置中,我需要每个post的两个版本:面向公众的版本和一个专门用于嵌入的品牌的准系统版本。

我为每种类型都有一个布局:

post.html post_embed.html 

我可以通过在前面的内容中制作具有不同布局的每个post文件的副本来完成这一点,但这显然是一种可怕的方法。 必须有一个更简单的解决方案,无论是在命令行级还是在前端?

更新:此SO问题涉及为每个post创建JSON文件 。 我真的只需要一个生成器循环遍历每个post,改变YAML前端物质中的一个值(embed_pa​​ge = True)并将其反馈回相同的模板。 所以每个post都会呈现两次,一次是embed_page true,另一次是false。 仍然没有完全掌握发电机。

这是我的Jekyll插件来实现这一目标。 这可能是荒谬的低效率,但我已经用Ruby写了两天了。

 module Jekyll # override write and destination functions to taking optional argument for pagename class Post def destination(dest, pagename) # The url needs to be unescaped in order to preserve the correct filename path = File.join(dest, CGI.unescape(self.url)) path = File.join(path, pagename) if template[/\.html$/].nil? path end def write(dest, pagename="index.html") path = destination(dest, pagename) puts path FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') do |f| f.write(self.output) end end end # the cleanup function was erasing our work class Site def cleanup end end class EmbedPostGenerator < Generator safe true priority :low def generate(site) site.posts.each do |post| if post.data["embeddable"] post.data["is_embed"] = true post.render(site.layouts, site.site_payload) post.write(site.dest, "embed.html") post.data["is_embed"] = false end end end end end