我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用docutils.nodes.classifier()。
def term(self, lines, lineno): """Return a definition_list's term and optional classifiers.""" assert len(lines) == 1 text_nodes, messages = self.inline_text(lines[0], lineno) term_node = nodes.term() (term_node.source, term_node.line) = self.state_machine.get_source_and_line(lineno) term_node.rawsource = unescape(lines[0]) node_list = [term_node] for i in range(len(text_nodes)): node = text_nodes[i] if isinstance(node, nodes.Text): parts = self.classifier_delimiter.split(node.rawsource) if len(parts) == 1: node_list[-1] += node else: node_list[-1] += nodes.Text(parts[0].rstrip()) for part in parts[1:]: classifier_node = nodes.classifier('', part) node_list.append(classifier_node) else: node_list[-1] += node return node_list, messages
def visit_definition_list_item(self, node): self._li_has_classifier = len(node) >= 2 and \ isinstance(node[1], nodes.classifier)
def apply_source_workaround(node): # workaround: nodes.term have wrong rawsource if classifier is specified. # The behavior of docutils-0.11, 0.12 is: # * when ``term text : classifier1 : classifier2`` is specified, # * rawsource of term node will have: ``term text : classifier1 : classifier2`` # * rawsource of classifier node will be None if isinstance(node, nodes.classifier) and not node.rawsource: definition_list_item = node.parent node.source = definition_list_item.source node.line = definition_list_item.line - 1 node.rawsource = node.astext() # set 'classifier1' (or 'classifier2') if isinstance(node, nodes.term): # strip classifier from rawsource of term for classifier in reversed(node.parent.traverse(nodes.classifier)): node.rawsource = re.sub( '\s*:\s*%s' % re.escape(classifier.astext()), '', node.rawsource) # workaround: recommonmark-0.2.0 doesn't set rawsource attribute if not node.rawsource: node.rawsource = node.astext() if node.source and node.rawsource: return # workaround: docutils-0.10.0 or older's nodes.caption for nodes.figure # and nodes.title for nodes.admonition doesn't have source, line. # this issue was filed to Docutils tracker: # sf.net/tracker/?func=detail&aid=3599485&group_id=38414&atid=422032 # sourceforge.net/p/docutils/patches/108/ if (isinstance(node, ( nodes.caption, nodes.title, nodes.rubric, nodes.line, ))): node.source = find_source_node(node) node.line = 0 # need fix docutils to get `node.line` return
def visit_definition_list_item(self, node): self._classifier_count_in_li = len(node.traverse(nodes.classifier))
def map_nested_definitions(nested_content): if nested_content is None: raise Exception('Nested content should be iterable, not null') # build definition dictionary definitions = {} for item in nested_content: if not isinstance(item, nodes.definition_list): continue for subitem in item: if not isinstance(subitem, nodes.definition_list_item): continue if not len(subitem.children) > 0: continue classifier = '@after' idx = subitem.first_child_matching_class(nodes.classifier) if idx is not None: ci = subitem[idx] if len(ci.children) > 0: classifier = ci.children[0].astext() if classifier is not None and classifier not in ( '@replace', '@before', '@after'): raise Exception('Unknown classifier: %s' % classifier) idx = subitem.first_child_matching_class(nodes.term) if idx is not None: ch = subitem[idx] if len(ch.children) > 0: term = ch.children[0].astext() idx = subitem.first_child_matching_class(nodes.definition) if idx is not None: def_node = subitem[idx] def_node.attributes['classifier'] = classifier definitions[term] = def_node return definitions
def apply_definition(definitions, my_def, name): if name in definitions: definition = definitions[name] classifier = definition['classifier'] if classifier == '@replace': return definition.children if classifier == '@after': return my_def + definition.children if classifier == '@before': return definition.children + my_def raise Exception('Unknown classifier: %s' % classifier) return my_def
def _parse_definition_list( def_list_node: nodes.definition_list) -> ExtraContentDict: """Parse a definition list inside the directive. Args: def_list_node: A definition list node containing definitions for extending the Sphinx output. Raises: ValueError: The given classifier was unrecognized. Returns: A dict where keys are item IDs and values contain the classifiers and the content as lists of docutils nodes. """ definitions = collections.defaultdict(lambda: None) for node in def_list_node: if not isinstance(node, nodes.definition_list_item): continue term = _get_matching_child(node, nodes.term, last=False).astext() classifiers = set() for child_node in node.children: if not isinstance(child_node, nodes.classifier): continue classifier = child_node.astext() if classifier not in ALL_CLASSIFIERS: raise ValueError("unknown classifier '{0}'".format(classifier)) classifiers.add(classifier) if not classifiers & CONTENT_CLASSIFIERS: classifiers.add("@after") if not classifiers & MARKUP_CLASSIFIERS: classifiers.add("@auto") content = _get_matching_child( node, nodes.definition, last=False).children if not definitions[term]: definitions[term] = [] definitions[term].append(ExtraContent(classifiers, content)) return definitions