mdx_neboard.py
239 lines
| 6.0 KiB
| text/x-python
|
PythonLexer
/ boards / mdx_neboard.py
|
r699 | # coding=utf-8 | ||
|
r736 | import re | ||
|
r1458 | import random | ||
|
r736 | import bbcode | ||
|
r1066 | |||
from urllib.parse import unquote | ||||
|
r1061 | from django.core.exceptions import ObjectDoesNotExist | ||
|
r990 | from django.core.urlresolvers import reverse | ||
|
r721 | |||
|
r98 | import boards | ||
|
r52 | |||
|
r721 | |||
|
r89 | __author__ = 'neko259' | ||
|
r52 | |||
|
r799 | REFLINK_PATTERN = re.compile(r'^\d+$') | ||
|
r754 | MULTI_NEWLINES_PATTERN = re.compile(r'(\r?\n){2,}') | ||
ONE_NEWLINE = '\n' | ||||
|
r1066 | REGEX_URL = re.compile(r'https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?') | ||
|
r1068 | LINE_BREAK_HTML = '<div class="br"></div>' | ||
|
r1460 | SPOILER_SPACE = ' ' | ||
MAX_SPOILER_MULTIPLIER = 2 | ||||
|
r302 | |||
|
r398 | |||
|
r438 | class TextFormatter(): | ||
""" | ||||
An interface for formatter that can be used in the text format panel | ||||
""" | ||||
|
r721 | def __init__(self): | ||
pass | ||||
|
r438 | name = '' | ||
# Left and right tags for the button preview | ||||
preview_left = '' | ||||
preview_right = '' | ||||
# Left and right characters for the textarea input | ||||
format_left = '' | ||||
format_right = '' | ||||
|
r736 | class AutolinkPattern(): | ||
|
r52 | def handleMatch(self, m): | ||
link_element = etree.Element('a') | ||||
href = m.group(2) | ||||
link_element.set('href', href) | ||||
link_element.text = href | ||||
return link_element | ||||
|
r736 | class QuotePattern(TextFormatter): | ||
|
r1068 | name = '>q' | ||
preview_left = '<span class="quote">' | ||||
|
r438 | preview_right = '</span>' | ||
|
r736 | format_left = '[quote]' | ||
format_right = '[/quote]' | ||||
|
r52 | |||
|
r736 | class SpoilerPattern(TextFormatter): | ||
name = 'spoiler' | ||||
|
r438 | preview_left = '<span class="spoiler">' | ||
preview_right = '</span>' | ||||
|
r736 | format_left = '[spoiler]' | ||
format_right = '[/spoiler]' | ||||
|
r438 | |||
|
r56 | |||
|
r736 | class CommentPattern(TextFormatter): | ||
|
r438 | name = '' | ||
preview_left = '<span class="comment">// ' | ||||
preview_right = '</span>' | ||||
|
r736 | format_left = '[comment]' | ||
format_right = '[/comment]' | ||||
|
r56 | |||
|
r755 | # TODO Use <s> tag here | ||
|
r736 | class StrikeThroughPattern(TextFormatter): | ||
|
r438 | name = 's' | ||
preview_left = '<span class="strikethrough">' | ||||
preview_right = '</span>' | ||||
|
r736 | format_left = '[s]' | ||
format_right = '[/s]' | ||||
|
r330 | |||
|
r438 | class ItalicPattern(TextFormatter): | ||
name = 'i' | ||||
preview_left = '<i>' | ||||
preview_right = '</i>' | ||||
|
r736 | format_left = '[i]' | ||
format_right = '[/i]' | ||||
|
r438 | |||
class BoldPattern(TextFormatter): | ||||
name = 'b' | ||||
preview_left = '<b>' | ||||
preview_right = '</b>' | ||||
|
r736 | format_left = '[b]' | ||
format_right = '[/b]' | ||||
|
r438 | |||
|
r440 | class CodePattern(TextFormatter): | ||
name = 'code' | ||||
preview_left = '<code>' | ||||
preview_right = '</code>' | ||||
|
r736 | format_left = '[code]' | ||
format_right = '[/code]' | ||||
|
r699 | |||
|
r736 | def render_reflink(tag_name, value, options, parent, context): | ||
|
r1061 | result = '>>%s' % value | ||
|
r100 | |||
|
r1061 | if REFLINK_PATTERN.match(value): | ||
post_id = int(value) | ||||
|
r100 | |||
|
r1061 | try: | ||
post = boards.models.Post.objects.get(id=post_id) | ||||
|
r52 | |||
|
r1309 | result = post.get_link_view() | ||
|
r1061 | except ObjectDoesNotExist: | ||
pass | ||||
return result | ||||
|
r90 | |||
|
r52 | |||
|
r755 | def render_quote(tag_name, value, options, parent, context): | ||
|
r831 | source = '' | ||
|
r755 | if 'source' in options: | ||
source = options['source'] | ||||
|
r1398 | elif 'quote' in options: | ||
source = options['quote'] | ||||
|
r755 | |||
if source: | ||||
|
r831 | result = '<div class="multiquote"><div class="quote-header">%s</div><div class="quote-text">%s</div></div>' % (source, value) | ||
|
r755 | else: | ||
|
r1068 | # Insert a ">" at the start of every line | ||
result = '<span class="quote">>{}</span>'.format( | ||||
value.replace(LINE_BREAK_HTML, | ||||
'{}>'.format(LINE_BREAK_HTML))) | ||||
|
r755 | |||
return result | ||||
|
r990 | def render_notification(tag_name, value, options, parent, content): | ||
|
r1008 | username = value.lower() | ||
|
r990 | return '<a href="{}" class="user-cast">@{}</a>'.format( | ||
|
r1008 | reverse('notifications', kwargs={'username': username}), username) | ||
|
r990 | |||
|
r1202 | def render_tag(tag_name, value, options, parent, context): | ||
tag_name = value.lower() | ||||
try: | ||||
url = boards.models.Tag.objects.get(name=tag_name).get_view() | ||||
except ObjectDoesNotExist: | ||||
url = tag_name | ||||
return url | ||||
|
r1458 | def render_spoiler(tag_name, value, options, parent, context): | ||
text_len = len(value) | ||||
|
r1460 | space_count = random.randint(0, text_len * MAX_SPOILER_MULTIPLIER) | ||
side_spaces = SPOILER_SPACE * (space_count // 2) | ||||
|
r1459 | return '<span class="spoiler">{}{}{}</span>'.format(side_spaces, value, | ||
side_spaces) | ||||
|
r1458 | |||
return quote_element | ||||
|
r438 | formatters = [ | ||
QuotePattern, | ||||
SpoilerPattern, | ||||
ItalicPattern, | ||||
BoldPattern, | ||||
CommentPattern, | ||||
StrikeThroughPattern, | ||||
|
r440 | CodePattern, | ||
|
r438 | ] | ||
|
r1066 | |||
PREPARSE_PATTERNS = { | ||||
r'(?<!>)>>(\d+)': r'[post]\1[/post]', # Reflink ">>123" | ||||
r'^>([^>].+)': r'[quote]\1[/quote]', # Quote ">text" | ||||
r'^//(.+)': r'[comment]\1[/comment]', # Comment "//text" | ||||
r'\B@(\w+)': r'[user]\1[/user]', # User notification "@user" | ||||
} | ||||
class Parser: | ||||
def __init__(self): | ||||
# The newline hack is added because br's margin does not work in all | ||||
# browsers except firefox, when the div's does. | ||||
|
r1068 | self.parser = bbcode.Parser(newline=LINE_BREAK_HTML) | ||
|
r1066 | |||
self.parser.add_formatter('post', render_reflink, strip=True) | ||||
self.parser.add_formatter('quote', render_quote, strip=True) | ||||
self.parser.add_formatter('user', render_notification, strip=True) | ||||
|
r1202 | self.parser.add_formatter('tag', render_tag, strip=True) | ||
|
r1458 | self.parser.add_formatter('spoiler', render_spoiler, strip=True) | ||
|
r1066 | self.parser.add_simple_formatter( | ||
'comment', '<span class="comment">//%(value)s</span>') | ||||
self.parser.add_simple_formatter( | ||||
's', '<span class="strikethrough">%(value)s</span>') | ||||
# TODO Why not use built-in tag? | ||||
self.parser.add_simple_formatter('code', | ||||
'<pre><code>%(value)s</pre></code>', | ||||
render_embedded=False) | ||||
def preparse(self, text): | ||||
""" | ||||
Performs manual parsing before the bbcode parser is used. | ||||
Preparsed text is saved as raw and the text before preparsing is lost. | ||||
""" | ||||
new_text = MULTI_NEWLINES_PATTERN.sub(ONE_NEWLINE, text) | ||||
for key, value in PREPARSE_PATTERNS.items(): | ||||
new_text = re.sub(key, value, new_text, flags=re.MULTILINE) | ||||
for link in REGEX_URL.findall(text): | ||||
new_text = new_text.replace(link, unquote(link)) | ||||
return new_text | ||||
def parse(self, text): | ||||
|
r1067 | return self.parser.format(text) | ||