From cc16b420d27bbee488bfb9d0b6048fe955f4cd62 Mon Sep 17 00:00:00 2001 From: Krivchikov Dmitry Date: Fri, 11 Sep 2015 10:33:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B8=20=D1=81=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=BE=D0=B4=D0=B8=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D0=BC=D0=B0=D1=80=D0=BA=D0=B4=D0=B0=D1=83=D0=BD?= =?UTF-8?q?-=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D1=81=D1=81=D0=B8=D0=BD=D0=B3,?= =?UTF-8?q?=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D1=8F=D0=B5=D1=82=D1=81?= =?UTF-8?q?=D1=8F=20ID=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B9=20=D1=84?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ground/classes/Helper.php | 28 +++++++++++++++++++++-- ground/classes/Markdown.php | 13 +++-------- ground/controllers/ArticlesController.php | 10 ++++++-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/ground/classes/Helper.php b/ground/classes/Helper.php index 9e217fd..691df62 100644 --- a/ground/classes/Helper.php +++ b/ground/classes/Helper.php @@ -62,11 +62,11 @@ class Helper $imgAndId = mb_substr($img, 2, -1); $imgAndId = explode('#', $imgAndId); - $imgId = $imgAndId[1]; + $imgId = (int)$imgAndId[1]; $imgObj = ArticlePhotos::model()->getByPK($imgId); - if ($imgObj) { + if ($imgObj->name) { switch ($replaceType) { case 'html': $imgObj->caption = $linkTitle = str_replace('"', 'ʺ', $imgObj->caption); @@ -78,6 +78,17 @@ class Helper $string = str_replace($img, $imgTag, $string); break; } + } else { + switch ($replaceType) { + case 'html': + $imgTag = '
'; + $string = str_replace($img, $imgTag, $string); + break; + default: + $imgTag = ''; + $string = str_replace($img, $imgTag, $string); + break; + } } } } @@ -85,6 +96,19 @@ class Helper return $string; } + public static function getFirstImg($string) + { + preg_match_all("(\[img[\|]#(\d+)\])", $string, $images); + $images = isset($images[0]) ? $images[0] : FALSE; + $img = isset($images[0]) ? $images[0] : FALSE; + if ($img) { + $imgId = (int) mb_substr($img, 6, -1); + $imgObj = ArticlePhotos::model()->getByPK($imgId); + return $imgObj->name ? (int)$imgObj->id : 0; + } + return 0; + } + public static function buildOG($point) { $og = array(); diff --git a/ground/classes/Markdown.php b/ground/classes/Markdown.php index 640c9ee..c8bb025 100644 --- a/ground/classes/Markdown.php +++ b/ground/classes/Markdown.php @@ -6,11 +6,11 @@ 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; } @@ -33,14 +33,7 @@ class Markdown return $string; } - static private function replaceBoldItalic($string) - { - $pr = '(\*{3}(.+?)\*{3})'; - $rep = '$1'; - return preg_replace($pr, $rep, $string); - } - - static private function replaceBold($string) + static private function replaceBold($string) { $pr = '(\*{2}(.+?)\*{2})'; $rep = '$1'; @@ -64,7 +57,7 @@ class Markdown static private function replaceUrls($string) { $pr = '((https{0,1}:\/\/[\da-z\.-]+\.[a-z]{2,6})[\/\w\?=&#%+\.,-]*\/?)'; - $rep = '$1'; + $rep = '$1'; return preg_replace($pr, $rep, $string); } } diff --git a/ground/controllers/ArticlesController.php b/ground/controllers/ArticlesController.php index 03b3428..e01f56e 100644 --- a/ground/controllers/ArticlesController.php +++ b/ground/controllers/ArticlesController.php @@ -76,8 +76,12 @@ class ArticlesController extends Controller $id = (int) $_POST['id']; $title = $_POST['title']; $text = $_POST['text']; - $text = strip_tags($text); - $text = str_replace(array('<','>','"',"'",'--'), array('<','>','″','′','–'), $text); + + $html = strip_tags($text); + $html = str_replace(array('<','>','"',"'",'--'), array('<','>','″','′','–'), $html); + $html = Markdown::processing($html); + $html = nl2br($html); + $allowComments = (int) isset($_POST['allowComments']); $article = $id ? Article::model()->getByPK($id) : new Article(); @@ -92,6 +96,8 @@ class ArticlesController extends Controller $article->title = $title; $article->text = $text; + $article->html = $html; + $article->photoId = Helper::getFirstImg($text); $article->allowComments = $allowComments; $article->save();