forked from matteocrippa/awesome-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.rb
153 lines (119 loc) · 3.54 KB
/
convert.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
README = 'README.md'
CONTENTS = 'contents.json'
def get_json()
require 'json'
JSON.parse(File.read CONTENTS)
end
def output_linux(tags)
return '' if tags.nil?
return ':penguin: ' if tags.include? 'linux'
''
end
def output_projects(proj, id)
o = ''
proj.select {|p| p['category']==id }
.sort_by {|k,v| k['title'].downcase}
.each do |p|
o << "* [#{p['title']}](#{p['homepage']}) #{output_linux p['tags']}- #{p['description']}\n"
end
o
end
def output_content_category(c, indent)
toc = "\n"
for i in 1..indent
toc << '#'
end
toc << " #{c['title']}\n"
toc << "*#{c['description']}* " unless c['description'].nil?
toc << "[back to top](#readme) \n" if indent>2
toc << "\n"
toc
end
def output_content(j)
toc = ''
projects = j['projects']
parents, children = j['categories'].partition { |c| c['parent'].nil? }
parents.each do |c|
id = c['id']
toc << output_content_category(c, 2)
toc << output_projects(projects, id)
children.sort_by {|k,v| k['id']}
.select {|c| c['parent']==id}.each do |c|
child_id = c['id']
toc << output_content_category(c, 3)
toc << output_projects(projects, child_id)
children.sort_by {|k,v| k['id']}
.select {|c| c['parent']==child_id}.each do |c|
child_id = c['id']
toc << output_content_category(c, 4)
toc << output_projects(projects, c['id'])
children.sort_by {|k,v| k['id']}
.select {|c| c['parent']==child_id}.each do |c|
child_id = c['id']
toc << output_content_category(c, 5)
toc << output_projects(projects, c['id'])
end
end
end
end
toc
end
def output_header(j)
header = j['header']
app = j['ios_app_link']
num_projects = j['projects'].count
o = header
o << "\n\n"
o << output_table(app, num_projects)
o
end
def output_contributing(j)
o = "\n\n### Contributing\n\n"
o << j['header_contributing']
o
end
def output_table(ios_app_link, num_projects)
require 'date'
date = DateTime.now
date_display = date.strftime "%B %d, %Y"
o = "| iOS App | Awesome | Linux | Projects | Updated\n| :-: | :-: | :-: | :-: | :-:\n"
o << "| [![Download on the App Store](https://img.shields.io/badge/download-app%20store-ff69b4.svg)](#{ios_app_link}) | "
o << '[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) | '
o << ' :penguin: | '
o << "#{num_projects} | "
o << date_display
o
end
def output_toc(j)
toc = "\n\n### Contents\n\n"
parents, children = j['categories'].partition { |c| c['parent'].nil? }
parents.each do |c|
id = c['id']
toc << "- [#{c['title']}](##{id})\n"
children.sort_by {|k,v| k['id']}
.select {|c| c['parent']==id}.each do |c|
child_id = c['id']
toc << " - [#{c['title']}](##{child_id})\n"
children.sort_by {|k,v| k['id']}
.select {|c| c['parent']==child_id}.each do |c|
child_id = c['id']
toc << " - [#{c['title']}](##{c['id']})\n"
children.sort_by {|k,v| k['id']}
.select {|c| c['parent']==child_id}.each do |c|
toc << " - [#{c['title']}](##{c['id']})\n"
end
end
end
end
toc
end
def write_readme(j, filename)
output = output_header(j)
output << output_toc(j)
output << output_content(j)
output << output_contributing(j)
File.open(filename, 'w') { |f| f.write output}
puts "Wrote #{filename} :-)"
end
j = get_json()
write_readme(j, README)