Commits

Ken Watford committed 1a28b1d

slightly more automation

Comments (0)

Files changed (1)

matlabdomain/sphinxcontrib/matlabdomain/__init__.py

                                               fullname, fullname))
     def get_index_text(self, name):
         raise NotImplementedError('must be implemented by subclasses')
-    
+    @classmethod
+    def make_object_type(cls):
+        return ObjType(l_(cls.objtype),  cls.objref)
+    @classmethod
+    def make_xrefrole(cls):
+        return MatlabXRef(cls)
+        
 
 matlab_sig_split_re = re.compile('[^\w~.]+')
 class MatlabFunction(MatlabObject):
       a tilde, indicating that it isn't used.
     * The input and output parameters varargin and varargout are
       special to Matlab, but probably shouldn't matter to us.
+
+    In actuality, we don't do any *real* parsing here. We basically
+    just separate out word-like items (including . and ~ characters).    
     """
     objtype = 'function'
+    objref = 'func'
     
     doc_field_types = [
         TypedField('parameter', label=l_('Parameters'),
     def get_index_text(self, name):
         return u_("%s (function)") % name
 
-class MatlabFuncRef(XRefRole):
+class MatlabXRef(XRefRole):
+    def __init__(self, objclass, *args, **kw):
+        super(MatlabXRef, self).__init__(*args, **kw)
+        self.objclass = objclass
     def process_link(self, env, refnode, has_explicit_title, title, target):
         return title, target
     def locate(self, target, domain):
-        objects = domain.data['function']
+        objects = domain.data[self.objclass.objtype]
         if target in objects:
             return objects[target], target
         else:
 class MatlabDomain(Domain):
     name='m'
     label='Matlab'
-    object_types = {
-        'function':  ObjType(l_('function'),  'func'),
-    }
 
-    directives = {
-        'function':  MatlabFunction
-    }
-
-    roles = {
-        'func': MatlabFuncRef(),
-        }
-
-    initial_data = {
-        'function': {}, # fullname -> docname
-    }
+    objclasses = MatlabObject.__subclasses__()
+    # Build these domain properties from information provided
+    # by classmethods and other info on the MatlabObject subclasses.
+    object_types = dict((c.objtype, c.make_object_type()) for c in objclasses)
+    directives = dict((c.objtype, c) for c in objclasses)    
+    roles = dict((c.objref, c.make_xrefrole()) for c in objclasses)
+    initial_data = dict((typename, dict()) for typename in object_types)
 
     indices = [] # XXX do this later
     
             pass
 
     def clear_doc(self, docname):
-        for objclass in [MatlabFunction]:
+        for objclass in self.objclasses:
             for fullname, dname in self.data[objclass.objtype].items():
                 if docname == dname:
-                    del self.data['functions'][fullname]
+                    del self.data[objclass.objtype][fullname]
 
 
 def setup(app):