Ignore:
Timestamp:
10/04/10 11:40:47 (3 years ago)
Author:
Tiago de Paula Peixoto <tiago@…>
Branches:
master, python3
Children:
cab630
Parents:
561521
git-author:
Tiago de Paula Peixoto <tiago@…> (10/03/10 23:40:49)
git-committer:
Tiago de Paula Peixoto <tiago@…> (10/04/10 11:40:47)
Message:
Replace jsmath with MathJax and update sphinxext
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/sphinxext/docscrape_sphinx.py

    rdd98225 r8f0b82  
    11import re, inspect, textwrap, pydoc 
     2import sphinx 
    23from docscrape import NumpyDocString, FunctionDoc, ClassDoc 
    34 
    45class SphinxDocString(NumpyDocString): 
     6    def __init__(self, docstring, config={}): 
     7        self.use_plots = config.get('use_plots', False) 
     8        NumpyDocString.__init__(self, docstring, config=config) 
     9 
    510    # string conversion routines 
    611    def _str_header(self, name, symbol='`'): 
     
    4045                out += self._str_indent(desc,8) 
    4146                out += [''] 
     47        return out 
     48 
     49    @property 
     50    def _obj(self): 
     51        if hasattr(self, '_cls'): 
     52            return self._cls 
     53        elif hasattr(self, '_f'): 
     54            return self._f 
     55        return None 
     56 
     57    def _str_member_list(self, name): 
     58        """ 
     59        Generate a member listing, autosummary:: table where possible, 
     60        and a table where not. 
     61 
     62        """ 
     63        out = [] 
     64        if self[name]: 
     65            out += ['.. rubric:: %s' % name, ''] 
     66            prefix = getattr(self, '_name', '') 
     67 
     68            if prefix: 
     69                prefix = '~%s.' % prefix 
     70 
     71            autosum = [] 
     72            others = [] 
     73            for param, param_type, desc in self[name]: 
     74                param = param.strip() 
     75                if not self._obj or hasattr(self._obj, param): 
     76                    autosum += ["   %s%s" % (prefix, param)] 
     77                else: 
     78                    others.append((param, param_type, desc)) 
     79 
     80            if autosum: 
     81                out += ['.. autosummary::', '   :toctree:', ''] 
     82                out += autosum 
     83 
     84            if others: 
     85                maxlen_0 = max([len(x[0]) for x in others]) 
     86                maxlen_1 = max([len(x[1]) for x in others]) 
     87                hdr = "="*maxlen_0 + "  " + "="*maxlen_1 + "  " + "="*10 
     88                fmt = '%%%ds  %%%ds  ' % (maxlen_0, maxlen_1) 
     89                n_indent = maxlen_0 + maxlen_1 + 4 
     90                out += [hdr] 
     91                for param, param_type, desc in others: 
     92                    out += [fmt % (param.strip(), param_type)] 
     93                    out += self._str_indent(desc, n_indent) 
     94                out += [hdr] 
     95            out += [''] 
    4296        return out 
    4397 
     
    91145            out.extend(self['References']) 
    92146            out += [''] 
    93         return out 
     147            # Latex collects all references to a separate bibliography, 
     148            # so we need to insert links to it 
     149            if sphinx.__version__ >= "0.6": 
     150                out += ['.. only:: latex',''] 
     151            else: 
     152                out += ['.. latexonly::',''] 
     153            items = [] 
     154            for line in self['References']: 
     155                m = re.match(r'.. \[([a-z0-9._-]+)\]', line, re.I) 
     156                if m: 
     157                    items.append(m.group(1)) 
     158            out += ['   ' + ", ".join(["[%s]_" % item for item in items]), ''] 
     159        return out 
     160 
     161    def _str_examples(self): 
     162        examples_str = "\n".join(self['Examples']) 
     163 
     164        if (self.use_plots and 'import matplotlib' in examples_str 
     165                and 'plot::' not in examples_str): 
     166            out = [] 
     167            out += self._str_header('Examples') 
     168            out += ['.. plot::', ''] 
     169            out += self._str_indent(self['Examples']) 
     170            out += [''] 
     171            return out 
     172        else: 
     173            return self._str_section('Examples') 
    94174 
    95175    def __str__(self, indent=0, func_role="obj"): 
     
    99179        out += self._str_summary() 
    100180        out += self._str_extended_summary() 
    101         for param_list in ('Parameters', 'Attributes', 'Methods', 
    102                            'Returns','Raises'): 
     181        for param_list in ('Parameters', 'Returns', 'Other Parameters', 
     182                           'Raises', 'Warns'): 
    103183            out += self._str_param_list(param_list) 
    104184        out += self._str_warnings() 
     
    106186        out += self._str_section('Notes') 
    107187        out += self._str_references() 
    108         out += self._str_section('Examples') 
     188        out += self._str_examples() 
     189        for param_list in ('Attributes', 'Methods'): 
     190            out += self._str_member_list(param_list) 
    109191        out = self._str_indent(out,indent) 
    110192        return '\n'.join(out) 
    111193 
    112194class SphinxFunctionDoc(SphinxDocString, FunctionDoc): 
    113     pass 
     195    def __init__(self, obj, doc=None, config={}): 
     196        self.use_plots = config.get('use_plots', False) 
     197        FunctionDoc.__init__(self, obj, doc=doc, config=config) 
    114198 
    115199class SphinxClassDoc(SphinxDocString, ClassDoc): 
    116     pass 
    117  
    118 def get_doc_object(obj, what=None): 
     200    def __init__(self, obj, doc=None, func_doc=None, config={}): 
     201        self.use_plots = config.get('use_plots', False) 
     202        ClassDoc.__init__(self, obj, doc=doc, func_doc=None, config=config) 
     203 
     204class SphinxObjDoc(SphinxDocString): 
     205    def __init__(self, obj, doc=None, config={}): 
     206        self._f = obj 
     207        SphinxDocString.__init__(self, doc, config=config) 
     208 
     209def get_doc_object(obj, what=None, doc=None, config={}): 
    119210    if what is None: 
    120211        if inspect.isclass(obj): 
     
    127218            what = 'object' 
    128219    if what == 'class': 
    129         return SphinxClassDoc(obj, '', func_doc=SphinxFunctionDoc) 
     220        return SphinxClassDoc(obj, func_doc=SphinxFunctionDoc, doc=doc, 
     221                              config=config) 
    130222    elif what in ('function', 'method'): 
    131         return SphinxFunctionDoc(obj, '') 
     223        return SphinxFunctionDoc(obj, doc=doc, config=config) 
    132224    else: 
    133         return SphinxDocString(pydoc.getdoc(obj)) 
     225        if doc is None: 
     226            doc = pydoc.getdoc(obj) 
     227        return SphinxObjDoc(obj, doc, config=config) 
Note: See TracChangeset for help on using the changeset viewer.