✨ feat: 优化样例输入输出处理逻辑并增强复制功能
This commit is contained in:
@@ -254,42 +254,140 @@ if ($pr_flag) {
|
||||
<?php } ?>
|
||||
|
||||
<?php
|
||||
$sinput = str_replace("<", "<", $row['sample_input']);
|
||||
$sinput = str_replace(">", ">", $sinput);
|
||||
$soutput = str_replace("<", "<", $row['sample_output']);
|
||||
$soutput = str_replace(">", ">", $soutput);
|
||||
function normalize_sample_text($text)
|
||||
{
|
||||
return str_replace(array("\r\n", "\r"), "\n", (string)$text);
|
||||
}
|
||||
|
||||
function trim_sample_chunk($text)
|
||||
{
|
||||
return trim(normalize_sample_text($text), "\n");
|
||||
}
|
||||
|
||||
function compact_sample_chunks($chunks)
|
||||
{
|
||||
$result = array();
|
||||
foreach ($chunks as $chunk) {
|
||||
$chunk = trim_sample_chunk($chunk);
|
||||
if ($chunk !== '') {
|
||||
$result[] = $chunk;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function split_sample_chunks_with_pattern($text, $pattern)
|
||||
{
|
||||
$chunks = preg_split($pattern, normalize_sample_text($text));
|
||||
if ($chunks === false) {
|
||||
return array();
|
||||
}
|
||||
return compact_sample_chunks($chunks);
|
||||
}
|
||||
|
||||
function build_sample_pairs($input, $output)
|
||||
{
|
||||
$input = normalize_sample_text($input);
|
||||
$output = normalize_sample_text($output);
|
||||
if ($input === '' || $output === '') {
|
||||
return array();
|
||||
}
|
||||
|
||||
$patterns = array(
|
||||
'/^\s*(?:sample|example|样例)\s*\d+\s*$/imu',
|
||||
'/^\s*(?:[-=*_]{3,})\s*$/m',
|
||||
"/\n\s*\n/"
|
||||
);
|
||||
|
||||
foreach ($patterns as $pattern) {
|
||||
$inputChunks = split_sample_chunks_with_pattern($input, $pattern);
|
||||
$outputChunks = split_sample_chunks_with_pattern($output, $pattern);
|
||||
|
||||
if (count($inputChunks) > 1 && count($inputChunks) === count($outputChunks)) {
|
||||
$pairs = array();
|
||||
for ($index = 0; $index < count($inputChunks); $index++) {
|
||||
$pairs[] = array(
|
||||
'input' => $inputChunks[$index],
|
||||
'output' => $outputChunks[$index]
|
||||
);
|
||||
}
|
||||
return $pairs;
|
||||
}
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
function format_sample_code($text)
|
||||
{
|
||||
return htmlspecialchars(trim_sample_chunk($text), ENT_NOQUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function format_sample_attr($text)
|
||||
{
|
||||
return htmlentities(trim_sample_chunk($text), ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
$sampleInputText = normalize_sample_text($row['sample_input']);
|
||||
$sampleOutputText = normalize_sample_text($row['sample_output']);
|
||||
$samplePairs = build_sample_pairs($sampleInputText, $sampleOutputText);
|
||||
$showSampleIndex = count($samplePairs) > 1;
|
||||
$singleSampleInput = trim_sample_chunk($sampleInputText);
|
||||
$singleSampleOutput = trim_sample_chunk($sampleOutputText);
|
||||
?>
|
||||
<?php if (strlen($sinput) > 0 && $sinput != "\n" || isset($_GET['spa'])) { ?>
|
||||
<?php if ($showSampleIndex) { ?>
|
||||
<?php foreach ($samplePairs as $sampleIndex => $samplePair) { ?>
|
||||
<div class="row">
|
||||
<div class="column">
|
||||
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Input ?></h4>
|
||||
<!-- <span class=copy id=\"copyin\" data-clipboard-text=\"".($sinput)."\"><?php echo $MSG_COPY; ?></span> -->
|
||||
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Input . ' ' . ($sampleIndex + 1); ?></h4>
|
||||
<div class="ui bottom attached segment font-content sample-box">
|
||||
<span class="copy sample-copy" id="copyin"
|
||||
data-clipboard-text="<?php echo htmlentities($sinput, ENT_QUOTES, 'UTF-8'); ?>"><?php echo $MSG_COPY; ?></span>
|
||||
<!-- <pre><?php echo ($sinput); ?></pre> -->
|
||||
<span class="copy sample-copy"
|
||||
data-clipboard-text="<?php echo format_sample_attr($samplePair['input']); ?>"><?php echo $MSG_COPY; ?></span>
|
||||
<pre
|
||||
style="margin-top: 0; margin-bottom: 0; "><code id='sinput' class="lang-plain"><?php echo ($sinput); ?></code></pre>
|
||||
style="margin-top: 0; margin-bottom: 0; "><code class="lang-plain"><?php echo format_sample_code($samplePair['input']); ?></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="column">
|
||||
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Output . ' ' . ($sampleIndex + 1); ?></h4>
|
||||
<div class="ui bottom attached segment font-content sample-box">
|
||||
<span class="copy sample-copy"
|
||||
data-clipboard-text="<?php echo format_sample_attr($samplePair['output']); ?>"><?php echo $MSG_COPY; ?></span>
|
||||
<pre
|
||||
style="margin-top: 0; margin-bottom: 0; "><code class="lang-plain"><?php echo format_sample_code($samplePair['output']); ?></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php if (strlen($soutput) > 0 && $soutput != "\n" || isset($_GET['spa'])) { ?>
|
||||
<?php } else { ?>
|
||||
<?php if ($singleSampleInput !== '' || isset($_GET['spa'])) { ?>
|
||||
<div class="row">
|
||||
<div class="column">
|
||||
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Input ?></h4>
|
||||
<div class="ui bottom attached segment font-content sample-box">
|
||||
<span class="copy sample-copy"
|
||||
data-clipboard-text="<?php echo format_sample_attr($singleSampleInput); ?>"><?php echo $MSG_COPY; ?></span>
|
||||
<pre
|
||||
style="margin-top: 0; margin-bottom: 0; "><code class="lang-plain"><?php echo format_sample_code($singleSampleInput); ?></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php if ($singleSampleOutput !== '' || isset($_GET['spa'])) { ?>
|
||||
<div class="row">
|
||||
<div class="column">
|
||||
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Output ?></h4>
|
||||
<!-- <span class=copy id=\"copyout\" data-clipboard-text=\"".($soutput)."\"><?php echo $MSG_COPY; ?></span> -->
|
||||
<div class="ui bottom attached segment font-content sample-box">
|
||||
<span class="copy sample-copy" id="copyout"
|
||||
data-clipboard-text="<?php echo htmlentities($soutput, ENT_QUOTES, 'UTF-8'); ?>"><?php echo $MSG_COPY; ?></span>
|
||||
<!-- <div class="ui existing segment"> -->
|
||||
<span class="copy sample-copy"
|
||||
data-clipboard-text="<?php echo format_sample_attr($singleSampleOutput); ?>"><?php echo $MSG_COPY; ?></span>
|
||||
<pre
|
||||
style="margin-top: 0; margin-bottom: 0; "><code id='soutput' class="lang-plain"><?php echo ($soutput); ?></code></pre>
|
||||
<!-- </div> -->
|
||||
style="margin-top: 0; margin-bottom: 0; "><code class="lang-plain"><?php echo format_sample_code($singleSampleOutput); ?></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
<?php if ($row['hint'] || isset($_GET['spa'])) { ?>
|
||||
<div class="row">
|
||||
<div class="column">
|
||||
@@ -701,34 +799,23 @@ if ($pr_flag) {
|
||||
|
||||
|
||||
<script>
|
||||
if ($('#copyin')[0] != undefined) {
|
||||
$('.sample-copy').each(function () {
|
||||
var button = $(this);
|
||||
var defaultLabel = button.text();
|
||||
var clipboard = new Clipboard(this);
|
||||
|
||||
var clipboardin = new Clipboard($('#copyin')[0]);
|
||||
clipboardin.on('success', function (e) {
|
||||
$("#copyin").text("<?php echo $MSG_COPY . $MSG_SUCCESS; ?>!");
|
||||
setTimeout(function () { $("#copyin").text("<?php echo $MSG_COPY; ?>"); }, 1500);
|
||||
clipboard.on('success', function (e) {
|
||||
button.text("<?php echo $MSG_COPY . $MSG_SUCCESS; ?>!");
|
||||
setTimeout(function () { button.text(defaultLabel); }, 1500);
|
||||
console.log(e);
|
||||
});
|
||||
clipboardin.on('error', function (e) {
|
||||
$("#copyin").text("<?php echo $MSG_COPY . $MSG_FAIL; ?>!");
|
||||
setTimeout(function () { $("#copyin").text("<?php echo $MSG_COPY; ?>"); }, 1500);
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
if ($('#copyout')[0] != undefined) {
|
||||
|
||||
var clipboardout = new Clipboard($('#copyout')[0]);
|
||||
clipboardout.on('success', function (e) {
|
||||
$("#copyout").text("<?php echo $MSG_COPY . $MSG_SUCCESS; ?>!");
|
||||
setTimeout(function () { $("#copyout").text("<?php echo $MSG_COPY; ?>"); }, 1500);
|
||||
clipboard.on('error', function (e) {
|
||||
button.text("<?php echo $MSG_COPY . $MSG_FAIL; ?>!");
|
||||
setTimeout(function () { button.text(defaultLabel); }, 1500);
|
||||
console.log(e);
|
||||
});
|
||||
clipboardout.on('error', function (e) {
|
||||
$("#copyout").text("<?php echo $MSG_COPY . $MSG_FAIL; ?>!");
|
||||
setTimeout(function () { $("#copyout").text("<?php echo $MSG_COPY; ?>"); }, 1500);
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
|
||||
</script>
|
||||
<?php if (isset($OJ_MATHJAX) && $OJ_MATHJAX) { ?>
|
||||
|
||||
Reference in New Issue
Block a user