def createHTML(self, token, parent): try: style = find_plugin('pybtex.style.formatting', token.style) except PluginNotFound: msg = 'Unknown bibliography style "{}".' raise exceptions.RenderException(msg, token.style) citations = set() for tok in anytree.PreOrderIter(token.root): if isinstance(tok, BibtexCite): citations.update(tok.keys) formatted_bibliography = style().format_bibliography( self.extension.database, citations) html_backend = find_plugin('pybtex.backends', 'html') ol = html.Tag(parent, 'ol', class_='moose-bibliogrpahy') backend = html_backend(encoding='utf-8') for entry in formatted_bibliography: text = entry.text.render(backend) html.Tag(ol, 'li', id_=entry.key, string=text) return ol
def createHTML(self, token, parent): page, tag, href = self.createHTMLHelper(token, parent, 'key') if token.bookmark is not None: tok = self.findToken(page, token) href += token.bookmark # Optionally include page header as prefix if token.header: h = self.findHeading(page) if h: for n in h.children: self.translator.renderer.process(tag, n) html.String(tag, content=u':') else: tok = self.findHeading(page) tag['href'] = href if tok is not None: for n in tok.children: self.translator.renderer.process(tag, n) else: msg = "Failed to locate a heading for '{}'." raise exceptions.RenderException(token.info, msg, page.source) return tag
def getShortcut(self, token): if token.key in self.__cache: return self.__cache[token.key] #TODO: error if more than one found for node in anytree.PreOrderIter(token.root, maxlevel=None): if isinstance(node, tokens.Shortcut) and node.key == token.key: self.__cache[token.key] = node return node msg = "The shortcut link key '{}' was not located in the list of shortcuts." raise exceptions.RenderException(token.info, msg, token.key)
def createHTMLHelper(self, token, parent, attr): """Create the html.Tag and other information needed by RenderComponent objects.""" # The HTML link tag tag = html.Tag(parent, 'a', **token.attributes) # Locate the desired page exc = lambda msg: exceptions.RenderException(token.info, msg) page = self.translator.current.findall(getattr(token, attr), exc=exc)[0] # Get the relative path to the page being referenced href = page.relativeDestination(self.translator.current) return page, tag, href
def createMaterialize(self, token, parent): active_groups = [group.lower() for group in token.groups] errors = [] groups = list(token.syntax.groups) if 'MOOSE' in groups: groups.remove('MOOSE') groups.insert(0, 'MOOSE') collection = html.Tag(None, 'ul', class_='collection with-header') n_groups = len(active_groups) for group in groups: if active_groups and group.lower() not in active_groups: continue if n_groups > 1: li = html.Tag(collection, 'li', class_='moose-syntax-header collection-header', string=unicode(mooseutils.camel_to_space(group))) count = len(collection.children) if token.actions: errors += self._addItems(collection, token, token.syntax.actions(group=group), 'moose-syntax-actions') if token.objects: errors += self._addItems(collection, token, token.syntax.objects(group=group), 'moose-syntax-objects') if token.subsystems: errors += self._addItems(collection, token, token.syntax.syntax(group=group), 'moose-syntax-subsystems') if n_groups > 1 and len(collection.children) == count: li.parent = None if collection.children: collection.parent = parent if errors: msg = "The following errors were reported when accessing the '{}' syntax.\n\n" msg += '\n\n'.join(errors) raise exceptions.RenderException(token.info, msg, token.syntax.fullpath)
def findToken(self, root, token): # Load the token from the cache or locate it try: return AutoLinkMixin.BOOKMARK_CACHE[token.bookmark] except KeyError: with self.translator.lock: ast = root.tokenize() for node in anytree.PreOrderIter(ast): if 'id' in node and node['id'] == token.bookmark[1:]: AutoLinkMixin.BOOKMARK_CACHE[token.bookmark] = node return node # If you get here the id does not exist msg = "Failed to locate a token with id '{}' in '{}'." raise exceptions.RenderException(token.info, msg, token.bookmark[1:], self.translator.current.source)
def testRenderException(self): with self.assertRaises(exceptions.RenderException) as e: raise exceptions.RenderException(42, "{}", 'foo') self.assertEqual('foo', e.exception.message) self.assertEqual(e.exception.info, 42)
def createHTML(self, token, parent): citep = token.cite == 'citep' if citep: html.String(parent, content=u'(') num_keys = len(token.keys) for i, key in enumerate(token.keys): if key not in self.extension.database.entries: msg = 'Unknown BibTeX key: {}' raise exceptions.RenderException(msg, key) entry = self.extension.database.entries[key] author_found = True if not 'author' in entry.persons.keys( ) and not 'Author' in entry.persons.keys(): author_found = False entities = ['institution', 'organization'] for entity in entities: if entity in entry.fields.keys(): author_found = True name = '' for word in entry.fields[entity]: if word[0].isupper(): name += word[0] entry.persons['author'] = [Person(name)] if not author_found: msg = 'No author, institution, or organization for {}' raise exceptions.RenderException(msg, key) a = entry.persons['author'] n = len(a) if n > 2: author = '{} et al.'.format(' '.join(a[0].last_names)) #print 'AUTHOR:', author elif n == 2: a0 = ' '.join(a[0].last_names) a1 = ' '.join(a[1].last_names) author = '{} and {}'.format(a0, a1) else: author = ' '.join(a[0].last_names) form = u'{}, {}' if citep else u'{} ({})' html.Tag(parent, 'a', href='#{}'.format(key), string=form.format(author, entry.fields['year'])) if citep: if num_keys > 1 and i != num_keys - 1: html.String(parent, content=u'; ') else: if num_keys == 2 and i == 0: html.String(parent, content=u' and ') elif num_keys > 2 and i == num_keys - 2: html.String(parent, content=u', and ') elif num_keys > 2 and i != num_keys - 1: html.String(parent, content=u', ') if citep: html.String(parent, content=u')') return parent