From c9fff89c4e0c69789ac049999773ab62c7e05262 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Feb 22 2018 09:20:15 +0000 Subject: Handle implicit issue link at start of line (#2987) This treats a line starting with #123 - where 123 is a valid issue or pull request ID - as an implicit link, rather than as a header line. Doing this is a bit complicated, as markdown does header line processing early, in the block processors (that's before the inline patterns we usually use). So we can't do this entirely with inline patterns. We have to add a preprocessor that runs before the block processors, and mungs the relevant strings so the block processor doesn't turn them into header HTML. Then we adjust the ImplicitIssuePattern to handle these modified strings as well. This also adds tests for implicit links, enabled by mocking out enough bits of flask that `_get_ns_repo_user` is happy. There are tests not only for implicit links at the start of a line but also other cases, including the case of an implicit link not surrounded by whitespace that was fixed in the previous commit. Fixes https://pagure.io/pagure/issue/2987 Signed-off-by: Adam Williamson --- diff --git a/pagure/pfmarkdown.py b/pagure/pfmarkdown.py index 363e94b..9fe6ddf 100644 --- a/pagure/pfmarkdown.py +++ b/pagure/pfmarkdown.py @@ -22,8 +22,10 @@ Author: Ralph Bean import flask import markdown.inlinepatterns +import markdown.preprocessors import markdown.util import pygit2 +import re import pagure.lib from pagure.config import config as pagure_config @@ -51,7 +53,10 @@ COMMIT_LINK_RE = \ '([a-zA-Z0-9_-]*?/)?'\ '([a-zA-Z0-9_-]+)'\ '#(?P[\w]{40})' -IMPLICIT_ISSUE_RE = r'(?pingou/ns/test#8 is private

', + # 'implicit link to #1', + '

implicit link to #1

', + # 'implicit link .#1. with non-whitespace, non-word characters', + '

implicit link .#1. with non-whitespace, non-word characters

', + # '#2 - implicit link at start of line', + '

#2 - implicit link at start of line

', + # '#2. implicit link at start of line with no whitespace after', + '

#2. implicit link at start of line with no whitespace after

', + # '#regular header', + '

regular header

', + # '#34 looks like an implicit link, but no issue 34', + '

34 looks like an implicit link, but no issue 34

', # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd', '

pingou committed on