From f6e7180a43651903079a45c4ca316cafc9311736 Mon Sep 17 00:00:00 2001 From: Menci Date: Sun, 27 Nov 2016 16:07:43 +0800 Subject: [PATCH] Fix code highlight for judge detail --- modules/judge.js | 2 +- utility.js | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) 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');