class Jekyll::DataPage

this class is used to tell Jekyll to generate a page

Public Class Methods

new(site, base, index_files, dir, page_data_prefix, data, name, name_expr, title, title_expr, template, extension, debug) click to toggle source
  • site and base are copied from other plugins: to be honest, I am not sure what they do

  • `index_files` specifies if we want to generate named folders (true) or not (false)

  • `dir` is the default output directory

  • `page_data_prefix` is the prefix used to output the page data

  • `data` is the data of the record for which we are generating a page

  • `name` is the key in `data` which determines the output filename

  • `name_expr` is an expression for generating the output filename

  • `title` is the key in `data` which determines the page title

  • `title_expr` is an expression for generating the page title

  • `template` is the name of the template for generating the page

  • `extension` is the extension for the generated file

# File lib/jekyll-datapage-generator.rb, line 37
def initialize(site, base, index_files, dir, page_data_prefix, data, name, name_expr, title, title_expr, template, extension, debug)
  @site = site
  @base = base

  if debug
    puts "debug (datapage-gen) Record read:"
    puts ">> #{data}"

    puts "debug (datapage-gen) Configuration variables:"
    [:index_files, :dir, :page_data_prefix, :name, :name_expr, :title, :title_expr, :template, :extension].each do |variable|
      puts ">> #{variable}: #{eval(variable.to_s)}"
    end
  end

  # @dir is the directory where we want to output the page
  # @name is the name of the page to generate
  # @name_expr is an expression for generating the name of the page
  #
  # the value of these variables changes according to whether we
  # want to generate named folders or not
  if name_expr
    record = data
    raw_filename = eval(name_expr)
    if raw_filename == nil
      puts "error (datapage-gen). name_expr '#{name_expr}' generated an empty value in record #{data}"
      return
    end
    puts "debug (datapage-gen). using name_expr: '#{raw_filename}' (sanitized) will be used as the filename" if debug
  else
    raw_filename = data[name]
    if raw_filename == nil
      puts "error (datapage-gen). empty value for field '#{name}' in record #{data}"
      return
    end
    puts "debug (datapage-gen). using name field: '#{raw_filename}' (sanitized) will be used as the filename" if debug
  end

  if title_expr
    record = data
    raw_title = eval(title_expr)
    if raw_title == nil
      puts "error (datapage-gen). title_expr '#{title_expr}' generated an empty value in record #{data}"
      return
    end
    puts "debug (datapage-gen). using title_expr: '#{raw_title}' will be used the page title" if debug
  else
    raw_title = data[title]
    if raw_title == nil
      raw_title = raw_filename # for backwards compatibility
      puts "debug (datapage-gen). empty title field: falling back to filename for the page title" if debug
    end
      puts "debug (datapage-gen). will use '#{raw_title}' as the page title" if debug
  end

  filename = sanitize_filename(raw_filename).to_s

  @dir = dir + (index_files ? "/" + filename + "/" : "")
  @name = (index_files ? "index" : filename) + "." + extension.to_s

  self.process(@name)

  if @site.layouts[template].path.end_with? 'html'
    @path = @site.layouts[template].path.dup
  else
    @path = File.join(@site.layouts[template].path, @site.layouts[template].name)
  end

  base_path = @site.layouts[template].path
  base_path.slice! @site.layouts[template].name
  self.read_yaml(base_path, @site.layouts[template].name)

  self.data['title'] = raw_title

  # add all the information defined in _data for the current record to the
  # current page (so that we can access it with liquid tags)
  if page_data_prefix
    self.data[page_data_prefix] = data
  else
    if data.key?('name')
      data['_name'] = data['name']
    end
    self.data.merge!(data)
  end

end