From 52f90c56732a698dc40ca581f2341267c22bfcf5 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 30 Dec 2018 14:35:49 +1300 Subject: [PATCH] support markdown links; strip markdown image urls; trim leading blank lines; support markdown multiline comments that include highlight tag --- lib/CProject.php | 19 ++++++++++++------- lib/util.php | 11 +++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/CProject.php b/lib/CProject.php index 4e05a46..bb871ab 100644 --- a/lib/CProject.php +++ b/lib/CProject.php @@ -92,13 +92,11 @@ class CProject { $this->longdesc ); - // Extract short description - $parts = explode("\n", $this->longdesc); - $this->shortdesc = array_shift($parts); - $this->shortdesc[0] = strtolower($this->shortdesc[0]); // cosmetic lowercase - - // Filters for longdesc + // Strip out any markdown image links + // [![](doc/image1.thumb.png)](doc/image1.png) + $this->longdesc = preg_replace('~\\[!.+?\\)\\]\\(.+?\\)~m', '', $this->longdesc); + // Find "Written in" tags $this->prefix_html = ''; $this->longdesc = preg_replace_callback('~\nWritten in ([^\\n]+)~ms', function($matches) { $this->prefix_html .= ( @@ -107,7 +105,7 @@ class CProject { ); return ''; }, $this->longdesc); - + // Find 'git-repository' tags $this->longdesc = preg_replace_callback('~\[git\](.+)\[/git\]~', function($matches) { $this->git_repo = $matches[1]; @@ -118,11 +116,18 @@ class CProject { return ''; }, $this->longdesc); + // Collapse multiple blank lines + $this->longdesc = ltrim($this->longdesc, "\n"); while(strpos($this->longdesc, "\n\n\n") !== false) { $this->longdesc = str_replace("\n\n\n", "\n\n", $this->longdesc); } $this->longdesc = rtrim($this->longdesc, "\n")."\n"; + // Extract short description (last) + $parts = explode("\n", $this->longdesc); + $this->shortdesc = array_shift($parts); + $this->shortdesc[0] = strtolower($this->shortdesc[0]); // cosmetic lowercase + continue; } diff --git a/lib/util.php b/lib/util.php index 7d354f7..b303e57 100644 --- a/lib/util.php +++ b/lib/util.php @@ -148,12 +148,19 @@ function text2html($sz) { $base = preg_replace('~^=+(.+)=+~m', '\\1', $base); $base = preg_replace('~\\[url=([^\\]]+?)\\](.+?)\\[/url\\]~m', '\\2', $base); - $base = preg_replace('~([^="])(https?://[^ \\r\\n\\t]+)~i', '\\1\\2', $base); + + $base = preg_replace('~\\[([^\\]]+?)\\]\\((https?://.+?)\\)~m', '\\1', $base); // Support markdown-style URLs + + $base = preg_replace('~([^="])(https?://[^ \\r\\n\\t]+)~i', '\\1\\2', $base); // Support standalone URLs $base = preg_replace('~\\[b\\](.+?)\\[/b\\]~m', '\\1', $base); $base = preg_replace('~\\[i\\](.+?)\\[/i\\]~m', '\\1', $base); $base = preg_replace('~\\[spoiler\\](.+?)\\[/spoiler\\]~m', '\\1', $base); $base = preg_replace('~\n- ~ms', "\n• ", $base); + $base = preg_replace('~^```.+$~m', '`', $base); // Convert ```html to single `{}` element + + // TODO support markdown tables + $btparts = explode('`', $base); if (count($btparts) > 1 && (count($btparts) % 2)) { for ($i = 1, $e = count($btparts); $i < $e; $i += 2) { @@ -161,7 +168,7 @@ function text2html($sz) { if (strpos($btparts[$i], "\n") !== false) { $class .= ' code-multiline'; } - $btparts[$i] = ''.$btparts[$i].''; + $btparts[$i] = ''.ltrim($btparts[$i], "\n").''; } }