99 lines
4.2 KiB
PHP
Executable File
99 lines
4.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace WMarkDown\Modules;
|
|
|
|
class CodeBlocks extends \WMarkDown\Abstracts\Modules{
|
|
|
|
private $matches_start = null;
|
|
private $matches_end = null;
|
|
private $delimiter = null;
|
|
private $start_characters = null;
|
|
private static $ampersand_code = [
|
|
"<" => "lt",
|
|
">" => "gt",
|
|
"&" => "emp"
|
|
];
|
|
|
|
public function get_next_position($string){
|
|
if(!$this->more || $this->from >= 0)
|
|
return;
|
|
$this->more = false;
|
|
|
|
$lower = null;
|
|
|
|
foreach(["```", "'''", '"""', "---"] as $base)
|
|
foreach([$base, preg_replace('/(.)/', "$1 ", $base)] as $delimiter){
|
|
if(
|
|
preg_match('/^' . $delimiter . '([^\n\r]+)?/m', $string, $matches_start, PREG_OFFSET_CAPTURE) &&
|
|
preg_match('/^' . $delimiter . '/m', substr($string, $matches_start[0][1] + ($start_characters = strlen($matches_start[0][0]))), $matches_end, PREG_OFFSET_CAPTURE) &&
|
|
($lower === null || $lower > $matches_start[0][1])
|
|
){
|
|
$lower = $matches_start[0][1];
|
|
$this->more = true;
|
|
$this->from = $matches_start[0][1];
|
|
$this->matches_start = $matches_start;
|
|
$this->matches_end = $matches_end;
|
|
$this->delimiter = $delimiter;
|
|
$this->start_characters = $start_characters;
|
|
};
|
|
};
|
|
|
|
}
|
|
|
|
public function process($string, $modules, $level){
|
|
|
|
$lines = '';
|
|
$data_html = '';
|
|
|
|
$this->more = true;
|
|
$this->length = $this->matches_end[0][1] + strlen($this->delimiter) + $this->start_characters;
|
|
$this->to = $this->from + $this->length;
|
|
$this->content = preg_replace_callback('/[<>&]/', function($values){
|
|
if(isset(\WMarkDown\Modules\CodeBlocks::$ampersand_code[$values[0]]))
|
|
return "&" . \WMarkDown\Modules\CodeBlocks::$ampersand_code[$values[0]] . ";";
|
|
return $values[0];
|
|
}, preg_replace('/^(\r\n|[\r\n])|(\r\n|[\r\n])$/', "", substr($string, $this->from + $this->start_characters, $this->matches_end[0][1])));
|
|
|
|
$language = isset($this->matches_start[1]) ? $this->matches_start[1][0] : "text";
|
|
$is_special = in_array($language, $this->wmarkdown->settings("special_code_blocks"));
|
|
|
|
foreach(($lines_splitted = preg_split('/\r\n|[\r\n]/', $this->content)) as $line)
|
|
$lines .= '<li></li>';
|
|
|
|
foreach([[
|
|
"name" => "language",
|
|
"text" => "Language",
|
|
"data" => $language
|
|
], [
|
|
"name" => "lines",
|
|
"text" => "Lines",
|
|
"data" => count($lines_splitted)
|
|
], [
|
|
"name" => "characters",
|
|
"text" => "Characters",
|
|
"data" => strlen($this->content)
|
|
]] as $data)
|
|
$data_html .= $this->wmarkdown->string_variables(('
|
|
<li class="{name}" data-i18n="{name}" data-i18n-without="true" title="{text}">
|
|
<span data-icon="{name}"></span>
|
|
<span data-i18n="{name}">{text}</span>
|
|
<span class="data">{data}</span>
|
|
</li>
|
|
'), $data);
|
|
|
|
$this->html = ('
|
|
<pre class="code-block" data-lang="' . $language . '" data-special="' . ($is_special ? "true" : "false") . '">
|
|
<ul class="data">' . $data_html . '</ul>
|
|
' . ($is_special ? ('
|
|
<div class="' . $language . '">' . $this->content . '</div>
|
|
') : ('
|
|
<div class="lines"><ol>' . $lines . '</ol></div>
|
|
<div class="code-box" onscroll="wmarkdown.block_code_scroll(this, event);">' . $this->content . '</div>
|
|
')) . '
|
|
</pre>
|
|
');
|
|
|
|
}
|
|
|
|
};
|