|
|
# coding=utf-8
|
|
|
|
|
|
import re
|
|
|
import bbcode
|
|
|
|
|
|
import boards
|
|
|
|
|
|
|
|
|
__author__ = 'neko259'
|
|
|
|
|
|
|
|
|
REFLINK_PATTERN = re.compile(r'\d+')
|
|
|
|
|
|
|
|
|
class TextFormatter():
|
|
|
"""
|
|
|
An interface for formatter that can be used in the text format panel
|
|
|
"""
|
|
|
|
|
|
def __init__(self):
|
|
|
pass
|
|
|
|
|
|
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 = ''
|
|
|
|
|
|
|
|
|
class AutolinkPattern():
|
|
|
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
|
|
|
|
|
|
|
|
|
class QuotePattern(TextFormatter):
|
|
|
name = 'q'
|
|
|
preview_left = '<span class="multiquote">'
|
|
|
preview_right = '</span>'
|
|
|
|
|
|
format_left = '[quote]'
|
|
|
format_right = '[/quote]'
|
|
|
|
|
|
|
|
|
class SpoilerPattern(TextFormatter):
|
|
|
name = 'spoiler'
|
|
|
preview_left = '<span class="spoiler">'
|
|
|
preview_right = '</span>'
|
|
|
|
|
|
format_left = '[spoiler]'
|
|
|
format_right = '[/spoiler]'
|
|
|
|
|
|
def handleMatch(self, m):
|
|
|
quote_element = etree.Element('span')
|
|
|
quote_element.set('class', 'spoiler')
|
|
|
quote_element.text = m.group(2)
|
|
|
|
|
|
return quote_element
|
|
|
|
|
|
|
|
|
class CommentPattern(TextFormatter):
|
|
|
name = ''
|
|
|
preview_left = '<span class="comment">// '
|
|
|
preview_right = '</span>'
|
|
|
|
|
|
format_left = '[comment]'
|
|
|
format_right = '[/comment]'
|
|
|
|
|
|
|
|
|
class StrikeThroughPattern(TextFormatter):
|
|
|
name = 's'
|
|
|
preview_left = '<span class="strikethrough">'
|
|
|
preview_right = '</span>'
|
|
|
|
|
|
format_left = '[s]'
|
|
|
format_right = '[/s]'
|
|
|
|
|
|
|
|
|
class ItalicPattern(TextFormatter):
|
|
|
name = 'i'
|
|
|
preview_left = '<i>'
|
|
|
preview_right = '</i>'
|
|
|
|
|
|
format_left = '[i]'
|
|
|
format_right = '[/i]'
|
|
|
|
|
|
|
|
|
class BoldPattern(TextFormatter):
|
|
|
name = 'b'
|
|
|
preview_left = '<b>'
|
|
|
preview_right = '</b>'
|
|
|
|
|
|
format_left = '[b]'
|
|
|
format_right = '[/b]'
|
|
|
|
|
|
|
|
|
class CodePattern(TextFormatter):
|
|
|
name = 'code'
|
|
|
preview_left = '<code>'
|
|
|
preview_right = '</code>'
|
|
|
|
|
|
format_left = '[code]'
|
|
|
format_right = '[/code]'
|
|
|
|
|
|
|
|
|
def render_reflink(tag_name, value, options, parent, context):
|
|
|
if not REFLINK_PATTERN.match(value):
|
|
|
return u'>>%s' % value
|
|
|
|
|
|
post_id = int(value)
|
|
|
|
|
|
posts = boards.models.Post.objects.filter(id=post_id)
|
|
|
if posts.exists():
|
|
|
post = posts[0]
|
|
|
|
|
|
return u'<a href=%s>>>%s</a>' % (post.get_url(), post_id)
|
|
|
else:
|
|
|
return u'>>%s' % value
|
|
|
|
|
|
|
|
|
def bbcode_extended(markup):
|
|
|
parser = bbcode.Parser(newline='</p><p>')
|
|
|
parser.add_formatter('post', render_reflink, strip=True)
|
|
|
parser.add_simple_formatter('quote',
|
|
|
u'<span class="multiquote">%(value)s</span>')
|
|
|
parser.add_simple_formatter('comment',
|
|
|
u'<span class="comment">//%(value)s</span>')
|
|
|
parser.add_simple_formatter('spoiler',
|
|
|
u'<span class="spoiler">%(value)s</span>')
|
|
|
parser.add_simple_formatter('s',
|
|
|
u'<span class="strikethrough">%(value)s</span>')
|
|
|
parser.add_simple_formatter('code',
|
|
|
u'<pre><code>%(value)s</pre></code>')
|
|
|
return '<p>%s</p>' % parser.format(markup)
|
|
|
|
|
|
formatters = [
|
|
|
QuotePattern,
|
|
|
SpoilerPattern,
|
|
|
ItalicPattern,
|
|
|
BoldPattern,
|
|
|
CommentPattern,
|
|
|
StrikeThroughPattern,
|
|
|
CodePattern,
|
|
|
]
|
|
|
|