通过在Jekyll中编程来生成文件 [英] Generating files by programming in Jekyll

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

问题描述

假设我要从中自动生成页面的_data文件夹中有一些数据.那就是:想象_data/structure.md是如下

Suppose I have some data in the _data folder from which I want to automatically generate pages. That is: imagine _data/structure.md is something as follows

chapters:
  -
    chapter1  
    chapter2 
    chapter3 
    ...

,我需要静态文件chapter1.mdchapter2.md ...等,它们在结构上非常相似(例如,chapter1.md

and I need static files chapter1.md, chapter2.md ... and so on which are quite similar in structure (for instance, chapter1.md is

---
title:chapter1
layout: default
---

This is chapter1!!

). 有没有一种方法可以自动创建这些文件,而无需手动创建它们,而只是更改或添加_data文件中的项目?

). Is there a way to create these files automatically, without doing them by hand, just changing or adding items in the _data file?

推荐答案

您可以使用生成器(文档). 可能是这样的:

You can use a generator (documentation). This can be something like this :

module Jekyll

  class DataPage < Page
    def initialize(site, base, dir, name)
      @site = site
      @base = base
      @dir = dir
      @name = name
      self.process(@name)
      self.data ||= {}
      self.data['layout'] = 'default'
      self.data['title'] = data
    end
  end

  class CategoryPageGenerator < Generator
    def generate(site)
      datas = site.data['structure']
      datas.each do |data|
        name = "#{data}.md"
        page = Jekyll::DataPage.new(site, site.source, @dir, name)
        page.data['title'] = data
        page.data['layout'] = 'default'
        page.content = "This is #{data}"
        site.pages << page
      end
    end
  end

end

这篇关于通过在Jekyll中编程来生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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