-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderReport.mjs
138 lines (107 loc) · 4.4 KB
/
renderReport.mjs
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
import { readFileSync, writeFileSync } from 'fs';
import { config } from 'dotenv';
import https from 'https';
config();
const results = JSON.parse(readFileSync('output/analysis.json', 'utf8'));
const templateString = (data, comment, newContent) => {
const index = data.indexOf(comment);
if (index === -1 ) {
throw new Error("Couldn't find comment");
}
return data.substring(0, index) + newContent + data.substring(index + comment.length);
}
let summaryString = "\n";
summaryString += "| Party | # of MPs | # of MPs mentioning their party | # of MPs not mentioning their party | # of MPs not on Twitter |\n";
summaryString += "| - | :-: | :-: | :-: | :-: |\n";
const partiesSortedBySize = Object.keys(results)
.sort((a, b) => {
const size = results[b].total - results[a].total;
return size !== 0 ? size : a.localeCompare(b);
});
const renderNumberWithPercent = (numerator, denominator) => {
const percent = 100*numerator/denominator;
return `${numerator} (${percent.toFixed(0)}%)`;
}
partiesSortedBySize
.filter(party => results[party].total > 9)
.forEach(party => {
const partyResults = results[party];
summaryString += `| ${party} | ${partyResults.total} | ${renderNumberWithPercent(partyResults.proud.length, partyResults.total)} | ${renderNumberWithPercent(partyResults.shy.length, partyResults.total)} | ${renderNumberWithPercent(partyResults.invisible.length, partyResults.total)} |\n`;
});
let resultsString = "\n";
const sanitiseDescription = (description) =>
description
.replaceAll("\n", "<br>")
.replaceAll("|", "\\|");
const renderDetails = (summary, details) =>
`<details>
<summary>${summary}</summary>
${details}
</details>
`;
const renderResultsTable = (description, mpList, total) => {
let outputString = "| Name | Constituency | Bio |\n";
outputString += "| - | - | - |\n";
mpList.forEach(mp => {
outputString += `| [${mp.name}](https://twitter.com/${mp.twitterUsername}) | ${mp.constituency} | ${sanitiseDescription(mp.description)} |\n`;
});
return renderDetails(`${description} (${mpList.length} of ${total})`, outputString);
}
const renderTwitterlessResultsTable = (description, mpList, total) => {
let outputString = "| Name | Constituency |\n";
outputString += "| - | - |\n";
mpList.forEach(mp => {
outputString += `| ${mp.name} | ${mp.constituency} |\n`;
});
return renderDetails(`${description} (${mpList.length} of ${total})`, outputString);
}
partiesSortedBySize
.forEach(party => {
let partyString = "";
const totalCount = results[party].total;
if (results[party].proud.length) {
partyString += renderResultsTable("MPs mentioning their party", results[party].proud, totalCount);
}
if (results[party].shy.length) {
partyString += renderResultsTable("MPs not mentioning their party", results[party].shy, totalCount);
}
if (results[party].invisible.length) {
partyString += renderTwitterlessResultsTable("MPs not on Twitter", results[party].invisible, totalCount);
}
resultsString += renderDetails(party, partyString);
});
let markdownString = readFileSync('./template.markdown', 'utf8');
markdownString = templateString(markdownString, "<!--summary-auto-gen-->", summaryString);
markdownString = templateString(markdownString, "<!--results-auto-gen-->", resultsString);
const options = {
hostname: 'api.github.com',
path: '/markdown/raw',
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `${process.env.GITHUB_TOKEN}`,
'X-GitHub-Api-Version': '2022-11-28',
'Content-Type': 'text/plain',
'User-Agent': 'sigh'
}
};
const req = https.request(options, res => {
let htmlString = '';
res.setEncoding('utf8');
res.on('data', d => {
htmlString += d;
});
res.on('end', () => {
if (res.statusCode !== 200) {
console.error(`Request failed with status code ${res.statusCode} and body ${htmlString}`);
}
const templateHtml = readFileSync('./template.html', 'utf8');
htmlString = templateString(templateHtml, "<insert-content-here />", htmlString);
writeFileSync('./docs/index.html', htmlString, 'utf8');
});
});
req.on('error', error => {
console.error(error);
});
req.write(markdownString);
req.end();