diff options
author | Arun SAG <sagarun@gmail.com> | 2011-09-24 19:53:10 +0530 |
---|---|---|
committer | Arun SAG <sagarun@gmail.com> | 2011-09-24 19:53:10 +0530 |
commit | d188a4a715c50b5a9bd57ce038985dcd72ae3292 (patch) | |
tree | 4b9829f25eb625c97044790dcd2ea7ee3d382aa1 | |
parent | 48c58a0ee87b5ea4b6f628640903216304808efa (diff) | |
parent | 87b56b761ac999e33e9c243f7f5b8aff4fba2cb1 (diff) | |
download | askbot-d188a4a715c50b5a9bd57ce038985dcd72ae3292.tar.gz askbot-d188a4a715c50b5a9bd57ce038985dcd72ae3292.tar.bz2 askbot-d188a4a715c50b5a9bd57ce038985dcd72ae3292.zip |
Merge branch 'auto-link'
-rw-r--r-- | askbot/conf/markup.py | 82 | ||||
-rw-r--r-- | askbot/utils/markup.py | 10 |
2 files changed, 91 insertions, 1 deletions
diff --git a/askbot/conf/markup.py b/askbot/conf/markup.py index 026c5536..09f65ce4 100644 --- a/askbot/conf/markup.py +++ b/askbot/conf/markup.py @@ -4,17 +4,43 @@ Settings that modify processing of user text input from askbot.conf.settings_wrapper import settings from askbot.deps.livesettings import ConfigurationGroup -from askbot.deps.livesettings import BooleanValue, StringValue +from askbot.deps.livesettings import BooleanValue, StringValue, LongStringValue from django.utils.translation import ugettext as _ import askbot from askbot import const import os +import re MARKUP = ConfigurationGroup( 'MARKUP', _('Markup formatting') ) +AUTOLINK = ConfigurationGroup( + 'AUTOLINK', + _('Auto link a pattern to an URL') + +) + +def regex_settings_validation(*args): + """ + Validate the regular expressions + + """ + try: + + new_value = args[1] + regex_list = new_value.split('\n') + + for i in range(0,len(regex_list)): + re.compile(regex_list[i].strip()) + return args[1] + + except Exception, e: + # The regex is invalid, so we overwrite it with empty string + return "" + + settings.register( BooleanValue( MARKUP, @@ -63,3 +89,57 @@ settings.register( default = '' ) ) + + +settings.register( + BooleanValue( + AUTOLINK, + 'ENABLE_AUTO_LINKING', + description=_('Enable autolinking a specifc pattern'), + help_text=_( + 'If you enable this feature, ' + 'the application will be able to ' + 'detect patterns and auto link to URLs' + ), + + default = False + ) + ) + + +settings.register( + LongStringValue( + AUTOLINK, + 'PATTERN', + description=_('Regex to detect the pattern'), + help_text=_( + 'Enter a valid regular expression to ' + 'detect the pattern. For example to' + 'detect something like #rhbz 637402 ' + 'use a regular expression like #rhbz\s(\d+)' + 'If you want to process multiple regex enter' + ' them line by line' + ), + update_callback=regex_settings_validation, + default = '' + ) + ) + +settings.register( + LongStringValue( + AUTOLINK, + 'AUTO_LINK_URL', + description=_('URL for autolinking'), + help_text=_( + 'Let us assume that to detect a pattern #rhbz 637402' + ' the regex is #rhbz\s(\d+) ' + 'then the autolink URL should be https://bugzilla.redhat.com/show_bug.cgi?id=\1' + ' Where \1 is the saved match (bugid) from the regular expression' + ' If you want to process multiple regex enter' + ' them line by line. The URL in first line will be used to link the' + ' pattern on the first line' + ), + default = '' + ) + ) + diff --git a/askbot/utils/markup.py b/askbot/utils/markup.py index acb8d03d..0c1a7f0f 100644 --- a/askbot/utils/markup.py +++ b/askbot/utils/markup.py @@ -10,6 +10,7 @@ LINK_PATTERNS = [ (URL_RE, r'\1'), ] + def get_parser(): extras = ['link-patterns', 'video'] if askbot_settings.ENABLE_MATHJAX or \ @@ -22,6 +23,15 @@ def get_parser(): #pip install -e git+git://github.com/andryuha/python-markdown2.git extras.append('video') + if askbot_settings.ENABLE_AUTO_LINKING: + pattern_list = askbot_settings.PATTERN.split('\n') + url_list = askbot_settings.AUTO_LINK_URL.split('\n') + + # Check whether we have matching links for all key terms, Other wise we ignore the key terms + if len(pattern_list) == len(url_list): + for i in range(0,len(pattern_list)): + LINK_PATTERNS.append((re.compile(pattern_list[i].strip()),url_list[i].strip())) + return Markdown( html4tags=True, extras=extras, |