-"""A more liberal autolinker
-
-Inspired by Django's urlize function.
-
-Positive examples:
-
->>> import markdown
->>> md = markdown.Markdown(extensions=['urlize'])
-
->>> md.convert('http://example.com/')
-u'<p><a href="http://example.com/">http://example.com/</a></p>'
-
->>> md.convert('go to http://example.com')
-u'<p>go to <a href="http://example.com">http://example.com</a></p>'
-
->>> md.convert('example.com')
-u'<p><a href="http://example.com">example.com</a></p>'
-
->>> md.convert('example.net')
-u'<p><a href="http://example.net">example.net</a></p>'
-
->>> md.convert('www.example.us')
-u'<p><a href="http://www.example.us">www.example.us</a></p>'
-
->>> md.convert('(www.example.us/path/?name=val)')
-u'<p>(<a href="http://www.example.us/path/?name=val">www.example.us/path/?name=val</a>)</p>'
-
->>> md.convert('go to <http://example.com> now!')
-u'<p>go to <a href="http://example.com">http://example.com</a> now!</p>'
-
-Negative examples:
-
->>> md.convert('del.icio.us')
-u'<p>del.icio.us</p>'
-
-"""
-
import markdown
# Global Vars
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from django.utils.html import strip_tags
+from forum.utils.userlinking import auto_user_link
from forum.utils.html import sanitize_html
from utils import PickledObjectField
def activate_revision(self, user, revision, extensions=['urlize']):
self.title = revision.title
self.tagnames = revision.tagnames
- self.body = self._as_markdown(revision.body, *extensions)
+ self.body = auto_user_link(self, self._as_markdown(revision.body, *extensions))
self.active_revision = revision
self.update_last_activity(user)
cache.set(cache_key, related_list, 60 * 60)
return [Question.objects.get(id=r['id']) for r in related_list]
+
+ def get_active_users(self):
+ active_users = []
+
+ active_users.append(self.author)
+
+ for answer in self.answers:
+ active_users.append(answer.author)
+
+ for child in self.children.all():
+ active_users.append(child.author)
+ for grandchild in child.children.all():
+ active_users.append(grandchild.author)
+
+ # Remove duplicates
+ unique_active_users = []
+ for user in active_users:
+ if user not in unique_active_users:
+ unique_active_users.append(user)
+ active_users = unique_active_users
+ del unique_active_users
+
+ return active_users
def question_viewed(instance, **kwargs):
--- /dev/null
+import re
+
+from forum.models.user import User
+
+def auto_user_link(node, content):
+ patern = r'@\w+'
+ appeals = re.findall(patern, content)
+
+ for appeal in appeals:
+ # Try to find the profile URL
+ username = appeal[1:]
+ profile_url = None
+
+ try:
+ user = User.objects.get(username__iexact=username)
+ profile_url = user.get_absolute_url()
+ except User.DoesNotExist:
+ """If we don't find the user from the first time, the interesting part
+ begins. We look through all the authors (looking through question,
+ comments, answers, and if it matches some of the -- we link him."""
+
+ # We should find the root of the node tree (question) the current node belongs to.
+ if node.node_type == "question":
+ question = node
+ elif node.node_type == "answer":
+ question = node.question
+ elif node.node_type == "comment":
+ if not node.question:
+ question = node
+ else:
+ question = node.question
+
+ # Now we've got the root question. Let's get the list of active users.
+ active_users = question.get_active_users()
+
+ for active_user in active_users:
+ if active_user.username.lower().startswith(username.lower()):
+ profile_url = active_user.get_absolute_url()
+
+ if (profile_url is not None) and (appeal is not None):
+ auto_link = '<a href="%s">%s</a>' % (profile_url, appeal)
+ content = content.replace(appeal, auto_link)
+
+ return content
\ No newline at end of file