Jekyll generator test

Following my previous post I build a Jekyll plugin for creating a central yml file built from front matters of pages and posts.

This way when you need this informations in a page or in a post you don’t need to cycle on the entire site (which has bad performance) but just on the data file created.

The following is the plugin, my first ruby code. It is customized for my project, but you can get the point.

require 'yaml'

module Jekyll

  class CommonData
    def initialize(site, base)
      @site = site
      @base = base

      dataPath = base + '/_data'
      relationsFile = dataPath + "/relations.yml" , 'w')
      relations = {}
      propertiesFile    = dataPath + "/properties.yml" , 'w')
      properties = {}

      site.pages.each do |page|
        layout =['layout']
        title =['title']
        if(layout == 'people' or layout == 'projects')
          values = {}
          id = page.url;
          values['title'] = title
          properties[id] = values
      propertiesFile.write( properties.to_yaml ); do |post|
        values = {}
        values['relations'] =['relations']
        values['title'] =['title']
        values['date'] =['date']
        relations[post.url] = values;
      relationsFile.write( relations.to_yaml );


  class CommonDataGenerator < Generator
    safe false

    def generate(site), site.source)


I don’t think this is an optimal solution. Generating data file cause the site to be regenerated… An improvement could be of checking the hash of the data agains the previous generated to write the file only if changed. Or maybe writing the Jekyll plugin using hooks instead of generators.

If you have any feedback, check this post on the Jekyll talks site

Edit this page