Class: Jekyll::DataPage

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

Overview

this class is used to tell Jekyll to generate a page

Instance Method Summary collapse

Methods included from Sanitizer

#sanitize_filename

Constructor Details

#initialize(site, base, index_files, dir, page_data_prefix, data, name, name_expr, title, title_expr, template, extension, debug) ⇒ DataPage

  • 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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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