Commit 7e42bce4 by Bridger Maxwell

Changed behavior of wikilinks extension to optionally disable database and…

Changed behavior of wikilinks extension to optionally disable database and prefer to stay at a certain level.
parent f00b7d30
...@@ -83,6 +83,9 @@ SHOW_MAX_CHILDREN = getattr(django_settings, 'WIKI_SHOW_MAX_CHILDREN', 20) ...@@ -83,6 +83,9 @@ SHOW_MAX_CHILDREN = getattr(django_settings, 'WIKI_SHOW_MAX_CHILDREN', 20)
USE_BOOTSTRAP_SELECT_WIDGET = getattr(django_settings, 'WIKI_USE_BOOTSTRAP_SELECT_WIDGET', True) USE_BOOTSTRAP_SELECT_WIDGET = getattr(django_settings, 'WIKI_USE_BOOTSTRAP_SELECT_WIDGET', True)
LINK_LIVE_LOOKUPS = getattr(django_settings, 'WIKI_LINK_LIVE_LOOKUPS', True)
LINK_DEFAULT_LEVEL = getattr(django_settings, 'WIKI_LINK_DEFAULT_LEVEL', 1)
#################### ####################
# PLANNED SETTINGS # # PLANNED SETTINGS #
#################### ####################
......
...@@ -35,7 +35,9 @@ class WikiPathExtension(markdown.Extension): ...@@ -35,7 +35,9 @@ class WikiPathExtension(markdown.Extension):
# set extension defaults # set extension defaults
self.config = { self.config = {
'base_url' : ['/', 'String to append to beginning of URL.'], 'base_url' : ['/', 'String to append to beginning of URL.'],
'html_class' : ['wikipath', 'CSS hook. Leave blank for none.'] 'html_class' : ['wikipath', 'CSS hook. Leave blank for none.'],
'live_lookups' : [True, 'If the plugin should try and match links to real articles'],
'default_level' : [2, 'The level that most articles are created at. Relative links will tend to start at that level.']
} }
# Override defaults with user settings # Override defaults with user settings
...@@ -73,19 +75,32 @@ class WikiPath(markdown.inlinepatterns.Pattern): ...@@ -73,19 +75,32 @@ class WikiPath(markdown.inlinepatterns.Pattern):
if absolute: if absolute:
base_path = self.config['base_url'][0] base_path = self.config['base_url'][0]
path_from_link = os_path.join(base_path, article_title) path_from_link = os_path.join(base_path, article_title)
try:
urlpath = models.URLPath.get_by_path(path_from_link) urlpath = None
path = urlpath.get_absolute_url() path = path_from_link
except models.URLPath.DoesNotExist: if self.config['live_lookups'][0]:
urlpath = None try:
path = path_from_link urlpath = models.URLPath.get_by_path(path_from_link)
path = urlpath.get_absolute_url()
except models.URLPath.DoesNotExist:
pass
else: else:
urlpath = models.URLPath.objects.get(article=self.markdown.article) urlpath = models.URLPath.objects.get(article=self.markdown.article)
path_from_link = os_path.join(urlpath.path, article_title) source_components = urlpath.path.strip("/").split("/")
if urlpath.parent: # We take the first (self.config['default_level'] - 1) components, so adding
lookup = urlpath.parent.get_descendants().filter(slug=article_title) # one more component would make a path of length self.config['default_level']
else: starting_level = max(0, self.config['default_level'][0] - 1 )
lookup = urlpath.get_descendants().filter(slug=article_title) starting_path = "/".join(source_components[ : starting_level ])
path_from_link = os_path.join(starting_path, article_title)
lookup = models.URLPath.objects.none()
if self.config['live_lookups'][0]:
if urlpath.parent:
lookup = urlpath.parent.get_descendants().filter(slug=article_title)
else:
lookup = urlpath.get_descendants().filter(slug=article_title)
if lookup.count() > 0: if lookup.count() > 0:
urlpath = lookup[0] urlpath = lookup[0]
path = urlpath.get_absolute_url() path = urlpath.get_absolute_url()
...@@ -96,7 +111,7 @@ class WikiPath(markdown.inlinepatterns.Pattern): ...@@ -96,7 +111,7 @@ class WikiPath(markdown.inlinepatterns.Pattern):
label = m.group('linkTitle') label = m.group('linkTitle')
a = etree.Element('a') a = etree.Element('a')
a.set('href', path) a.set('href', path)
if not urlpath: if not urlpath and self.config['live_lookups']:
a.set('class', self.config['html_class'][0] + " linknotfound") a.set('class', self.config['html_class'][0] + " linknotfound")
else: else:
a.set('class', self.config['html_class'][0]) a.set('class', self.config['html_class'][0])
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from django.conf.urls.defaults import patterns, url from django.conf.urls.defaults import patterns, url
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from wiki.conf import settings
from wiki.core.plugins import registry from wiki.core.plugins import registry
from wiki.core.plugins.base import BasePlugin from wiki.core.plugins.base import BasePlugin
from wiki.plugins.links import views from wiki.plugins.links import views
...@@ -22,7 +23,13 @@ class LinkPlugin(BasePlugin): ...@@ -22,7 +23,13 @@ class LinkPlugin(BasePlugin):
'form_class': None, 'form_class': None,
'get_form_kwargs': (lambda a: {})} 'get_form_kwargs': (lambda a: {})}
markdown_extensions = [makeExtension(), WikiPathExtension([('base_url', reverse_lazy('wiki:get', kwargs={'path': ''}))])] wikipath_config = [
('base_url', reverse_lazy('wiki:get', kwargs={'path': ''}) ),
('live_lookups', settings.LINK_LIVE_LOOKUPS ),
('default_level', settings.LINK_DEFAULT_LEVEL ),
]
markdown_extensions = [makeExtension(), WikiPathExtension(wikipath_config)]
def __init__(self): def __init__(self):
pass pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment