changeset 100:fd6eccb24882

Refactoring to get rid of `slugify` callbacks.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 25 May 2013 22:35:23 -0700
parents 58a1a7baca25
children 13249e5ca51c
files tests/mock.py wikked/db.py wikked/formatter.py wikked/fs.py wikked/metautils.py wikked/page.py wikked/resolver.py wikked/utils.py wikked/views.py wikked/wiki.py
diffstat 10 files changed, 79 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/tests/mock.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/tests/mock.py	Sat May 25 22:35:23 2013 -0700
@@ -9,6 +9,7 @@
 from wikked.db import Database
 from wikked.indexer import WikiIndex
 from wikked.scm import SourceControl
+from wikked.utils import title_to_url
 
 
 class MockWikiParameters(object):
@@ -86,13 +87,10 @@
 
 
 class MockFileSystem():
-    def __init__(self, structure=None, slugify=Page.title_to_url, logger=None):
+    def __init__(self, structure=None, logger=None):
         if not structure:
             structure = []
-        if not slugify:
-            slugify = lambda x: x
         self.structure = structure
-        self.slugify = slugify
         self.logger = logger
         self.excluded = []
 
@@ -131,7 +129,7 @@
 
     def _getPageInfo(self, node, with_content=False):
         path_split = os.path.splitext(node['path'])
-        url = self.slugify(path_split[0])
+        url = title_to_url(path_split[0])
         info = PageInfo(url, node['path'])
         if with_content:
             info.content = node['content']
@@ -176,7 +174,7 @@
         parts = unicode(url).lower().split('/')
         for i, part in enumerate(parts):
             for name in current:
-                name_slug = self.slugify(name)
+                name_slug = title_to_url(name)
                 if is_file and i == len(parts) - 1:
                     if re.match(r"%s\.[a-z]+" % re.escape(part), name_slug):
                         current = current[name]
--- a/wikked/db.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/wikked/db.py	Sat May 25 22:35:23 2013 -0700
@@ -64,7 +64,6 @@
         raise NotImplementedError()
 
 
-
 class SQLitePageInfo(object):
     def __init__(self, row):
         self.url = row['url']
--- a/wikked/formatter.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/wikked/formatter.py	Sat May 25 22:35:23 2013 -0700
@@ -1,43 +1,26 @@
 import os
 import os.path
 import re
-from metautils import get_meta_name_and_modifiers
+from utils import get_absolute_url, get_meta_name_and_modifiers
 
 
 class BaseContext(object):
     """ Base context for formatting pages. """
-    def __init__(self, url, slugify=None):
+    def __init__(self, url):
         self.url = url
-        self.slugify = slugify
 
     @property
     def urldir(self):
         return os.path.dirname(self.url)
 
     def getAbsoluteUrl(self, url, do_slugify=True):
-        if url.startswith('/'):
-            # Absolute page URL.
-            abs_url = url[1:]
-        else:
-            # Relative page URL. Let's normalize all `..` in it,
-            # which could also replace forward slashes by backslashes
-            # on Windows, so we need to convert that back.
-            raw_abs_url = os.path.join(self.urldir, url)
-            abs_url = os.path.normpath(raw_abs_url).replace('\\', '/')
-        if do_slugify and self.slugify is not None:
-            abs_url_parts = abs_url.split('/')
-            abs_url = ''
-            for i, part in enumerate(abs_url_parts):
-                if i > 0:
-                    abs_url += '/'
-                abs_url += self.slugify(part)
-        return abs_url
+        return get_absolute_url(self.url, url, do_slugify)
 
 
 class FormattingContext(BaseContext):
     """ Context for formatting pages. """
-    def __init__(self, url, slugify):
-        BaseContext.__init__(self, url, slugify)
+    def __init__(self, url):
+        BaseContext.__init__(self, url)
         self.out_links = []
         self.meta = {}
 
--- a/wikked/fs.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/wikked/fs.py	Sat May 25 22:35:23 2013 -0700
@@ -4,6 +4,7 @@
 import string
 import codecs
 import logging
+from utils import title_to_url
 
 
 class PageNotFoundError(Exception):
@@ -29,13 +30,9 @@
         file-system paths, and for scanning the file-system
         to list existing pages.
     """
-    def __init__(self, root, slugify=None, logger=None):
+    def __init__(self, root, logger=None):
         self.root = unicode(root)
 
-        if slugify is None:
-            slugify = lambda x: x
-        self.slugify = slugify
-
         if logger is None:
             logger = logging.getLogger('wikked.fs')
         self.logger = logger
@@ -104,7 +101,7 @@
         for i, part in enumerate(parts):
             if i > 0:
                 url += '/'
-            url += self.slugify(part)
+            url += title_to_url(part)
         return PageInfo(url, path, None)
 
     def _getPhysicalPath(self, url, is_file):
@@ -119,7 +116,7 @@
         for i, part in enumerate(parts):
             names = os.listdir(current)
             for name in names:
-                name_formatted = self.slugify(name)
+                name_formatted = title_to_url(name)
                 if is_file and i == len(parts) - 1:
                     # If we're looking for a file and this is the last part,
                     # look for something similar but with an extension.
--- a/wikked/metautils.py	Sun Apr 21 08:12:18 2013 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-
-def get_meta_name_and_modifiers(name):
-    """ Strips a meta name from any leading modifiers like `__` or `+`
-        and returns both as a tuple. If no modifier was found, the
-        second tuple value is `None`.
-    """
-    clean_name = name
-    modifiers = None
-    if name[:2] == '__':
-        modifiers = '__'
-        clean_name = name[3:]
-    elif name[0] == '+':
-        modifiers = '+'
-        clean_name = name[1:]
-    return (clean_name, modifiers)
-
-
--- a/wikked/page.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/wikked/page.py	Sat May 25 22:35:23 2013 -0700
@@ -2,7 +2,6 @@
 import os.path
 import re
 import datetime
-import unicodedata
 import jinja2
 from formatter import PageFormatter, FormattingContext
 from resolver import PageResolver, CircularIncludeError
@@ -128,7 +127,7 @@
         # Format the page and get the meta properties.
         filename = os.path.basename(data.path)
         filename_split = os.path.splitext(filename)
-        ctx = FormattingContext(self.url, slugify=Page.title_to_url)
+        ctx = FormattingContext(self.url)
         f = PageFormatter(self.wiki)
         data.formatted_text = f.formatText(ctx, data.raw_text)
         data.local_meta = ctx.meta
@@ -168,22 +167,6 @@
                     })
 
     @staticmethod
-    def title_to_url(title):
-        # Remove diacritics (accents, etc.) and replace them with ASCII
-        # equivelent.
-        ansi_title = ''.join((c for c in
-            unicodedata.normalize('NFD', unicode(title))
-            if unicodedata.category(c) != 'Mn'))
-        # Now replace spaces and punctuation with a hyphen.
-        return re.sub(r'[^A-Za-z0-9_\.\-\(\)]+', '-', ansi_title.lower())
-
-    @staticmethod
-    def url_to_title(url):
-        def upperChar(m):
-            return m.group(0).upper()
-        return re.sub(r'^.|\s\S', upperChar, url.lower().replace('-', ' '))
-
-    @staticmethod
     def factory(wiki, url):
         return Page(wiki, url)
 
--- a/wikked/resolver.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/wikked/resolver.py	Sat May 25 22:35:23 2013 -0700
@@ -1,6 +1,6 @@
 import re
 import jinja2
-from metautils import get_meta_name_and_modifiers
+from utils import get_meta_name_and_modifiers
 
 
 class FormatterNotFound(Exception):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wikked/utils.py	Sat May 25 22:35:23 2013 -0700
@@ -0,0 +1,58 @@
+import re
+import os.path
+import unicodedata
+
+
+def get_absolute_url(base_url, url, do_slugify=True):
+    if url.startswith('/'):
+        # Absolute page URL.
+        abs_url = url[1:]
+    else:
+        # Relative page URL. Let's normalize all `..` in it,
+        # which could also replace forward slashes by backslashes
+        # on Windows, so we need to convert that back.
+        urldir = os.path.dirname(base_url)
+        raw_abs_url = os.path.join(urldir, url)
+        abs_url = os.path.normpath(raw_abs_url).replace('\\', '/')
+    if do_slugify:
+        abs_url_parts = abs_url.split('/')
+        abs_url = ''
+        for i, part in enumerate(abs_url_parts):
+            if i > 0:
+                abs_url += '/'
+            abs_url += title_to_url(part)
+    return abs_url
+
+
+def title_to_url(title):
+    # Remove diacritics (accents, etc.) and replace them with ASCII
+    # equivelent.
+    ansi_title = ''.join((c for c in
+        unicodedata.normalize('NFD', unicode(title))
+        if unicodedata.category(c) != 'Mn'))
+    # Now replace spaces and punctuation with a hyphen.
+    return re.sub(r'[^A-Za-z0-9_\.\-\(\)]+', '-', ansi_title.lower())
+
+
+def url_to_title(url):
+    def upperChar(m):
+        return m.group(0).upper()
+    return re.sub(r'^.|\s\S', upperChar, url.lower().replace('-', ' '))
+
+
+def get_meta_name_and_modifiers(name):
+    """ Strips a meta name from any leading modifiers like `__` or `+`
+        and returns both as a tuple. If no modifier was found, the
+        second tuple value is `None`.
+    """
+    clean_name = name
+    modifiers = None
+    if name[:2] == '__':
+        modifiers = '__'
+        clean_name = name[3:]
+    elif name[0] == '+':
+        modifiers = '+'
+        clean_name = name[1:]
+    return (clean_name, modifiers)
+
+
--- a/wikked/views.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/wikked/views.py	Sat May 25 22:35:23 2013 -0700
@@ -1,4 +1,3 @@
-import re
 import time
 import os.path
 from flask import render_template, abort, request, g, jsonify
@@ -10,6 +9,7 @@
 from page import Page, PageData
 from fs import PageNotFoundError
 from formatter import PageFormatter, FormattingContext
+from utils import title_to_url
 import scm
 
 
@@ -22,13 +22,13 @@
     result = []
     for item in category:
         result.append({
-            'url': Page.title_to_url(item),
+            'url': title_to_url(item),
             'name': item
             })
     return result
 
 COERCE_META = {
-    'redirect': Page.title_to_url,
+    'redirect': title_to_url,
     'category': get_category_meta
     }
 
@@ -46,7 +46,7 @@
         data.extension = extension
         data.raw_text = self._text
 
-        ctx = FormattingContext(self.url, slugify=Page.title_to_url)
+        ctx = FormattingContext(self.url)
         f = PageFormatter(self.wiki)
         data.formatted_text = f.formatText(ctx, data.raw_text)
         data.local_meta = ctx.meta
--- a/wikked/wiki.py	Sun Apr 21 08:12:18 2013 -0700
+++ b/wikked/wiki.py	Sat May 25 22:35:23 2013 -0700
@@ -32,7 +32,7 @@
         if root is None:
             root = os.getcwd()
         self.root = root
-        
+
         self.formatters = self.getFormatters()
 
         self.config_path = os.path.join(self.root, '.wikirc')
@@ -51,7 +51,7 @@
         return open(self.config_path)
 
     def fs_factory(self, config):
-        return FileSystem(self.root, slugify=Page.title_to_url, logger=self.logger_factory())
+        return FileSystem(self.root, logger=self.logger_factory())
 
     def index_factory(self, config):
         return WhooshWikiIndex(self.index_path, logger=self.logger_factory())