Show More
@@ -1,105 +1,106 b'' | |||||
1 | from django.core.urlresolvers import reverse |
|
1 | from django.core.urlresolvers import reverse | |
2 | import markdown |
|
2 | import markdown | |
3 | from markdown.inlinepatterns import Pattern |
|
3 | from markdown.inlinepatterns import Pattern | |
4 | from markdown.util import etree |
|
4 | from markdown.util import etree | |
5 | import boards |
|
5 | import boards | |
6 |
|
6 | |||
7 | __author__ = 'neko259' |
|
7 | __author__ = 'neko259' | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | AUTOLINK_PATTERN = r'(https?://\S+)' |
|
10 | AUTOLINK_PATTERN = r'(https?://\S+)' | |
11 | QUOTE_PATTERN = r'^(?<!>)(>[^>].+)$' |
|
11 | QUOTE_PATTERN = r'^(?<!>)(>[^>].+)$' | |
12 | REFLINK_PATTERN = r'((>>)(\d+))' |
|
12 | REFLINK_PATTERN = r'((>>)(\d+))' | |
13 | SPOILER_PATTERN = r'%%(.+)%%' |
|
13 | SPOILER_PATTERN = r'%%(.+)%%' | |
14 | COMMENT_PATTERN = r'^(//(.+))' |
|
14 | COMMENT_PATTERN = r'^(//(.+))' | |
15 |
|
15 | |||
16 | class AutolinkPattern(Pattern): |
|
16 | class AutolinkPattern(Pattern): | |
17 | def handleMatch(self, m): |
|
17 | def handleMatch(self, m): | |
18 | link_element = etree.Element('a') |
|
18 | link_element = etree.Element('a') | |
19 | href = m.group(2) |
|
19 | href = m.group(2) | |
20 | link_element.set('href', href) |
|
20 | link_element.set('href', href) | |
21 | link_element.text = href |
|
21 | link_element.text = href | |
22 |
|
22 | |||
23 | return link_element |
|
23 | return link_element | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | class QuotePattern(Pattern): |
|
26 | class QuotePattern(Pattern): | |
27 | def handleMatch(self, m): |
|
27 | def handleMatch(self, m): | |
28 | quote_element = etree.Element('span') |
|
28 | quote_element = etree.Element('span') | |
29 | quote_element.set('class', 'quote') |
|
29 | quote_element.set('class', 'quote') | |
30 | quote_element.text = m.group(2) |
|
30 | quote_element.text = m.group(2) | |
31 |
|
31 | |||
32 | return quote_element |
|
32 | return quote_element | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | class ReflinkPattern(Pattern): |
|
35 | class ReflinkPattern(Pattern): | |
36 | def handleMatch(self, m): |
|
36 | def handleMatch(self, m): | |
37 | post_id = m.group(4) |
|
37 | post_id = m.group(4) | |
38 |
|
38 | |||
39 | post = Post.objects.filter(id=post_id) |
|
39 | posts = boards.models.Post.objects.filter(id=post_id) | |
40 | if post: |
|
40 | if posts.count() > 0: | |
41 | ref_element = etree.Element('a') |
|
41 | ref_element = etree.Element('a') | |
42 |
|
42 | |||
|
43 | post = posts[0] | |||
43 | if post.thread: |
|
44 | if post.thread: | |
44 |
link = reverse(thread, kwargs={'post_id': post.thread.id}) |
|
45 | link = reverse(boards.views.thread, kwargs={'post_id': post.thread.id}) \ | |
45 |
+ '#' + post_id |
|
46 | + '#' + post_id | |
46 | else: |
|
47 | else: | |
47 | link = reverse(thread, post_id=post_id) |
|
48 | link = reverse(boards.views.thread, post_id=post_id) | |
48 |
|
49 | |||
49 | ref_element.set('href', link) |
|
50 | ref_element.set('href', link) | |
50 | ref_element.text = m.group(2) |
|
51 | ref_element.text = m.group(2) | |
51 |
|
52 | |||
52 | return ref_element |
|
53 | return ref_element | |
53 |
|
54 | |||
54 |
|
55 | |||
55 | class SpoilerPattern(Pattern): |
|
56 | class SpoilerPattern(Pattern): | |
56 | def handleMatch(self, m): |
|
57 | def handleMatch(self, m): | |
57 | quote_element = etree.Element('span') |
|
58 | quote_element = etree.Element('span') | |
58 | quote_element.set('class', 'spoiler') |
|
59 | quote_element.set('class', 'spoiler') | |
59 | quote_element.text = m.group(2) |
|
60 | quote_element.text = m.group(2) | |
60 |
|
61 | |||
61 | return quote_element |
|
62 | return quote_element | |
62 |
|
63 | |||
63 |
|
64 | |||
64 | class CommentPattern(Pattern): |
|
65 | class CommentPattern(Pattern): | |
65 | def handleMatch(self, m): |
|
66 | def handleMatch(self, m): | |
66 | quote_element = etree.Element('span') |
|
67 | quote_element = etree.Element('span') | |
67 | quote_element.set('class', 'comment') |
|
68 | quote_element.set('class', 'comment') | |
68 | quote_element.text = '//' + m.group(3) |
|
69 | quote_element.text = '//' + m.group(3) | |
69 |
|
70 | |||
70 | return quote_element |
|
71 | return quote_element | |
71 |
|
72 | |||
72 |
|
73 | |||
73 | class NeboardMarkdown(markdown.Extension): |
|
74 | class NeboardMarkdown(markdown.Extension): | |
74 | def extendMarkdown(self, md, md_globals): |
|
75 | def extendMarkdown(self, md, md_globals): | |
75 | self._add_neboard_patterns(md) |
|
76 | self._add_neboard_patterns(md) | |
76 | self._delete_patterns(md) |
|
77 | self._delete_patterns(md) | |
77 |
|
78 | |||
78 | def _delete_patterns(self, md): |
|
79 | def _delete_patterns(self, md): | |
79 | del md.parser.blockprocessors['quote'] |
|
80 | del md.parser.blockprocessors['quote'] | |
80 |
|
81 | |||
81 | del md.inlinePatterns['image_link'] |
|
82 | del md.inlinePatterns['image_link'] | |
82 | del md.inlinePatterns['image_reference'] |
|
83 | del md.inlinePatterns['image_reference'] | |
83 |
|
84 | |||
84 | def _add_neboard_patterns(self, md): |
|
85 | def _add_neboard_patterns(self, md): | |
85 | autolink = AutolinkPattern(AUTOLINK_PATTERN, md) |
|
86 | autolink = AutolinkPattern(AUTOLINK_PATTERN, md) | |
86 | quote = QuotePattern(QUOTE_PATTERN, md) |
|
87 | quote = QuotePattern(QUOTE_PATTERN, md) | |
87 | reflink = ReflinkPattern(REFLINK_PATTERN, md) |
|
88 | reflink = ReflinkPattern(REFLINK_PATTERN, md) | |
88 | spoiler = SpoilerPattern(SPOILER_PATTERN, md) |
|
89 | spoiler = SpoilerPattern(SPOILER_PATTERN, md) | |
89 | comment = CommentPattern(COMMENT_PATTERN, md) |
|
90 | comment = CommentPattern(COMMENT_PATTERN, md) | |
90 |
|
91 | |||
91 | md.inlinePatterns[u'autolink_ext'] = autolink |
|
92 | md.inlinePatterns[u'autolink_ext'] = autolink | |
92 | md.inlinePatterns[u'spoiler'] = spoiler |
|
93 | md.inlinePatterns[u'spoiler'] = spoiler | |
93 | md.inlinePatterns[u'comment'] = comment |
|
94 | md.inlinePatterns[u'comment'] = comment | |
94 | md.inlinePatterns[u'reflink'] = reflink |
|
95 | md.inlinePatterns[u'reflink'] = reflink | |
95 | md.inlinePatterns[u'quote'] = quote |
|
96 | md.inlinePatterns[u'quote'] = quote | |
96 |
|
97 | |||
97 |
|
98 | |||
98 | def makeExtension(configs=None): |
|
99 | def makeExtension(configs=None): | |
99 | return NeboardMarkdown(configs=configs) |
|
100 | return NeboardMarkdown(configs=configs) | |
100 |
|
101 | |||
101 | neboard_extension = makeExtension() |
|
102 | neboard_extension = makeExtension() | |
102 |
|
103 | |||
103 |
|
104 | |||
104 | def markdown_extended(markup): |
|
105 | def markdown_extended(markup): | |
105 | return markdown.markdown(markup, [neboard_extension], safe_mode=True) |
|
106 | return markdown.markdown(markup, [neboard_extension], safe_mode=True) |
General Comments 0
You need to be logged in to leave comments.
Login now