57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace WMarkDown\Modules;
|
||
|
|
||
|
class Headers extends \WMarkDown\Abstracts\Modules{
|
||
|
|
||
|
private static $ids = [];
|
||
|
private $id;
|
||
|
private $level;
|
||
|
|
||
|
public static function id($text){
|
||
|
|
||
|
$id = preg_replace('/[^a-z\d\-]+/i', "-", trim($text));
|
||
|
$test = "" . $id;
|
||
|
$i = 0;
|
||
|
|
||
|
while(in_array($test, self::$ids))
|
||
|
$test = $id . (++ $i == 1 ? "" : "-" . $i);
|
||
|
|
||
|
self::$ids[] = $id = preg_replace('/\-{2,}/', "-", $test);
|
||
|
|
||
|
return $id;
|
||
|
}
|
||
|
|
||
|
public function get_next_position($string){
|
||
|
if(!$this->more || $this->from >= 0)
|
||
|
return;
|
||
|
|
||
|
$last = null;
|
||
|
|
||
|
foreach(['/^(\={1,6})(.+)\={1,6}?$/m', '/^(\#{1,6})(.+)$/m'] as $pattern)
|
||
|
if(preg_match($pattern, $string, $matches, PREG_OFFSET_CAPTURE) && ($last === null || $last > $matches[0][1])){
|
||
|
$this->from = $matches[0][1];
|
||
|
$this->matches = $matches;
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
public function process($string, $modules, $level){
|
||
|
|
||
|
$html = trim(\WMarkDown\Modules::format($this->wmarkdown, $this->matches[2][0], ["paragraphs"], $modules)["content"]);
|
||
|
$i = $this->level + 1;
|
||
|
|
||
|
$this->length = strlen($this->matches[0][0]);
|
||
|
$this->to = $this->from + $this->length;
|
||
|
$this->content = $html;
|
||
|
$modules->menu_level = $this->level = strlen($this->matches[1][0]) + $level;
|
||
|
$this->id = self::id($html);
|
||
|
$this->html = '<h' . $i . ' id="' . $this->id . '">' . $html . '</h' . $i . '>';
|
||
|
|
||
|
}
|
||
|
|
||
|
public function get_id(){return "" . $this->id;}
|
||
|
public function get_level(){return 0 | $this->level;}
|
||
|
|
||
|
}
|