我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用docutils.nodes.Text()。
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 doctree_resolved(app, doctree, docname): secnums = app.builder.env.toc_secnumbers for node in doctree.traverse(nodes.reference): if 'refdocname' in node: refdocname = node['refdocname'] if refdocname in secnums: secnum = secnums[refdocname] toclist = app.builder.env.tocs[refdocname] for child in node.traverse(): if isinstance(child, nodes.Text): text = child.astext() anchorname = None for refnode in toclist.traverse(nodes.reference): if refnode.astext() == text: anchorname = refnode['anchorname'] if anchorname is None: continue linktext = '.'.join(map(str, secnum[anchorname])) child.parent.replace(child, nodes.Text(linktext))
def expand_show_urls(self): show_urls = self.document.settings.env.config.latex_show_urls if show_urls is False or show_urls == 'no': return for node in self.document.traverse(nodes.reference): uri = node.get('refuri', '') if uri.startswith(URI_SCHEMES): if uri.startswith('mailto:'): uri = uri[7:] if node.astext() != uri: index = node.parent.index(node) if show_urls == 'footnote': if list(traverse_parent(node, nodes.topic)): # should not expand references in topics pass else: footnote_nodes = self.create_footnote(uri) for i, fn in enumerate(footnote_nodes): node.parent.insert(index + i + 1, fn) self.expanded = True else: # all other true values (b/w compat) textnode = nodes.Text(" (%s)" % uri) node.parent.insert(index + 1, textnode)
def maybe_resolve_abbreviations(app, env, node, contnode): domainname = node.get('refdomain') target = node['reftarget'] type = node['reftype'] if target.startswith('@'): newtarget, src_dict = resolve(target, type) node['reftarget'] = newtarget # shorten text if '~' is not enabled. if len(contnode) and isinstance(contnode[0], nodes.Text): contnode[0] = modify_textnode(target, newtarget, node, src_dict, type) if domainname: try: domain = env.domains[node.get('refdomain')] except KeyError: raise NoUri return domain.resolve_xref(env, node['refdoc'], app.builder, type, newtarget, node, contnode)
def depart_citation_reference(self, node): if self._use_latex_citations: followup_citation = False # check for a following citation separated by a space or newline next_siblings = node.traverse(descend=False, siblings=True, include_self=False) if len(next_siblings) > 1: next = next_siblings[0] if (isinstance(next, nodes.Text) and next.astext() in (' ', '\n')): if next_siblings[1].__class__ == node.__class__: followup_citation = True if followup_citation: self.out.append(',') else: self.out.append('}') self.inside_citation_reference_label = False else: self.out.append(']}')
def run(self): if not isinstance(self.state, states.SubstitutionDef): raise self.error( 'Invalid context: the "%s" directive can only be used within ' 'a substitution definition.' % self.name) substitution_definition = self.state_machine.node if 'trim' in self.options: substitution_definition.attributes['ltrim'] = 1 substitution_definition.attributes['rtrim'] = 1 if 'ltrim' in self.options: substitution_definition.attributes['ltrim'] = 1 if 'rtrim' in self.options: substitution_definition.attributes['rtrim'] = 1 codes = self.comment_pattern.split(self.arguments[0])[0].split() element = nodes.Element() for code in codes: try: decoded = directives.unicode_code(code) except ValueError, error: raise self.error(u'Invalid character code: %s\n%s' % (code, ErrorString(error))) element += nodes.Text(decoded) return element.children
def implicit_inline(self, text, lineno): """ Check each of the patterns in `self.implicit_dispatch` for a match, and dispatch to the stored method for the pattern. Recursively check the text before and after the match. Return a list of `nodes.Text` and inline element nodes. """ if not text: return [] for pattern, method in self.implicit_dispatch: match = pattern.search(text) if match: try: # Must recurse on strings before *and* after the match; # there may be multiple patterns. return (self.implicit_inline(text[:match.start()], lineno) + method(match, lineno) + self.implicit_inline(text[match.end():], lineno)) except MarkupMismatch: pass return [nodes.Text(unescape(text), rawsource=unescape(text, 1))]
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 _strong_node(self, refuri, text_nodes): cp = docutils_nodes.inline(classes=['link-container']) n1 = docutils_nodes.strong() n1.extend(text_nodes) cp.append(n1) paramlink = docutils_nodes.reference( '', '', docutils_nodes.Text(u"¶", u"¶"), refid="", # paramlink is our own CSS class, headerlink # is theirs. Trying to get everything we can for existing # symbols... classes=['paramlink', 'headerlink'] ) cp.append(paramlink) return cp
def run(self): pnode = nodes.compound(classes=['impl-detail']) content = self.content add_text = nodes.strong('CPython implementation detail:', 'CPython implementation detail:') if self.arguments: n, m = self.state.inline_text(self.arguments[0], self.lineno) pnode.append(nodes.paragraph('', '', *(n + m))) self.state.nested_parse(content, self.content_offset, pnode) if pnode.children and isinstance(pnode[0], nodes.paragraph): pnode[0].insert(0, add_text) pnode[0].insert(1, nodes.Text(' ')) else: pnode.insert(0, nodes.paragraph('', '', add_text)) return [pnode] # Support for documenting decorators
def format_parser_error(name, error, filename, state, lineno, do_unicode_warning): warning = '%s: Unable to parse xml file "%s". ' % (name, filename) explanation = 'Reported error: %s. ' % error unicode_explanation_text = "" unicode_explanation = [] if do_unicode_warning: unicode_explanation_text = textwrap.dedent(""" Parsing errors are often due to unicode errors associated with the encoding of the original source files. Doxygen propagates invalid characters from the input source files to the output xml.""").strip().replace("\n", " ") unicode_explanation = [nodes.paragraph("", "", nodes.Text(unicode_explanation_text))] return [nodes.warning("", nodes.paragraph("", "", nodes.Text(warning)), nodes.paragraph("", "", nodes.Text(explanation)), *unicode_explanation ), state.document.reporter.warning(warning + explanation + unicode_explanation_text, line=lineno) ]
def run(self): if not isinstance(self.state, states.SubstitutionDef): raise self.error( 'Invalid context: the "%s" directive can only be used within ' 'a substitution definition.' % self.name) format_str = '\n'.join(self.content) or '%Y-%m-%d' if sys.version_info< (3, 0): try: format_str = format_str.encode(locale_encoding or 'utf-8') except UnicodeEncodeError: raise self.warning(u'Cannot encode date format string ' u'with locale encoding "%s".' % locale_encoding) text = time.strftime(format_str) if sys.version_info< (3, 0): # `text` is a byte string that may contain non-ASCII characters: try: text = text.decode(locale_encoding or 'utf-8') except UnicodeDecodeError: text = text.decode(locale_encoding or 'utf-8', 'replace') raise self.warning(u'Error decoding "%s"' u'with locale encoding "%s".' % (text, locale_encoding)) return [nodes.Text(text)]
def run(self): env = self.state.document.settings.env if not env.config.show_authors: return [] para = nodes.paragraph(translatable=False) emph = nodes.emphasis() para += emph if self.name == 'sectionauthor': text = _('Section author: ') elif self.name == 'moduleauthor': text = _('Module author: ') elif self.name == 'codeauthor': text = _('Code author: ') else: text = _('Author: ') emph += nodes.Text(text, text) inodes, messages = self.state.inline_text(self.arguments[0], self.lineno) emph.extend(inodes) return [para] + messages
def _resolve_citation(self, builder, fromdocname, node, contnode): docname, labelid = self.citations.get(node['reftarget'], ('', '')) if docname: try: newnode = make_refnode(builder, fromdocname, docname, labelid, contnode) return newnode except NoUri: # remove the ids we added in the CitationReferences # transform since they can't be transfered to # the contnode (if it's a Text node) if not isinstance(contnode, nodes.Element): del node['ids'][:] raise elif 'ids' in node: # remove ids attribute that annotated at # transforms.CitationReference.apply. del node['ids'][:]
def apply(self): # move doctest blocks out of blockquotes for node in self.document.traverse(nodes.block_quote): if all(isinstance(child, nodes.doctest_block) for child in node.children): node.replace_self(node.children) # combine successive doctest blocks # for node in self.document.traverse(nodes.doctest_block): # if node not in node.parent.children: # continue # parindex = node.parent.index(node) # while len(node.parent) > parindex+1 and \ # isinstance(node.parent[parindex+1], nodes.doctest_block): # node[0] = nodes.Text(node[0] + '\n\n' + # node.parent[parindex+1][0]) # del node.parent[parindex+1]
def describe_signature(self, signode, mode, env, prefix, parentScope): _verify_description_mode(mode) if mode == 'markType': targetText = prefix + text_type(self) pnode = addnodes.pending_xref( '', refdomain='cpp', reftype='type', reftarget=targetText, modname=None, classname=None) pnode['cpp:parent'] = [parentScope] pnode += nodes.Text(text_type(self.identifier)) signode += pnode elif mode == 'lastIsName': name = text_type(self.identifier) signode += addnodes.desc_name(name, name) else: raise Exception('Unknown description mode: %s' % mode) if self.templateArgs: signode += nodes.Text('<') first = True for a in self.templateArgs: if not first: signode += nodes.Text(', ') first = False a.describe_signature(signode, 'markType', env, parentScope=parentScope) signode += nodes.Text('>')
def describe_signature(self, modifiers): def _add(modifiers, text): if len(modifiers) > 0: modifiers.append(nodes.Text(' ')) modifiers.append(addnodes.desc_annotation(text, text)) if self.storage: _add(modifiers, self.storage) if self.inline: _add(modifiers, 'inline') if self.virtual: _add(modifiers, 'virtual') if self.explicit: _add(modifiers, 'explicit') if self.constexpr: _add(modifiers, 'constexpr') if self.volatile: _add(modifiers, 'volatile') if self.const: _add(modifiers, 'const')
def menusel_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): text = utils.unescape(text) if typ == 'menuselection': text = text.replace('-->', u'\N{TRIANGULAR BULLET}') spans = _amp_re.split(text) node = nodes.inline(rawtext=rawtext) for i, span in enumerate(spans): span = span.replace('&&', '&') if i == 0: if len(span) > 0: textnode = nodes.Text(span) node += textnode continue accel_node = nodes.inline() letter_node = nodes.Text(span[0]) accel_node += letter_node accel_node['classes'].append('accelerator') node += accel_node textnode = nodes.Text(span[1:]) node += textnode node['classes'].append(typ) return [node], []
def autosummary_table_visit_html(self, node): """Make the first column of the table non-breaking.""" try: tbody = node[0][0][-1] for row in tbody: col1_entry = row[0] par = col1_entry[0] for j, subnode in enumerate(list(par)): if isinstance(subnode, nodes.Text): new_text = text_type(subnode.astext()) new_text = new_text.replace(u" ", u"\u00a0") par[j] = nodes.Text(new_text) except IndexError: pass # -- autodoc integration -------------------------------------------------------
def expand_show_urls(self): show_urls = self.document.settings.env.config.latex_show_urls if show_urls is False or show_urls == 'no': return for node in self.document.traverse(nodes.reference): uri = node.get('refuri', '') if uri.startswith(URI_SCHEMES): if uri.startswith('mailto:'): uri = uri[7:] if node.astext() != uri: index = node.parent.index(node) if show_urls == 'footnote': footnote_nodes = self.create_footnote(uri) for i, fn in enumerate(footnote_nodes): node.parent.insert(index + i + 1, fn) self.expanded = True else: # all other true values (b/w compat) textnode = nodes.Text(" (%s)" % uri) node.parent.insert(index + 1, textnode)
def dispatch_visit(self, node): nodetype = type(node) if issubclass(nodetype, comment): raise SkipNode if issubclass(nodetype, raw): # Some people might put content in raw HTML that should be searched, # so we just amateurishly strip HTML tags and index the remaining # content nodetext = re.sub(r'(?is)<style.*?</style>', '', node.astext()) nodetext = re.sub(r'(?is)<script.*?</script>', '', nodetext) nodetext = re.sub(r'<[^<]+?>', '', nodetext) self.found_words.extend(self.lang.split(nodetext)) raise SkipNode if issubclass(nodetype, Text): self.found_words.extend(self.lang.split(node.astext())) elif issubclass(nodetype, title): self.found_title_words.extend(self.lang.split(node.astext()))
def textcolor_role(name, rawtext, text, lineno, inliner, options={}, content=[]): """\ This role is interpreted in the following way: :textcolor:`<color_spec> text ` where color spec is in HTML model, e.g. #FFFFFF, ... in latex: \\textcolor[HTML]{color_spec}{text} (the leading # is removed from color_spec) in html <font color="color_spec">text</font> """ color_spec = text[1:text.index('>')] text = (text[text.index('>')+1:]).strip() textcolor_node = textcolor() textcolor_node.children.append(nodes.Text(text)) textcolor_node['color_spec'] = color_spec return [textcolor_node], []
def generate_options(self, env, node): options = [] for i, key in enumerate(['agreement4', 'agreement3', 'agreement2', 'agreement1', 'agreement0']): options.append({ u'value': 4 - i, u'label|i18n': translations.opt(key), }) choice = aplus_nodes.html(u'div', {u'class':u'radio'}) label = aplus_nodes.html(u'label', {}) label.append(aplus_nodes.html(u'input', { u'type': u'radio', u'name': u'field_{:d}'.format(env.question_count - 1), u'value': 4 - i, })) label.append(nodes.Text(translations.get(env, key))) choice.append(label) node.append(choice) return options
def run(self): if not isinstance(self.state, states.SubstitutionDef): raise self.error( 'Invalid context: the "%s" directive can only be used within ' 'a substitution definition.' % self.name) substitution_definition = self.state_machine.node if 'trim' in self.options: substitution_definition.attributes['ltrim'] = 1 substitution_definition.attributes['rtrim'] = 1 if 'ltrim' in self.options: substitution_definition.attributes['ltrim'] = 1 if 'rtrim' in self.options: substitution_definition.attributes['rtrim'] = 1 codes = self.comment_pattern.split(self.arguments[0])[0].split() element = nodes.Element() for code in codes: try: decoded = directives.unicode_code(code) except ValueError as error: raise self.error('Invalid character code: %s\n%s' % (code, ErrorString(error))) element += nodes.Text(decoded) return element.children
def write_dl(self, rows, col_max=30, col_spacing=2): """Writes a definition list into the buffer. This is how options and commands are usually formatted. :param rows: a list of two item tuples for the terms and values. :param col_max: the maximum width of the first column. :param col_spacing: the number of spaces between the first and second column. """ rows = list(rows) dl = nodes.bullet_list() self._node.append(dl) for (option, help_text) in rows: item = nodes.list_item() dl.append(item) p = nodes.paragraph() p.append(nodes.literal('', option)) p.append(nodes.Text(': ')) p.append(nodes.Text(help_text)) item.append(p)
def visit_productionlist(self, node): replacement=nodes.literal_block(classes=["code"]) names = [] for production in node: names.append(production['tokenname']) maxlen = max(len(name) for name in names) for production in node: if production['tokenname']: lastname = production['tokenname'].ljust(maxlen) n=nodes.strong() n+=nodes.Text(lastname) replacement+=n replacement+=nodes.Text(' ::= ') else: replacement+=nodes.Text('%s ' % (' '*len(lastname))) production.walkabout(self) replacement.children.extend(production.children) replacement+=nodes.Text('\n') node.parent.replace(node,replacement) raise nodes.SkipNode
def process_readingtime_nodes(app, doctree, fromdocname): env = app.builder.env count = 0 for node in doctree.traverse(nodes.paragraph): tt=node.astext() count+=len(tt.split(" ")) for node in doctree.traverse(ReadingTime): para = nodes.rubric() minutes = int(round(count / 200.0)) minutes = max(minutes, 1) para += nodes.Text("Reading time: {} {}".format( minutes, 'minute' if minutes == 1 else 'minutes' )) node.replace_self([para])
def create_footnote(self, uri): label = nodes.label('', '#') para = nodes.paragraph() para.append(nodes.reference('', nodes.Text(uri), refuri=uri, nolinkurl=True)) footnote = nodes.footnote(uri, label, para, auto=1) footnote['names'].append('#') self.document.note_autofootnote(footnote) label = nodes.Text('#') footnote_ref = nodes.footnote_reference('[#]_', label, auto=1, refid=footnote['ids'][0]) self.document.note_autofootnote_ref(footnote_ref) footnote.add_backref(footnote_ref['ids'][0]) return [footnote, footnote_ref]
def renumber_footnotes(self): def is_used_number(number): for node in self.document.traverse(nodes.footnote): if not node.get('auto') and number in node['names']: return True return False def is_auto_footnote(node): return isinstance(node, nodes.footnote) and node.get('auto') def footnote_ref_by(node): ids = node['ids'] parent = list(traverse_parent(node, (nodes.document, addnodes.start_of_file)))[0] def is_footnote_ref(node): return (isinstance(node, nodes.footnote_reference) and ids[0] == node['refid'] and parent in list(traverse_parent(node))) return is_footnote_ref startnum = 1 for footnote in self.document.traverse(is_auto_footnote): while True: label = str(startnum) startnum += 1 if not is_used_number(label): break old_label = footnote[0].astext() footnote.remove(footnote[0]) footnote.insert(0, nodes.label('', label)) if old_label in footnote['names']: footnote['names'].remove(old_label) footnote['names'].append(label) for footnote_ref in self.document.traverse(footnote_ref_by(footnote)): footnote_ref.remove(footnote_ref[0]) footnote_ref += nodes.Text(label)
def modify_textnode(T, newtarget, node, src_dict, type): src = node.children[0].rawsource return nodes.Text( (typeify(basename(T), type) if '~' in src else typeify(shorten(T, newtarget, src_dict), type)), src, )
def apply(self): config = self.document.settings.env.config github_project = config.github_project issue_pattern = config.github_issue_pattern if isinstance(issue_pattern, str_t): issue_pattern = re.compile(issue_pattern) for node in self.document.traverse(nodes.Text): parent = node.parent if isinstance(parent, (nodes.literal, nodes.FixedTextElement)): continue text = text_t(node) new_nodes = [] last_issue_ref_end = 0 for match in issue_pattern.finditer(text): head = text[last_issue_ref_end:match.start()] if head: new_nodes.append(nodes.Text(head)) last_issue_ref_end = match.end() issuetext = match.group(0) issue_id = match.group(1) refnode = pending_xref() refnode['reftarget'] = issue_id refnode['reftype'] = 'issue' refnode['github_project'] = github_project reftitle = issuetext refnode.append(nodes.inline( issuetext, reftitle, classes=['xref', 'issue'])) new_nodes.append(refnode) if not new_nodes: continue tail = text[last_issue_ref_end:] if tail: new_nodes.append(nodes.Text(tail)) parent.replace(node, new_nodes)
def resolve_issue_reference(app, env, node, contnode): if node['reftype'] != 'issue': return issue_id = node['reftarget'] project = node['github_project'] issue = Issue(issue_id, None, URL.format(project=project, issue_id=issue_id)) conttext = text_t(contnode[0]) formatted_conttext = nodes.Text(conttext.format(issue=issue)) formatted_contnode = nodes.inline(conttext, formatted_conttext, classes=contnode['classes']) return make_issue_reference(issue, formatted_contnode)