-
Notifications
You must be signed in to change notification settings - Fork 8
/
sponsors-html.zig
169 lines (151 loc) · 5.34 KB
/
sponsors-html.zig
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const std = @import("std");
pub const log_level: std.log.Level = .warn;
const usage =
\\Usage: sponsors-html <tool>
\\
\\Available tools:
\\ homepage generate the sponsors section for the homepage
\\ release generate the sponsors section for releases
\\
;
const Tier = struct {
id: []const u8,
homepage: Mention,
release: Mention,
amt: u32,
};
const Mention = enum { none, name, hyperlink };
/// Manually sorted descending by amount.
const tiers = [_]Tier{
.{
.id = "ST_kwHOAarWdc3EaQ",
.homepage = .hyperlink,
.release = .hyperlink,
.amt = 5000,
},
.{
.id = "ST_kwHOAarWdc3DXg",
.homepage = .hyperlink,
.release = .hyperlink,
.amt = 1200,
},
.{
.id = "ST_kwHOAarWdc2FHQ",
.homepage = .hyperlink,
.release = .hyperlink,
.amt = 400,
},
.{
.id = "ST_kwHOAarWdc2FGw",
.homepage = .name,
.release = .hyperlink,
.amt = 200,
},
.{
.id = "ST_kwHOAarWdc2FFg",
.homepage = .none,
.release = .hyperlink,
.amt = 100,
},
.{
.id = "ST_kwHOAarWdc2FFw",
.homepage = .none,
.release = .name,
.amt = 50,
},
};
fn dumpUsageAndExit() noreturn {
std.debug.print("{s}", .{usage});
std.process.exit(1);
}
pub fn main() !void {
var arena_allocator = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const arena = arena_allocator.allocator();
const args = try std.process.argsAlloc(arena);
if (args.len < 2) dumpUsageAndExit();
const Tool = enum { homepage, release };
const tool = std.meta.stringToEnum(Tool, args[1]) orelse dumpUsageAndExit();
var dir = try std.fs.cwd().openDir("tiers-data", .{});
const stdout_file = std.io.getStdOut();
var unbuffered_stdout = stdout_file.writer();
var bw = std.io.bufferedWriter(unbuffered_stdout);
const stdout = bw.writer();
for (tiers) |tier| {
const basename = try std.fmt.allocPrint(arena, "{s}.json", .{tier.id});
std.log.debug("looking at {s}", .{basename});
const json_text = try dir.readFileAlloc(arena, basename, 10 * 1024 * 1024);
var parser = std.json.Parser.init(arena, .alloc_always);
const tree = try parser.parse(json_text);
const data_obj = &tree.root.object.get("data").?.object;
const sponsors_obj = &data_obj.get("organization").?.object.get("sponsors").?.object;
const nodes_array = &sponsors_obj.get("nodes").?.array;
for (nodes_array.items) |node| {
const skip = switch (tool) {
.homepage => tier.homepage == .none,
.release => tier.release == .none,
};
if (skip) continue;
const name = name: {
if (node.object.get("name")) |n| switch (n) {
.string => |s| break :name s,
else => {},
};
break :name node.object.get("login").?.string;
};
const need_website = switch (tool) {
.homepage => tier.homepage == .hyperlink,
.release => tier.release == .hyperlink,
};
var website: ?[]const u8 = null;
if (need_website) website: {
if (node.object.get("websiteUrl")) |n| switch (n) {
.string => |s| {
website = s;
break :website;
},
else => {},
};
if (node.object.get("twitterUsername")) |n| switch (n) {
.string => |s| {
website = try std.fmt.allocPrint(arena, "https://twitter.com/{s}", .{s});
break :website;
},
else => {},
};
const login = node.object.get("login").?.string;
website = try std.fmt.allocPrint(arena, "https://github.com/{s}", .{login});
}
switch (tool) {
.homepage => {
const escaped_name = try escapeHtml(arena, name);
if (website) |w| {
const escaped_w = try escapeHtml(arena, w);
try stdout.print(
\\<li><a href="{s}" rel="nofollow noopener" target="_blank" class="external-link">{s}</a></li>
\\
, .{
escaped_w, escaped_name,
});
} else {
try stdout.print("<li>{s}</li>\n", .{escaped_name});
}
},
.release => {
const escaped_name = try escapeHtml(arena, name);
if (website) |w| {
const escaped_w = try escapeHtml(arena, w);
try stdout.print("<li><a href=\"{s}\">{s}</a></li>\n", .{
escaped_w, escaped_name,
});
} else {
try stdout.print("<li>{s}</li>\n", .{escaped_name});
}
},
}
}
}
try bw.flush();
}
fn escapeHtml(arena: std.mem.Allocator, text: []const u8) ![]u8 {
return arena.dupe(u8, text);
}