def highlight(self, text, mime=None, syntax=None, line_no=0): """Colorize the source code.""" if pygments is None: yield html.pre(html(text)) return formatter = pygments.formatters.HtmlFormatter() formatter.line_no = line_no def wrapper(source, unused_outfile): """Wrap each line of formatted output.""" yield 0, '<div class="highlight"><pre>' for lineno, line in source: yield (lineno, html.span(line, id_="line_%d" % formatter.line_no)) formatter.line_no += 1 yield 0, '</pre></div>' formatter.wrap = wrapper try: if mime: lexer = pygments.lexers.get_lexer_for_mimetype(mime) elif syntax: lexer = pygments.lexers.get_lexer_by_name(syntax) else: lexer = pygments.lexers.guess_lexer(text) except: yield html.pre(html(text)) return yield pygments.highlight(text, lexer, formatter)
def _block_conflict(self, block): for self.line_no, part in block: yield '<div class="conflict">' local = "\n".join(self.lines_until(self.conflict_sep_re)) yield html.pre(html(local), class_="local", id="line_%d" % self.line_no) other = "\n".join(self.lines_until(self.conflict_close_re)) yield html.pre(html(other), class_="other", id="line_%d" % self.line_no) yield '</div>'
def content_iter(self, lines): last_lines = [] in_header = False in_bug = False attributes = {} title = None for line_no, line in enumerate(lines): if last_lines and line.startswith('----'): title = ''.join(last_lines) last_lines = [] in_header = True attributes = {} elif in_header and ':' in line: attribute, value = line.split(':', 1) attributes[attribute.strip()] = value.strip() else: if in_header: if in_bug: yield '</div>' #tags = [tag.strip() for tag in # attributes.get('tags', '').split() # if tag.strip()] yield '<div id="line_%d">' % (line_no) in_bug = True if title: yield html.h2(html(title)) if attributes: yield '<dl>' for attribute, value in attributes.items(): yield html.dt(html(attribute)) yield html.dd(html(value)) yield '</dl>' in_header = False if not line.strip(): if last_lines: if last_lines[0][0] in ' \t': yield html.pre(html(''.join(last_lines))) else: yield html.p(html(''.join(last_lines))) last_lines = [] else: last_lines.append(line) if last_lines: if last_lines[0][0] in ' \t': yield html.pre(html(''.join(last_lines))) else: yield html.p(html(''.join(last_lines))) if in_bug: yield '</div>'
def _block_indent(self, block): parts = [] first_line = None for self.line_no, part in block: if first_line is None: first_line = self.line_no parts.append(part.rstrip()) text = "\n".join(parts) yield html.pre(html(text), id="line_%d" % first_line)
def _block_syntax(self, block): for self.line_no, part in block: syntax = part.lstrip('{#!').strip() inside = "\n".join(self.lines_until(self.code_close_re)) if self.wiki_syntax: return self.wiki_syntax(inside, syntax=syntax, line_no=self.line_no) else: return [ html.div(html.pre(html(inside), id="line_%d" % self.line_no), class_="highlight") ]
def revision(request, title, rev): _ = request.wiki.gettext text = request.wiki.storage.get_revision(title, rev).text link = html.a(html(title), href=request.get_url(title)) content = [ html.p( html(_('Content of revision %(rev)s of page %(title)s:')) % { 'rev': rev[:8], 'title': link }), html.pre(html(text)), ] special_title = _('Revision of "%(title)s"') % {'title': title} page = hatta.page.get_page(request, title) resp = page.template('page_special.html', content=content, special_title=special_title) return response(request, title, resp, rev=rev, etag='/old')
def _block_code(self, block): for self.line_no, part in block: inside = "\n".join(self.lines_until(self.code_close_re)) yield html.pre(html(inside), class_="code", id="line_%d" % self.line_no)