Class: Jekyll::JekyllDatapageGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-datapage-generator.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object

the function =generate= loops over the =_config.yml/page_gen= specification, determining what sets of pages have to be generated, reading the data for each set and invoking the =DataPage= constructor for each record found in the data



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/jekyll-datapage-generator.rb', line 132

def generate(site)
  # page_gen-dirs is a global option which determines whether we want to
  # generate index pages (name/index.html) or HTML files (name.html) for
  # all sets
  index_files = site.config['page_gen-dirs'] == true

  # data contains the specification of all the datasets for which we want
  # to generate individual pages (look at the README file for its documentation)
  data = site.config['page_gen']
  if data
    data.each do |data_spec|
      index_files_for_this_data = data_spec['index_files'] != nil ? data_spec['index_files'] : index_files
      template         = data_spec['template'] || data_spec['data']
      name             = data_spec['name']
      name_expr        = data_spec['name_expr']
      title            = data_spec['title']
      title_expr       = data_spec['title_expr']
      dir              = data_spec['dir'] || data_spec['data']
      extension        = data_spec['extension'] || "html"
      page_data_prefix = data_spec['page_data_prefix']
      debug            = data_spec['debug']
      
      if not site.layouts.key? template
        puts "error (datapage-gen). could not find template #{template}. Skipping dataset #{name}."
      else
        # records is the list of records for which we want to generate
        # individual pages
        records = nil

        data_spec['data'].split('.').each do |level|
          if records.nil?
            records = site.data[level]
          else
            records = records[level]
          end
        end
        if (records.kind_of?(Hash))
          records = records.values
        end

        # apply filtering conditions:
        # - filter requires the name of a boolean field
        # - filter_condition evals a ruby expression which can use =record= as argument
        records = records.select { |record| record[data_spec['filter']] } if data_spec['filter']
        records = records.select { |record| eval(data_spec['filter_condition']) } if data_spec['filter_condition']

        # we now have the list of all records for which we want to generate individual pages
        # iterate and call the constructor
        records.each do |record|
          site.pages << DataPage.new(site, site.source, index_files_for_this_data, dir, page_data_prefix, record, name, name_expr, title, title_expr, template, extension, debug)
        end
      end
    end
  end
end