support markdown links; strip markdown image urls; trim leading blank lines; support markdown multiline comments that include highlight tag

This commit is contained in:
mappu 2018-12-30 14:35:49 +13:00
parent 428303c9fb
commit 52f90c5673
2 changed files with 21 additions and 9 deletions

View File

@ -92,13 +92,11 @@ class CProject {
$this->longdesc $this->longdesc
); );
// Extract short description // Strip out any markdown image links
$parts = explode("\n", $this->longdesc); // [![](doc/image1.thumb.png)](doc/image1.png)
$this->shortdesc = array_shift($parts); $this->longdesc = preg_replace('~\\[!.+?\\)\\]\\(.+?\\)~m', '', $this->longdesc);
$this->shortdesc[0] = strtolower($this->shortdesc[0]); // cosmetic lowercase
// Filters for longdesc
// Find "Written in" tags
$this->prefix_html = ''; $this->prefix_html = '';
$this->longdesc = preg_replace_callback('~\nWritten in ([^\\n]+)~ms', function($matches) { $this->longdesc = preg_replace_callback('~\nWritten in ([^\\n]+)~ms', function($matches) {
$this->prefix_html .= ( $this->prefix_html .= (
@ -107,7 +105,7 @@ class CProject {
); );
return ''; return '';
}, $this->longdesc); }, $this->longdesc);
// Find 'git-repository' tags // Find 'git-repository' tags
$this->longdesc = preg_replace_callback('~\[git\](.+)\[/git\]~', function($matches) { $this->longdesc = preg_replace_callback('~\[git\](.+)\[/git\]~', function($matches) {
$this->git_repo = $matches[1]; $this->git_repo = $matches[1];
@ -118,11 +116,18 @@ class CProject {
return ''; return '';
}, $this->longdesc); }, $this->longdesc);
// Collapse multiple blank lines
$this->longdesc = ltrim($this->longdesc, "\n");
while(strpos($this->longdesc, "\n\n\n") !== false) { while(strpos($this->longdesc, "\n\n\n") !== false) {
$this->longdesc = str_replace("\n\n\n", "\n\n", $this->longdesc); $this->longdesc = str_replace("\n\n\n", "\n\n", $this->longdesc);
} }
$this->longdesc = rtrim($this->longdesc, "\n")."\n"; $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; continue;
} }

View File

@ -148,12 +148,19 @@ function text2html($sz) {
$base = preg_replace('~^=+(.+)=+~m', '<strong>\\1</strong>', $base); $base = preg_replace('~^=+(.+)=+~m', '<strong>\\1</strong>', $base);
$base = preg_replace('~\\[url=([^\\]]+?)\\](.+?)\\[/url\\]~m', '<a href="\\1">\\2</a>', $base); $base = preg_replace('~\\[url=([^\\]]+?)\\](.+?)\\[/url\\]~m', '<a href="\\1">\\2</a>', $base);
$base = preg_replace('~([^="])(https?://[^ \\r\\n\\t]+)~i', '\\1<a href="\\2">\\2</a>', $base);
$base = preg_replace('~\\[([^\\]]+?)\\]\\((https?://.+?)\\)~m', '<a href="\\2">\\1</a>', $base); // Support markdown-style URLs
$base = preg_replace('~([^="])(https?://[^ \\r\\n\\t]+)~i', '\\1<a href="\\2">\\2</a>', $base); // Support standalone URLs
$base = preg_replace('~\\[b\\](.+?)\\[/b\\]~m', '<strong>\\1</strong>', $base); $base = preg_replace('~\\[b\\](.+?)\\[/b\\]~m', '<strong>\\1</strong>', $base);
$base = preg_replace('~\\[i\\](.+?)\\[/i\\]~m', '<i>\\1</i>', $base); $base = preg_replace('~\\[i\\](.+?)\\[/i\\]~m', '<i>\\1</i>', $base);
$base = preg_replace('~\\[spoiler\\](.+?)\\[/spoiler\\]~m', '<span class="spoiler">\\1</span>', $base); $base = preg_replace('~\\[spoiler\\](.+?)\\[/spoiler\\]~m', '<span class="spoiler">\\1</span>', $base);
$base = preg_replace('~\n- ~ms', "\n&bull; ", $base); $base = preg_replace('~\n- ~ms', "\n&bull; ", $base);
$base = preg_replace('~^```.+$~m', '`', $base); // Convert ```html to single `{}` element
// TODO support markdown tables
$btparts = explode('`', $base); $btparts = explode('`', $base);
if (count($btparts) > 1 && (count($btparts) % 2)) { if (count($btparts) > 1 && (count($btparts) % 2)) {
for ($i = 1, $e = count($btparts); $i < $e; $i += 2) { for ($i = 1, $e = count($btparts); $i < $e; $i += 2) {
@ -161,7 +168,7 @@ function text2html($sz) {
if (strpos($btparts[$i], "\n") !== false) { if (strpos($btparts[$i], "\n") !== false) {
$class .= ' code-multiline'; $class .= ' code-multiline';
} }
$btparts[$i] = '<span class="'.$class.'">'.$btparts[$i].'</span>'; $btparts[$i] = '<span class="'.$class.'">'.ltrim($btparts[$i], "\n").'</span>';
} }
} }