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();