我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用sphinx.addnodes.desc_parameter()。
def handle_signature(self, sig, signode): print("looking for: " + sig) cache = _APP_CACHES.get(self.env.app, {}) key = CursorKind.FUNCTION_DECL, (sig, ) if key in cache: print("KEY FOunD!") node, comment, start, end, _ = cache[key] result_type = node.type.get_result() signode += addnodes.desc_type(result_type.spelling, result_type.spelling + ' ') signode += addnodes.desc_name(node.spelling, node.spelling) paramlist = addnodes.desc_parameterlist() for argument in node.get_arguments(): parameter = addnodes.desc_parameter() parameter += addnodes.desc_type(argument.type.spelling, argument.type.spelling + ' ') parameter += nodes.Text(argument.spelling, argument.spelling) paramlist += parameter signode += paramlist self.content = ViewList() comment = ''.join(comment) for lineno, line in enumerate(comment.splitlines(), start[0]): self.content.append(line, '<unknown>', lineno) return sig
def visit_desc_parameterlist(self, node): self.body.append('(') # by default sphinx puts <big> around the "(" self.first_param = 1 self.optional_param_level = 0 self.param_separator = node.child_text_separator self.required_params_left = sum(isinstance(c, addnodes.desc_parameter) for c in node.children)
def handle_signature(self, sig, signode): cache = _APP_CACHES.get(self.env.app, {}) key = CursorKind.MACRO_DEFINITION, (sig, ) if key in cache: node, comment, start, end, _ = cache[key] signode += addnodes.desc_name(node.displayname, node.displayname) # There is unfortunately no API to get the parameters of a macro, # so we identify them by looking at the tokens. tokens = list(node.get_tokens()) if ( tokens[1].kind is TokenKind.PUNCTUATION and tokens[1].spelling == '(' ): paramlist = addnodes.desc_parameterlist() for token in tokens[2:]: if ( token.kind is TokenKind.PUNCTUATION and token.spelling == ')' ): break elif token.kind is TokenKind.IDENTIFIER: paramlist += addnodes.desc_parameter(token.spelling, token.spelling) signode += paramlist self.content = ViewList() for lineno, line in enumerate(comment.splitlines(), start[0]): self.content.append(line, '<unknown>', lineno) return sig
def parse_opcode_signature(env, sig, signode): """Transform an opcode signature into RST nodes.""" m = opcode_sig_re.match(sig) if m is None: raise ValueError opname, arglist = m.groups() signode += addnodes.desc_name(opname, opname) if arglist is not None: paramlist = addnodes.desc_parameterlist() signode += paramlist paramlist += addnodes.desc_parameter(arglist, arglist) return opname.strip() # Support for documenting pdb commands
def describe_signature(self, signode, mode, env, parentScope): _verify_description_mode(mode) paramlist = addnodes.desc_parameterlist() for arg in self.args: param = addnodes.desc_parameter('', '', noemph=True) if mode == 'lastIsName': # i.e., outer-function params arg.describe_signature(param, 'param', env, parentScope=parentScope) else: arg.describe_signature(param, 'markType', env, parentScope=parentScope) paramlist += param signode += paramlist def _add_anno(signode, text): signode += nodes.Text(' ') signode += addnodes.desc_annotation(text, text) def _add_text(signode, text): signode += nodes.Text(' ' + text) if self.volatile: _add_anno(signode, 'volatile') if self.const: _add_anno(signode, 'const') if self.refQual: _add_text(signode, self.refQual) if self.exceptionSpec: _add_anno(signode, text_type(self.exceptionSpec)) if self.final: _add_anno(signode, 'final') if self.override: _add_anno(signode, 'override') if self.initializer: _add_text(signode, '= ' + text_type(self.initializer))
def visit_desc_parameterlist(self, node): self.body.append('<span class="sig-paren">(</span>') self.first_param = 1 self.optional_param_level = 0 # How many required parameters are left. self.required_params_left = sum([isinstance(c, addnodes.desc_parameter) for c in node.children]) self.param_separator = node.child_text_separator
def userdesc_parse(env, sig, signode): x, y = sig.split(':') signode += addnodes.desc_name(x, x) signode += addnodes.desc_parameterlist() signode[-1] += addnodes.desc_parameter(y, y) return x
def parse_event(env, sig, signode): m = event_sig_re.match(sig) if not m: signode += addnodes.desc_name(sig, sig) return sig name, args = m.groups() signode += addnodes.desc_name(name, name) plist = addnodes.desc_parameterlist() for arg in args.split(','): arg = arg.strip() plist += addnodes.desc_parameter(arg, arg) signode += plist return name
def _pseudo_parse_arglist(signode, arglist): # type: (addnodes.desc_signature, unicode) -> None """"Parse" a list of arguments separated by commas. Arguments can have "optional" annotations given by enclosing them in brackets. Currently, this will split at any comma, even if it's inside a string literal (e.g. default argument value). This function comes from sphinx.domains.python. """ paramlist = addnodes.desc_parameterlist() stack = [paramlist] try: for argument in arglist.split(','): argument = argument.strip() ends_open = ends_close = 0 while argument.startswith('['): stack.append(addnodes.desc_optional()) stack[-2] += stack[-1] argument = argument[1:].strip() while argument.startswith(']'): stack.pop() argument = argument[1:].strip() while argument.endswith(']') and not argument.endswith('[]'): ends_close += 1 argument = argument[:-1].strip() while argument.endswith('['): ends_open += 1 argument = argument[:-1].strip() if argument: stack[-1] += addnodes.desc_parameter(argument, argument) while ends_open: stack.append(addnodes.desc_optional()) stack[-2] += stack[-1] ends_open -= 1 while ends_close: stack.pop() ends_close -= 1 if len(stack) != 1: raise IndexError except IndexError: # if there are too few or too many elements on the stack, just give up # and treat the whole argument list as one argument, discarding the # already partially populated paramlist node signode += addnodes.desc_parameterlist() signode[-1] += addnodes.desc_parameter(arglist, arglist) else: signode += paramlist
def _pseudo_parse_arglist(signode, arglist): """"Parse" a list of arguments separated by commas. Arguments can have "optional" annotations given by enclosing them in brackets. Currently, this will split at any comma, even if it's inside a string literal (e.g. default argument value). """ paramlist = addnodes.desc_parameterlist() stack = [paramlist] try: for argument in arglist.split(','): argument = argument.strip() ends_open = ends_close = 0 while argument.startswith('['): stack.append(addnodes.desc_optional()) stack[-2] += stack[-1] argument = argument[1:].strip() while argument.startswith(']'): stack.pop() argument = argument[1:].strip() while argument.endswith(']') and not argument.endswith('[]'): ends_close += 1 argument = argument[:-1].strip() while argument.endswith('['): ends_open += 1 argument = argument[:-1].strip() if argument: stack[-1] += addnodes.desc_parameter(argument, argument) while ends_open: stack.append(addnodes.desc_optional()) stack[-2] += stack[-1] ends_open -= 1 while ends_close: stack.pop() ends_close -= 1 if len(stack) != 1: raise IndexError except IndexError: # if there are too few or too many elements on the stack, just give up # and treat the whole argument list as one argument, discarding the # already partially populated paramlist node signode += addnodes.desc_parameterlist() signode[-1] += addnodes.desc_parameter(arglist, arglist) else: signode += paramlist # This override allows our inline type specifiers to behave like :class: link # when it comes to handling "." and "~" prefixes.