From 1980d43ecfb7f30f2904eb924319d5c6aff0e4ac Mon Sep 17 00:00:00 2001 From: Arun SAG Date: Sun, 18 Sep 2011 01:22:20 +0530 Subject: Add autolinking settings to conf/markup.py - See #607 for more details on this feature - We use regex to match a pattern and autolink it --- askbot/conf/markup.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/askbot/conf/markup.py b/askbot/conf/markup.py index 026c5536..9eeb3149 100644 --- a/askbot/conf/markup.py +++ b/askbot/conf/markup.py @@ -15,6 +15,13 @@ MARKUP = ConfigurationGroup( _('Markup formatting') ) +AUTOLINK = ConfigurationGroup( + 'AUTOLINK', + _('Auto link a pattern to an URL') + +) + + settings.register( BooleanValue( MARKUP, @@ -63,3 +70,51 @@ settings.register( default = '' ) ) + + +settings.register( + BooleanValue( + AUTOLINK, + 'ENABLE_AUTO_LINK', + 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( + StringValue( + 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 r"#rhbz\s(\d+)"' + ), + default = '' + ) + ) + +settings.register( + StringValue( + AUTOLINK, + 'AutoLinkURL', + description=_('URL for autolinking'), + help_text=_( + 'Let us assume that to detect a pattern #rhbz 637402' + ' the regex is r"#rhbz\s(\d+)" ' + 'then the autolink URL should be https://bugzilla.redhat.com/show_bug.cgi?id=\\1' + ' Where \\1 is the saved match from the regular expression' + ), + default = '' + ) + ) + -- cgit v1.2.3-1-g7c22 From 015a46ddf251032155cec161fed8300d15c6537f Mon Sep 17 00:00:00 2001 From: Arun SAG Date: Sun, 18 Sep 2011 17:23:16 +0530 Subject: Autolinking urls based on regex now works - Fix settings name for readbility - Make utils/markup.py process autlinks --- askbot/conf/markup.py | 6 +++--- askbot/utils/markup.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/askbot/conf/markup.py b/askbot/conf/markup.py index 9eeb3149..f7e92bd4 100644 --- a/askbot/conf/markup.py +++ b/askbot/conf/markup.py @@ -75,7 +75,7 @@ settings.register( settings.register( BooleanValue( AUTOLINK, - 'ENABLE_AUTO_LINK', + 'ENABLE_AUTO_LINKING', description=_('Enable autolinking a specifc pattern'), help_text=_( 'If you enable this feature, ' @@ -91,7 +91,7 @@ settings.register( settings.register( StringValue( AUTOLINK, - 'Pattern', + 'PATTERN', description=_('Regex to detect the pattern'), help_text=_( 'Enter a valid regular expression to' @@ -106,7 +106,7 @@ settings.register( settings.register( StringValue( AUTOLINK, - 'AutoLinkURL', + 'AUTO_LINK_URL', description=_('URL for autolinking'), help_text=_( 'Let us assume that to detect a pattern #rhbz 637402' diff --git a/askbot/utils/markup.py b/askbot/utils/markup.py index acb8d03d..ef67b315 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,10 @@ def get_parser(): #pip install -e git+git://github.com/andryuha/python-markdown2.git extras.append('video') + if askbot_settings.ENABLE_AUTO_LINKING: + LINK_PATTERNS.append((re.compile(askbot_settings.PATTERN),askbot_settings.AUTO_LINK_URL)) + + return Markdown( html4tags=True, extras=extras, -- cgit v1.2.3-1-g7c22 From 28007df0bc6e944901486e337387787e2d7ddc69 Mon Sep 17 00:00:00 2001 From: Arun SAG Date: Sun, 18 Sep 2011 18:13:35 +0530 Subject: Fix examples in autolink settings --- askbot/conf/markup.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/askbot/conf/markup.py b/askbot/conf/markup.py index f7e92bd4..07f60c39 100644 --- a/askbot/conf/markup.py +++ b/askbot/conf/markup.py @@ -94,10 +94,10 @@ settings.register( 'PATTERN', description=_('Regex to detect the pattern'), help_text=_( - 'Enter a valid regular expression to' + 'Enter a valid regular expression to ' 'detect the pattern. For example to' 'detect something like #rhbz 637402 ' - 'use a regular expression like r"#rhbz\s(\d+)"' + 'use a regular expression like #rhbz\s(\d+)' ), default = '' ) @@ -110,9 +110,9 @@ settings.register( description=_('URL for autolinking'), help_text=_( 'Let us assume that to detect a pattern #rhbz 637402' - ' the regex is r"#rhbz\s(\d+)" ' - 'then the autolink URL should be https://bugzilla.redhat.com/show_bug.cgi?id=\\1' - ' Where \\1 is the saved match from the regular expression' + ' 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' ), default = '' ) -- cgit v1.2.3-1-g7c22 From 335e1cfc513dec5065f0a79a9f7180c3abeb25e7 Mon Sep 17 00:00:00 2001 From: Arun SAG Date: Tue, 20 Sep 2011 00:04:45 +0530 Subject: Add support for linking multiple key terms - Use LongStringValue of livesettings to capture multiple patterns and links - Honor all the patterns for autolinking with URLS in utils/markup.py --- askbot/conf/markup.py | 55 +++++++++++++++++++++++++++----------------------- askbot/utils/markup.py | 9 ++++++--- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/askbot/conf/markup.py b/askbot/conf/markup.py index 07f60c39..7ec0d73f 100644 --- a/askbot/conf/markup.py +++ b/askbot/conf/markup.py @@ -4,7 +4,7 @@ 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 @@ -89,32 +89,37 @@ settings.register( settings.register( - StringValue( - 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+)' - ), - default = '' - ) + 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' + ), + default = '' ) + ) settings.register( - StringValue( - 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' - ), - default = '' - ) + 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 ef67b315..8d072325 100644 --- a/askbot/utils/markup.py +++ b/askbot/utils/markup.py @@ -24,9 +24,12 @@ def get_parser(): extras.append('video') if askbot_settings.ENABLE_AUTO_LINKING: - LINK_PATTERNS.append((re.compile(askbot_settings.PATTERN),askbot_settings.AUTO_LINK_URL)) - - + pattern_list = askbot_settings.PATTERN.split('\n') + url_list = askbot_settings.AUTO_LINK_URL.split('\n') + + 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, -- cgit v1.2.3-1-g7c22 From e10f078f4ce8862871593eb5e0b357cca409b2f1 Mon Sep 17 00:00:00 2001 From: Arun SAG Date: Sat, 24 Sep 2011 19:47:05 +0530 Subject: Add validation callback - Validate the regular expressions using update_callback --- askbot/conf/markup.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/askbot/conf/markup.py b/askbot/conf/markup.py index 7ec0d73f..09f65ce4 100644 --- a/askbot/conf/markup.py +++ b/askbot/conf/markup.py @@ -9,6 +9,7 @@ from django.utils.translation import ugettext as _ import askbot from askbot import const import os +import re MARKUP = ConfigurationGroup( 'MARKUP', @@ -21,6 +22,24 @@ AUTOLINK = ConfigurationGroup( ) +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( @@ -101,6 +120,7 @@ settings.register( 'If you want to process multiple regex enter' ' them line by line' ), + update_callback=regex_settings_validation, default = '' ) ) -- cgit v1.2.3-1-g7c22 From 87b56b761ac999e33e9c243f7f5b8aff4fba2cb1 Mon Sep 17 00:00:00 2001 From: Arun SAG Date: Sat, 24 Sep 2011 19:51:52 +0530 Subject: Ignore key terms if there are no sufficient links --- askbot/utils/markup.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/askbot/utils/markup.py b/askbot/utils/markup.py index 8d072325..0c1a7f0f 100644 --- a/askbot/utils/markup.py +++ b/askbot/utils/markup.py @@ -27,8 +27,10 @@ def get_parser(): pattern_list = askbot_settings.PATTERN.split('\n') url_list = askbot_settings.AUTO_LINK_URL.split('\n') - for i in range(0,len(pattern_list)): - LINK_PATTERNS.append((re.compile(pattern_list[i].strip()),url_list[i].strip())) + # 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, -- cgit v1.2.3-1-g7c22