Show More
@@ -1,119 +1,120 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 | STRIKETHROUGH_PATTERN = r'~(.+)~' |
|
15 | STRIKETHROUGH_PATTERN = r'~(.+)~' | |
16 |
|
16 | |||
17 |
|
17 | |||
18 | class AutolinkPattern(Pattern): |
|
18 | class AutolinkPattern(Pattern): | |
19 | def handleMatch(self, m): |
|
19 | def handleMatch(self, m): | |
20 | link_element = etree.Element('a') |
|
20 | link_element = etree.Element('a') | |
21 | href = m.group(2) |
|
21 | href = m.group(2) | |
22 | link_element.set('href', href) |
|
22 | link_element.set('href', href) | |
23 | link_element.text = href |
|
23 | link_element.text = href | |
24 |
|
24 | |||
25 | return link_element |
|
25 | return link_element | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | class QuotePattern(Pattern): |
|
28 | class QuotePattern(Pattern): | |
29 | def handleMatch(self, m): |
|
29 | def handleMatch(self, m): | |
30 | quote_element = etree.Element('span') |
|
30 | quote_element = etree.Element('span') | |
31 | quote_element.set('class', 'quote') |
|
31 | quote_element.set('class', 'quote') | |
32 | quote_element.text = m.group(2) |
|
32 | quote_element.text = m.group(2) | |
33 |
|
33 | |||
34 | return quote_element |
|
34 | return quote_element | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | class ReflinkPattern(Pattern): |
|
37 | class ReflinkPattern(Pattern): | |
38 | def handleMatch(self, m): |
|
38 | def handleMatch(self, m): | |
39 | post_id = m.group(4) |
|
39 | post_id = m.group(4) | |
40 |
|
40 | |||
41 | posts = boards.models.Post.objects.filter(id=post_id) |
|
41 | posts = boards.models.Post.objects.filter(id=post_id) | |
42 | if posts.count() > 0: |
|
42 | if posts.count() > 0: | |
43 | ref_element = etree.Element('a') |
|
43 | ref_element = etree.Element('a') | |
44 |
|
44 | |||
45 | post = posts[0] |
|
45 | post = posts[0] | |
46 |
if post. |
|
46 | if not post.is_opening(): | |
47 |
link = reverse(boards.views.thread, kwargs={ |
|
47 | link = reverse(boards.views.thread, kwargs={ | |
48 | + '#' + post_id |
|
48 | 'post_id': post.thread_new.get_opening_post().id})\ | |
|
49 | + '#' + post_id | |||
49 | else: |
|
50 | else: | |
50 | link = reverse(boards.views.thread, kwargs={'post_id': post_id}) |
|
51 | link = reverse(boards.views.thread, kwargs={'post_id': post_id}) | |
51 |
|
52 | |||
52 | ref_element.set('href', link) |
|
53 | ref_element.set('href', link) | |
53 | ref_element.text = m.group(2) |
|
54 | ref_element.text = m.group(2) | |
54 |
|
55 | |||
55 | return ref_element |
|
56 | return ref_element | |
56 |
|
57 | |||
57 |
|
58 | |||
58 | class SpoilerPattern(Pattern): |
|
59 | class SpoilerPattern(Pattern): | |
59 | def handleMatch(self, m): |
|
60 | def handleMatch(self, m): | |
60 | quote_element = etree.Element('span') |
|
61 | quote_element = etree.Element('span') | |
61 | quote_element.set('class', 'spoiler') |
|
62 | quote_element.set('class', 'spoiler') | |
62 | quote_element.text = m.group(2) |
|
63 | quote_element.text = m.group(2) | |
63 |
|
64 | |||
64 | return quote_element |
|
65 | return quote_element | |
65 |
|
66 | |||
66 |
|
67 | |||
67 | class CommentPattern(Pattern): |
|
68 | class CommentPattern(Pattern): | |
68 | def handleMatch(self, m): |
|
69 | def handleMatch(self, m): | |
69 | quote_element = etree.Element('span') |
|
70 | quote_element = etree.Element('span') | |
70 | quote_element.set('class', 'comment') |
|
71 | quote_element.set('class', 'comment') | |
71 | quote_element.text = '//' + m.group(3) |
|
72 | quote_element.text = '//' + m.group(3) | |
72 |
|
73 | |||
73 | return quote_element |
|
74 | return quote_element | |
74 |
|
75 | |||
75 |
|
76 | |||
76 | class StrikeThroughPattern(Pattern): |
|
77 | class StrikeThroughPattern(Pattern): | |
77 | def handleMatch(self, m): |
|
78 | def handleMatch(self, m): | |
78 | quote_element = etree.Element('span') |
|
79 | quote_element = etree.Element('span') | |
79 | quote_element.set('class', 'strikethrough') |
|
80 | quote_element.set('class', 'strikethrough') | |
80 | quote_element.text = m.group(2) |
|
81 | quote_element.text = m.group(2) | |
81 |
|
82 | |||
82 | return quote_element |
|
83 | return quote_element | |
83 |
|
84 | |||
84 |
|
85 | |||
85 | class NeboardMarkdown(markdown.Extension): |
|
86 | class NeboardMarkdown(markdown.Extension): | |
86 | def extendMarkdown(self, md, md_globals): |
|
87 | def extendMarkdown(self, md, md_globals): | |
87 | self._add_neboard_patterns(md) |
|
88 | self._add_neboard_patterns(md) | |
88 | self._delete_patterns(md) |
|
89 | self._delete_patterns(md) | |
89 |
|
90 | |||
90 | def _delete_patterns(self, md): |
|
91 | def _delete_patterns(self, md): | |
91 | del md.parser.blockprocessors['quote'] |
|
92 | del md.parser.blockprocessors['quote'] | |
92 |
|
93 | |||
93 | del md.inlinePatterns['image_link'] |
|
94 | del md.inlinePatterns['image_link'] | |
94 | del md.inlinePatterns['image_reference'] |
|
95 | del md.inlinePatterns['image_reference'] | |
95 |
|
96 | |||
96 | def _add_neboard_patterns(self, md): |
|
97 | def _add_neboard_patterns(self, md): | |
97 | autolink = AutolinkPattern(AUTOLINK_PATTERN, md) |
|
98 | autolink = AutolinkPattern(AUTOLINK_PATTERN, md) | |
98 | quote = QuotePattern(QUOTE_PATTERN, md) |
|
99 | quote = QuotePattern(QUOTE_PATTERN, md) | |
99 | reflink = ReflinkPattern(REFLINK_PATTERN, md) |
|
100 | reflink = ReflinkPattern(REFLINK_PATTERN, md) | |
100 | spoiler = SpoilerPattern(SPOILER_PATTERN, md) |
|
101 | spoiler = SpoilerPattern(SPOILER_PATTERN, md) | |
101 | comment = CommentPattern(COMMENT_PATTERN, md) |
|
102 | comment = CommentPattern(COMMENT_PATTERN, md) | |
102 |
|
|
103 | strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md) | |
103 |
|
104 | |||
104 | md.inlinePatterns[u'autolink_ext'] = autolink |
|
105 | md.inlinePatterns[u'autolink_ext'] = autolink | |
105 | md.inlinePatterns[u'spoiler'] = spoiler |
|
106 | md.inlinePatterns[u'spoiler'] = spoiler | |
106 |
|
|
107 | md.inlinePatterns[u'strikethrough'] = strikethrough | |
107 | md.inlinePatterns[u'comment'] = comment |
|
108 | md.inlinePatterns[u'comment'] = comment | |
108 | md.inlinePatterns[u'reflink'] = reflink |
|
109 | md.inlinePatterns[u'reflink'] = reflink | |
109 | md.inlinePatterns[u'quote'] = quote |
|
110 | md.inlinePatterns[u'quote'] = quote | |
110 |
|
111 | |||
111 |
|
112 | |||
112 | def makeExtension(configs=None): |
|
113 | def makeExtension(configs=None): | |
113 | return NeboardMarkdown(configs=configs) |
|
114 | return NeboardMarkdown(configs=configs) | |
114 |
|
115 | |||
115 | neboard_extension = makeExtension() |
|
116 | neboard_extension = makeExtension() | |
116 |
|
117 | |||
117 |
|
118 | |||
118 | def markdown_extended(markup): |
|
119 | def markdown_extended(markup): | |
119 | return markdown.markdown(markup, [neboard_extension], safe_mode=True) |
|
120 | return markdown.markdown(markup, [neboard_extension], safe_mode=True) |
General Comments 0
You need to be logged in to leave comments.
Login now