$level){ $closed = true; $l --; for($i = 0; !$i || $i < $levels[$l]["levels"]; $i ++) $html .= '' . $levels[$l]["tag"] . '>'; if($levels[$l]["close"]) $html .= ''; array_pop($levels); }; return $closed; } private static function open(&$html, &$levels, &$l, $level, $header){ if(!$l || $levels[$l - 1]["level"] < $level){ $start = $header[empty($header[2]) ? 3 : 2]; $type = null; $tag = "ul"; switch($start[0]){ case "a": case "i": case "A": case "I": case "1": $type = $start; $tag = "ol"; break; case "#": $type = "1"; $tag = "ol"; $start = "1"; break; default: $tag = "ol"; if(preg_match('/^[a-z]+$/', $start)) $type = "a"; elseif(preg_match('/^[A-Z]+$/', $start)) $type = "A"; elseif(preg_match('/^[0-9]+$/', $start)) $type = "1"; else $tag = "ul"; break; }; $levels[] = [ "levels" => $level - ($l ? $levels[$l - 1]["level"] : 0), "level" => $level, "tag" => $tag, "start" => $start, "type" => $type, "close" => !!$l ]; for($i = 0; (!$l && !$i) || $i < $levels[$l]["levels"]; $i ++) $html .= ('<' . $levels[$l]["tag"] . ($levels[$l]["type"] ? ' type="' . $levels[$l]["type"] . '"' : '') . ($levels[$l]["tag"] == "ol" ? ' start="' . $levels[$l]["start"] . '"' : '') . '>'); $l ++; }; } public function get_next_position($string){ if( $this->more && $this->from < 0 && $this->more = preg_match('/^(([ \t]*(\-+|\#+|\++|\*+|(\d+|[ia])\.) *[^\r\n]+(\r\n|[\r\n])([ \t]*([^\-\+\#\r\n\s\t]|(?!(\d+|[ia])\.).)[^\r\n]+(\r\n|[\r\n]))*){2,})/mi', $string, $this->matches, PREG_OFFSET_CAPTURE) ) $this->from = $this->matches[0][1]; } public function process($string, $modules, $level){ $html = ''; $content = ''; $levels = []; $l = 0; $first = true; foreach(preg_split('/\r\n|[\r\n]/', preg_replace('/\t/', " ", $this->matches[0][0])) as $line){ preg_match('/^ +/', $line, $spaces); $level = isset($spaces[0]) ? strlen($spaces[0]) / 4 >> 0 : 0; if(($line = trim($line)) && preg_match('/^(([\-\+\#\+])|(\d+|[ai])\.) *(.+)/', $line, $header)){ if($started = !!$content){ if(!$l) self::open($html, $levels, $l, 0, $header); $html .= '