def pre_processor(app, what, name, obj, options, lines,
                  namer=lambda name: ":obj:`{0}`".format(name)):
  """
  Callback preprocessor function for docstrings.
  Converts data from Spyder pattern to Sphinx, using a ``namer`` function
  that defaults to ``lambda name: ":obj:`{0}`".format(name)`` (specific for
  ``.. seealso::``).

  """
  # Duplication removal
  if what == "module": # For some reason, summary appears twice
    idxs = [idx for idx, el in enumerate(lines) if el.startswith("Summary")]
    if len(idxs) >= 2:
      del lines[idxs.pop():] # Remove the last summary
    if len(idxs) >= 1:
      lines.insert(idxs[-1] + 1, "")
      if obj is audiolazy.lazy_math:
        lines.insert(idxs[-1] + 1, ".. tabularcolumns:: cl")
      else:
        lines.insert(idxs[-1] + 1, ".. tabularcolumns:: CJ")
      lines.insert(idxs[-1] + 1, "")

  # Real docstring format pre-processing
  result = []
  for name, blk in iteritems(splitter(lines)):
    nlower =  name.lower()

    if nlower == "parameters":
      starters = audiolazy.Stream(idx for idx, el in enumerate(blk)
                                      if len(el) > 0
                                      and not el.startswith(" ")
                                 ).append([len(blk)])
      for idx1, idx2 in starters.blocks(size=2, hop=1):
        param_data = " ".join(b.strip() for b in blk[idx1:idx2])
        param, expl = param_data.split(":", 1)
        if "," in param:
          param = param.strip()
          if not param[0] in ("(", "[", "<", "{"):
            param = "[{0}]".format(param)
        while "," in param:
          fparam, param = param.split(",", 1)
          result.append(":param {0}: {1}".format(fparam.strip(), "\.\.\."))
        result.append(":param {0}: {1}".format(param.strip(), expl.strip()))

    elif nlower == "returns":
      result.append(":returns: " + " ".join(blk))

    elif nlower in ("note", "warning", "hint"):
      result.append(".. {0}::".format(nlower))
      result.extend("  " + el for el in blk)

    elif nlower == "examples":
      result.append("**Examples**:")
      result.extend("  " + el for el in blk)

    elif nlower == "see also":
      result.append(".. seealso::")
      for el in blk:
        if el.endswith(":"):
          result.append("") # Skip a line
           # Sphinx may need help here to find some object locations
          refs = [namer(f.strip()) for f in el[:-1].split(",")]
          result.append("  " + ", ".join(refs))
        else:
          result.append("  " + el)

    else: # Unkown block name, perhaps the starting one (empty)
      result.extend(blk)

    # Skip a line after each block
    result.append("")

  # Replace lines with the processed data while keeping the actual lines id
  del lines[:]
  lines.extend(result)
def pre_processor ( app , what , name , obj , options , lines , namer = lambda name : ":obj:`{0}`" . format ( name ) ) : if what == "module" : idxs = [ idx for idx , el in enumerate ( lines ) if el . startswith ( "Summary" ) ] if len ( idxs ) >= 2 : del lines [ idxs . pop ( ) : ] if len ( idxs ) >= 1 : lines . insert ( idxs [ - 1 ] + 1 , "" ) if obj is audiolazy . lazy_math : lines . insert ( idxs [ - 1 ] + 1 , ".. tabularcolumns:: cl" ) else : lines . insert ( idxs [ - 1 ] + 1 , ".. tabularcolumns:: CJ" ) lines . insert ( idxs [ - 1 ] + 1 , "" ) result = [ ] for name , blk in iteritems ( splitter ( lines ) ) : nlower = name . lower ( ) if nlower == "parameters" : starters = audiolazy . Stream ( idx for idx , el in enumerate ( blk ) if len ( el ) > 0 and not el . startswith ( " " ) ) . append ( [ len ( blk ) ] ) for idx1 , idx2 in starters . blocks ( size = 2 , hop = 1 ) : param_data = " " . join ( b . strip ( ) for b in blk [ idx1 : idx2 ] ) param , expl = param_data . split ( ":" , 1 ) if "," in param : param = param . strip ( ) if not param [ 0 ] in ( "(" , "[" , "<" , "{" ) : param = "[{0}]" . format ( param ) while "," in param : fparam , param = param . split ( "," , 1 ) result . append ( ":param {0}: {1}" . format ( fparam . strip ( ) , "\.\.\." ) ) result . append ( ":param {0}: {1}" . format ( param . strip ( ) , expl . strip ( ) ) ) elif nlower == "returns" : result . append ( ":returns: " + " " . join ( blk ) ) elif nlower in ( "note" , "warning" , "hint" ) : result . append ( ".. {0}::" . format ( nlower ) ) result . extend ( "  " + el for el in blk ) elif nlower == "examples" : result . append ( "**Examples**:" ) result . extend ( "  " + el for el in blk ) elif nlower == "see also" : result . append ( ".. seealso::" ) for el in blk : if el . endswith ( ":" ) : result . append ( "" ) refs = [ namer ( f . strip ( ) ) for f in el [ : - 1 ] . split ( "," ) ] result . append ( "  " + ", " . join ( refs ) ) else : result . append ( "  " + el ) else : result . extend ( blk ) result . append ( "" ) del lines [ : ] lines . extend ( result )
Callback preprocessor function for docstrings.
  Converts data from Spyder pattern to Sphinx, using a ``namer`` function
  that defaults to ``lambda name: ":obj:`{0}`".format(name)`` (specific for
  ``.. seealso::``).