我们从Python开源项目中,提取了以下41个代码示例,用于说明如何使用docutils.nodes.admonition()。
def visit_admonition(self, node, name=None): # # Make admonitions a simple block quote # with a strong heading # # Using .IP/.RE doesn't preserve indentation # when admonitions contain bullets, literal, # and/or block quotes. # if name: # .. admonition:: has no name self.body.append('.sp\n') name = '%s%s:%s\n' % ( self.defs['strong'][0], self.language.labels.get(name, name).upper(), self.defs['strong'][1], ) self.body.append(name) self.visit_block_quote(node)
def visit_title(self, node): if isinstance(node.parent, nodes.topic): self.body.append(self.defs['topic-title'][0]) elif isinstance(node.parent, nodes.sidebar): self.body.append(self.defs['sidebar-title'][0]) elif isinstance(node.parent, nodes.admonition): self.body.append('.IP "') elif self.section_level == 0: self._docinfo['title'] = node.astext() # document title for .TH self._docinfo['title_upper'] = node.astext().upper() raise nodes.SkipNode elif self.section_level == 1: self.body.append('.SH %s\n' % self.deunicode(node.astext().upper())) raise nodes.SkipNode else: self.body.append('.SS ')
def run(self): set_classes(self.options) self.assert_has_content() text = '\n'.join(self.content) admonition_node = self.node_class(text, **self.options) self.add_name(admonition_node) if self.node_class is nodes.admonition: title_text = self.arguments[0] textnodes, messages = self.state.inline_text(title_text, self.lineno) title = nodes.title(title_text, '', *textnodes) title.source, title.line = ( self.state_machine.get_source_and_line(self.lineno)) admonition_node += title admonition_node += messages if not 'classes' in self.options: admonition_node['classes'] += ['admonition-' + nodes.make_id(title_text)] self.state.nested_parse(self.content, self.content_offset, admonition_node) return [admonition_node]
def run(self): title = u'' if self.arguments: title += self.arguments[0] if 'class' in self.options: self.options['class'].append(self.css_class) else: self.options['class'] = [self.css_class] ret = make_admonition( nodes.admonition, self.name, [title], self.options, self.content, self.lineno, self.content_offset, self.block_text, self.state, self.state_machine) ret[0].attributes['name'] = self.name return ret
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 depart_title(self, node): if isinstance(node.parent, nodes.admonition): self.body.append('"') self.body.append('\n')
def visit_admonition(self, node): self.fallbacks['admonition'] = PreambleCmds.admonition if 'error' in node['classes']: self.fallbacks['error'] = PreambleCmds.error # strip the generic 'admonition' from the list of classes node['classes'] = [cls for cls in node['classes'] if cls != 'admonition'] self.out.append('\n\\DUadmonition[%s]{\n' % ','.join(node['classes']))
def visit_admonition(self, node): self.fallbacks['admonition'] = PreambleCmds.admonition if 'error' in node['classes']: self.fallbacks['error'] = PreambleCmds.error # strip the generic 'admonition' from the list of classes node['classes'] = [cls for cls in node['classes'] if cls != 'admonition'] self.out.append('\n\\DUadmonition[%s]{' % ','.join(node['classes']))
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 run(self): # ‘BaseAdmonition’ checks whether ‘node_class’ is ‘nodes.admonition’, # and uses arguments[0] as the title in that case (in other cases, the # title is unset, and it is instead set in the HTML visitor). assert not self.arguments # Arguments have been parsed as content self.arguments = ['Example'] self.options['classes'] = ['admonition', 'note'] return super().run()
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
def fix_rst_pep(input_lines, outfile, inpath, pepnum): class XXXDirective(rst.Directive): has_content = True def run(self): # Raise an error if the directive does not have contents. self.assert_has_content() text = '\n'.join(self.content) # Create the admonition node, to be populated by `nested_parse`. admonition_node = nodes.admonition(rawsource=text) # Parse the directive contents. self.state.nested_parse(self.content, self.content_offset, admonition_node) title = nodes.title('', 'XXX') admonition_node.insert(0, title) return [admonition_node] directives.register_directive('xxx', XXXDirective) handle, template_file_name = tempfile.mkstemp(text=True) try: orig_template_name = pep_html.Writer.default_template_path with open(orig_template_name) as inf, open(handle, 'w') as outf: content = inf.read() content = content.replace('%(pepnum)s.txt', '%(pepnum)s.rst') content = content.replace("%(pepindex)s/", "%(pepindex)s") outf.write(content) output = core.publish_string( source=''.join(input_lines), source_path=inpath, destination_path=outfile.name, reader=Reader(), parser_name='restructuredtext', writer=Writer(pepnum), settings=None, # Allow Docutils traceback if there's an exception: settings_overrides={ 'traceback': 1, 'template': template_file_name, }) outfile.write(output.decode('utf-8')) finally: os.unlink(template_file_name)