Skip to content

Commit

Permalink
jinja: backport helper regex for our custom urlize
Browse files Browse the repository at this point in the history
  • Loading branch information
anthraxx committed Feb 5, 2021
1 parent 6b8809f commit 64ce2c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions tracker/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import re
from functools import wraps

from flask import json

from config import atom_feeds

word_split_re = re.compile(r'(\s+)')
punctuation_re = re.compile(
'^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % (
'|'.join(map(re.escape, ('(', '<', '&lt;'))),
'|'.join(map(re.escape, ('.', ',', ')', '>', '\n', '&gt;')))
)
)

def multiline_to_list(data, whitespace_separator=True, unique_only=True, filter_empty=True):
if not data:
Expand Down
8 changes: 4 additions & 4 deletions tracker/view/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from jinja2.filters import do_urlize
from jinja2.filters import evalcontextfilter
from jinja2.utils import Markup
from jinja2.utils import _punctuation_re
from jinja2.utils import _word_split_re
from jinja2.utils import escape

from tracker.model.cve import cve_id_regex
from tracker.model.cvegroup import vulnerability_group_regex
from tracker.util import issue_to_numeric
from tracker.util import punctuation_re
from tracker.util import word_split_re

blueprint = Blueprint('filters', __name__)

Expand Down Expand Up @@ -56,9 +56,9 @@ def urlize(ctx, text, trim_url_limit=None, rel=None, target=None):
If target is not None, a target attribute will be added to the link.
"""

words = _word_split_re.split(escape(text))
words = word_split_re.split(escape(text))
for i, word in enumerate(words):
match = _punctuation_re.match(word)
match = punctuation_re.match(word)
if match:
lead, word, trail = match.groups()
word = sub('({})'.format(cve_id_regex), '<a href="/\\1" rel="noopener">\\1</a>', word)
Expand Down

0 comments on commit 64ce2c0

Please sign in to comment.