This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (66 loc) · 2.45 KB
/
index.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
var express = require("express");
var path = require("path");
var fs = require("fs");
var glob = require("glob");
var MarkdownIt = require("markdown-it");
var hljs = require("highlight.js");
var bodyParser = require("body-parser");
const { exec } = require('child_process');
// Start date of the semester
var startTime = new Date('2024-01-18T10:00:00Z');
var app = express();
var port = process.env.PORT || 3000;
var md = new MarkdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (__) { }
}
return ""; // use external default escaping
},
});
var labs = {};
glob("src/lab*/manifest.json", {}, function (err, files) {
files.forEach((file) => {
const labPath = file.split("/").slice(0, -1).join("/");
const labName = labPath.split("/").slice(-1).pop();
const index = parseInt(labName.match(new RegExp("lab(\\d+)"))[1]);
var manifest = JSON.parse(fs.readFileSync(file), {
encoding: "utf8",
});
var markdown = fs
.readFileSync(path.join(labPath, "note.marp.md"))
.toString();
// Calculate the target time by adding n * 7 days to the start time
var targetTime = new Date(startTime.getTime() + index * 7 * 24 * 60 * 60 * 1000);
labs[labName] = {
enabledSince: targetTime,
md: md.render(markdown),
pdf: path.join(labPath.replace('src/', ''), "note.marp.pdf"),
video: manifest.video
};
});
});
app.use(express.static("src"));
app.use("/public", express.static("public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.set("view engine", "pug");
app.get("/", (req, res) => {
res.render("index", { labs });
});
app.post("/coolc", (req, res) => {
const command = `echo '${req.body.code}' > temp.cool && coolc temp.cool && coolspim temp.s`;
exec(command, (_, stdout, stderr) => {
res.json({ result: `${stdout}\n${stderr}`.trim(), code: req.body.code });
});
});
app.post("/format", (req, res) => {
const PA4jar = '/usr/local/lib/PA4.jar';
const command = `echo '${req.body.code}' > temp.cool && coolc -P p temp.cool > temp.in && scalac -nowarn -cp .:${PA4jar} PrettyPrinter.scala && scala -cp .:${PA4jar} Main < temp.in`;
exec(command, (_, stdout, stderr) => {
res.json({ result: `${stdout}\n${stderr}`.trim(), code: req.body.code });
});
});
app.listen(port, () => console.log(`app listening on port ${port}!`));