Archived
1
0

remove recursive fun for markup filter

This commit is contained in:
Jeff Becker 2016-11-06 10:49:57 -05:00
parent a9860d82ba
commit dac258a978
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B

View File

@ -9,11 +9,12 @@ from nntpchan.frontend.models import Newsgroup, Post
import re import re
from urllib.parse import urlparse from urllib.parse import urlparse
from html import unescape
register = template.Library() register = template.Library()
re_postcite = re.compile('>> ?([0-9a-fA-F]+)') re_postcite = re.compile('>> ?([0-9a-fA-F]+)')
re_boardlink = re.compile('>>> ?([a-zA-Z0-9\.]+[a-zA-Z0-9])') re_boardlink = re.compile('>>> ?/([a-zA-Z0-9\.]+[a-zA-Z0-9])/')
re_redtext = re.compile('== ?(.+) ?==') re_redtext = re.compile('== ?(.+) ?==')
re_psytext = re.compile('@@ ?(.+) ?@@') re_psytext = re.compile('@@ ?(.+) ?@@')
@ -28,7 +29,7 @@ def greentext(text, esc):
return_text += '<span class="greentext">%s </span>' % esc ( line ) + '\n' return_text += '<span class="greentext">%s </span>' % esc ( line ) + '\n'
f = True f = True
else: else:
return_text += line + '\n' return_text += esc(line) + '\n'
return return_text, f return return_text, f
def blocktext(text, esc, delim='', css='', tag='span'): def blocktext(text, esc, delim='', css='', tag='span'):
@ -56,7 +57,7 @@ def postcite(text, esc):
filtered = False filtered = False
for line in text.split('\n'): for line in text.split('\n'):
for word in line.split(' '): for word in line.split(' '):
match = re_postcite.match(word) match = re_postcite.match(unescape(word))
if match: if match:
posthash = match.groups()[0] posthash = match.groups()[0]
posts = Post.objects.filter(posthash__startswith=posthash) posts = Post.objects.filter(posthash__startswith=posthash)
@ -78,7 +79,7 @@ def boardlink(text, esc):
filtered = False filtered = False
for line in text.split('\n'): for line in text.split('\n'):
for word in line.split(' '): for word in line.split(' '):
match = re_boardlink.match(word) match = re_boardlink.match(unescape(word))
if match: if match:
name = match.groups()[0] name = match.groups()[0]
group = Newsgroup.objects.filter(name=name) group = Newsgroup.objects.filter(name=name)
@ -87,8 +88,6 @@ def boardlink(text, esc):
return_text += '<a href="%s" class="boardlink">%s</a> ' % ( group[0].get_absolute_url(), esc(match.string ) ) return_text += '<a href="%s" class="boardlink">%s</a> ' % ( group[0].get_absolute_url(), esc(match.string ) )
else: else:
return_text += '<span class="greentext">%s</span> ' % esc (match.string) return_text += '<span class="greentext">%s</span> ' % esc (match.string)
elif filtered:
return_text += word + ' '
else: else:
return_text += esc(word) + ' ' return_text += esc(word) + ' '
return_text += '\n' return_text += '\n'
@ -112,36 +111,19 @@ def urlify(text, esc):
line_funcs = [ line_funcs = [
greentext, greentext,
redtext, redtext,
postcite,
boardlink,
urlify, urlify,
psytext, psytext,
codeblock, codeblock,
postcite,
boardlink,
] ]
@register.filter(needs_autoescape=True, name='memepost') @register.filter(needs_autoescape=True, name='memepost')
def memepost(text, autoescape=True): def memepost(text, autoescape=True):
if autoescape: text, _ = line_funcs[0](text, conditional_escape)
esc = conditional_escape for f in line_funcs[1:]:
else: text, _ = f(text, lambda x : x)
esc = lambda x : x return mark_safe(text)
return_text = text
def doFilter(funcs, text, filter):
"""
RECURSIVE FUNCTIONS ARE FUN :^DDDDDD
"""
if len(funcs) == 1:
t, filtered = funcs[0](text, lambda x : x)
return t
else:
t, filtered = funcs[0](text, filter)
if filtered:
return doFilter(funcs[1:], t, lambda x : x)
else:
return doFilter(funcs[1:], t, filter)
return mark_safe(doFilter(line_funcs, return_text, conditional_escape))
@register.filter(name='truncate') @register.filter(name='truncate')