##// END OF EJS Templates
Image deduplication (BB-53). When an image with the same hash is uploaded, it...
Image deduplication (BB-53). When an image with the same hash is uploaded, it won't be saved. Instead, post will be linked to the same PostImage object

File last commit:

r933:ce97c754 merge decentral
r944:6ed17cb6 default
Show More
mdx_neboard.py
183 lines | 4.2 KiB | text/x-python | PythonLexer
neko259
Replace '--' with '—' character
r699 # coding=utf-8
neko259
Very alpha bbcode support (markdown removed)
r736 import re
import bbcode
neko259
Small code cleanups
r721
neko259
Added jump view to open message of any thread by id. This fixes #49
r98 import boards
neko259
#12 Added markdown extension for reflinks, quotes and autolinks.
r52
neko259
Small code cleanups
r721
neko259
Implemented RSS support. This fixes #11
r89 __author__ = 'neko259'
neko259
#12 Added markdown extension for reflinks, quotes and autolinks.
r52
neko259
Fixed parsing an invalid reflink and reflink href attributes
r799 REFLINK_PATTERN = re.compile(r'^\d+$')
neko259
Strip multiple newlines to one in the post before parsing with bbcode
r754 MULTI_NEWLINES_PATTERN = re.compile(r'(\r?\n){2,}')
ONE_NEWLINE = '\n'
neko259
Speed up parsing by moving constants to module scape
r302
neko259
Split up post model into post and thread to normalise models. Still need some refactoring
r398
neko259
Added a new format panel to the text form
r438 class TextFormatter():
"""
An interface for formatter that can be used in the text format panel
"""
neko259
Small code cleanups
r721 def __init__(self):
pass
neko259
Added a new format panel to the text form
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 = ''
neko259
Very alpha bbcode support (markdown removed)
r736 class AutolinkPattern():
neko259
#12 Added markdown extension for reflinks, quotes and autolinks.
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
neko259
Very alpha bbcode support (markdown removed)
r736 class QuotePattern(TextFormatter):
name = 'q'
preview_left = '<span class="multiquote">'
neko259
Added a new format panel to the text form
r438 preview_right = '</span>'
neko259
Very alpha bbcode support (markdown removed)
r736 format_left = '[quote]'
format_right = '[/quote]'
neko259
#12 Added markdown extension for reflinks, quotes and autolinks.
r52
neko259
Very alpha bbcode support (markdown removed)
r736 class SpoilerPattern(TextFormatter):
name = 'spoiler'
neko259
Added a new format panel to the text form
r438 preview_left = '<span class="spoiler">'
preview_right = '</span>'
neko259
Very alpha bbcode support (markdown removed)
r736 format_left = '[spoiler]'
format_right = '[/spoiler]'
neko259
Added a new format panel to the text form
r438
neko259
#12 Added spoilers and comments.
r56 def handleMatch(self, m):
quote_element = etree.Element('span')
quote_element.set('class', 'spoiler')
quote_element.text = m.group(2)
return quote_element
neko259
Very alpha bbcode support (markdown removed)
r736 class CommentPattern(TextFormatter):
neko259
Added a new format panel to the text form
r438 name = ''
preview_left = '<span class="comment">// '
preview_right = '</span>'
neko259
Very alpha bbcode support (markdown removed)
r736 format_left = '[comment]'
format_right = '[/comment]'
neko259
#12 Added spoilers and comments.
r56
neko259
Added source parameter to quotes
r755 # TODO Use <s> tag here
neko259
Very alpha bbcode support (markdown removed)
r736 class StrikeThroughPattern(TextFormatter):
neko259
Added a new format panel to the text form
r438 name = 's'
preview_left = '<span class="strikethrough">'
preview_right = '</span>'
neko259
Very alpha bbcode support (markdown removed)
r736 format_left = '[s]'
format_right = '[/s]'
neko259
Added strikethrough text to the text parser
r330
neko259
Added a new format panel to the text form
r438 class ItalicPattern(TextFormatter):
name = 'i'
preview_left = '<i>'
preview_right = '</i>'
neko259
Very alpha bbcode support (markdown removed)
r736 format_left = '[i]'
format_right = '[/i]'
neko259
Added a new format panel to the text form
r438
class BoldPattern(TextFormatter):
name = 'b'
preview_left = '<b>'
preview_right = '</b>'
neko259
Very alpha bbcode support (markdown removed)
r736 format_left = '[b]'
format_right = '[/b]'
neko259
Added a new format panel to the text form
r438
neko259
Added proper formatting when several lines are selected
r440 class CodePattern(TextFormatter):
name = 'code'
preview_left = '<code>'
preview_right = '</code>'
neko259
Very alpha bbcode support (markdown removed)
r736 format_left = '[code]'
format_right = '[/code]'
neko259
Replace '--' with '—' character
r699
neko259
Very alpha bbcode support (markdown removed)
r736 def render_reflink(tag_name, value, options, parent, context):
if not REFLINK_PATTERN.match(value):
neko259
Use <div> as a line break. Hell yeah!
r831 return '>>%s' % value
neko259
Removed image tag from markdown. Refactored markdown extension code. This fixes #21
r100
neko259
Very alpha bbcode support (markdown removed)
r736 post_id = int(value)
neko259
Removed image tag from markdown. Refactored markdown extension code. This fixes #21
r100
neko259
Very alpha bbcode support (markdown removed)
r736 posts = boards.models.Post.objects.filter(id=post_id)
if posts.exists():
post = posts[0]
neko259
#12 Added markdown extension for reflinks, quotes and autolinks.
r52
neko259
Use <div> as a line break. Hell yeah!
r831 return '<a href="%s">&gt;&gt;%s</a>' % (post.get_url(), post_id)
neko259
Very alpha bbcode support (markdown removed)
r736 else:
neko259
Use <div> as a line break. Hell yeah!
r831 return '>>%s' % value
neko259
Changed parser to show quotes and reflinks as they are posted, not with borders and '#' character.
r90
neko259
#12 Added markdown extension for reflinks, quotes and autolinks.
r52
neko259
Added source parameter to quotes
r755 def render_quote(tag_name, value, options, parent, context):
neko259
Use <div> as a line break. Hell yeah!
r831 source = ''
neko259
Added source parameter to quotes
r755 if 'source' in options:
source = options['source']
neko259
Use <div> as a line break. Hell yeah!
r831 result = ''
neko259
Added source parameter to quotes
r755 if source:
neko259
Use <div> as a line break. Hell yeah!
r831 result = '<div class="multiquote"><div class="quote-header">%s</div><div class="quote-text">%s</div></div>' % (source, value)
neko259
Added source parameter to quotes
r755 else:
neko259
Use <div> as a line break. Hell yeah!
r831 result = '<div class="multiquote"><div class="quote-text">%s</div></div>' % value
neko259
Added source parameter to quotes
r755
return result
neko259
Strip multiple newlines to one in the post before parsing with bbcode
r754 def preparse_text(text):
"""
Performs manual parsing before the bbcode parser is used.
"""
return MULTI_NEWLINES_PATTERN.sub(ONE_NEWLINE, text)
neko259
Very alpha bbcode support (markdown removed)
r736 def bbcode_extended(markup):
neko259
Use <div> as a line break. Hell yeah!
r831 # The newline hack is added because br's margin does not work in all
# browsers except firefox, when the div's does.
parser = bbcode.Parser(newline='<div class="br"></div>')
neko259
Very alpha bbcode support (markdown removed)
r736 parser.add_formatter('post', render_reflink, strip=True)
neko259
Added source parameter to quotes
r755 parser.add_formatter('quote', render_quote, strip=True)
neko259
Very alpha bbcode support (markdown removed)
r736 parser.add_simple_formatter('comment',
neko259
Removed django-markupfield as it is incompatible with the new migrations. Use 2 fields for storing raw and rendered text and work with them directly
r881 '<span class="comment">//%(value)s</span>')
neko259
Very alpha bbcode support (markdown removed)
r736 parser.add_simple_formatter('spoiler',
neko259
Removed django-markupfield as it is incompatible with the new migrations. Use 2 fields for storing raw and rendered text and work with them directly
r881 '<span class="spoiler">%(value)s</span>')
neko259
Added some todos to the parser
r815 # TODO Use <s> here
neko259
Very alpha bbcode support (markdown removed)
r736 parser.add_simple_formatter('s',
neko259
Removed django-markupfield as it is incompatible with the new migrations. Use 2 fields for storing raw and rendered text and work with them directly
r881 '<span class="strikethrough">%(value)s</span>')
neko259
Added some todos to the parser
r815 # TODO Why not use built-in tag?
neko259
Very alpha bbcode support (markdown removed)
r736 parser.add_simple_formatter('code',
neko259
Removed django-markupfield as it is incompatible with the new migrations. Use 2 fields for storing raw and rendered text and work with them directly
r881 '<pre><code>%(value)s</pre></code>',
render_embedded=False)
neko259
Strip multiple newlines to one in the post before parsing with bbcode
r754
text = preparse_text(markup)
return parser.format(text)
neko259
Added a new format panel to the text form
r438
formatters = [
QuotePattern,
SpoilerPattern,
ItalicPattern,
BoldPattern,
CommentPattern,
StrikeThroughPattern,
neko259
Added proper formatting when several lines are selected
r440 CodePattern,
neko259
Added a new format panel to the text form
r438 ]