diff --git a/modules/judge.js b/modules/judge.js index f3458f2..d6fa6c3 100644 --- a/modules/judge.js +++ b/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 diff --git a/utility.js b/utility.js index d295436..fda5b52 100644 --- a/utility.js +++ b/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');