Signed-off-by: Marcel Korpel <[email protected]>
---
 web/lib/aur.inc.php | 47 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 38 insertions(+), 9 deletions(-)

diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php
index 4b1850f..d0c8c98 100644
--- a/web/lib/aur.inc.php
+++ b/web/lib/aur.inc.php
@@ -597,7 +597,8 @@ function comment_by_id($comment_id) {
 
 /**
  * Process submitted comments so any links can be followed and
- * parse markdown code blocks (within ```<newline>)
+ * parse markdown code blocks (within ```<newline>) and inline
+ * code (like `this`)
  *
  * @param string $comment Raw user submitted package comment
  *
@@ -608,6 +609,8 @@ function parse_comment($comment) {
                '(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))/iS';
        $md_codeblock_delimiter = '/(```)\R/';
        $md_codeblock_active = false;
+       $md_code_fragment_delimiter = '/(`)/';
+       $md_code_fragment_active = false;
 
        $blocks = preg_split($md_codeblock_delimiter, $comment, -1,
                PREG_SPLIT_DELIM_CAPTURE);
@@ -629,23 +632,49 @@ function parse_comment($comment) {
                                // preserved)
                                $html .= htmlspecialchars($blocks[$i]);
                        } else {
-                               $matches = preg_split($url_pattern, 
$blocks[$i], -1,
-                                       PREG_SPLIT_DELIM_CAPTURE);
+                               $code_fragments = 
preg_split($md_code_fragment_delimiter, $blocks[$i],
+                                       -1, PREG_SPLIT_DELIM_CAPTURE);
 
-                               for ($j = 0; $j < count($matches); $j++) {
+                               for ($j = 0; $j < count($code_fragments); $j++) 
{
                                        if ($j % 2) {
-                                               // convert links
-                                               $html .= '<a href="' . 
htmlspecialchars($matches[$j]) .
-                                                       '">' .  
htmlspecialchars($matches[$j]) . '</a>';
+                                               // add start/end tag of code 
fragment
+                                               if ($md_code_fragment_active) {
+                                                       $html .= '</code>';
+                                                       
$md_code_fragment_active = false;
+                                               } else {
+                                                       $html .= '<code>';
+                                                       
$md_code_fragment_active = true;
+                                               }
                                        } else {
-                                               // convert everything else
-                                               $html .= 
nl2br(htmlspecialchars($matches[$j]));
+                                               if ($md_code_fragment_active) {
+                                                       // do not convert links 
within code blocks
+                                                       $html .= 
nl2br(htmlspecialchars($code_fragments[$j]));
+                                               } else {
+                                                       $matches = 
preg_split($url_pattern, $code_fragments[$j],
+                                                               -1, 
PREG_SPLIT_DELIM_CAPTURE);
+
+                                                       for ($k = 0; $k < 
count($matches); $k++) {
+                                                               if ($k % 2) {
+                                                                       // 
convert links
+                                                                       $html 
.= '<a href="' . htmlspecialchars($matches[$k]) .
+                                                                               
'">' .  htmlspecialchars($matches[$k]) . '</a>';
+                                                               } else {
+                                                                       // 
convert everything else
+                                                                       $html 
.= nl2br(htmlspecialchars($matches[$k]));
+                                                               }
+                                                       }
+                                               }
                                        }
                                }
                        }
                }
        }
 
+       // close possible open code fragment
+       if ($md_code_fragment_active) {
+               $html .= '</code>';
+       }
+
        // close possible open code block
        if ($md_codeblock_active) {
                $html .= '</code></pre><p>';
-- 
2.4.5

Reply via email to