file = $filename; //设置当前文件
$this->mdGen = $this->content($filename); //获取生成器
$this->flag = 'idel'; //闲置
$this->lines = array(); //所有行状态
$this->line_count = 0; //行数归0
}
private function content($file){ //获取生成器
if(file_exists($file)){
$fh = fopen($file,"r");
while(!feof($fh)){
yield fgets($fh);
}
fclose($fh);
}else{
return null;
}
}
//遍历每一个字符
private function every($str){
$l = strlen($str);
for($i=0;$i<$l;$i++)
if(ord($str[$i])<0x80)
yield $str[$i];
else{
yield $str[$i].$str[$i+1].$str[$i+2]; //三个字节
$i+=2;
}
}
//解析
private function parse(){
$ctnt = $this->mdGen; //生成器
$ret=array();
if($ctnt != null){
$ret = array();
foreach($ctnt as $line){
if($this->flag != 'code')
$line=ltrim($line); //删除左边
if(strlen($line)){
$ch = $line[0]; //判断第一个字符
$type=ctype_punct($ch);//标点符号
if($this->flag !== 'code') $line=preg_replace('/\s{2,}$/','
',$line); //非代码模式
$ret[] = $this->parseLine($line,$type); //解析行
}else{ //空行
switch($this->flag){
case 'ul':
case 'ol':
$ret[]="$this->flag>";
$this->flag_pre = $this->flag;
$this->flag = 'idel';
break;
case 'table':
$this->flag = 'idel';
$ret[]='';
break;
default:
if($this->flag != 'el') $ret[] = '
';
$this->flag = 'el'; //空行
}
}
}
}
$this->lines = $ret;
return $ret;
}
//生成blockquote
private function genQuote($level,$content){
if($level<=0) return "";
return str_repeat('
',$level).$content.str_repeat('',$level); } //检查是否处于某个状态,默认检查是否处于代码状态 private function check($value='code'){ return ($this->flag == $value); } /** * 生成一行新的表格列 * $data:需要输入的数据 * $type:生成类型 * $wrap:最快层包裹 * $sep:分隔符 */ private function genRow($data,$type='td',$wrap='tr',$sep='|'){ $ret=''; $mat = explode($sep,$data); foreach($mat as $elem) $ret.="<$type>".$elem."$type>"; return "<$wrap>".$ret."$wrap>"; } /** * 解析行内元素 * bold,italic,code,link,pic */ private function parseInner($str){ $str=preg_replace('/\*\*\*([^\*\n\r]+)\*\*\*/','\1',$str); //加粗并加斜字体 $str=preg_replace('/\*\*([^\*\n\r]+)\*\*/','\1',$str); //加粗字体 $str=preg_replace('/\*([^\*\n\r]+)\*/','\1',$str); //斜体 $str=preg_replace('/```([^`\n\r]+)```/','
\1',$str); //代码
$str=preg_replace('/`([^`\n\r]+)`/','\1',$str); //代码
$str=preg_replace('/!\[([^\]\r\n]+)\]\(([^\)\r\n]+)\)/','';
if(preg_match('/^\s*```([^`]+)```([^\s]+)\s*$/',$str,$mat)){ //单行代码
$output.="$mat[1]$mat[2]";
$str = '';
$data_str = '';
}else{
$this->flag_pre = $this->flag;
$this->flag = 'code'; //代码
}
}else{
$output.='';
$this->flag_pre = $this->flag;
$this->flag = 'idel'; //闲置
}
break;
case '[':
if(preg_match('/^\[\s\]\s*(.+)$/',$str,$mat)){ //选择
$output.="