def createHTML(self, token, parent): #pylint: disable=no-self-use if isinstance(token, LatexInlineEquation): div = html.Tag(parent, 'span', class_='moose-katex-inline-equation', **token.attributes) display = 'false' else: # Wrap all equation related items in an outer div div = html.Tag(parent, 'span', class_='moose-katex-block-equation') display = 'true' # Create equation content and number (if it is valid) html.Tag(div, 'span', class_='moose-katex-equation table-cell', **token.attributes) if token.number is not None: num = html.Tag(div, 'span', class_='moose-katex-equation-number') html.String(num, content=u'({})'.format(token.number)) # Build the KaTeX script script = html.Tag(div, 'script') content = u'var element = document.getElementById("%s");' % token['id'] content += u'katex.render("%s", element, {displayMode:%s,throwOnError:false});' % \ (token.tex, display) html.String(script, content=content) return parent
def createHTML(self, token, parent): #pylint: disable=no-self-use if token.content == u'--': return html.String(parent, content=u'–') elif token.content == u'---': return html.String(parent, content=u'—') return RenderString.createHTML(self, token, parent)
def createHTML(self, parent, token, page): #pylint: disable=no-self-use content = token['content'] if content == '--': return html.String(parent, content='–') elif content == '---': return html.String(parent, content='—') return RenderString.createHTML(self, parent, token, page)
def testTagString(self): tag = html.Tag(None, 'h1') html.String(content=u'foo', parent=tag) self.assertEqual(tag.write(), '<h1>foo</h1>') with self.assertRaises(TypeError) as e: html.String(parent=html.String()) gold = "If set, the parent of he html.String 'String' must be a html.Tag object, a " \ "'String' was provided." self.assertEqual(e.exception.message, gold)
def _addBreadcrumbs(self, container, page): #pylint: disable=no-self-use """ Inserts breadcrumb links at the top of the page. Inputs: root[tree.html.Tag]: The tag to which the breadcrumbs should be inserted. TODO: This is relying on hard-coded .md/.html extensions, that should not be the case. """ row = html.Tag(None, 'div', class_="row") col = html.Tag(row, 'div', class_="col hide-on-med-and-down l12") nav = html.Tag(col, 'nav', class_="breadcrumb-nav") div = html.Tag(nav, 'div', class_="nav-wrapper") container.insert(0, row) parts = page.local.split(os.sep) for i in range(1, len(parts)): current = self.translator.findPage( lambda p: p.local == os.path.join(*parts[:i])) #pylint: disable=cell-var-from-loop if isinstance(current, pages.Directory): idx = self.translator.findPages( os.path.join(current.local, 'index.md')) if idx: url = os.path.relpath(current.local, os.path.dirname(page.local)).replace( '.md', '.html') a = html.Tag(div, 'a', href=url, class_="breadcrumb", string=unicode(current.name)) else: span = html.Tag(div, 'span', class_="breadcrumb") html.String(span, content=unicode(current.name)) elif isinstance(current, pages.File) and current.name != 'index.md': url = os.path.relpath(current.local, os.path.dirname(page.local)).replace( '.md', '.html') a = html.Tag(div, 'a', href=url, class_="breadcrumb") html.String(a, content=unicode(os.path.splitext(current.name)[0])) if not page.local.endswith('index.md'): html.Tag(div, 'a', href='#', class_="breadcrumb", string=unicode(os.path.splitext(page.name)[0]))
def createHTML(self, parent, token, page): #pylint: disable=no-self-use if token.name == 'LatexInlineEquation': div = html.Tag(parent, 'span', class_='moose-katex-inline-equation', id_=token['bookmark'], **token.attributes) display = 'false' else: # Wrap all equation related items in an outer div div = html.Tag(parent, 'span', class_='moose-katex-block-equation') display = 'true' # Create equation content and number (if it is valid) html.Tag(div, 'span', class_='moose-katex-equation table-cell', id_=token['bookmark'], **token.attributes) if token['label'] is not None: num = html.Tag(div, 'span', class_='moose-katex-equation-number') html.String(num, content='({})'.format(token['number'])) # Build the KaTeX script script = html.Tag(div, 'script') config = dict() config['displayMode'] = display config['throwOnError'] = 'false' if self.extension.macros: config['macros'] = self.extension.macros config_str = ','.join('{}:{}'.format(key, value) for key, value in config.items()) if sys.version_info[0] == 2: config_str = config_str.encode('unicode_escape') tex = token['tex'].encode('unicode_escape') else: config_str = config_str.encode('unicode_escape').decode('utf-8') tex = token['tex'].encode('unicode_escape').decode('utf-8') content = 'var element = document.getElementById("%s");' % token[ 'bookmark'] content += 'katex.render("%s", element, {%s});' % \ (tex, config_str) html.String(script, content=content) return parent
def createHTML(self, token, parent): a = html.Tag(parent, 'a', **token.attributes) node = self.getShortcut(token) if node.content is not None: html.String(a, content=node.content) elif node.token: for n in node.token.children: self.translator.renderer.process(n, a) else: html.String(a, content=node.key) a['href'] = node.link return a
def _insert_parameter(parent, name, param): """ Insert parameter in to the supplied <ul> tag. Input: parent[html.Tag]: The 'ul' tag that parameter <li> item is to belong. name[str]: The name of the parameter. param: The parameter object from JSON dump. """ if param['deprecated']: return li = html.Tag(parent, 'li') header = html.Tag(li, 'div', class_='collapsible-header') body = html.Tag(li, 'div', class_='collapsible-body') html.Tag(header, 'span', class_='moose-parameter-name', string=name) default = _format_default(param) if default: html.Tag(header, 'span', class_='moose-parameter-header-default', string=default) p = html.Tag(body, 'p', class_='moose-parameter-description-default') html.Tag(p, 'span', string=u'Default:') html.String(p, content=default) cpp_type = param['cpp_type'] p = html.Tag(body, 'p', class_='moose-parameter-description-cpptype') html.Tag(p, 'span', string=u'C++ Type:') html.String(p, content=cpp_type) if 'options' in param: p = html.Tag(body, 'p', class_='moose-parameter-description-options') html.Tag(p, 'span', string=u'Options:') html.String(p, content=param['options']) p = html.Tag(body, 'p', class_='moose-parameter-description') desc = param['description'] if desc: html.Tag(header, 'span', class_='moose-parameter-header-description', string=unicode(desc)) html.Tag(p, 'span', string=u'Description:') html.String(p, content=unicode(desc))
def createMaterialize(self, token, parent): #pylint: disable=no-self-use content = RenderError.createMaterialize(self, token, parent) pre = html.Tag(content, 'pre', style="font-size:80%;") html.String(pre, content=unicode(token.traceback), escape=True) return content
def createHTML(self, token, parent): #pylint: disable=no-self-use div = html.Tag(parent, 'div', class_="moose-exception", **token.attributes) html.String(div, content=token.info[0]) return div
def createHTML(self, parent, token, page): headings = self.extension.binContent(page, token['location'], ContentExtension.FOLDER) links = self.extension.get('source_links') # Build lists for head in sorted(headings.keys()): items = headings[head] if head: h = html.Tag(parent, 'h{:d}'.format(int(token['level'])), class_='moose-a-to-z') if head in links: p = self.translator.findPage(links[head]) dest = p.relativeDestination(page) html.Tag(h, 'a', href=dest, string=unicode(head) + u' ') else: html.String(h, content=unicode(head)) row = html.Tag(parent, 'div', class_='row') for chunk in mooseutils.make_chunks(list(items), 3): col = html.Tag(row, 'div', class_='col s12 m6 l4') ul = html.Tag(col, 'ul', class_='moose-a-to-z') for text, path, _ in chunk: li = html.Tag(ul, 'li') html.Tag(li, 'a', href=path, string=unicode(text.replace('.md', '')))
def _addItems(self, parent, items): for name, href, description in items: li = html.Tag(parent, 'li', class_='moose-syntax-item collection-item') html.Tag(li, 'a', class_='moose-syntax-item-name', string=unicode(name), href=href) if description: desc = html.Tag(li, 'span', class_='moose-syntax-item-description') html.String(desc, content=description)
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 createHTML(self, parent, token, page): #pylint: disable=no-self-use language = 'language-{}'.format(token['language']) pre = html.Tag(parent, 'pre', token) pre.addClass('moose-pre') code = html.Tag(pre, 'code', class_=language) html.String(code, content=token['content'], escape=token['escape']) return pre
def createMaterialize(self, parent, token, page): #pylint: disable=no-self-use id_ = uuid.uuid4() a = html.Tag(parent, 'a', class_="moose-exception modal-trigger", href='#{}'.format(id_)) if token.info: html.String(a, content=token.info[0]) modal = html.Tag(parent.root, 'div', id_=id_, class_="modal") content = html.Tag(modal, 'div', class_="modal-content") head = html.Tag(content, 'h2') html.String(head, content=u'Tokenize Error') p = html.Tag(content, 'p') html.String(p, content=unicode(token['message'])) html.Tag(p, 'br', close=False) if token.info: html.String(p, content=u'{}:{}'.format(page.local, token.info.line)) html.Tag(p, 'br', close=False) if token.info: pre = html.Tag(content, 'pre') code = html.Tag(pre, 'code', class_="language-markdown") html.String(code, content=token.info[0], escape=True) footer = html.Tag(modal, 'div', class_="modal-footer grey lighten-3") done = html.Tag(footer, 'a', class_="modal-action modal-close btn-flat") html.String(done, content=u"Done") trace = token.get('traceback', None) if trace is not None: pre = html.Tag(content, 'pre', style="font-size:80%;") html.String(pre, content=trace, escape=True) return content
def createHTML(self, token, parent): #pylint: disable=no-self-use caption = html.Tag(parent, 'p', class_="moose-caption") if token.prefix: heading = html.Tag(caption, 'span', class_="moose-caption-heading") html.String(heading, content=u"{} {}: ".format(token.prefix, token.number)) text = html.Tag(caption, 'span', class_="moose-caption-text") return text
def createHTML(self, parent, token, page): #pylint: disable=no-self-use caption = html.Tag(parent, 'p', class_="moose-caption") prefix = token.get('prefix', None) if prefix: heading = html.Tag(caption, 'span', class_="moose-caption-heading") html.String(heading, content=u"{} {}: ".format(prefix, token['number'])) text = html.Tag(caption, 'span', class_="moose-caption-text") return text
def createMaterialize(self, parent, token, page): title = html.Tag(parent, 'div', class_='card-title moose-alert-title') if token.get('prefix'): brand = token['brand'] if brand == u'construction': brand = u'under construction' prefix = html.Tag(title, 'span', string=brand, class_='moose-alert-title-brand') if token.children: html.String(prefix, content=u':') return title
def createMaterialize(self, token, parent): #pylint: disable=no-self-use id_ = uuid.uuid4() a = html.Tag(parent, 'a', class_="moose-exception modal-trigger", href='#{}'.format(id_)) html.String(a, content=token.info[0]) modal = html.Tag(parent.root, 'div', id_=id_, class_="modal") content = html.Tag(modal, 'div', class_="modal-content") head = html.Tag(content, 'h2') html.String(head, content=u'Tokenize Error') p = html.Tag(content, 'p') html.String(p, content=unicode(token.message)) if self.translator.current: html.Tag(p, 'br', close=False) html.String(p, content=u'{}:{}'.format(self.translator.current.local, token.info.line)) html.Tag(p, 'br', close=False) pre = html.Tag(content, 'pre') code = html.Tag(pre, 'code', class_="language-markdown") html.String(code, content=token.info[0], escape=True) footer = html.Tag(modal, 'div', class_="modal-footer grey lighten-3") done = html.Tag(footer, 'a', class_="modal-action modal-close btn-flat") html.String(done, content=u"Done") return content
def _addBreadcrumbs(self, config, container, root_page): #pylint: disable=no-self-use """ Inserts breadcrumb links at the top of the page. Inputs: root[tree.html.Tag]: The tag to which the breadcrumbs should be inserted. TODO: This is relying on hard-coded .md/.html extensions, that should not be the case. """ if not (config.get('breadcrumbs', None) and root_page): return row = html.Tag(container, 'div', class_="row") col = html.Tag(row, 'div', class_="col hide-on-med-and-down l12") nav = html.Tag(col, 'nav', class_="breadcrumb-nav") div = html.Tag(nav, 'div', class_="nav-wrapper") node = root_page for n in node.path: if not n.local: continue if isinstance(n, page.DirectoryNode): idx = n.find('index.md', maxlevel=2) if idx: url = os.path.relpath(n.local, os.path.dirname(node.local)).replace( '.md', '.html') a = html.Tag(div, 'a', href=url, class_="breadcrumb") html.String(a, content=unicode(n.name)) else: span = html.Tag(div, 'span', class_="breadcrumb") html.String(span, content=unicode(n.name)) elif isinstance(n, page.FileNode) and n.name != 'index.md': url = os.path.relpath(n.local, os.path.dirname(node.local)).replace( '.md', '.html') a = html.Tag(div, 'a', href=url, class_="breadcrumb") html.String(a, content=unicode(os.path.splitext(n.name)[0]))
def createHTML(self, parent, token, page): #pylint: disable=no-self-use if token.name == 'LatexInlineEquation': div = html.Tag(parent, 'span', class_='moose-katex-inline-equation', id_=token['bookmark'], **token.attributes) display = 'false' else: # Wrap all equation related items in an outer div div = html.Tag(parent, 'span', class_='moose-katex-block-equation') display = 'true' # Create equation content and number (if it is valid) html.Tag(div, 'span', class_='moose-katex-equation table-cell', id_=token['bookmark'], **token.attributes) if token['numbered']: num = html.Tag(div, 'span', class_='moose-katex-equation-number') html.String(num, content=u'({})'.format(token['number'])) # Build the KaTeX script script = html.Tag(div, 'script') content = u'var element = document.getElementById("%s");' % token[ 'bookmark'] content += u'katex.render("%s", element, {displayMode:%s,throwOnError:false});' % \ (token['tex'].encode('string-escape'), display) html.String(script, content=content) return parent
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 for n in tok.children: self.translator.renderer.process(tag, n) return tag
def createMaterialize(self, parent, token, page): tag = html.Tag(parent, 'li', class_='collection-header') category = token.get('category') if category: tag.insert(0, html.String(content='{}: '.format(category))) return tag
def createHTML(self, parent, token, page): #pylint: disable=no-self-use div = html.Tag(parent, 'div', token) div.addClass("moose-exception") html.String(div, content=token.info[0]) return div
def createHTML(self, parent, token, page): #pylint: disable=no-self-use return html.String(parent, content=' ' * token['count'])
def createHTML(self, parent, token, page): #pylint: disable=no-self-use return html.String(parent, content=token['content'], escape=token.get('escape', True))
def createHTML(self, parent, token, page): #pylint: disable=no-self-use,unused-argument return html.String(parent, content=' ')
def createHTML(self, parent, token, page): #pylint: disable=no-self-use code = html.Tag(parent, 'code') html.String(code, content=token['content'], escape=True) return code
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
def testTagString(self): tag = html.Tag(None, 'h1') html.String(content='foo', parent=tag) self.assertEqual(tag.write(), '<h1>foo</h1>')