##// END OF EJS Templates
Fixed reflink parsing
neko259 -
r556:10b29819 1.7-dev
parent child Browse files
Show More
@@ -1,198 +1,198 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 TextFormatter():
18 class TextFormatter():
19 """
19 """
20 An interface for formatter that can be used in the text format panel
20 An interface for formatter that can be used in the text format panel
21 """
21 """
22
22
23 name = ''
23 name = ''
24
24
25 # Left and right tags for the button preview
25 # Left and right tags for the button preview
26 preview_left = ''
26 preview_left = ''
27 preview_right = ''
27 preview_right = ''
28
28
29 # Left and right characters for the textarea input
29 # Left and right characters for the textarea input
30 format_left = ''
30 format_left = ''
31 format_right = ''
31 format_right = ''
32
32
33
33
34 class AutolinkPattern(Pattern):
34 class AutolinkPattern(Pattern):
35 def handleMatch(self, m):
35 def handleMatch(self, m):
36 link_element = etree.Element('a')
36 link_element = etree.Element('a')
37 href = m.group(2)
37 href = m.group(2)
38 link_element.set('href', href)
38 link_element.set('href', href)
39 link_element.text = href
39 link_element.text = href
40
40
41 return link_element
41 return link_element
42
42
43
43
44 class QuotePattern(Pattern, TextFormatter):
44 class QuotePattern(Pattern, TextFormatter):
45 name = ''
45 name = ''
46 preview_left = '<span class="quote">&gt; '
46 preview_left = '<span class="quote">&gt; '
47 preview_right = '</span>'
47 preview_right = '</span>'
48
48
49 format_left = '&gt;'
49 format_left = '&gt;'
50
50
51 def handleMatch(self, m):
51 def handleMatch(self, m):
52 quote_element = etree.Element('span')
52 quote_element = etree.Element('span')
53 quote_element.set('class', 'quote')
53 quote_element.set('class', 'quote')
54 quote_element.text = m.group(2)
54 quote_element.text = m.group(2)
55
55
56 return quote_element
56 return quote_element
57
57
58
58
59 class ReflinkPattern(Pattern):
59 class ReflinkPattern(Pattern):
60 def handleMatch(self, m):
60 def handleMatch(self, m):
61 post_id = m.group(4)
61 post_id = m.group(4)
62
62
63 posts = boards.models.Post.objects.filter(id=post_id)
63 posts = boards.models.Post.objects.filter(id=post_id)
64 if posts.count() > 0:
64 if posts.count() > 0:
65 ref_element = etree.Element('a')
65 ref_element = etree.Element('a')
66
66
67 post = posts[0]
67 post = posts[0]
68 if not post.is_opening():
68 if not post.is_opening():
69 link = reverse(boards.views.thread, kwargs={
69 link = reverse('thread', kwargs={
70 'post_id': post.thread_new.get_opening_post().id})\
70 'post_id': post.thread_new.get_opening_post().id})\
71 + '#' + post_id
71 + '#' + post_id
72 else:
72 else:
73 link = reverse(boards.views.thread, kwargs={'post_id': post_id})
73 link = reverse('thread', kwargs={'post_id': post_id})
74
74
75 ref_element.set('href', link)
75 ref_element.set('href', link)
76 ref_element.text = m.group(2)
76 ref_element.text = m.group(2)
77
77
78 return ref_element
78 return ref_element
79
79
80
80
81 class SpoilerPattern(Pattern, TextFormatter):
81 class SpoilerPattern(Pattern, TextFormatter):
82 name = 's'
82 name = 's'
83 preview_left = '<span class="spoiler">'
83 preview_left = '<span class="spoiler">'
84 preview_right = '</span>'
84 preview_right = '</span>'
85
85
86 format_left = '%%'
86 format_left = '%%'
87 format_right = '%%'
87 format_right = '%%'
88
88
89 def handleMatch(self, m):
89 def handleMatch(self, m):
90 quote_element = etree.Element('span')
90 quote_element = etree.Element('span')
91 quote_element.set('class', 'spoiler')
91 quote_element.set('class', 'spoiler')
92 quote_element.text = m.group(2)
92 quote_element.text = m.group(2)
93
93
94 return quote_element
94 return quote_element
95
95
96
96
97 class CommentPattern(Pattern, TextFormatter):
97 class CommentPattern(Pattern, TextFormatter):
98 name = ''
98 name = ''
99 preview_left = '<span class="comment">// '
99 preview_left = '<span class="comment">// '
100 preview_right = '</span>'
100 preview_right = '</span>'
101
101
102 format_left = '//'
102 format_left = '//'
103
103
104 def handleMatch(self, m):
104 def handleMatch(self, m):
105 quote_element = etree.Element('span')
105 quote_element = etree.Element('span')
106 quote_element.set('class', 'comment')
106 quote_element.set('class', 'comment')
107 quote_element.text = '//' + m.group(3)
107 quote_element.text = '//' + m.group(3)
108
108
109 return quote_element
109 return quote_element
110
110
111
111
112 class StrikeThroughPattern(Pattern, TextFormatter):
112 class StrikeThroughPattern(Pattern, TextFormatter):
113 name = 's'
113 name = 's'
114 preview_left = '<span class="strikethrough">'
114 preview_left = '<span class="strikethrough">'
115 preview_right = '</span>'
115 preview_right = '</span>'
116
116
117 format_left = '~'
117 format_left = '~'
118 format_right = '~'
118 format_right = '~'
119
119
120 def handleMatch(self, m):
120 def handleMatch(self, m):
121 quote_element = etree.Element('span')
121 quote_element = etree.Element('span')
122 quote_element.set('class', 'strikethrough')
122 quote_element.set('class', 'strikethrough')
123 quote_element.text = m.group(2)
123 quote_element.text = m.group(2)
124
124
125 return quote_element
125 return quote_element
126
126
127
127
128 class ItalicPattern(TextFormatter):
128 class ItalicPattern(TextFormatter):
129 name = 'i'
129 name = 'i'
130 preview_left = '<i>'
130 preview_left = '<i>'
131 preview_right = '</i>'
131 preview_right = '</i>'
132
132
133 format_left = '_'
133 format_left = '_'
134 format_right = '_'
134 format_right = '_'
135
135
136
136
137 class BoldPattern(TextFormatter):
137 class BoldPattern(TextFormatter):
138 name = 'b'
138 name = 'b'
139 preview_left = '<b>'
139 preview_left = '<b>'
140 preview_right = '</b>'
140 preview_right = '</b>'
141
141
142 format_left = '__'
142 format_left = '__'
143 format_right = '__'
143 format_right = '__'
144
144
145
145
146 class CodePattern(TextFormatter):
146 class CodePattern(TextFormatter):
147 name = 'code'
147 name = 'code'
148 preview_left = '<code>'
148 preview_left = '<code>'
149 preview_right = '</code>'
149 preview_right = '</code>'
150
150
151 format_left = ' '
151 format_left = ' '
152
152
153
153
154 class NeboardMarkdown(markdown.Extension):
154 class NeboardMarkdown(markdown.Extension):
155 def extendMarkdown(self, md, md_globals):
155 def extendMarkdown(self, md, md_globals):
156 self._add_neboard_patterns(md)
156 self._add_neboard_patterns(md)
157 self._delete_patterns(md)
157 self._delete_patterns(md)
158
158
159 def _delete_patterns(self, md):
159 def _delete_patterns(self, md):
160 del md.parser.blockprocessors['quote']
160 del md.parser.blockprocessors['quote']
161
161
162 del md.inlinePatterns['image_link']
162 del md.inlinePatterns['image_link']
163 del md.inlinePatterns['image_reference']
163 del md.inlinePatterns['image_reference']
164
164
165 def _add_neboard_patterns(self, md):
165 def _add_neboard_patterns(self, md):
166 autolink = AutolinkPattern(AUTOLINK_PATTERN, md)
166 autolink = AutolinkPattern(AUTOLINK_PATTERN, md)
167 quote = QuotePattern(QUOTE_PATTERN, md)
167 quote = QuotePattern(QUOTE_PATTERN, md)
168 reflink = ReflinkPattern(REFLINK_PATTERN, md)
168 reflink = ReflinkPattern(REFLINK_PATTERN, md)
169 spoiler = SpoilerPattern(SPOILER_PATTERN, md)
169 spoiler = SpoilerPattern(SPOILER_PATTERN, md)
170 comment = CommentPattern(COMMENT_PATTERN, md)
170 comment = CommentPattern(COMMENT_PATTERN, md)
171 strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md)
171 strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md)
172
172
173 md.inlinePatterns[u'autolink_ext'] = autolink
173 md.inlinePatterns[u'autolink_ext'] = autolink
174 md.inlinePatterns[u'spoiler'] = spoiler
174 md.inlinePatterns[u'spoiler'] = spoiler
175 md.inlinePatterns[u'strikethrough'] = strikethrough
175 md.inlinePatterns[u'strikethrough'] = strikethrough
176 md.inlinePatterns[u'comment'] = comment
176 md.inlinePatterns[u'comment'] = comment
177 md.inlinePatterns[u'reflink'] = reflink
177 md.inlinePatterns[u'reflink'] = reflink
178 md.inlinePatterns[u'quote'] = quote
178 md.inlinePatterns[u'quote'] = quote
179
179
180
180
181 def make_extension(configs=None):
181 def make_extension(configs=None):
182 return NeboardMarkdown(configs=configs)
182 return NeboardMarkdown(configs=configs)
183
183
184 neboard_extension = make_extension()
184 neboard_extension = make_extension()
185
185
186
186
187 def markdown_extended(markup):
187 def markdown_extended(markup):
188 return markdown.markdown(markup, [neboard_extension], safe_mode=True)
188 return markdown.markdown(markup, [neboard_extension], safe_mode=True)
189
189
190 formatters = [
190 formatters = [
191 QuotePattern,
191 QuotePattern,
192 SpoilerPattern,
192 SpoilerPattern,
193 ItalicPattern,
193 ItalicPattern,
194 BoldPattern,
194 BoldPattern,
195 CommentPattern,
195 CommentPattern,
196 StrikeThroughPattern,
196 StrikeThroughPattern,
197 CodePattern,
197 CodePattern,
198 ]
198 ]
@@ -1,172 +1,165 b''
1 __author__ = 'neko259'
1 __author__ = 'neko259'
2
2
3 import hashlib
3 import hashlib
4
4
5 from django.core import serializers
5 from django.core import serializers
6 from django.core.urlresolvers import reverse
6 from django.core.urlresolvers import reverse
7 from django.http import HttpResponseRedirect
7 from django.http import HttpResponseRedirect
8 from django.http.response import HttpResponse
8 from django.http.response import HttpResponse
9 from django.template import RequestContext
9 from django.template import RequestContext
10 from django.shortcuts import render, redirect, get_object_or_404
10 from django.shortcuts import render, redirect, get_object_or_404
11 from django.utils import timezone
11 from django.utils import timezone
12 from django.db import transaction
12 from django.db import transaction
13 from django.views.decorators.cache import cache_page
13 from django.views.decorators.cache import cache_page
14 from django.views.i18n import javascript_catalog
14 from django.views.i18n import javascript_catalog
15
15
16 import boards
16 import boards
17 from boards.forms import PlainErrorList
17 from boards.forms import PlainErrorList
18 from boards.models import Post, Tag, Ban, User
18 from boards.models import Post, Tag, Ban, User
19 from boards.models.post import SETTING_MODERATE
19 from boards.models.post import SETTING_MODERATE
20 from boards.models.user import RANK_USER
20 from boards.models.user import RANK_USER
21 from boards import authors
21 from boards import authors
22 import neboard
22 import neboard
23
23
24
24
25 BAN_REASON_SPAM = 'Autoban: spam bot'
26
27 DEFAULT_PAGE = 1
28
29
30
31
32 @transaction.atomic
25 @transaction.atomic
33 def tag_subscribe(request, tag_name):
26 def tag_subscribe(request, tag_name):
34 """Add tag to favorites"""
27 """Add tag to favorites"""
35
28
36 user = _get_user(request)
29 user = _get_user(request)
37 tag = get_object_or_404(Tag, name=tag_name)
30 tag = get_object_or_404(Tag, name=tag_name)
38
31
39 if not tag in user.fav_tags.all():
32 if not tag in user.fav_tags.all():
40 user.add_tag(tag)
33 user.add_tag(tag)
41
34
42 return _redirect_to_next(request)
35 return _redirect_to_next(request)
43
36
44
37
45 @transaction.atomic
38 @transaction.atomic
46 def tag_unsubscribe(request, tag_name):
39 def tag_unsubscribe(request, tag_name):
47 """Remove tag from favorites"""
40 """Remove tag from favorites"""
48
41
49 user = _get_user(request)
42 user = _get_user(request)
50 tag = get_object_or_404(Tag, name=tag_name)
43 tag = get_object_or_404(Tag, name=tag_name)
51
44
52 if tag in user.fav_tags.all():
45 if tag in user.fav_tags.all():
53 user.remove_tag(tag)
46 user.remove_tag(tag)
54
47
55 return _redirect_to_next(request)
48 return _redirect_to_next(request)
56
49
57
50
58 def static_page(request, name):
51 def static_page(request, name):
59 """Show a static page that needs only tags list and a CSS"""
52 """Show a static page that needs only tags list and a CSS"""
60
53
61 context = _init_default_context(request)
54 context = _init_default_context(request)
62 return render(request, 'boards/staticpages/' + name + '.html', context)
55 return render(request, 'boards/staticpages/' + name + '.html', context)
63
56
64
57
65 # TODO This has to be moved under the api module
58 # TODO This has to be moved under the api module
66 def api_get_post(request, post_id):
59 def api_get_post(request, post_id):
67 """
60 """
68 Get the JSON of a post. This can be
61 Get the JSON of a post. This can be
69 used as and API for external clients.
62 used as and API for external clients.
70 """
63 """
71
64
72 post = get_object_or_404(Post, id=post_id)
65 post = get_object_or_404(Post, id=post_id)
73
66
74 json = serializers.serialize("json", [post], fields=(
67 json = serializers.serialize("json", [post], fields=(
75 "pub_time", "_text_rendered", "title", "text", "image",
68 "pub_time", "_text_rendered", "title", "text", "image",
76 "image_width", "image_height", "replies", "tags"
69 "image_width", "image_height", "replies", "tags"
77 ))
70 ))
78
71
79 return HttpResponse(content=json)
72 return HttpResponse(content=json)
80
73
81
74
82 @cache_page(86400)
75 @cache_page(86400)
83 def cached_js_catalog(request, domain='djangojs', packages=None):
76 def cached_js_catalog(request, domain='djangojs', packages=None):
84 return javascript_catalog(request, domain, packages)
77 return javascript_catalog(request, domain, packages)
85
78
86
79
87 # TODO This method is deprecated and should be removed after switching to
80 # TODO This method is deprecated and should be removed after switching to
88 # class-based view
81 # class-based view
89 def _get_theme(request, user=None):
82 def _get_theme(request, user=None):
90 """Get user's CSS theme"""
83 """Get user's CSS theme"""
91
84
92 if not user:
85 if not user:
93 user = _get_user(request)
86 user = _get_user(request)
94 theme = user.get_setting('theme')
87 theme = user.get_setting('theme')
95 if not theme:
88 if not theme:
96 theme = neboard.settings.DEFAULT_THEME
89 theme = neboard.settings.DEFAULT_THEME
97
90
98 return theme
91 return theme
99
92
100
93
101 # TODO This method is deprecated and should be removed after switching to
94 # TODO This method is deprecated and should be removed after switching to
102 # class-based view
95 # class-based view
103 def _init_default_context(request):
96 def _init_default_context(request):
104 """Create context with default values that are used in most views"""
97 """Create context with default values that are used in most views"""
105
98
106 context = RequestContext(request)
99 context = RequestContext(request)
107
100
108 user = _get_user(request)
101 user = _get_user(request)
109 context['user'] = user
102 context['user'] = user
110 context['tags'] = user.get_sorted_fav_tags()
103 context['tags'] = user.get_sorted_fav_tags()
111 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
104 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
112
105
113 theme = _get_theme(request, user)
106 theme = _get_theme(request, user)
114 context['theme'] = theme
107 context['theme'] = theme
115 context['theme_css'] = 'css/' + theme + '/base_page.css'
108 context['theme_css'] = 'css/' + theme + '/base_page.css'
116
109
117 # This shows the moderator panel
110 # This shows the moderator panel
118 moderate = user.get_setting(SETTING_MODERATE)
111 moderate = user.get_setting(SETTING_MODERATE)
119 if moderate == 'True':
112 if moderate == 'True':
120 context['moderator'] = user.is_moderator()
113 context['moderator'] = user.is_moderator()
121 else:
114 else:
122 context['moderator'] = False
115 context['moderator'] = False
123
116
124 return context
117 return context
125
118
126
119
127 # TODO This method is deprecated and should be removed after switching to
120 # TODO This method is deprecated and should be removed after switching to
128 # class-based view
121 # class-based view
129 def _get_user(request):
122 def _get_user(request):
130 """
123 """
131 Get current user from the session. If the user does not exist, create
124 Get current user from the session. If the user does not exist, create
132 a new one.
125 a new one.
133 """
126 """
134
127
135 session = request.session
128 session = request.session
136 if not 'user_id' in session:
129 if not 'user_id' in session:
137 request.session.save()
130 request.session.save()
138
131
139 md5 = hashlib.md5()
132 md5 = hashlib.md5()
140 md5.update(session.session_key)
133 md5.update(session.session_key)
141 new_id = md5.hexdigest()
134 new_id = md5.hexdigest()
142
135
143 while User.objects.filter(user_id=new_id).exists():
136 while User.objects.filter(user_id=new_id).exists():
144 md5.update(str(timezone.now()))
137 md5.update(str(timezone.now()))
145 new_id = md5.hexdigest()
138 new_id = md5.hexdigest()
146
139
147 time_now = timezone.now()
140 time_now = timezone.now()
148 user = User.objects.create(user_id=new_id, rank=RANK_USER,
141 user = User.objects.create(user_id=new_id, rank=RANK_USER,
149 registration_time=time_now)
142 registration_time=time_now)
150
143
151 # TODO This is just a test. This method should be removed
144 # TODO This is just a test. This method should be removed
152 # _delete_old_users()
145 # _delete_old_users()
153
146
154 session['user_id'] = user.id
147 session['user_id'] = user.id
155 else:
148 else:
156 user = User.objects.get(id=session['user_id'])
149 user = User.objects.get(id=session['user_id'])
157
150
158 return user
151 return user
159
152
160
153
161 def _redirect_to_next(request):
154 def _redirect_to_next(request):
162 """
155 """
163 If a 'next' parameter was specified, redirect to the next page. This is
156 If a 'next' parameter was specified, redirect to the next page. This is
164 used when the user is required to return to some page after the current
157 used when the user is required to return to some page after the current
165 view has finished its work.
158 view has finished its work.
166 """
159 """
167
160
168 if 'next' in request.GET:
161 if 'next' in request.GET:
169 next_page = request.GET['next']
162 next_page = request.GET['next']
170 return HttpResponseRedirect(next_page)
163 return HttpResponseRedirect(next_page)
171 else:
164 else:
172 return redirect('index')
165 return redirect('index')
General Comments 0
You need to be logged in to leave comments. Login now