Skip to content

Commit f39f735

Browse files
committed
WP: tweaking creation from contents.json and nav uses
1 parent 997facd commit f39f735

File tree

3 files changed

+325
-96
lines changed

3 files changed

+325
-96
lines changed

.github/convert.rb

Lines changed: 145 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -12,85 +12,115 @@ def output_linux(tags)
1212
''
1313
end
1414

15-
def output_projects(proj, id)
16-
o = ''
17-
# puts proj.inspect
18-
proj.select {|p| p['category']==id }
19-
.sort_by {|k,v| k['title']}
20-
.each do |p|
21-
o << "* [#{p['title'].force_encoding('utf-8')}](#{p['homepage']}) #{output_linux p['tags']} - #{p['description']}\n"
15+
def output_projects(projects, category_id)
16+
output = ''
17+
projects.select { |project| project['category'] == category_id }
18+
.sort_by { |project| project['title'] }
19+
.each do |project|
20+
output << "* [#{project['title'].force_encoding('utf-8')}](#{project['homepage']}) #{output_linux project['tags']} - #{project['description']}\n"
2221
end
23-
o
22+
output
2423
end
2524

26-
def output_content_category(c, indent)
27-
toc = "\n"
25+
# Returns true if the category has any projects
26+
def has_projects(projects, category_id)
27+
projects.any? { |project| project['category'] == category_id }
28+
end
2829

29-
for i in 1..indent
30-
toc << '#'
30+
# Returns true if the category or any of its subcategories have projects
31+
def has_projects_recursive(projects, categories, category_id)
32+
return true if has_projects(projects, category_id)
33+
34+
# Check if any children have projects
35+
children = categories.select { |category| category['parent'] == category_id }
36+
children.each do |child|
37+
return true if has_projects_recursive(projects, categories, child['id'])
3138
end
32-
33-
toc << " #{c['title']}\n"
34-
toc << "*#{c['description']}*\n" unless c['description'].nil?
35-
toc << "[back to top](#readme) \n" if indent>2
36-
toc << "\n"
37-
38-
toc
39+
40+
return false
3941
end
4042

41-
def output_content(j)
42-
toc = ''
43-
44-
projects = j['projects']
45-
46-
parents, children = j['categories'].partition { |c| c['parent'].nil? }
47-
parents.each do |c|
48-
id = c['id']
49-
toc << output_content_category(c, 2)
50-
toc << output_projects(projects, id)
51-
52-
children.sort_by {|k,v| k['id']}
53-
.select {|c| c['parent']==id}.each do |c|
54-
child_id = c['id']
55-
56-
toc << output_content_category(c, 3)
57-
toc << output_projects(projects, child_id)
43+
def output_content_category(category, indent)
44+
output = "\n"
5845

59-
children.sort_by {|k,v| k['id']}
60-
.select {|c| c['parent']==child_id}.each do |c|
61-
child_id = c['id']
46+
for i in 1..indent
47+
output << '#'
48+
end
6249

63-
toc << output_content_category(c, 4)
64-
toc << output_projects(projects, c['id'])
50+
output << " #{category['title']}\n"
51+
output << "*#{category['description']}*\n" unless category['description'].nil?
52+
output << "[back to top](#readme) \n" if indent > 2
53+
output << "\n"
6554

66-
children.sort_by {|k,v| k['id']}
67-
.select {|c| c['parent']==child_id}.each do |c|
68-
child_id = c['id']
55+
output
56+
end
6957

70-
toc << output_content_category(c, 5)
71-
toc << output_projects(projects, c['id'])
72-
end
58+
def output_content(data)
59+
output = ''
60+
61+
projects = data['projects']
62+
categories = data['categories']
63+
64+
parents, children = categories.partition { |category| category['parent'].nil? }
65+
parents.each do |parent|
66+
parent_id = parent['id']
67+
# Only include parent categories with projects or that have children with projects
68+
if has_projects_recursive(projects, categories, parent_id)
69+
output << output_content_category(parent, 2)
70+
output << output_projects(projects, parent_id)
71+
72+
children.sort_by { |category| category['id'] }
73+
.select { |category| category['parent'] == parent_id }.each do |child|
74+
child_id = child['id']
75+
76+
# Only include child categories with projects or that have children with projects
77+
if has_projects_recursive(projects, categories, child_id)
78+
output << output_content_category(child, 3)
79+
output << output_projects(projects, child_id)
80+
81+
children.sort_by { |category| category['id'] }
82+
.select { |category| category['parent'] == child_id }.each do |grandchild|
83+
grandchild_id = grandchild['id']
84+
85+
# Only include grandchild categories with projects or that have children with projects
86+
if has_projects_recursive(projects, categories, grandchild_id)
87+
output << output_content_category(grandchild, 4)
88+
output << output_projects(projects, grandchild_id)
89+
90+
children.sort_by { |category| category['id'] }
91+
.select { |category| category['parent'] == grandchild_id }.each do |great_grandchild|
92+
great_grandchild_id = great_grandchild['id']
93+
94+
# Only include great-grandchild categories with projects
95+
if has_projects(projects, great_grandchild_id)
96+
output << output_content_category(great_grandchild, 5)
97+
output << output_projects(projects, great_grandchild_id)
98+
end
99+
end
100+
end
101+
end
102+
end
73103
end
74104
end
75105
end
76106

77-
toc
107+
output
78108
end
79109

80-
def output_header(j)
81-
header = j['header']
82-
num_projects = j['projects'].count
110+
def output_header(data)
111+
header = data['header']
112+
num_projects = data['projects'].count
83113

84-
o = header
85-
o << "\n\n"
86-
# o << output_table(num_projects)
87-
# o
114+
output = header
115+
output << "\n\n"
116+
# output << output_table(num_projects)
117+
output
88118
end
89119

90-
def output_contributing(j)
91-
o = "\n\n### Contributing\n\n"
92-
o << j['header_contributing']
93-
o
120+
def output_contributing(data)
121+
output = "\n\n### Contributing\n\n"
122+
output << data['header_contributing']
123+
output
94124
end
95125

96126
def output_table(num_projects)
@@ -99,53 +129,72 @@ def output_table(num_projects)
99129
date = DateTime.now
100130
date_display = date.strftime "%B %d, %Y"
101131

102-
o = "| Awesome | Linux | Projects | Updated\n| :-: | :-: | :-: | :-: | :-:\n"
103-
o << '[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) | '
104-
o << ' :penguin: | '
105-
o << "#{num_projects} | "
106-
o << date_display
132+
output = "| Awesome | Linux | Projects | Updated\n| :-: | :-: | :-: | :-: | :-:\n"
133+
output << '[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) | '
134+
output << ' :penguin: | '
135+
output << "#{num_projects} | "
136+
output << date_display
107137

108-
o
138+
output
109139
end
110140

111-
def output_toc(j)
112-
toc = "\n\n### Contents\n\n"
113-
114-
parents, children = j['categories'].partition { |c| c['parent'].nil? }
115-
parents.each do |c|
116-
id = c['id']
117-
toc << "- [#{c['title']}](##{id})\n"
118-
119-
children.sort_by {|k,v| k['id']}
120-
.select {|c| c['parent']==id}.each do |c|
121-
child_id = c['id']
122-
toc << " - [#{c['title']}](##{child_id})\n"
123-
124-
children.sort_by {|k,v| k['id']}
125-
.select {|c| c['parent']==child_id}.each do |c|
126-
child_id = c['id']
127-
toc << " - [#{c['title']}](##{c['id']})\n"
128-
129-
children.sort_by {|k,v| k['id']}
130-
.select {|c| c['parent']==child_id}.each do |c|
131-
toc << " - [#{c['title']}](##{c['id']})\n"
141+
def output_toc(data)
142+
output = "\n\n### Contents\n\n"
143+
144+
projects = data['projects']
145+
categories = data['categories']
146+
147+
parents, children = categories.partition { |category| category['parent'].nil? }
148+
parents.each do |parent|
149+
parent_id = parent['id']
150+
# Only include parent categories with projects or with children that have projects
151+
if has_projects_recursive(projects, categories, parent_id)
152+
output << "- [#{parent['title']}](##{parent_id})\n"
153+
154+
children.sort_by { |category| category['id'] }
155+
.select { |category| category['parent'] == parent_id }.each do |child|
156+
child_id = child['id']
157+
158+
# Only include child categories with projects or with grandchildren that have projects
159+
if has_projects_recursive(projects, categories, child_id)
160+
output << " - [#{child['title']}](##{child_id})\n"
161+
162+
children.sort_by { |category| category['id'] }
163+
.select { |category| category['parent'] == child_id }.each do |grandchild|
164+
grandchild_id = grandchild['id']
165+
166+
# Only include grandchild categories with projects or with great-grandchildren that have projects
167+
if has_projects_recursive(projects, categories, grandchild_id)
168+
output << " - [#{grandchild['title']}](##{grandchild_id})\n"
169+
170+
children.sort_by { |category| category['id'] }
171+
.select { |category| category['parent'] == grandchild_id }.each do |great_grandchild|
172+
great_grandchild_id = great_grandchild['id']
173+
174+
# Only include great-grandchild categories with projects
175+
if has_projects(projects, great_grandchild_id)
176+
output << " - [#{great_grandchild['title']}](##{great_grandchild_id})\n"
177+
end
178+
end
179+
end
180+
end
132181
end
133182
end
134183
end
135184
end
136185

137-
toc
186+
output
138187
end
139188

140-
def write_readme(j, filename)
141-
output = output_header(j)
142-
output << output_toc(j)
143-
output << output_content(j)
144-
output << output_contributing(j)
189+
def write_readme(data, filename)
190+
output = output_header(data)
191+
output << output_toc(data)
192+
output << output_content(data)
193+
output << output_contributing(data)
145194

146-
File.open(filename, 'w') { |f| f.write output}
147-
puts "Wrote #{filename} :-)"
195+
File.open(filename, 'w') { |file| file.write output }
196+
puts "Wrote #{filename} :-)"
148197
end
149198

150-
j = get_json()
151-
write_readme(j, README)
199+
data = get_json()
200+
write_readme(data, README)

0 commit comments

Comments
 (0)