-
Notifications
You must be signed in to change notification settings - Fork 291
/
poster_data_utils.js
209 lines (192 loc) · 5.68 KB
/
poster_data_utils.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint-disable @typescript-eslint/no-var-requires */
const glob = require("glob")
const fs = require("fs")
const yaml = require("js-yaml")
const path = require("path")
const getGroupsBySlug = async graphql => {
const allPosterPrevImagesByRelativeDirectory = indexByRelativeDirectory(
await query_poster_prev_images(graphql),
)
const allPosterPdfsByRelativeDirectory = indexByRelativeDirectory(
await query_poster_pdfs(graphql),
)
const groups = glob
.sync("content/posters/**/group_info.yaml")
.map(pathToGroupInfo =>
load_group(
pathToGroupInfo,
allPosterPrevImagesByRelativeDirectory,
allPosterPdfsByRelativeDirectory,
),
)
return groupBy(groups, group => `/${group["year"]}/Q${group["quarter"]}`)
}
const query_poster_prev_images = async graphql => {
return await graphql(`
query {
allFile(
filter: {
relativePath: { regex: "/posters/.*/" }
extension: { regex: "/(jpg)|(jpeg)|(png)/" }
}
) {
edges {
node {
id
relativeDirectory
publicURL
childImageSharp {
fixed(width: 250, height: 200, cropFocus: CENTER) {
base64
width
height
src
srcSet
}
}
extension
}
}
}
}
`)
}
const indexByRelativeDirectory = results => {
return results.data.allFile.edges.reduce((indexedMap, edge) => {
indexedMap[path.join("content", edge.node.relativeDirectory)] = edge.node
return indexedMap
}, {})
}
const query_poster_pdfs = async graphql => {
return await graphql(`
query {
allFile(
filter: {
relativePath: { regex: "/posters/[0-9]+/" }
extension: { regex: "/(pdf)/" }
}
) {
edges {
node {
id
relativeDirectory
publicURL
}
}
}
}
`)
}
const load_group = (
pathToGroupInfo,
allPosterPrevImagesByRelativeDirectory,
allPosterPdfsByRelativeDirectory,
) => {
const group_root_path = path.dirname(pathToGroupInfo)
const group_info = yaml.load(fs.readFileSync(pathToGroupInfo, "utf8"))
group_info["students"] = glob
.sync(`${group_root_path}/**/student_info.yaml`)
.map(pathToStudentFolder =>
load_student_data(
pathToStudentFolder,
allPosterPrevImagesByRelativeDirectory,
allPosterPdfsByRelativeDirectory,
),
)
const requiredKeys = ["projectTitle", "quarter", "supervisors", "year"]
requiredKeys.forEach(requiredKey => {
if (!(requiredKey in group_info)) {
throw new Error(
`The required attribute "${requiredKey}" is missing. Please add a this attribute in the file:"\n${pathToGroupInfo}\n"`,
)
}
})
return group_info
}
const load_student_data = (
pathToStudentFolder,
allPosterPrevImagesByRelativeDirectory,
allPosterPdfsByRelativeDirectory,
) => {
const student_data = yaml.load(fs.readFileSync(pathToStudentFolder, "utf8"))
const poster_path = path.join(path.dirname(pathToStudentFolder), "poster.pdf")
if (!fs.existsSync(poster_path)) {
throw new Error(
`There is not pdf file for the poster. Please add a pdf of your poster at:"\n${poster_path}\n"`,
)
}
const image_path = path.join(path.dirname(pathToStudentFolder), "poster.jpg")
if (!fs.existsSync(image_path)) {
throw new Error(
`The poster has no preview image. Please add a preview image at:"\n${image_path}\n"`,
)
}
const requiredKeys = ["firstName", "lastName", "paperUrl", "thesisTitle"]
requiredKeys.forEach(requiredKey => {
if (!(requiredKey in student_data)) {
throw new Error(
`The required attribute "${requiredKey}" is missing. Please add a this attribute in the file:"\n${pathToStudentFolder}\n"`,
)
}
})
const paper_domain =
/(^$|(https?:\/\/(.+?\.)?tudelft\.nl\/([A-Za-z0-9\/\-:%\?=]*)))/g
if (
"paperUrl" in student_data &&
!student_data["paperUrl"].match(paper_domain)
) {
throw new Error(
`The poster at ${pathToStudentFolder} links to an invalid domain:"\n${student_data["paperUrl"]}\n"`,
)
}
const repo_domain =
/(^$|(https:\/\/(gitlab((\.ewi)?(\.tudelft\.nl)|\.com)|(github|hub\.docker)\.com(\/r)?)(\/[A-Za-z0-9\.\-_]*)+))/g
if (
"repositoryUrl" in student_data &&
!student_data["repositoryUrl"].match(repo_domain)
) {
throw new Error(
`The repository at ${pathToStudentFolder} links to an invalid domain:"\n${student_data["repositoryUrl"]}\n"`,
)
}
//if windows then replace / with \
if (process.platform === "win32") {
pathToStudentFolder = pathToStudentFolder.replace(/\//g, "\\")
const newpath = pathToStudentFolder.replace(/\//g, "\\"); // Windows path
student_data["prevImage"] =
allPosterPrevImagesByRelativeDirectory[path.dirname(newpath)]
student_data["pdfUrl"] =
allPosterPdfsByRelativeDirectory[path.dirname(newpath)][
"publicURL"
]
} else {
student_data["prevImage"] = allPosterPrevImagesByRelativeDirectory[path.dirname(pathToStudentFolder)]
student_data["pdfUrl"] = allPosterPdfsByRelativeDirectory[path.dirname(pathToStudentFolder)][
"publicURL"
]
}
return student_data
}
const compareGroupsByTitle = (a, b) => {
if (a.projectTitle < b.projectTitle) {
return -1
}
if (a.projectTitle > b.projectTitle) {
return 1
}
return 0
}
const groupBy = function (xs, getKey) {
return xs.reduce(function (acc, x) {
const key = getKey(x)
if (!acc[key]) {
acc[key] = []
}
acc[key].push(x)
return acc
}, {})
}
module.exports = {
getGroupsBySlug,
compareGroupsByTitle,
}