-
Notifications
You must be signed in to change notification settings - Fork 17
/
portfolio.rb
80 lines (67 loc) · 2.05 KB
/
portfolio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
module Jekyll
class PortfolioIndex < Page
def initialize(site, base, dir)
@site = site
@base = base
@dir = dir
@name = "index.html"
self.process(@name)
self.read_yaml(File.join(base, '_layouts'), 'portfolio.html')
self.data['projects'] = self.get_projects(site)
end
def get_projects(site)
{}.tap do |projects|
Dir['_projects/*.yml'].each do |path|
name = File.basename(path, '.yml')
config = YAML.load(File.read(File.join(@base, path)))
projects[name] = config if config['published']
end
end
end
end
class ProjectIndex < Page
def initialize(site, base, dir, path)
@site = site
@base = base
@dir = dir
@name = "index.html"
self.data = YAML.load(File.read(File.join(@base, path)))
self.process(@name) if self.data['published']
end
end
class GeneratePortfolio < Generator
safe true
priority :normal
def generate(site)
self.write_portfolio(site)
end
# Loops through the list of project pages and processes each one.
def write_portfolio(site)
if Dir.exists?('_projects')
Dir.chdir('_projects')
Dir["*.yml"].each do |path|
name = File.basename(path, '.yml')
self.write_project_index(site, "_projects/#{path}", name)
end
Dir.chdir(site.source)
self.write_portfolio_index(site)
end
end
def write_portfolio_index(site)
portfolio = PortfolioIndex.new(site, site.source, "/portfolio")
portfolio.render(site.layouts, site.site_payload)
portfolio.write(site.dest)
site.pages << portfolio
site.static_files << portfolio
end
def write_project_index(site, path, name)
project = ProjectIndex.new(site, site.source, "/portfolio/#{name}", path)
if project.data['published']
project.render(site.layouts, site.site_payload)
project.write(site.dest)
site.pages << project
site.static_files << project
end
end
end
end