Class: Jekyll::DataPagesGenerator

Inherits:
Generator
  • Object
show all
Defined in:
jekyll/_plugins/data_page_generator.rb

Instance Method Summary collapse

Instance Method Details

#digit(a, key, *rest) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'jekyll/_plugins/data_page_generator.rb', line 54

def digit(a, key, *rest)
	value = a[key]
	if value.nil? || rest.empty?
	  value
	elsif value.is_a? Hash
	  digit(value, *rest)
	else
	  fail TypeError, "#{value.class} is not hash"
	end
end

#generate(site) ⇒ Object



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
122
123
124
125
126
127
128
# File 'jekyll/_plugins/data_page_generator.rb', line 65

def generate(site)
  data = site.config['page_gen']
  if data
    data.each do |data_spec|
      # todo: check input data correctness
      template = data_spec['template'] || data_spec['data']
      name = data_spec['name']
      dir = data_spec['dir'] || data_spec['data']
      extension = data_spec['extension'] || "html"
      
      if data_spec['data'].is_a? Array
          records = digit(site.data, *data_spec['data']) # site.data.dig(*data_spec['data'])
          dataname = data_spec['data'].join('_');
      else
       records = site.data[data_spec['data']]
       dataname = data_spec['data']
   end
      
      # if no data just return without generating any pages
      return unless records
      
  
      # convert hash to array
      if records.respond_to? :values
          records = records.values
      end       
      
      
      if site.layouts.key? template
        if site.config['incremental']

cache_file = site.in_source_dir(".jekyll-metadata-#{dataname}");
oldrecords =  read_cache(cache_file);
count = 0
            if oldrecords.respond_to? :values
                oldrecords = oldrecords.values
            end       
                 
records.each_index do |i|

  if records[i] == oldrecords[i]
      site.keep_files << dir + '/' + sanitize_filename2(records[i][name]).to_s + ".html"            
  else
	  site.pages << DataPage.new(site, site.source, dir, records[i], name, template, extension)
	  count += 1
  end
end

write_cache(cache_file, records);
        	
        else
records.each_index do |i|
    site.pages << DataPage.new(site, site.source, dir, records[i], name, template, extension)
end
        end
        
        
      else
        # puts "error. could not find #{data_file}" if not File.exists?(data_file)
        puts "error. could not find template #{template}" if not site.layouts.key? template
      end
    end
  end
end

#read_cache(f) ⇒ Object



46
47
48
# File 'jekyll/_plugins/data_page_generator.rb', line 46

def read_cache(f)
	return File.file?(f) ? Marshal.load(File.binread(f)) : [];
end

#sanitize_filename2(name) ⇒ Object

strip characters and whitespace to create valid filenames, also lowercase



42
43
44
# File 'jekyll/_plugins/data_page_generator.rb', line 42

def sanitize_filename2(name)
  return name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end

#write_cache(f, cache) ⇒ Object



50
51
52
# File 'jekyll/_plugins/data_page_generator.rb', line 50

def write_cache(f, cache)
	File.binwrite(f, Marshal.dump(cache));
end