Generate file inside _site with Jekyll plugin

随声附和 提交于 2019-12-24 18:40:01

问题


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

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.

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


回答1:


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


来源:https://stackoverflow.com/questions/12594990/generate-file-inside-site-with-jekyll-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!