134 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
<?php
 | 
						|
 | 
						|
    namespace WMarkDown\Modules;
 | 
						|
 | 
						|
    class Lists extends \WMarkDown\Abstracts\Modules{
 | 
						|
 | 
						|
        private static function close(&$html, &$levels, &$l, $level){
 | 
						|
 | 
						|
            $closed = false;
 | 
						|
 | 
						|
            while($l && $levels[$l - 1]["level"] > $level){
 | 
						|
                $closed = true;
 | 
						|
                $l --;
 | 
						|
                for($i = 0; !$i || $i < $levels[$l]["levels"]; $i ++)
 | 
						|
                    $html .= '</' . $levels[$l]["tag"] . '>';
 | 
						|
                if($levels[$l]["close"])
 | 
						|
                    $html .= '</li>';
 | 
						|
                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 .= '<li>' . \WMarkDown\Modules::format($this->wmarkdown, $content, ["paragraphs"], $modules)["content"];
 | 
						|
                        $content = "";
 | 
						|
                    };
 | 
						|
 | 
						|
                    $level += strlen(preg_replace('/\.?$/', "", $header[1])) - 1;
 | 
						|
 | 
						|
                    self::close($html, $levels, $l, $level);
 | 
						|
 | 
						|
                    self::open($html, $levels, $l, $level, $header);
 | 
						|
 | 
						|
                    $content = $header[4];
 | 
						|
 | 
						|
                }else
 | 
						|
                    $content .= " " . $line;
 | 
						|
            };
 | 
						|
            if($content)
 | 
						|
                $html .= '<li>' . \WMarkDown\Modules::format($this->wmarkdown, $content, ["paragraphs"], $modules)["content"];
 | 
						|
            self::close($html, $levels, $l, 0);
 | 
						|
            preg_match('/^<([ou]l)(.+|[\r\n])+$/', $html, $tag);
 | 
						|
            $html .= '</' . $tag[1] . '>';
 | 
						|
 | 
						|
            $this->length = strlen($this->matches[0][0]);
 | 
						|
            $this->to = $this->from + $this->length;
 | 
						|
            $this->content = $this->matches[1][0];
 | 
						|
            $this->html = $html;
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
    };
 |