我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用docutils.nodes.Admonition()。
def run(self): env = self.state.document.settings.env targetid = "tag-%d" % env.new_serialno('tag') targetnode = nodes.target('', '', ids=[targetid]) # The tags fetched from the custom directive are one piece of text # sitting in self.content[0] taggs = self.content[0].split(", ") links = [] for tagg in taggs: # Create Sphinx doc refs of format :ref:`Tagname<Tagname>` link = ":ref:`" + tagg + "<" + tagg + ">`" links.append(link) # Put links back in a single comma-separated string together linkjoin = ", ".join(links) # Replace content[0] with hyperlinks to display in admonition self.content[0] = linkjoin ad = Admonition(self.name, [_('Tags')], self.options, self.content, self.lineno, self.content_offset, self.block_text, self.state, self.state_machine) return [targetnode] + ad.run()
def visit_title(self, node): if isinstance(node.parent, nodes.Admonition): self.add_text(node.astext()+': ') raise nodes.SkipNode self.new_state(0)
def visit_paragraph(self, node): if not isinstance(node.parent, ( nodes.Admonition, nodes.citation, nodes.entry, nodes.footnote, nodes.list_item, addnodes.seealso, )): self.new_state(0)
def depart_paragraph(self, node): if isinstance(node.parent, nodes.list_item): self.add_text(self.nl) elif not isinstance(node.parent, ( nodes.Admonition, nodes.citation, nodes.entry, nodes.footnote, addnodes.seealso, )): self.end_state()
def apply(self): language = languages.get_language(self.document.settings.language_code, self.document.reporter) for node in self.document.traverse(nodes.Admonition): node_name = node.__class__.__name__ # Set class, so that we know what node this admonition came from. node['classes'].append(node_name) if not isinstance(node, nodes.admonition): # Specific admonition. Transform into a generic admonition. admonition = nodes.admonition(node.rawsource, *node.children, **node.attributes) title = nodes.title('', language.labels[node_name]) admonition.insert(0, title) node.replace_self(admonition)
def visit_title(self, node): """Only 6 section levels are supported by HTML.""" check_id = 0 # TODO: is this a bool (False) or a counter? close_tag = '</p>\n' if isinstance(node.parent, nodes.topic): self.body.append( self.starttag(node, 'p', '', CLASS='topic-title first')) elif isinstance(node.parent, nodes.sidebar): self.body.append( self.starttag(node, 'p', '', CLASS='sidebar-title')) elif isinstance(node.parent, nodes.Admonition): self.body.append( self.starttag(node, 'p', '', CLASS='admonition-title')) elif isinstance(node.parent, nodes.table): self.body.append( self.starttag(node, 'caption', '')) close_tag = '</caption>\n' elif isinstance(node.parent, nodes.document): self.body.append(self.starttag(node, 'h1', '', CLASS='title')) close_tag = '</h1>\n' self.in_document_title = len(self.body) else: assert isinstance(node.parent, nodes.section) h_level = self.section_level + self.initial_header_level - 1 atts = {} if (len(node.parent) >= 2 and isinstance(node.parent[1], nodes.subtitle)): atts['CLASS'] = 'with-subtitle' self.body.append( self.starttag(node, 'h%s' % h_level, '', **atts)) atts = {} if node.hasattr('refid'): atts['class'] = 'toc-backref' atts['href'] = '#' + node['refid'] if atts: self.body.append(self.starttag({}, 'a', '', **atts)) close_tag = '</a></h%s>\n' % (h_level) else: close_tag = '</h%s>\n' % (h_level) self.context.append(close_tag)
def visit_paragraph(self, node): if not isinstance(node.parent, nodes.Admonition) or \ isinstance(node.parent, addnodes.seealso): self.new_state(0)
def depart_paragraph(self, node): if not isinstance(node.parent, nodes.Admonition) or \ isinstance(node.parent, addnodes.seealso): self.end_state()
def visit_title(self, node): if isinstance(node.parent, nodes.Admonition): self.add_text(node.astext() + ': ') raise nodes.SkipNode self.new_state(0)
def visit_title(self, node): parent = node.parent if isinstance(parent, addnodes.seealso): # the environment already handles this raise nodes.SkipNode elif self.this_is_the_title: if len(node.children) != 1 and not isinstance(node.children[0], nodes.Text): self.builder.warn('document title is not a single Text node', (self.curfilestack[-1], node.line)) if not self.elements['title']: # text needs to be escaped since it is inserted into # the output literally self.elements['title'] = node.astext().translate(tex_escape_map) self.this_is_the_title = 0 raise nodes.SkipNode elif isinstance(parent, nodes.section): short = '' if node.traverse(nodes.image): short = ('[%s]' % u' '.join(clean_astext(node).split()).translate(tex_escape_map)) try: self.body.append(r'\%s%s{' % (self.sectionnames[self.sectionlevel], short)) except IndexError: # just use "subparagraph", it's not numbered anyway self.body.append(r'\%s%s{' % (self.sectionnames[-1], short)) self.context.append('}\n') self.restrict_footnote(node) if self.next_section_ids: for id in sorted(self.next_section_ids): self.context[-1] += self.hypertarget(id, anchor=False) self.next_section_ids.clear() elif isinstance(parent, (nodes.topic, nodes.sidebar)): self.body.append(r'\textbf{') self.context.append('}\n\n\medskip\n\n') elif isinstance(parent, nodes.Admonition): self.body.append('{') self.context.append('}\n') elif isinstance(parent, nodes.table): # Redirect body output until title is finished. self.pushbody([]) else: self.builder.warn( 'encountered title node not in section, topic, table, ' 'admonition or sidebar', (self.curfilestack[-1], node.line or '')) self.body.append('\\textbf{') self.context.append('}\n') self.in_title = 1
def visit_title(self, node): parent = node.parent if isinstance(parent, addnodes.seealso): # the environment already handles this raise nodes.SkipNode elif self.this_is_the_title: if len(node.children) != 1 and not isinstance(node.children[0], nodes.Text): self.builder.warn('document title is not a single Text node', (self.curfilestack[-1], node.line)) if not self.elements['title']: # text needs to be escaped since it is inserted into # the output literally self.elements['title'] = node.astext().translate(tex_escape_map) self.this_is_the_title = 0 raise nodes.SkipNode elif isinstance(parent, nodes.section): short = '' if node.traverse(nodes.image): short = '[%s]' % ' '.join(clean_astext(node).split()).translate(tex_escape_map) try: self.body.append(r'\%s%s{' % (self.sectionnames[self.sectionlevel], short)) except IndexError: # just use "subparagraph", it's not numbered anyway self.body.append(r'\%s%s{' % (self.sectionnames[-1], short)) self.context.append('}\n') self.restrict_footnote(node) if self.next_section_ids: for id in self.next_section_ids: self.context[-1] += self.hypertarget(id, anchor=False) self.next_section_ids.clear() elif isinstance(parent, (nodes.topic, nodes.sidebar)): self.body.append(r'\textbf{') self.context.append('}\n\n\medskip\n\n') elif isinstance(parent, nodes.Admonition): self.body.append('{') self.context.append('}\n') elif isinstance(parent, nodes.table): # Redirect body output until title is finished. self.pushbody([]) else: self.builder.warn( 'encountered title node not in section, topic, table, ' 'admonition or sidebar', (self.curfilestack[-1], node.line or '')) self.body.append('\\textbf{') self.context.append('}\n') self.in_title = 1