使用Jekyll插件在_site内部生成文件 [英] Generate file inside _site with Jekyll plugin

查看:145
本文介绍了使用Jekyll插件在_site内部生成文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Jekyll插件"Tags",它会生成一个文件并返回指向该文件的链接字符串.

I have written a Jekyll plugin, "Tags", which generates a file and returns string of links to that file.

一切都很好,但是如果我将该文件直接写入_site文件夹,则会将其删除.如果我将该文件放在_site文件夹之外,则不会在_site内部生成.

Everything is fine, but if I write that file directly into the _site folder, it is removed. If I put that file outside the _site folder, it is not generated inside _site.

我应该在哪里以及如何添加文件,以便可以在_site文件夹中找到文件?

Where and how should I add my file so that it is available inside the _site folder?

推荐答案

您应该为此使用类Page并调用方法renderwrite.

You should use class Page for this and call methods render and write.

这是在我的博客上生成存档页面的示例:

This is an example to generate the archive page at my blog:

module Jekyll
  class ArchiveIndex < Page
    def initialize(site, base, dir, periods)
      @site = site
      @base = base
      @dir = dir
      @name = 'archive.html'
      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'archive_index.html')
      self.data['periods'] = periods
    end   
  end

  class ArchiveGenerator < Generator
    priority :low

    def generate(site)
        periods = site.posts.reverse.group_by{ |c| {"month" => Date::MONTHNAMES[c.date.month], "year" => c.date.year} }

        index = ArchiveIndex.new(site, site.source, '/', periods)
        index.render(site.layouts, site.site_payload)
        index.write(site.dest)
        site.pages << index
    end
  end
end

这篇关于使用Jekyll插件在_site内部生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆