S2OJ/web/app/models/UOJMarkdown.php

123 lines
3.4 KiB
PHP
Raw Normal View History

2022-10-18 00:53:57 +00:00
<?php
class UOJMarkdown extends ParsedownMath {
public function __construct($options = '') {
2023-01-15 03:13:43 +00:00
if (method_exists(get_parent_class(), "__construct")) {
2022-10-18 00:53:57 +00:00
parent::__construct($options);
}
2023-01-15 03:13:43 +00:00
$this->options['username_with_color'] = $options['username_with_color'] ?: false;
2023-01-30 12:27:53 +00:00
// Special Block
$this->inlineMarkerList .= '@';
$this->InlineTypes['@'][] = 'SpecialBlock';
2022-10-18 00:53:57 +00:00
// https://gist.github.com/ShNURoK42/b5ce8baa570975db487c
$this->InlineTypes['@'][] = 'UserMention';
}
// https://github.com/taufik-nurrohman/parsedown-extra-plugin/blob/1653418c5a9cf5277cd28b0b23ba2d95d18e9bc4/ParsedownExtraPlugin.php#L340-L345
protected function doGetAttributes($Element) {
if (isset($Element['attributes'])) {
return (array) $Element['attributes'];
}
return array();
}
// https://github.com/taufik-nurrohman/parsedown-extra-plugin/blob/1653418c5a9cf5277cd28b0b23ba2d95d18e9bc4/ParsedownExtraPlugin.php#L347-L358
2023-01-15 03:13:43 +00:00
protected function doGetContent($Element) {
if (isset($Element['text'])) {
return $Element['text'];
}
if (isset($Element['rawHtml'])) {
return $Element['rawHtml'];
}
if (isset($Element['handler']['argument'])) {
return implode("\n", (array) $Element['handler']['argument']);
}
return null;
}
2022-10-18 00:53:57 +00:00
// https://github.com/taufik-nurrohman/parsedown-extra-plugin/blob/1653418c5a9cf5277cd28b0b23ba2d95d18e9bc4/ParsedownExtraPlugin.php#L369-L378
protected function doSetAttributes(&$Element, $From, $Args = array()) {
$Attributes = $this->doGetAttributes($Element);
$Content = $this->doGetContent($Element);
if (is_callable($From)) {
$Args = array_merge(array($Content, $Attributes, &$Element), $Args);
$Element['attributes'] = array_replace($Attributes, (array) call_user_func_array($From, $Args));
} else {
$Element['attributes'] = array_replace($Attributes, (array) $From);
}
}
// Add classes to <table>
protected function blockTableComplete($Block) {
$this->doSetAttributes($Block['element'], ['class' => 'table table-bordered']);
return $Block;
}
// https://gist.github.com/ShNURoK42/b5ce8baa570975db487c
2023-01-15 03:13:43 +00:00
protected function inlineUserMention($Excerpt) {
if (preg_match('/^@([^\s]+)/', $Excerpt['text'], $matches)) {
$mentioned_user = UOJUser::query($matches[1]);
2022-10-18 00:53:57 +00:00
2023-01-15 03:13:43 +00:00
if ($mentioned_user) {
2023-02-24 14:10:29 +00:00
$color = 'blue';
2023-01-15 03:13:43 +00:00
if ($this->options['username_with_color']) {
$color = UOJUser::getUserColor($mentioned_user);
}
return [
'extent' => strlen($matches[0]),
'element' => [
'name' => 'span',
'text' => '@' . $mentioned_user['username'],
'attributes' => [
2023-02-24 14:10:29 +00:00
'class' => "uoj-username uoj-username-{$color}",
2023-01-15 03:13:43 +00:00
'data-realname' => UOJUser::getRealname($mentioned_user),
'data-color' => $color,
],
],
];
}
return [
'extent' => strlen($matches[0]),
'markup' => $matches[0],
];
}
}
2023-01-30 12:27:53 +00:00
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;
}
2022-10-18 00:53:57 +00:00
}