diff MoinMoin/formatter/__init__.py @ 2576:f4c371b00fec

refactor the ID generation/writing code
author Johannes Berg <johannes AT sipsolutions DOT net>
date Fri, 27 Jul 2007 11:37:37 +0200
parents ce0787373150
children 398af77c7ede 59b3d8b8971f
line wrap: on
line diff
--- a/MoinMoin/formatter/__init__.py	Fri Jul 27 11:37:15 2007 +0200
+++ b/MoinMoin/formatter/__init__.py	Fri Jul 27 11:37:37 2007 +0200
@@ -364,9 +364,40 @@
     def comment(self, text, **kw):
         return ""
 
+    # ID handling #################################################
+
+    def sanitize_to_id(self, text):
+        '''
+        Take 'text' and return something that is a valid ID
+        for this formatter.
+        The default returns the first non-space character of the string.
+
+        Because of the way this is used, it must be idempotent,
+        i.e. calling it on an already sanitized id must yield the
+        original id.
+        '''
+        return text.strip()[:1]
+
     def make_id_unique(self, id):
-        id = self.request.make_unique_id(id, self.request.include_id)
-        if self.request.include_id:
-            id = '%s.%s' % (
-                wikiutil.anchor_name_from_text(self.request.include_id), id)
+        '''
+        Take an ID and make it unique in the current namespace.
+        '''
+        ns = self.request.include_id
+        if not ns is None:
+            ns = self.sanitize_to_id(ns)
+        id = self.sanitize_to_id(id)
+        id = self.request.make_unique_id(id, ns)
         return id
+
+    def qualify_id(self, id):
+        '''
+        Take an ID and return a string that is qualified by
+        the current namespace; this default implementation
+        is suitable if the dot ('.') is valid in IDs for your
+        formatter.
+        '''
+        ns = self.request.include_id
+        if not ns is None:
+            ns = self.sanitize_to_id(ns)
+            return '%s.%s' % (ns, id)
+        return id