mdx_neboard.py
96 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
/ boards / mdx_neboard.py
neko259
|
r98 | from django.core.urlresolvers import reverse | ||
neko259
|
r52 | import markdown | ||
from markdown.inlinepatterns import Pattern | ||||
from markdown.util import etree | ||||
neko259
|
r98 | import boards | ||
neko259
|
r52 | |||
neko259
|
r89 | __author__ = 'neko259' | ||
neko259
|
r52 | |||
class AutolinkPattern(Pattern): | ||||
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(Pattern): | ||||
def handleMatch(self, m): | ||||
quote_element = etree.Element('span') | ||||
quote_element.set('class', 'quote') | ||||
neko259
|
r90 | quote_element.text = m.group(2) | ||
neko259
|
r52 | |||
return quote_element | ||||
class ReflinkPattern(Pattern): | ||||
def handleMatch(self, m): | ||||
ref_element = etree.Element('a') | ||||
post_id = m.group(4) | ||||
neko259
|
r98 | ref_element.set('href', reverse(boards.views.jump_to_post, | ||
kwargs={'post_id': post_id})) | ||||
neko259
|
r90 | ref_element.text = m.group(2) | ||
neko259
|
r52 | |||
return ref_element | ||||
neko259
|
r56 | class SpoilerPattern(Pattern): | ||
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(Pattern): | ||||
def handleMatch(self, m): | ||||
quote_element = etree.Element('span') | ||||
quote_element.set('class', 'comment') | ||||
quote_element.text = '//' + m.group(3) | ||||
return quote_element | ||||
neko259
|
r52 | class NeboardMarkdown(markdown.Extension): | ||
neko259
|
r62 | AUTOLINK_PATTERN = r'(https?://\S+)' | ||
neko259
|
r90 | QUOTE_PATTERN = r'^(?<!>)(>[^>]+)$' | ||
REFLINK_PATTERN = r'((>>)(\d+))' | ||||
neko259
|
r56 | SPOILER_PATTERN = r'%%(.+)%%' | ||
neko259
|
r62 | COMMENT_PATTERN = r'^(//(.+))' | ||
neko259
|
r52 | |||
def extendMarkdown(self, md, md_globals): | ||||
neko259
|
r100 | self._add_neboard_patterns(md) | ||
self._delete_patterns(md) | ||||
def _delete_patterns(self, md): | ||||
del md.parser.blockprocessors['quote'] | ||||
del md.inlinePatterns['image_link'] | ||||
del md.inlinePatterns['image_reference'] | ||||
def _add_neboard_patterns(self, md): | ||||
neko259
|
r52 | autolink = AutolinkPattern(self.AUTOLINK_PATTERN, md) | ||
quote = QuotePattern(self.QUOTE_PATTERN, md) | ||||
reflink = ReflinkPattern(self.REFLINK_PATTERN, md) | ||||
neko259
|
r56 | spoiler = SpoilerPattern(self.SPOILER_PATTERN, md) | ||
comment = CommentPattern(self.COMMENT_PATTERN, md) | ||||
neko259
|
r52 | |||
md.inlinePatterns[u'autolink_ext'] = autolink | ||||
neko259
|
r56 | md.inlinePatterns[u'spoiler'] = spoiler | ||
md.inlinePatterns[u'comment'] = comment | ||||
neko259
|
r90 | md.inlinePatterns[u'reflink'] = reflink | ||
md.inlinePatterns[u'quote'] = quote | ||||
neko259
|
r52 | |||
def makeExtension(configs=None): | ||||
return NeboardMarkdown(configs=configs) | ||||
neboard_extension = makeExtension() | ||||
def markdown_extended(markup): | ||||
neko259
|
r90 | return markdown.markdown(markup, [neboard_extension], safe_mode=True) | ||