diff --git a/config/main.php b/config/main.php index 77c7ac3..bdc5c00 100755 --- a/config/main.php +++ b/config/main.php @@ -5,5 +5,5 @@ $config = array( 'dbHost' => 'localhost', 'dbUser' => 'usr', 'dbPasswd' => 'JFZZxF+^}U:SZ<7EE=n!_(gQs', - 'dbName' => 'blog' + 'dbName' => 'blog_TEST' ); diff --git a/lib/md.php b/lib/md.php new file mode 100644 index 0000000..12ee978 --- /dev/null +++ b/lib/md.php @@ -0,0 +1,145 @@ +".$text[$i].""; + $i++; + $transcripted = true; + } + if(!$transcripted && preg_match("#^-(-)+$#", $text[$i+1])){ + $converted .= "

".$text[$i]."

"; + $i++; + $transcripted = true; + } + } + + if(!$transcripted){ + if(preg_match("#^\# (.)+$#", $text[$i])){ + $converted .= preg_replace("#^\# (.+)$#", "

$1

", $text[$i]); + $transcripted = true; + } + + if(preg_match("#^\#\# (.)+$#", $text[$i])){ + $converted .= preg_replace("#^\#\# (.+)$#", "

$1

", $text[$i]); + $transcripted = true; + } + + if(preg_match("#^\#\#\# (.)+$#", $text[$i])){ + $converted .= preg_replace("#^\#\#\# (.+)$#", "

$1

", $text[$i]); + $transcripted = true; + } + + if(preg_match("#^\#\#\#\# (.)+$#", $text[$i])){ + $converted .= preg_replace("#^\#\#\#\# (.+)$#", "

$1

", $text[$i]); + $transcripted = true; + } + + if(preg_match("#^\#\#\#\#\# (.)+$#", $text[$i])){ + $converted .= preg_replace("#^\#\#\#\#\# (.+)$#", "
$1
", $text[$i]); + $transcripted = true; + } + + if(preg_match("#^\#\#\#\#\#\# (.)+$#", $text[$i])){ + $converted .= preg_replace("#^\#\#\#\#\#\# (.+)$#", "
$1
", $text[$i]); + $transcripted = true; + } + + if(preg_match("#^---+$#", $text[$i])){ + $converted .= preg_replace("#^---+$#", "
", $text[$i]); + $transcripted = true; + } + } + + if(!$transcripted){ + if(preg_match("#^[+*-] (.*)$#", $text[$i])){ + $converted .= ""; + $transcripted = true; + } + + if(preg_match("#^1\. (.*)$#", $text[$i])){ + $converted .= "
    "; + + 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 .= "
  1. ".convert($li)."
  2. "; + + $i++; + } + + $i--; + $converted .= "
"; + $transcripted = true; + } + } + + if(!$transcripted){ + $text[$i] = preg_replace("#^(.*)\!\[(.*)\]\((.*)\)(.*)$#", "$1\"$2\"$4", $text[$i]); + $text[$i] = preg_replace("#^(.*)\[(.*)\]\((.*)\)(.*)$#", "$1$2$4", $text[$i]); + $text[$i] = preg_replace("#^(.*)\*\*(.+)\*\*(.*)$#", "$1$2$3", $text[$i]); + $text[$i] = preg_replace("#^(.*)\*(.+)\*(.*)$#", "$1$2$3", $text[$i]); + $text[$i] = preg_replace("#^(.*)(https?://[^ ]*[a-z])(.*)$#", "$1$2$3", $text[$i]); + $text[$i] = preg_replace("#^(.*)`(.*)`(.*)$#", "$1$2$3", $text[$i]); + + $converted .= $text[$i]."
"; + } + } + + 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")); +?> diff --git a/views/article.php b/views/article.php index 39d2a22..2c474cb 100644 --- a/views/article.php +++ b/views/article.php @@ -3,12 +3,13 @@ if(isset($_GET["ID"])){ if(!empty($_GET["ID"])){ include_once("lib/articles.php"); include_once("lib/users.php"); + include_once("lib/md.php"); $article = getArticle($_GET["ID"]); $authorName = getUserName($article["author"]); - $content = "

".$article["title"]."

".$article["content"]."
written by ".$authorName." on ".$article["date"].""; + $content = "

".$article["title"]."

".convert($article["content"])."
written by ".$authorName." on ".$article["date"].""; }else{ $content = "Article ID is empty"; }