# HG changeset patch # User neko259 # Date 2014-07-08 12:26:02 # Node ID 8ecc77aa05f052897cf27243425cfd87a6e05a1f # Parent 9400215dced76d127b80d6077832f6dc7f4f730e Very alpha bbcode support (markdown removed) diff --git a/boards/mdx_neboard.py b/boards/mdx_neboard.py --- a/boards/mdx_neboard.py +++ b/boards/mdx_neboard.py @@ -1,8 +1,7 @@ # coding=utf-8 -import markdown -from markdown.inlinepatterns import Pattern -from markdown.util import etree +import re +import bbcode import boards @@ -10,13 +9,7 @@ import boards __author__ = 'neko259' -AUTOLINK_PATTERN = r'(https?://\S+)' -QUOTE_PATTERN = r'^(?)(>[^>].*)$' -REFLINK_PATTERN = r'((>>)(\d+))' -SPOILER_PATTERN = r'%%([^(%%)]+)%%' -COMMENT_PATTERN = r'^(//(.+))' -STRIKETHROUGH_PATTERN = r'~(.+)~' -DASH_PATTERN = r'--' +REFLINK_PATTERN = re.compile(r'\d+') class TextFormatter(): @@ -38,7 +31,7 @@ class TextFormatter(): format_right = '' -class AutolinkPattern(Pattern): +class AutolinkPattern(): def handleMatch(self, m): link_element = etree.Element('a') href = m.group(2) @@ -48,44 +41,22 @@ class AutolinkPattern(Pattern): return link_element -class QuotePattern(Pattern, TextFormatter): - name = '' - preview_left = '> ' +class QuotePattern(TextFormatter): + name = 'q' + preview_left = '' preview_right = '' - format_left = '>' - - def handleMatch(self, m): - quote_element = etree.Element('span') - quote_element.set('class', 'quote') - quote_element.text = m.group(2) - - return quote_element + format_left = '[quote]' + format_right = '[/quote]' -class ReflinkPattern(Pattern): - def handleMatch(self, m): - post_id = m.group(4) - - posts = boards.models.Post.objects.filter(id=post_id) - if posts.count() > 0: - ref_element = etree.Element('a') - - post = posts[0] - - ref_element.set('href', post.get_url()) - ref_element.text = m.group(2) - - return ref_element - - -class SpoilerPattern(Pattern, TextFormatter): - name = 's' +class SpoilerPattern(TextFormatter): + name = 'spoiler' preview_left = '' preview_right = '' - format_left = '%%' - format_right = '%%' + format_left = '[spoiler]' + format_right = '[/spoiler]' def handleMatch(self, m): quote_element = etree.Element('span') @@ -95,35 +66,22 @@ class SpoilerPattern(Pattern, TextFormat return quote_element -class CommentPattern(Pattern, TextFormatter): +class CommentPattern(TextFormatter): name = '' preview_left = '// ' preview_right = '' - format_left = '//' - - def handleMatch(self, m): - quote_element = etree.Element('span') - quote_element.set('class', 'comment') - quote_element.text = '//' + m.group(3) - - return quote_element + format_left = '[comment]' + format_right = '[/comment]' -class StrikeThroughPattern(Pattern, TextFormatter): +class StrikeThroughPattern(TextFormatter): name = 's' preview_left = '' preview_right = '' - format_left = '~' - format_right = '~' - - def handleMatch(self, m): - quote_element = etree.Element('span') - quote_element.set('class', 'strikethrough') - quote_element.text = m.group(2) - - return quote_element + format_left = '[s]' + format_right = '[/s]' class ItalicPattern(TextFormatter): @@ -131,8 +89,8 @@ class ItalicPattern(TextFormatter): preview_left = '' preview_right = '' - format_left = '_' - format_right = '_' + format_left = '[i]' + format_right = '[/i]' class BoldPattern(TextFormatter): @@ -140,8 +98,8 @@ class BoldPattern(TextFormatter): preview_left = '' preview_right = '' - format_left = '__' - format_right = '__' + format_left = '[b]' + format_right = '[/b]' class CodePattern(TextFormatter): @@ -149,52 +107,39 @@ class CodePattern(TextFormatter): preview_left = '' preview_right = '' - format_left = ' ' - - -class DashPattern(Pattern): - def handleMatch(self, m): - return u'—' + format_left = '[code]' + format_right = '[/code]' -class NeboardMarkdown(markdown.Extension): - def extendMarkdown(self, md, md_globals): - self._add_neboard_patterns(md) - self._delete_patterns(md) +def render_reflink(tag_name, value, options, parent, context): + if not REFLINK_PATTERN.match(value): + return u'>>%s' % value - def _delete_patterns(self, md): - del md.parser.blockprocessors['quote'] - - del md.inlinePatterns['image_link'] - del md.inlinePatterns['image_reference'] + post_id = int(value) - def _add_neboard_patterns(self, md): - autolink = AutolinkPattern(AUTOLINK_PATTERN, md) - quote = QuotePattern(QUOTE_PATTERN, md) - reflink = ReflinkPattern(REFLINK_PATTERN, md) - spoiler = SpoilerPattern(SPOILER_PATTERN, md) - comment = CommentPattern(COMMENT_PATTERN, md) - strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md) - dash = DashPattern(DASH_PATTERN, md) + posts = boards.models.Post.objects.filter(id=post_id) + if posts.exists(): + post = posts[0] - md.inlinePatterns[u'autolink_ext'] = autolink - md.inlinePatterns[u'spoiler'] = spoiler - md.inlinePatterns[u'strikethrough'] = strikethrough - md.inlinePatterns[u'comment'] = comment - md.inlinePatterns[u'reflink'] = reflink - md.inlinePatterns[u'quote'] = quote - md.inlinePatterns[u'dash'] = dash + return u'>>%s' % (post.get_url(), post_id) + else: + return u'>>%s' % value -def make_extension(configs=None): - return NeboardMarkdown(configs=configs) - -neboard_extension = make_extension() - - -def markdown_extended(markup): - return markdown.markdown(markup, [neboard_extension, 'nl2br'], - safe_mode='escape') +def bbcode_extended(markup): + parser = bbcode.Parser() + parser.add_formatter('post', render_reflink, strip=True) + parser.add_simple_formatter('quote', + u'%(value)s') + parser.add_simple_formatter('comment', + u'//%(value)s') + parser.add_simple_formatter('spoiler', + u'%(value)s') + parser.add_simple_formatter('s', + u'%(value)s') + parser.add_simple_formatter('code', + u'
%(value)s
') + return parser.format(markup) formatters = [ QuotePattern, diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -28,7 +28,7 @@ IMAGE_THUMB_SIZE = (200, 150) TITLE_MAX_LENGTH = 200 -DEFAULT_MARKUP_TYPE = 'markdown' +DEFAULT_MARKUP_TYPE = 'bbcode' # TODO This should be removed NO_IP = '0.0.0.0' @@ -346,4 +346,4 @@ class Post(models.Model, Viewable): self.images.all().delete() - super(Post, self).delete(using) \ No newline at end of file + super(Post, self).delete(using) diff --git a/boards/static/css/md/base_page.css b/boards/static/css/md/base_page.css --- a/boards/static/css/md/base_page.css +++ b/boards/static/css/md/base_page.css @@ -225,6 +225,14 @@ blockquote { font-style: italic; } +.multiquote { + color: #92cf38; + font-style: italic; + border-left: solid 3px #00aa00; + padding-left: 3px; + display: inline-block; +} + .spoiler { background: white; color: white; diff --git a/boards/static/js/thread.js b/boards/static/js/thread.js --- a/boards/static/js/thread.js +++ b/boards/static/js/thread.js @@ -35,10 +35,10 @@ function moveCaretToEnd(el) { } function addQuickReply(postId) { - var textToAdd = '>>' + postId + '\n\n'; + var textToAdd = '[post]' + postId + '[/post]\n\n'; var selection = window.getSelection().toString(); if (selection.length > 0) { - textToAdd += '> ' + selection + '\n\n'; + textToAdd += '[quote]' + selection + '[/quote]\n\n'; } var textAreaId = 'textarea'; diff --git a/boards/templates/boards/staticpages/help.html b/boards/templates/boards/staticpages/help.html --- a/boards/templates/boards/staticpages/help.html +++ b/boards/templates/boards/staticpages/help.html @@ -8,13 +8,11 @@ {% block staticcontent %}

{% trans 'Syntax' %}

-

{% trans '2 line breaks for a new line.' %}

-

_{% trans 'Italic text' %}_

-

__{% trans 'Bold text' %}__

-

%%{% trans 'Spoiler' %}%%

-

>>123 -- {% trans 'Link to a post' %}

-

~{% trans 'Strikethrough text' %}~

-

{% trans 'You need to new line before:' %}

-

//{% trans 'Comment' %}

-

> {% trans 'Quote' %}

+

[i]{% trans 'Italic text' %}[/i]

+

[b]{% trans 'Bold text' %}[/b]

+

[spoiler]{% trans 'Spoiler' %}[/spoiler]

+

[post]123[/post] -- {% trans 'Link to a post' %}

+

[s]{% trans 'Strikethrough text' %}[/s]

+

[comment]{% trans 'Comment' %}[/comment]

+

[quote]{% trans 'Quote' %}[/quote]

{% endblock %} diff --git a/neboard/settings.py b/neboard/settings.py --- a/neboard/settings.py +++ b/neboard/settings.py @@ -1,6 +1,6 @@ # Django settings for neboard project. import os -from boards.mdx_neboard import markdown_extended +from boards.mdx_neboard import bbcode_extended DEBUG = True TEMPLATE_DEBUG = DEBUG @@ -217,7 +217,7 @@ HAYSTACK_CONNECTIONS = { } MARKUP_FIELD_TYPES = ( - ('markdown', markdown_extended), + ('bbcode', bbcode_extended), ) THEMES = [ diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,6 @@ pillow django>=1.6 django_cleanup django-markupfield -markdown django-simple-captcha line-profiler +bbcode