我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用docutils.nodes.authors()。
def extract_authors(self, field, name, docinfo): try: if len(field[1]) == 1: if isinstance(field[1][0], nodes.paragraph): authors = self.authors_from_one_paragraph(field) elif isinstance(field[1][0], nodes.bullet_list): authors = self.authors_from_bullet_list(field) else: raise TransformError else: authors = self.authors_from_paragraphs(field) authornodes = [nodes.author('', '', *author) for author in authors if author] if len(authornodes) >= 1: docinfo.append(nodes.authors('', *authornodes)) else: raise TransformError except TransformError: field[-1] += self.document.reporter.warning( 'Bibliographic field "%s" incompatible with extraction: ' 'it must contain either a single paragraph (with authors ' 'separated by one of "%s"), multiple paragraphs (one per ' 'author), or a bullet list with one paragraph (one author) ' 'per item.' % (name, ''.join(self.language.author_separators)), base_node=field) raise
def authors_from_one_paragraph(self, field): text = field[1][0].astext().strip() if not text: raise TransformError for authorsep in self.language.author_separators: authornames = text.split(authorsep) if len(authornames) > 1: break authornames = [author.strip() for author in authornames] authors = [[nodes.Text(author)] for author in authornames if author] return authors
def authors_from_bullet_list(self, field): authors = [] for item in field[1][0]: if len(item) != 1 or not isinstance(item[0], nodes.paragraph): raise TransformError authors.append(item[0].children) if not authors: raise TransformError return authors
def authors_from_paragraphs(self, field): for item in field[1]: if not isinstance(item, nodes.paragraph): raise TransformError authors = [item.children for item in field[1]] return authors
def visit_author(self, node): if isinstance(node.parent, nodes.authors): if self.author_in_authors: self.body.append('\n<br />') else: self.visit_docinfo_item(node, 'author')
def depart_author(self, node): if isinstance(node.parent, nodes.authors): self.author_in_authors = True else: self.depart_docinfo_item()
def visit_authors(self, node): self.visit_docinfo_item(node, 'authors') self.author_in_authors = False # initialize
def visit_author(self, node): if isinstance(node.parent, nodes.authors): el = self.append_p('blockindent') else: el = self.generate_labeled_block(node, 'author') self.set_current_element(el)
def visit_authors(self, node): label = '%s:' % (self.language.labels['authors'], ) el = self.append_p('textbody') el1 = SubElement(el, 'text:span', attrib={'text:style-name': self.rststyle('strong')}) el1.text = label
def depart_author(self, node): self.body.append('</p>') if isinstance(node.parent, nodes.authors): self.body.append('\n') else: self.depart_docinfo_item()
def visit_authors(self, node): self.visit_docinfo_item(node, 'authors')
def process_metadata(self, docname, doctree): """Process the docinfo part of the doctree as metadata. Keep processing minimal -- just return what docutils says. """ self.metadata[docname] = md = {} try: docinfo = doctree[0] except IndexError: # probably an empty document return if docinfo.__class__ is not nodes.docinfo: # nothing to see here return for node in docinfo: # nodes are multiply inherited... if isinstance(node, nodes.authors): md['authors'] = [author.astext() for author in node] elif isinstance(node, nodes.TextElement): # e.g. author md[node.__class__.__name__] = node.astext() else: name, body = node md[name.astext()] = body.astext() for name, value in md.items(): if name in ('tocdepth',): try: value = int(value) except ValueError: value = 0 md[name] = value del doctree[0]