Replace task references in comments with actual links
diff mbox

Message ID CAE179zu=BF7GUoryQj4URWSfSoitR0Z0OWgahm2KDHCP-hk_4Q@mail.gmail.com
State Superseded, archived
Headers show

Commit Message

Lukas Fleischer via aur-dev April 21, 2017, 9:15 a.m. UTC
Search comments for references to Flyspray tasks (in the format FS#???)
and make them into clickable links.

Signed-off-by: Ragnis Armus <ragnis@aragnis.com>
---
 conf/config.proto   |  1 +
 web/lib/aur.inc.php | 34 +++++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

Comments

Mark Weiman April 22, 2017, 4:51 a.m. UTC | #1
I think your mail client borked your message. You should try sending the patch
via `git send-email`.

If not, I would probably recommend looking at the rest of the file and following
its style along with the guidelines [1].

Mark Weiman

[1] https://git.archlinux.org/aurweb.git/tree/doc/CodingGuidelines
Lukas Fleischer April 23, 2017, 4:51 p.m. UTC | #2
On Fri, 21 Apr 2017 at 11:15:34, Ragnis Armus via aur-dev wrote:
> Search comments for references to Flyspray tasks (in the format FS#???)
> and make them into clickable links.
> 
> Signed-off-by: Ragnis Armus <ragnis@aragnis.com>
> ---
>  conf/config.proto   |  1 +
>  web/lib/aur.inc.php | 34 +++++++++++++++++++++++++++++++++-
>  2 files changed, 34 insertions(+), 1 deletion(-)
> [...]

This reminded me of a previous attempt to move comment rendering to an
external script which makes the comment parser much more pleasant to
read and also makes it much easier to add extended functionality. I just
revived the code I had written a while ago and built a patch series that
I am going to submit in a minute.

Unfortunately, this makes your patch kind of obsolete, but maybe you
want to redo it based on the new implementation? If you do, please also
use git-send-email(1) as Mark suggested.

Thank you for contributing!

Regards,
Lukas

Patch
diff mbox

diff --git a/conf/config.proto b/conf/config.proto
index df10b99..39257f6 100644
--- a/conf/config.proto
+++ b/conf/config.proto
@@ -33,6 +33,7 @@  log_uri = https://aur.archlinux.org/cgit/aur.git/log/?h=%s
 snapshot_uri = /cgit/aur.git/snapshot/%s.tar.gz
 enable-maintenance = 1
 maintenance-exceptions = 127.0.0.1
+fs_task_uri = https://bugs.archlinux.org/task/%d

 [notifications]
 notify-cmd = /usr/local/bin/aurweb-notify
diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php
index d58df40..418b7cc 100644
--- a/web/lib/aur.inc.php
+++ b/web/lib/aur.inc.php
@@ -557,6 +557,35 @@  function comment_by_id($comment_id) {
 }

 /**
+ * Process comment body replacing any references to tasks with an actual link
+ *
+ * @param string $text The input text
+ *
+ * @return string The text with any task references replaces with HTML links
+ */
+function parse_comment_flyspray_links($text) {
+ $matches = preg_split('/(?<=\W|^)FS#(\d+)(?=\W|$)/', $text, -1,
+ PREG_SPLIT_DELIM_CAPTURE);
+
+ $output = '';
+
+ for ($i = 0; $i < count($matches); $i++) {
+ if ($i % 2) {
+ # link the tasks
+ $url = sprintf(config_get('options', 'fs_task_uri'), $matches[$i]);
+
+ $output .= '<a href="' . htmlspecialchars($url) .
+ '" rel="nofollow">FS#' . htmlspecialchars($matches[$i]) . '</a>';
+ } else {
+ # keep everything else
+ $output .= $matches[$i];
+ }
+ }
+
+ return $output;
+}
+
+/**
  * Process submitted comments so any links can be followed
  *
  * @param string $comment Raw user submitted package comment
@@ -579,7 +608,10 @@  function parse_comment($comment) {
  }
  else {
  # convert everything else
- $html .= nl2br(htmlspecialchars($matches[$i]));
+ $value = nl2br(htmlspecialchars($matches[$i]));
+ $value = parse_comment_flyspray_links($value);
+
+ $html .= $value;
  }
  }