我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用docutils.nodes.Node()。
def _get_matching_child( parent_node: nodes.Element, child_class: nodes.Element, last=True): """Get the first or last child node that matches a Node subclass. Args: parent_node: The node to find the child of. child_class: The Node subclass to check against. last: If True, find the last matching child. If False, find the first one. Returns: The first or last child Node object that is an instance of the given class. """ if last: children = reversed(parent_node.children) else: children = parent_node.children for child_node in children: if isinstance(child_node, child_class): return child_node
def _make_visit_admonition(name): def visit_admonition(self, node): # type: (nodes.NodeVisitor, nodes.Node) -> None # self.end_state(first=admonitionlabels[name] + ': ') info("visit", node) return visit_admonition
def visit_paragraph(self, node): # type: (nodes.Node) -> None if self.active(): self.contents.append([0, JOIN_BEFORE, '<break time="%dms" />' % self.builder.ssml_break_after_paragraph]) info("visit", node)
def run(self) -> List[nodes.Node]: """Run the directive. Returns: A list of Node objects. """ root_item = self._retrieve_item() if "item_id" in self.options: root_item = root_item.get_item_by_id( self.options["item_id"], raising=True) if "children" in self.options: root_item.parent = None # Parse directive and get content to extend items with. nested_nodes = nodes.paragraph() self.state.nested_parse( self.content, self.content_offset, nested_nodes) def_list = _get_matching_child( nested_nodes, nodes.definition_list, last=False) if def_list is not None: definitions = _parse_definition_list(def_list) else: definitions = collections.defaultdict(lambda: None) return self._parse_tree(root_item, definitions)
def figure_wrapper(directive, node, caption): # type: (Directive, nodes.Node, unicode) -> nodes.figure figure_node = nodes.figure('', node) if 'align' in node: figure_node['align'] = node.attributes.pop('align') parsed = nodes.Element() directive.state.nested_parse(ViewList([caption], source=''), directive.content_offset, parsed) caption_node = nodes.caption(parsed[0].rawsource, '', *parsed[0].children) caption_node.source = parsed[0].source caption_node.line = parsed[0].line figure_node += caption_node return figure_node
def run_directive(self, directive, match, type_name, option_presets): """ Parse a directive then run its directive function. Parameters: - `directive`: The class implementing the directive. Must be a subclass of `rst.Directive`. - `match`: A regular expression match object which matched the first line of the directive. - `type_name`: The directive name, as used in the source text. - `option_presets`: A dictionary of preset options, defaults for the directive options. Currently, only an "alt" option is passed by substitution definitions (value: the substitution name), which may be used by an embedded image directive. Returns a 2-tuple: list of nodes, and a "blank finish" boolean. """ if isinstance(directive, (FunctionType, MethodType)): from docutils.parsers.rst import convert_directive_function directive = convert_directive_function(directive) lineno = self.state_machine.abs_line_number() initial_line_offset = self.state_machine.line_offset indented, indent, line_offset, blank_finish \ = self.state_machine.get_first_known_indented(match.end(), strip_top=0) block_text = '\n'.join(self.state_machine.input_lines[ initial_line_offset : self.state_machine.line_offset + 1]) try: arguments, options, content, content_offset = ( self.parse_directive_block(indented, line_offset, directive, option_presets)) except MarkupError, detail: error = self.reporter.error( 'Error in "%s" directive:\n%s.' % (type_name, ' '.join(detail.args)), nodes.literal_block(block_text, block_text), line=lineno) return [error], blank_finish directive_instance = directive( type_name, arguments, options, content, lineno, content_offset, block_text, self, self.state_machine) try: result = directive_instance.run() except docutils.parsers.rst.DirectiveError, error: msg_node = self.reporter.system_message(error.level, error.msg, line=lineno) msg_node += nodes.literal_block(block_text, block_text) result = [msg_node] assert isinstance(result, list), \ 'Directive "%s" must return a list of nodes.' % type_name for i in range(len(result)): assert isinstance(result[i], nodes.Node), \ ('Directive "%s" returned non-Node object (index %s): %r' % (type_name, i, result[i])) return (result, blank_finish or self.state_machine.is_next_line_blank())
def run_directive(self, directive, match, type_name, option_presets): """ Parse a directive then run its directive function. Parameters: - `directive`: The class implementing the directive. Must be a subclass of `rst.Directive`. - `match`: A regular expression match object which matched the first line of the directive. - `type_name`: The directive name, as used in the source text. - `option_presets`: A dictionary of preset options, defaults for the directive options. Currently, only an "alt" option is passed by substitution definitions (value: the substitution name), which may be used by an embedded image directive. Returns a 2-tuple: list of nodes, and a "blank finish" boolean. """ if isinstance(directive, (FunctionType, MethodType)): from docutils.parsers.rst import convert_directive_function directive = convert_directive_function(directive) lineno = self.state_machine.abs_line_number() initial_line_offset = self.state_machine.line_offset indented, indent, line_offset, blank_finish \ = self.state_machine.get_first_known_indented(match.end(), strip_top=0) block_text = '\n'.join(self.state_machine.input_lines[ initial_line_offset : self.state_machine.line_offset + 1]) try: arguments, options, content, content_offset = ( self.parse_directive_block(indented, line_offset, directive, option_presets)) except MarkupError as detail: error = self.reporter.error( 'Error in "%s" directive:\n%s.' % (type_name, ' '.join(detail.args)), nodes.literal_block(block_text, block_text), line=lineno) return [error], blank_finish directive_instance = directive( type_name, arguments, options, content, lineno, content_offset, block_text, self, self.state_machine) try: result = directive_instance.run() except docutils.parsers.rst.DirectiveError as error: msg_node = self.reporter.system_message(error.level, error.msg, line=lineno) msg_node += nodes.literal_block(block_text, block_text) result = [msg_node] assert isinstance(result, list), \ 'Directive "%s" must return a list of nodes.' % type_name for i in range(len(result)): assert isinstance(result[i], nodes.Node), \ ('Directive "%s" returned non-Node object (index %s): %r' % (type_name, i, result[i])) return (result, blank_finish or self.state_machine.is_next_line_blank())
def run(self): # type: () -> List[nodes.Node] if self.arguments: document = self.state.document if self.content: return [document.reporter.warning( __('Symbolator directive cannot have both content and ' 'a filename argument'), line=self.lineno)] env = self.state.document.settings.env argument = search_image_for_language(self.arguments[0], env) rel_filename, filename = env.relfn2path(argument) env.note_dependency(rel_filename) try: with codecs.open(filename, 'r', 'utf-8') as fp: symbolator_code = fp.read() except (IOError, OSError): return [document.reporter.warning( __('External Symbolator file %r not found or reading ' 'it failed') % filename, line=self.lineno)] else: symbolator_code = '\n'.join(self.content) if not symbolator_code.strip(): return [self.state_machine.reporter.warning( __('Ignoring "symbolator" directive without content.'), line=self.lineno)] node = symbolator() node['code'] = symbolator_code node['options'] = {} if 'symbolator_cmd' in self.options: node['options']['symbolator_cmd'] = self.options['symbolator_cmd'] if 'alt' in self.options: node['alt'] = self.options['alt'] if 'align' in self.options: node['align'] = self.options['align'] if 'name' in self.options: node['options']['name'] = self.options['name'] caption = self.options.get('caption') if caption: node = figure_wrapper(self, node, caption) self.add_name(node) return [node]