我们从Python开源项目中,提取了以下32个代码示例,用于说明如何使用docutils.nodes.field_body()。
def field(self, match): name = self.parse_field_marker(match) src, srcline = self.state_machine.get_source_and_line() lineno = self.state_machine.abs_line_number() indented, indent, line_offset, blank_finish = \ self.state_machine.get_first_known_indented(match.end()) field_node = nodes.field() field_node.source = src field_node.line = srcline name_nodes, name_messages = self.inline_text(name, lineno) field_node += nodes.field_name(name, '', *name_nodes) field_body = nodes.field_body('\n'.join(indented), *name_messages) field_node += field_body if indented: self.parse_field_body(indented, line_offset, field_body) return field_node, blank_finish
def field(self, match): name = self.parse_field_marker(match) src, srcline = self.state_machine.get_source_and_line() lineno = self.state_machine.abs_line_number() first = self.state_machine.get_first_known_indented indented, indent, line_offset, blank_finish = first(match.end()) field_node = nodes.field() field_node.realsource = match.string field_node.source = src field_node.line = srcline name_nodes, name_messages = self.inline_text(name, lineno) field_node += nodes.field_name(name, '', *name_nodes) field_body = nodes.field_body('\n'.join(indented), *name_messages) field_node += field_body if indented: self.parse_field_body(indented, line_offset, field_body) return field_node, blank_finish
def patched_make_field(self, types, domain, items, **kw): # `kw` catches `env=None` needed for newer sphinx while maintaining # backwards compatibility when passed along further down! # type: (List, unicode, Tuple) -> nodes.field def handle_item(fieldarg, content): par = nodes.paragraph() par += addnodes.literal_strong('', fieldarg) # Patch: this line added # par.extend(self.make_xrefs(self.rolename, domain, fieldarg, # addnodes.literal_strong)) if fieldarg in types: par += nodes.Text(' (') # NOTE: using .pop() here to prevent a single type node to be # inserted twice into the doctree, which leads to # inconsistencies later when references are resolved fieldtype = types.pop(fieldarg) if len(fieldtype) == 1 and isinstance(fieldtype[0], nodes.Text): typename = u''.join(n.astext() for n in fieldtype) typename = typename.replace('int', 'python:int') typename = typename.replace('long', 'python:long') typename = typename.replace('float', 'python:float') typename = typename.replace('type', 'python:type') par.extend(self.make_xrefs(self.typerolename, domain, typename, addnodes.literal_emphasis, **kw)) else: par += fieldtype par += nodes.Text(')') par += nodes.Text(' -- ') par += content return par fieldname = nodes.field_name('', self.label) if len(items) == 1 and self.can_collapse: fieldarg, content = items[0] bodynode = handle_item(fieldarg, content) else: bodynode = self.list_type() for fieldarg, content in items: bodynode += nodes.list_item('', handle_item(fieldarg, content)) fieldbody = nodes.field_body('', bodynode) return nodes.field('', fieldname, fieldbody)
def visit_field_list(self, node): self.context.append((self.compact_field_list, self.compact_p)) self.compact_p = None if 'compact' in node['classes']: self.compact_field_list = True elif (self.settings.compact_field_lists and 'open' not in node['classes']): self.compact_field_list = True if self.compact_field_list: for field in node: field_body = field[-1] assert isinstance(field_body, nodes.field_body) children = [n for n in field_body if not isinstance(n, nodes.Invisible)] if not (len(children) == 0 or len(children) == 1 and isinstance(children[0], (nodes.paragraph, nodes.line_block))): self.compact_field_list = False break self.body.append(self.starttag(node, 'table', frame='void', rules='none', CLASS='docutils field-list')) self.body.append('<col class="field-name" />\n' '<col class="field-body" />\n' '<tbody valign="top">\n')
def rfc2822_field(self, match): name = match.string[:match.string.find(':')] indented, indent, line_offset, blank_finish = \ self.state_machine.get_first_known_indented(match.end(), until_blank=True) fieldnode = nodes.field() fieldnode += nodes.field_name(name, name) fieldbody = nodes.field_body('\n'.join(indented)) fieldnode += fieldbody if indented: self.nested_parse(indented, input_offset=line_offset, node=fieldbody) return fieldnode, blank_finish
def patched_make_field(self, types, domain, items, **kw): # `kw` catches `env=None` needed for newer sphinx while maingaining # backwards compatibility when passed along further down! # type: (List, unicode, Tuple) -> nodes.field def handle_item(fieldarg, content): par = nodes.paragraph() par += addnodes.literal_strong('', fieldarg) # Patch: this line added # par.extend(self.make_xrefs(self.rolename, domain, fieldarg, # addnodes.literal_strong)) if fieldarg in types: par += nodes.Text(' (') # NOTE: using .pop() here to prevent a single type node to be # inserted twice into the doctree, which leads to # inconsistencies later when references are resolved fieldtype = types.pop(fieldarg) if len(fieldtype) == 1 and isinstance(fieldtype[0], nodes.Text): typename = u''.join(n.astext() for n in fieldtype) typename = typename.replace('int', 'python:int') typename = typename.replace('long', 'python:long') typename = typename.replace('float', 'python:float') typename = typename.replace('type', 'python:type') par.extend(self.make_xrefs(self.typerolename, domain, typename, addnodes.literal_emphasis, **kw)) else: par += fieldtype par += nodes.Text(')') par += nodes.Text(' -- ') par += content return par fieldname = nodes.field_name('', self.label) if len(items) == 1 and self.can_collapse: fieldarg, content = items[0] bodynode = handle_item(fieldarg, content) else: bodynode = self.list_type() for fieldarg, content in items: bodynode += nodes.list_item('', handle_item(fieldarg, content)) fieldbody = nodes.field_body('', bodynode) return nodes.field('', fieldname, fieldbody)
def make_field(self, types, domain, item): fieldarg, content = item fieldname = nodes.field_name('', self.label) if fieldarg: fieldname += nodes.Text(' ') fieldname += self.make_xref(self.rolename, domain, fieldarg, nodes.Text) if len(content) == 1 and ( isinstance(content[0], nodes.Text) or (isinstance(content[0], nodes.inline) and len(content[0]) == 1 and isinstance(content[0][0], nodes.Text))): content = [self.make_xref(self.bodyrolename, domain, content[0].astext(), contnode=content[0])] fieldbody = nodes.field_body('', nodes.paragraph('', '', *content)) return nodes.field('', fieldname, fieldbody)
def make_field(self, types, domain, items): fieldname = nodes.field_name('', self.label) listnode = self.list_type() for fieldarg, content in items: par = nodes.paragraph() par += self.make_xref(self.rolename, domain, fieldarg, addnodes.literal_strong) par += nodes.Text(' -- ') par += content listnode += nodes.list_item('', par) if len(items) == 1 and self.can_collapse: fieldbody = nodes.field_body('', listnode[0][0]) return nodes.field('', fieldname, fieldbody) fieldbody = nodes.field_body('', listnode) return nodes.field('', fieldname, fieldbody)
def _render_response_information(body): """ :param nodes.field_body body: :rtype: dict|NoneType """ if len(body.children) > 1 or not isinstance(body[0], nodes.bullet_list): return None bullet_list = body[0] response_obj = { 'description': '', 'schema': { 'type': 'object', 'required': [], 'properties': {}, }, } for list_item in bullet_list.children: assert isinstance(list_item, nodes.list_item) assert len(list_item.children) == 1 para = list_item[0] assert isinstance(para, nodes.paragraph) obj_info = _parsed_typed_object(para) if obj_info: response_obj['schema']['required'].append(obj_info['name']) response_obj['schema']['properties'][obj_info['name']] = { 'type': obj_info['type'], 'description': obj_info['description'], } return response_obj
def make_arg(self, key, value): name = nodes.field_name(text=key) body = nodes.field_body() body.append(nodes.emphasis(text=value)) field = nodes.field() field += [name, body] return field
def visit_field(self, node): """ :param docutils.nodes.field node: """ idx = node.first_child_matching_class(nodes.field_name) if idx is not None: name_node = node[idx] idx = node.first_child_matching_class(nodes.field_body) value_node = node[idx] name = name_node.astext() if name == 'Status Codes': visitor = StatusVisitor(self.document) value_node.walkabout(visitor) self.endpoint.add_response_codes(visitor.status_info) elif name == 'Request Headers': visitor = HeaderVisitor(self.document) value_node.walkabout(visitor) self.endpoint.add_request_headers(visitor.headers) elif name == 'Response Headers': visitor = HeaderVisitor(self.document) value_node.walkabout(visitor) self.endpoint.add_response_headers(visitor.headers) elif name == 'Parameters': visitor = ParameterVisitor(self.document, {'in': 'path', 'required': True}) value_node.walkabout(visitor) self.endpoint.parameters.extend(visitor.parameters) elif name == 'Query Parameters': visitor = ParameterVisitor(self.document, {'in': 'query'}) value_node.walkabout(visitor) self.endpoint.parameters.extend(visitor.parameters) elif name == 'Request JSON Object': visitor = ParameterVisitor(self.document) value_node.walkabout(visitor) self.endpoint.parameters.append({ 'name': 'request-body', 'in': 'body', 'required': True, 'description': 'A serialized request body', 'schema': visitor.get_schema()}) elif name == 'Request JSON Array of Objects': visitor = ParameterVisitor(self.document) value_node.walkabout(visitor) self.endpoint.parameters.append({ 'name': 'request-body', 'in': 'body', 'required': True, 'schema': {'type': 'array', 'items': visitor.get_schema()} }) elif name == 'Response JSON Object': visitor = ParameterVisitor(self.document) value_node.walkabout(visitor) self.endpoint.set_default_response_structure(visitor.parameters) elif name == 'Response JSON Array of Objects': visitor = ParameterVisitor(self.document) value_node.walkabout(visitor) self.endpoint.set_default_response_structure( visitor.parameters, is_array=True) else: self.document.reporter.warning( 'unhandled field type: {}'.format(name), base_node=node) raise nodes.SkipChildren
def make_node(self, lang='en'): if lang not in texts.keys(): lang = 'en' arg_map = texts[lang]["arg_map"] task_title = texts[lang]["task_title"] module_title = texts[lang]["module_title"] module = "" module_args = [] # first, search module for arg, m in self.args.items(): if arg not in arg_map.keys(): module = arg module_args.append(m) item = nodes.admonition() title = nodes.title(text=self.name) item.append(title) for m in module_args: if isinstance(m, str): item.append(nodes.paragraph(text=m)) else: mlist = [] for k, v in m.items(): mlist.append("%s=%s" % (k, v)) item.append(nodes.paragraph(text=" ".join(mlist))) field_list = nodes.field_list() field_list.append(self.make_arg(module_title, module)) # second, create node tree for arg, txt in arg_map.items(): if not txt: # skip name etc... continue if arg not in self.args: continue value = self.args[arg] # value of that task arg if isinstance(value, list): bl = nodes.bullet_list() for v in value: body = nodes.emphasis(text=v) bl.append(nodes.list_item('', body)) name = nodes.field_name(text=txt) body = nodes.field_body() body.append(bl) field = nodes.field() field += [name, body] field_list.append(field) else: field_list.append(self.make_arg(txt, value)) item.append(field_list) return item