feat(problem): insert pdf in statement
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Baoshuo Ren 2023-01-30 20:27:53 +08:00
parent 24c5ad4e5f
commit 5f7bf17fe0
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
12 changed files with 206 additions and 33 deletions

View File

@ -98,6 +98,12 @@ $new_remote_problem_form->handle = function (&$vdata) {
]);
$id = DB::insert_id();
dataNewProblem($id);
if ($data['type'] == 'pdf') {
file_put_contents(UOJContext::storagePath() , "/problem_resources/$id/statement.pdf", $data['pdf_data']);
$data['statement'] = "<div data-pdf data-src=\"/problem/$id/resources/statement.pdf\"></div>\n" . $data['statement'];
}
DB::insert([
"insert into problems_contents",
@ -105,7 +111,6 @@ $new_remote_problem_form->handle = function (&$vdata) {
"values",
DB::tuple([$id, HTML::purifier(['a' => ['target' => 'Enum#_blank']])->purify($data['statement']), '', ''])
]);
dataNewProblem($id);
DB::insert([
"insert into problems_tags",

View File

@ -2,6 +2,7 @@
requireLib('bootstrap5');
requireLib('hljs');
requireLib('mathjax');
requireLib('pdf.js');
requirePHPLib('form');
requirePHPLib('judger');

View File

@ -144,6 +144,11 @@ if (UOJProblem::info('type') == 'remote') {
],
]);
if ($data['type'] == 'pdf') {
file_put_contents(UOJContext::storagePath() . "/problem_resources/" . UOJProblem::info('id') . "/statement.pdf", $data['pdf_data']);
$data['statement'] = '<div data-pdf data-src="/problem/' . UOJProblem::info('id') . '/resources/statement.pdf"></div>' . "\n" . $data['statement'];
}
DB::update([
"update problems_contents",
"set", [

View File

@ -436,6 +436,10 @@ class HTML {
$def->addElement('footer', 'Block', 'Flow', 'Common');
mergeConfig($extra_allowed_html, [
'div' => [
'data-pdf' => 'Text',
'data-src' => 'URI',
],
'span' => [
'class' => 'Enum#uoj-username',
'data-realname' => 'Text',

View File

@ -27,7 +27,7 @@ class UOJBlogEditor {
$REQUIRE_LIB['blog-editor'] = '';
$this->validator = [
'title' => function(&$title) {
'title' => function (&$title) {
if ($title == '') {
return '标题不能为空';
}
@ -39,13 +39,13 @@ class UOJBlogEditor {
}
return '';
},
'content_md' => function(&$content_md) {
'content_md' => function (&$content_md) {
if (strlen($content_md) > 1000000) {
return '内容过长';
}
return '';
},
'tags' => function(&$tags) {
'tags' => function (&$tags) {
$tags = str_replace('', ',', $tags);
$tags_raw = explode(',', $tags);
if (count($tags_raw) > 10) {
@ -58,10 +58,10 @@ class UOJBlogEditor {
continue;
}
if (strlen($tag) > 30) {
return '标签 “' . HTML::escape($tag) .'” 太长';
return '标签 “' . HTML::escape($tag) . '” 太长';
}
if (in_array($tag, $tags, true)) {
return '标签 “' . HTML::escape($tag) .'” 重复出现';
return '标签 “' . HTML::escape($tag) . '” 重复出现';
}
$tags[] = $tag;
}
@ -113,7 +113,7 @@ class UOJBlogEditor {
if (preg_match('/^.*<!--.*readmore.*-->.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) {
$content_less = substr($this->post_data['content'], 0, $matches[0][1]);
$content_more = substr($this->post_data['content'], $matches[0][1] + strlen($matches[0][0]));
$this->post_data['content'] = $purifier->purify($content_less).'<!-- readmore -->'.$purifier->purify($content_more);
$this->post_data['content'] = $purifier->purify($content_less) . '<!-- readmore -->' . $purifier->purify($content_more);
} else {
$this->post_data['content'] = $purifier->purify($this->post_data['content']);
}
@ -123,14 +123,16 @@ class UOJBlogEditor {
die(json_encode(array('content_md' => '不合法的 YAML 格式')));
}
$marked = function($md) use ($parsedown, $purifier) {
$marked = function ($md) use ($parsedown, $purifier) {
$dom = new DOMDocument;
$dom->loadHTML(mb_convert_encoding($parsedown->text($md), 'HTML-ENTITIES', 'UTF-8'));
$elements = $dom->getElementsByTagName('li');
foreach ($elements as $element) {
$element->setAttribute('class',
$element->getAttribute('class') . ' fragment');
$element->setAttribute(
'class',
$element->getAttribute('class') . ' fragment'
);
}
return $purifier->purify($dom->saveHTML());
@ -181,7 +183,10 @@ class UOJBlogEditor {
if (isset($_POST['need_preview'])) {
ob_start();
if ($this->type == 'blog') {
$req_lib = array('mathjax' => '');
$req_lib = [
'mathjax' => '',
'pdf.js' => '',
];
if (isset($REQUIRE_LIB['bootstrap5'])) {
$req_lib['bootstrap5'] = '';

View File

@ -7,9 +7,12 @@ class UOJMarkdown extends ParsedownMath {
$this->options['username_with_color'] = $options['username_with_color'] ?: false;
// Special Block
$this->inlineMarkerList .= '@';
$this->InlineTypes['@'][] = 'SpecialBlock';
// https://gist.github.com/ShNURoK42/b5ce8baa570975db487c
$this->InlineTypes['@'][] = 'UserMention';
$this->inlineMarkerList .= '@';
}
// https://github.com/taufik-nurrohman/parsedown-extra-plugin/blob/1653418c5a9cf5277cd28b0b23ba2d95d18e9bc4/ParsedownExtraPlugin.php#L340-L345
@ -85,4 +88,35 @@ class UOJMarkdown extends ParsedownMath {
];
}
}
protected function inlineSpecialBlock($Excerpt) {
if (!isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') {
return;
}
$Excerpt['text'] = substr($Excerpt['text'], 1);
$Link = $this->inlineLink($Excerpt);
if ($Link === null) {
return;
}
$Inline = [
'extent' => $Link['extent'] + 1,
'element' => [
'name' => 'div',
'attributes' => [
'data-src' => $Link['element']['attributes']['href'],
"data-{$Link['element']['text']}" => $Link['element']['text'],
],
],
];
$Inline['element']['attributes'] += $Link['element']['attributes'];
unset($Inline['element']['attributes']['href']);
return $Inline;
}
}

View File

@ -196,13 +196,14 @@ class UOJRemoteProblem {
'time_limit' => null,
'memory_limit' => null,
'difficulty' => -1,
'pdf_data' => $res['response'],
'statement' => HTML::tag('h3', [], '提示') .
HTML::tag(
'p',
[],
'本题题面为 PDF 题面,请' .
'若无法正常加载 PDF,请' .
HTML::tag('a', ['href' => static::getCodeforcesProblemUrl($id), 'target' => '_blank'], '点此') .
'查看题面。'
'查看题面。'
),
];
} else {

View File

@ -265,6 +265,54 @@ if (!isset($ShowPageHeader)) {
<?= HTML::css_link('/css/font-awesome.min.css') ?>
<?php endif ?>
<?php if (isset($REQUIRE_LIB['pdf.js'])) : ?>
<!-- pdf.js -->
<?= HTML::js_src('/js/pdf.js') ?>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';
function randstr(prefix) {
return Math.random().toString(36).replace('0.', prefix || '');
}
$(document).ready(function() {
$('div[data-pdf]').each(function() {
var _this = $(this);
var pdf_src = $(this).data('src');
$(this).css('width', '100%').css('height', '100%');
var task = pdfjsLib.getDocument(pdf_src);
var id = 'pdf_' + task.docId;
$(this).attr('id', id + '_container');
task.promise.then(function(pdf) {
for (var i = 1; i <= pdf.numPages; i++) {
$(_this).append('<canvas id="' + id + '_page_' + i + '" class="pdf-page-canvas"></canvas>');
pdf.getPage(i).then(function(page) {
var viewport = page.getViewport({
scale: 2,
});
var canvas = document.getElementById(id + '_page_' + page.pageNumber);
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({
canvasContext: canvas.getContext('2d'),
viewport: viewport,
});
});
}
});
});
});
</script>
<?php endif ?>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>

View File

@ -238,6 +238,10 @@ h6,
margin-bottom: 1em;
}
.pdf-page-canvas {
width: 100% !important;
}
form.uoj-bs4-form-inline {
display: inline-block;
margin-right: 0.3em;

22
web/js/pdf.js Normal file

File diff suppressed because one or more lines are too long

22
web/js/pdf.sandbox.js Normal file

File diff suppressed because one or more lines are too long

22
web/js/pdf.worker.js vendored Normal file

File diff suppressed because one or more lines are too long