Browse Source

Fix code highlight for judge detail

master
Menci 8 years ago
parent
commit
f6e7180a43
  1. 2
      modules/judge.js
  2. 15
      utility.js

2
modules/judge.js

@ -66,7 +66,7 @@ app.get('/judge_detail/:id', async (req, res) => {
if (!await judge.isAllowedSeeCodeBy(res.locals.user)) throw 'Permission denied';
judge.code = syzoj.utils.highlight(judge.code);
judge.code = syzoj.utils.highlight(judge.code, judge.language);
res.render('judge_detail', {
judge: judge

15
utility.js

@ -84,8 +84,19 @@ module.exports = {
if (encoded) res += '?' + encoded;
return res;
},
highlight(code) {
return highlightjs.highlightAuto(code).value;
escapeHTML(s) {
// Code from http://stackoverflow.com/questions/5251520/how-do-i-escape-some-html-in-javascript/5251551
return s.replace(/[^0-9A-Za-z ]/g, (c) => {
return "&#" + c.charCodeAt(0) + ";";
});
},
highlight(code, lang) {
try {
if (!lang) return highlightjs.highlightAuto(code).value;
else return highlightjs.highlight(lang, code).value;
} catch (e) {
return escapeHTML(code);
}
},
gravatar(email, size) {
return gravatar.url(email, { s: size, d: 'mm' }).replace('www', 'cn');

Loading…
Cancel
Save