3 changed files with 148 additions and 2 deletions
@ -0,0 +1,145 @@
|
||||
<?php |
||||
function split($text){ |
||||
$text = str_replace("\r\n", "\n", $text); |
||||
$text = str_replace(" ", "\t", $text); |
||||
$text = trim($text, "\n"); |
||||
|
||||
return explode("\n", $text); |
||||
} |
||||
|
||||
function convert($text){ |
||||
$converted = ""; |
||||
|
||||
for($i=0;$i<count($text);$i++){ |
||||
$transcripted = false; |
||||
$text[$i] = trim($text[$i], " "); |
||||
|
||||
if($i+1<count($text)){ |
||||
if(!$transcripted && preg_match("#^=(=)+$#", $text[$i+1])){ |
||||
$converted .= "<h1>".$text[$i]."</h1>"; |
||||
$i++; |
||||
$transcripted = true; |
||||
} |
||||
if(!$transcripted && preg_match("#^-(-)+$#", $text[$i+1])){ |
||||
$converted .= "<h2>".$text[$i]."</h2>"; |
||||
$i++; |
||||
$transcripted = true; |
||||
} |
||||
} |
||||
|
||||
if(!$transcripted){ |
||||
if(preg_match("#^\# (.)+$#", $text[$i])){ |
||||
$converted .= preg_replace("#^\# (.+)$#", "<h1>$1</h1>", $text[$i]); |
||||
$transcripted = true; |
||||
} |
||||
|
||||
if(preg_match("#^\#\# (.)+$#", $text[$i])){ |
||||
$converted .= preg_replace("#^\#\# (.+)$#", "<h2>$1</h2>", $text[$i]); |
||||
$transcripted = true; |
||||
} |
||||
|
||||
if(preg_match("#^\#\#\# (.)+$#", $text[$i])){ |
||||
$converted .= preg_replace("#^\#\#\# (.+)$#", "<h3>$1</h3>", $text[$i]); |
||||
$transcripted = true; |
||||
} |
||||
|
||||
if(preg_match("#^\#\#\#\# (.)+$#", $text[$i])){ |
||||
$converted .= preg_replace("#^\#\#\#\# (.+)$#", "<h4>$1</h4>", $text[$i]); |
||||
$transcripted = true; |
||||
} |
||||
|
||||
if(preg_match("#^\#\#\#\#\# (.)+$#", $text[$i])){ |
||||
$converted .= preg_replace("#^\#\#\#\#\# (.+)$#", "<h5>$1</h5>", $text[$i]); |
||||
$transcripted = true; |
||||
} |
||||
|
||||
if(preg_match("#^\#\#\#\#\#\# (.)+$#", $text[$i])){ |
||||
$converted .= preg_replace("#^\#\#\#\#\#\# (.+)$#", "<h6>$1</h6>", $text[$i]); |
||||
$transcripted = true; |
||||
} |
||||
|
||||
if(preg_match("#^---+$#", $text[$i])){ |
||||
$converted .= preg_replace("#^---+$#", "<hr />", $text[$i]); |
||||
$transcripted = true; |
||||
} |
||||
} |
||||
|
||||
if(!$transcripted){ |
||||
if(preg_match("#^[+*-] (.*)$#", $text[$i])){ |
||||
$converted .= "<ul>"; |
||||
|
||||
while($i < count($text) && preg_match("#^[+*-] (.*)$#", $text[$i])){ |
||||
$li = array(preg_replace("#^[+*-] (.*)$#", "$1", $text[$i])); |
||||
$i++; |
||||
|
||||
while($i < count($text) && preg_match("#^\t+(.*)$#", $text[$i])){ |
||||
$li[] = preg_replace("#^\t+(.*)$#", "$1", $text[$i]); |
||||
$i++; |
||||
} |
||||
|
||||
$i--; |
||||
|
||||
$converted .= "<li>".convert($li)."</li>"; |
||||
|
||||
$i++; |
||||
} |
||||
|
||||
$i--; |
||||
$converted .= "</ul>"; |
||||
$transcripted = true; |
||||
} |
||||
|
||||
if(preg_match("#^1\. (.*)$#", $text[$i])){ |
||||
$converted .= "<ol>"; |
||||
|
||||
while($i < count($text) && preg_match("#^[0-9]+\. (.*)$#", $text[$i])){ |
||||
$li = array(preg_replace("#^[0-9]+\. (.*)$#", "$1", $text[$i])); |
||||
$i++; |
||||
|
||||
while($i < count($text) && preg_match("#^\t+(.*)$#", $text[$i])){ |
||||
$li[] = preg_replace("#^\t+(.*)$#", "$1", $text[$i]); |
||||
$i++; |
||||
} |
||||
|
||||
$i--; |
||||
|
||||
$converted .= "<li>".convert($li)."</li>"; |
||||
|
||||
$i++; |
||||
} |
||||
|
||||
$i--; |
||||
$converted .= "</ol>"; |
||||
$transcripted = true; |
||||
} |
||||
} |
||||
|
||||
if(!$transcripted){ |
||||
$text[$i] = preg_replace("#^(.*)\!\[(.*)\]\((.*)\)(.*)$#", "$1<img alt=\"$2\" src=\"$3\" />$4", $text[$i]); |
||||
$text[$i] = preg_replace("#^(.*)\[(.*)\]\((.*)\)(.*)$#", "$1<a href=\"$3\">$2</a>$4", $text[$i]); |
||||
$text[$i] = preg_replace("#^(.*)\*\*(.+)\*\*(.*)$#", "$1<b>$2</b>$3", $text[$i]); |
||||
$text[$i] = preg_replace("#^(.*)\*(.+)\*(.*)$#", "$1<em>$2</em>$3", $text[$i]); |
||||
$text[$i] = preg_replace("#^(.*)(https?://[^ ]*[a-z])(.*)$#", "$1<a href=\"$2\">$2</a>$3", $text[$i]); |
||||
$text[$i] = preg_replace("#^(.*)`(.*)`(.*)$#", "$1<code>$2</code>$3", $text[$i]); |
||||
|
||||
$converted .= $text[$i]."<br />"; |
||||
} |
||||
} |
||||
|
||||
return $converted; |
||||
} |
||||
|
||||
function countIndentation($string){ |
||||
$i = 0; |
||||
|
||||
while($i < strlen($string) && $string[$i] == "\t") $i++; |
||||
|
||||
return $i; |
||||
} |
||||
|
||||
function md2html($text){ |
||||
return convert(split($text)); |
||||
} |
||||
|
||||
echo md2html(file_get_contents("test.md")); |
||||
?> |
Reference in new issue