✨ feat: 优化样例输入输出处理逻辑并增强复制功能
This commit is contained in:
@@ -254,41 +254,139 @@ if ($pr_flag) {
|
|||||||
<?php } ?>
|
<?php } ?>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
$sinput = str_replace("<", "<", $row['sample_input']);
|
function normalize_sample_text($text)
|
||||||
$sinput = str_replace(">", ">", $sinput);
|
{
|
||||||
$soutput = str_replace("<", "<", $row['sample_output']);
|
return str_replace(array("\r\n", "\r"), "\n", (string)$text);
|
||||||
$soutput = str_replace(">", ">", $soutput);
|
}
|
||||||
|
|
||||||
|
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) { ?>
|
||||||
<div class="row">
|
<?php foreach ($samplePairs as $sampleIndex => $samplePair) { ?>
|
||||||
<div class="column">
|
<div class="row">
|
||||||
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Input ?></h4>
|
<div class="column">
|
||||||
<!-- <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">
|
<div class="ui bottom attached segment font-content sample-box">
|
||||||
<span class="copy sample-copy" id="copyin"
|
<span class="copy sample-copy"
|
||||||
data-clipboard-text="<?php echo htmlentities($sinput, ENT_QUOTES, 'UTF-8'); ?>"><?php echo $MSG_COPY; ?></span>
|
data-clipboard-text="<?php echo format_sample_attr($samplePair['input']); ?>"><?php echo $MSG_COPY; ?></span>
|
||||||
<!-- <pre><?php echo ($sinput); ?></pre> -->
|
<pre
|
||||||
<pre
|
style="margin-top: 0; margin-bottom: 0; "><code class="lang-plain"><?php echo format_sample_code($samplePair['input']); ?></code></pre>
|
||||||
style="margin-top: 0; margin-bottom: 0; "><code id='sinput' class="lang-plain"><?php echo ($sinput); ?></code></pre>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<div class="row">
|
||||||
<?php } ?>
|
<div class="column">
|
||||||
<?php if (strlen($soutput) > 0 && $soutput != "\n" || isset($_GET['spa'])) { ?>
|
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Output . ' ' . ($sampleIndex + 1); ?></h4>
|
||||||
<div class="row">
|
<div class="ui bottom attached segment font-content sample-box">
|
||||||
<div class="column">
|
<span class="copy sample-copy"
|
||||||
<h4 class="ui top attached block header sample-header"><?php echo $MSG_Sample_Output ?></h4>
|
data-clipboard-text="<?php echo format_sample_attr($samplePair['output']); ?>"><?php echo $MSG_COPY; ?></span>
|
||||||
<!-- <span class=copy id=\"copyout\" data-clipboard-text=\"".($soutput)."\"><?php echo $MSG_COPY; ?></span> -->
|
<pre
|
||||||
<div class="ui bottom attached segment font-content sample-box">
|
style="margin-top: 0; margin-bottom: 0; "><code class="lang-plain"><?php echo format_sample_code($samplePair['output']); ?></code></pre>
|
||||||
<span class="copy sample-copy" id="copyout"
|
</div>
|
||||||
data-clipboard-text="<?php echo htmlentities($soutput, ENT_QUOTES, 'UTF-8'); ?>"><?php echo $MSG_COPY; ?></span>
|
|
||||||
<!-- <div class="ui existing segment"> -->
|
|
||||||
<pre
|
|
||||||
style="margin-top: 0; margin-bottom: 0; "><code id='soutput' class="lang-plain"><?php echo ($soutput); ?></code></pre>
|
|
||||||
<!-- </div> -->
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<?php } ?>
|
||||||
|
<?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>
|
||||||
|
<div class="ui bottom attached segment font-content sample-box">
|
||||||
|
<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 class="lang-plain"><?php echo format_sample_code($singleSampleOutput); ?></code></pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php } ?>
|
||||||
<?php } ?>
|
<?php } ?>
|
||||||
<?php if ($row['hint'] || isset($_GET['spa'])) { ?>
|
<?php if ($row['hint'] || isset($_GET['spa'])) { ?>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
@@ -701,34 +799,23 @@ if ($pr_flag) {
|
|||||||
|
|
||||||
|
|
||||||
<script>
|
<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]);
|
clipboard.on('success', function (e) {
|
||||||
clipboardin.on('success', function (e) {
|
button.text("<?php echo $MSG_COPY . $MSG_SUCCESS; ?>!");
|
||||||
$("#copyin").text("<?php echo $MSG_COPY . $MSG_SUCCESS; ?>!");
|
setTimeout(function () { button.text(defaultLabel); }, 1500);
|
||||||
setTimeout(function () { $("#copyin").text("<?php echo $MSG_COPY; ?>"); }, 1500);
|
|
||||||
console.log(e);
|
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]);
|
clipboard.on('error', function (e) {
|
||||||
clipboardout.on('success', function (e) {
|
button.text("<?php echo $MSG_COPY . $MSG_FAIL; ?>!");
|
||||||
$("#copyout").text("<?php echo $MSG_COPY . $MSG_SUCCESS; ?>!");
|
setTimeout(function () { button.text(defaultLabel); }, 1500);
|
||||||
setTimeout(function () { $("#copyout").text("<?php echo $MSG_COPY; ?>"); }, 1500);
|
|
||||||
console.log(e);
|
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>
|
</script>
|
||||||
<?php if (isset($OJ_MATHJAX) && $OJ_MATHJAX) { ?>
|
<?php if (isset($OJ_MATHJAX) && $OJ_MATHJAX) { ?>
|
||||||
|
|||||||
Reference in New Issue
Block a user