Первая версия разметки маркдаун, удаление тегов из сохраняемого текста статьи

This commit is contained in:
Krivchikov Dmitry 2015-09-09 23:52:58 +03:00
parent 4e08b64a01
commit cb0d6c614f
3 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,70 @@
<?php
class Markdown
{
static function processing($string)
{
$string = self::replaceHeaders($string);
$string = self::replaceBoldItalic($string);
$string = self::replaceBold($string);
$string = self::replaceItalic($string);
$string = self::replaceUnderline($string);
$string = self::replaceUrls($string);
return $string;
}
static private function replaceHeaders($string)
{
for ($index = 3; $index > 0; $index--) {
$pr = '/(^#{' . $index . '} .{1,})/m';
preg_match_all($pr, $string, $headers);
if (isset($headers[0][0])) {
foreach ($headers[0] as $header) {
$hI = $index+1;
$string = str_replace($header, "<h$hI>" . mb_substr($header, $index + 1, NULL, 'UTF-8') . "</h$hI>", $string);
}
}
}
return $string;
}
static private function replaceBoldItalic($string)
{
$pr = '(\*{3}(.+?)\*{3})';
$rep = '<b><i>$1</i></b>';
return preg_replace($pr, $rep, $string);
}
static private function replaceBold($string)
{
$pr = '(\*{2}(.+?)\*{2})';
$rep = '<b>$1</b>';
return preg_replace($pr, $rep, $string);
}
static private function replaceItalic($string)
{
$pr = '(\*{1}(.+?)\*{1})';
$rep = '<i>$1</i>';
return preg_replace($pr, $rep, $string);
}
static private function replaceUnderline($string)
{
$pr = '(\_{1}(.+?)\_{1})';
$rep = '<u>$1</u>';
return preg_replace($pr, $rep, $string);
}
static private function replaceUrls($string)
{
$pr = '((https{0,1}:\/\/[\da-z\.-]+\.[a-z]{2,6})[\/\w\?=&#%+\.,-]*\/?)';
$rep = '<a href="$0" target="_blank">$1</a>';
return preg_replace($pr, $rep, $string);
}
}

View File

@ -76,6 +76,7 @@ class ArticlesController extends Controller
$id = (int) $_POST['id'];
$title = $_POST['title'];
$text = $_POST['text'];
$text = strip_tags($text);
$allowComments = (int) isset($_POST['allowComments']);
$article = $id ? Article::model()->getByPK($id) : new Article();

View File

@ -195,6 +195,7 @@ class PageController extends Controller
App::setConfig('title', $article->title . ' - wikipoints.ru');
$article->text = Helper::replaceLinks($article->text, 'html');
$article->text = Helper::replaceImgs($article->text, 'html');
$article->text = Markdown::processing($article->text);
$article->text = nl2br($article->text);
self::render('article.php', array(