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