core: update buildContent

pull/156/head
undefined 3 years ago
parent 7bd7325db6
commit 308605f884

@ -11,7 +11,7 @@ import solution from 'hydrooj/dist/model/solution';
import problem from 'hydrooj/dist/model/problem';
import { PERM } from 'hydrooj/dist/model/builtin';
import { FileTooLargeError, ValidationError } from 'hydrooj/dist/error';
import type { ProblemConfigFile } from 'hydrooj';
import type { ContentNode, ProblemConfigFile } from 'hydrooj';
class FpsProblemImportHandler extends Handler {
async get() {
@ -20,22 +20,53 @@ class FpsProblemImportHandler extends Handler {
async run(domainId: string, result: any) {
for (const p of result.fps.item) {
const content = buildContent({
description: p.description,
input: p.input,
output: p.output,
samples: p.sample_input
? p.sample_input.map((si: string, i: number) => [si, p.sample_output[i]])
: [],
hint: p.hint,
}, 'html', this.translate.bind(this));
const content: ContentNode[] = [];
if (p.description?.[0]) {
content.push({
type: 'Text',
subType: 'html',
sectionTitle: this.translate('Description'),
text: p.description[0],
});
}
if (p.input?.[0]) {
content.push({
type: 'Text',
subType: 'html',
sectionTitle: this.translate('Input Format'),
text: p.input[0],
});
}
if (p.output?.[0]) {
content.push({
type: 'Text',
subType: 'html',
sectionTitle: this.translate('Output Format'),
text: p.output[0],
});
}
if (p.sample_input?.length) {
content.push(...p.sample_input.map((input, i) => ({
type: 'Sample',
sectionTitle: this.translate('Sample'),
payload: [input, p.sample_output[i]],
})));
}
if (p.hint?.[0]) {
content.push({
type: 'Text',
subType: 'html',
sectionTitle: this.translate('Hint'),
text: p.hint[0],
});
}
const config: ProblemConfigFile = {
time: p.time_limit[0]._ + p.time_limit[0].$.unit,
memory: p.memory_limit[0]._ + p.memory_limit[0].$.unit,
};
const title = decodeHTML(p.title.join(' '));
const tags = filter(p.source, (i: string) => i.trim());
const pid = await problem.add(domainId, null, title, content, this.user._id, tags);
const pid = await problem.add(domainId, null, title, buildContent(content, 'html'), this.user._id, tags);
await problem.addTestdata(domainId, pid, 'config.yaml', Buffer.from(`time: ${config.time}\nmemory: ${config.memory}`));
if (p.test_output) {
for (let i = 0; i < p.test_input.length; i++) {
@ -75,6 +106,7 @@ class FpsProblemImportHandler extends Handler {
const result = await xml2js.parseStringPromise(content);
tasks.push(result);
} catch (e) {
console.log(e);
const zip = new AdmZip(this.request.files.file.path);
for (const entry of zip.getEntries()) {
try {

@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.28.35",
"version": "2.28.36",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",

@ -17,7 +17,8 @@ export function buildContent(source: ProblemSource | ContentNode[], type: 'markd
? source.map((node) => [
node.type !== 'Plain' ? `<h2>${node.sectionTitle}</h2>` : '',
node.type === 'Sample'
? `<h2>${_('Sample Input')}<h2><pre>${node.payload[0]}</pre><h2>${_('Sample Output')}</h2><pre>${node.payload[1]}</pre>`
? `<pre><code class="language-input${++cnt}">${node.payload[0]}</code></pre>`
+ `<pre><code class="language-output${cnt}">${node.payload[1]}</code></pre>`
: '',
node.text,
].join('\n')).join('\n')

Loading…
Cancel
Save