【问题标题】:Generate file inside _site with Jekyll plugin使用 Jekyll 插件在 _site 内生成文件
【发布时间】:2012-09-26 05:19:31
【问题描述】:

我写了一个 Jekyll 插件,“Tags”,它会生成一个文件并返回指向该文件的链接字符串。

一切都很好,但是如果我将该文件直接写入 _site 文件夹,它就会被删除。如果我将该文件放在 _site 文件夹之外,它不会在 _site 中生成。

我应该在哪里以及如何添加我的文件以便它在 _site 文件夹中可用?

【问题讨论】:

    标签: ruby jekyll jekyll-extensions


    【解决方案1】:

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

    这是在我的博客上生成archive page 的示例:

    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::StaticFile 可以扩展生成静态文件!!
    • @roxxypoxxy 究竟如何?您是否即时生成这些文件,然后将其删除? (即不在源文件夹中)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 2020-06-05
    • 2019-01-26
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多