def format_unencoded(self, tokenstream, outfile): MAP = tok.STANDARD_TYPES outfile.write("<div class='xcode'>") for ttype, tvalue in self.filter(tokenstream): if isinstance(ttype, str): if ttype == "Code:start": outfile.write("<code class='%s'>" % self._lang) elif ttype == "Code:end": outfile.write("</code>") elif ttype == "Input:start": outfile.write("<div class='input-numbered-block'>") outfile.write("<code class='%s input'>" % self._lang) elif ttype == "Input:end": outfile.write("</code>") outfile.write("</div>") elif ttype == "Output:start": outfile.write("<div class='output'>") elif ttype == "Output:end": outfile.write("</div>") elif ttype == "Exception:start": outfile.write("<div class='exception'>") elif ttype == "Exception:end": outfile.write("</div>") elif ttype == "raw": assert isinstance(tvalue, str) outfile.write(tvalue) else: cls = MAP.get(ttype, None) tvalue = escape_html(tvalue) if cls: outfile.write("<span class=%s>%s</span>" % (cls, tvalue)) else: outfile.write(tvalue) outfile.write("</div>")
def test_correct_output(): hfmt = HtmlFormatter(nowrap=True) houtfile = StringIO() hfmt.format(tokensource, houtfile) nfmt = NullFormatter() noutfile = StringIO() nfmt.format(tokensource, noutfile) stripped_html = re.sub('<.*?>', '', houtfile.getvalue()) escaped_text = escape_html(noutfile.getvalue()) assert stripped_html == escaped_text
def test_correct_output(self): hfmt = HtmlFormatter(nowrap=True) houtfile = StringIO.StringIO() hfmt.format(tokensource, houtfile) nfmt = NullFormatter() noutfile = StringIO.StringIO() nfmt.format(tokensource, noutfile) stripped_html = re.sub('<.*?>', '', houtfile.getvalue()) escaped_text = escape_html(noutfile.getvalue()) self.assertEquals(stripped_html, escaped_text)
def test_correct_output(self): hfmt = CodeHtmlFormatter(instance_class=type, nowrap=True) houtfile = StringIO() hfmt.format(tokensource, houtfile) nfmt = NullFormatter() noutfile = StringIO() nfmt.format(tokensource, noutfile) stripped_html = re.sub('<.*?>', '', houtfile.getvalue()) escaped_text = escape_html(noutfile.getvalue()) self.assertEqual(stripped_html, escaped_text)
def format_string(source, lexername=None, lexer=None, look="", hl_lines=None, pre=False): source = dedent(source.strip("\r\n")) lexer = lexer or lexer_for(lexername) if lexer: hf = CustomHtmlFormatter(hl_lines=hl_lines) hi = highlight(source, lexer, hf) else: hi = escape_html(source) if pre: hi = "<pre class='pre codehilite %s'>%s</pre>" % (look, hi) return hi
def get_feed(username): """ Uses the github gist API and constructs an RSS feed for it """ url = GIST_URL % username try: result = urlfetch.fetch(url) except urlfetch.DownloadError: result = urlfetch.fetch(url) logging.error("Died on get_feed(%s)", url) if result.status_code != 200 or result.content == 'error': return 'error' feed = [ FEED_HEAD_TEMPLATE % { "username": username, "pub_date": strftime(DATE_FORMAT, gmtime()) }, ] obj = json.loads(result.content) for i in range(0, len(obj) if len(obj) < 10 else 10): gist = obj[i] date = parse(gist['created_at']) desc = gist['description'] title = escape_html( ', '.join( map(lambda f: f[1]['filename'], gist['files'].items()) ) if desc is None else desc) feed.append(ITEM_TEMPLATE % { "id": gist['id'], "author": username, "title": title, "post_date": date.strftime('%b %d, %Y'), "gist_url": gist['html_url'], "raw_gist": get_raw(gist['files']), "pub_date": date.strftime(DATE_FORMAT), } ) feed.append(FEED_FOOT_TEMPLATE) return ''.join(feed)
def process_dtframe(self, header_lines, sep_line, body_lines, nrows, ncols): sep = sep_line.find('+') key_class = "row_index" if header_lines[0][:sep].strip() == '' else \ "key" columns = [mm.span() for mm in re.finditer('-+', sep_line)] out = "<div class='dtframe'><table>" out += "<thead>" for line in header_lines: cls = "colnames" if line == header_lines[0] else "coltypes" out += f"<tr class={cls}>" for i, j in columns: out += f"<th class={key_class}>" if j < sep else "<th>" out += line[i:j].strip() out += "</th>" out += "<th></th></tr>" out += "</thead>" out += "<tbody>" for line in body_lines: out += "<tr>" for i, j in columns: classes = [] value = escape_html(line[i:j].strip()) if j < sep: classes.append(key_class) if value == '…': classes.append("etc") if value == 'NA': classes.append("NA") if classes: out += f"<td class='{' '.join(classes)}'>" else: out += "<td>" out += value out += "</td>" out += "<td></td></tr>" out += "</tbody>" out += "</table>" rows = f"{comma_separated(nrows)} row{'' if nrows == 1 else 's'}" cols = f"{comma_separated(ncols)} column{'' if ncols == 1 else 's'}" out += f"<div class='dtframe-footer'>{rows} × {cols}</div>" out += '</div>' yield ("raw", out)
def process_data(self): dot_width = 0 if self.type in ['time64']: for value in self.data: if '.' in value: t = len(value) - value.rindex('.') dot_width = max(dot_width, t) for i, value in enumerate(self.data): value = escape_html(value) if value == "NA": value = "<span class=NA>NA</span>" if value == '…': value = "<span class=dim>…</span>" if dot_width: if '.' in value: t = len(value) - value.rindex('.') else: t = 0 value += " " * (dot_width - t) if self.type == "time64": value = value.replace("T", "<span class=dim>T</span>") self.data[i] = value
def _format_lines(self, tokensource): """ Just format the tokens, without any wrapping tags. Yield individual lines. """ nocls = self.noclasses lsep = self.lineseparator # for <span style=""> lookup only getcls = self.ttype2class.get c2s = self.class2style lspan = '' line = '' myescape = lambda x: x.replace('"', '\'') f = open('output','w+') f.close() for ttype, value in tokensource: curstates = self.linestates.get(self.curlineno, None) dumpfile = getglobal('dumpfile') if dumpfile: f = open(dumpfile, 'a') # print '%i: %r' % (self.curlineno, curstates) f.write('%i: %r\n' % (self.curlineno, curstates)) f.close() #if curstates is None: #print '%i: %r' % (self.curlineno, curstates) if ttype == Token.Text and value == '\n': self.curlineno += 1 if Token.Name in (ttype, ttype.parent) or value == 'self': f = open('output', 'a') f.write('%i : %s : type = %s, value = %s\n' % (self.curlineno, value, myescape(BaseModuleInfo.get_vartypes(curstates, value)), myescape(BaseModuleInfo.get_varvalues(curstates, value)))) f.close() addedstyle = 'style="%s" title="%s: %s, val: %s"' % (myvar, value, myescape(BaseModuleInfo.get_vartypes(curstates, value)), myescape(BaseModuleInfo.get_varvalues(curstates, value))) elif Token.Literal in (ttype.parent, ttype.parent, ttype.parent.parent) and value not in ('\'', '"'): # addedstyle= 'style="%s" title="const \'%s\': TODO"' % \ # (myconst, value) addedstyle= 'style="%s" title="constant"' % myconst else: addedstyle = '' if nocls: cclass = getcls(ttype) while cclass is None: ttype = ttype.parent cclass = getcls(ttype) cspan = cclass and '<span style="%s" %s>' % \ (c2s[cclass][0], addedstyle) or '' else: cls = self._get_css_class(ttype) cspan = cls and '<span class="%s" %s>' % \ (cls, addedstyle) or '' parts = escape_html(value).split('\n') # for all but the last line for part in parts[:-1]: if line: if lspan != cspan: line += (lspan and '</span>') + cspan + part + \ (cspan and '</span>') + lsep else: # both are the same line += part + (lspan and '</span>') + lsep yield 1, line line = '' elif part: yield 1, cspan + part + (cspan and '</span>') + lsep else: yield 1, lsep # for the last line if line and parts[-1]: if lspan != cspan: line += (lspan and '</span>') + cspan + parts[-1] lspan = cspan else: line += parts[-1] elif parts[-1]: line = cspan + parts[-1] lspan = cspan # else we neither have to open a new span nor set lspan if line: yield 1, line + (lspan and '</span>') + lsep
def _format_lines(self, tokensource): """ Just format the tokens, without any wrapping tags. Yield individual lines. """ nocls = self.noclasses lsep = self.lineseparator # for <span style=""> lookup only getcls = self.ttype2class.get c2s = self.class2style lspan = '' line = '' myescape = lambda x: x.replace('"', '\'') f = open('output', 'w+') f.close() for ttype, value in tokensource: curstates = self.linestates.get(self.curlineno, None) dumpfile = getglobal('dumpfile') if dumpfile: f = open(dumpfile, 'a') # print '%i: %r' % (self.curlineno, curstates) f.write('%i: %r\n' % (self.curlineno, curstates)) f.close() #if curstates is None: #print '%i: %r' % (self.curlineno, curstates) if ttype == Token.Text and value == '\n': self.curlineno += 1 if Token.Name in (ttype, ttype.parent) or value == 'self': f = open('output', 'a') f.write( '%i : %s : type = %s, value = %s\n' % (self.curlineno, value, myescape(BaseModuleInfo.get_vartypes(curstates, value)), myescape(BaseModuleInfo.get_varvalues(curstates, value)))) f.close() addedstyle = 'style="%s" title="%s: %s, val: %s"' % ( myvar, value, myescape(BaseModuleInfo.get_vartypes(curstates, value)), myescape(BaseModuleInfo.get_varvalues(curstates, value))) elif Token.Literal in (ttype.parent, ttype.parent, ttype.parent.parent) and value not in ('\'', '"'): # addedstyle= 'style="%s" title="const \'%s\': TODO"' % \ # (myconst, value) addedstyle = 'style="%s" title="constant"' % myconst else: addedstyle = '' if nocls: cclass = getcls(ttype) while cclass is None: ttype = ttype.parent cclass = getcls(ttype) cspan = cclass and '<span style="%s" %s>' % \ (c2s[cclass][0], addedstyle) or '' else: cls = self._get_css_class(ttype) cspan = cls and '<span class="%s" %s>' % \ (cls, addedstyle) or '' parts = escape_html(value).split('\n') # for all but the last line for part in parts[:-1]: if line: if lspan != cspan: line += (lspan and '</span>') + cspan + part + \ (cspan and '</span>') + lsep else: # both are the same line += part + (lspan and '</span>') + lsep yield 1, line line = '' elif part: yield 1, cspan + part + (cspan and '</span>') + lsep else: yield 1, lsep # for the last line if line and parts[-1]: if lspan != cspan: line += (lspan and '</span>') + cspan + parts[-1] lspan = cspan else: line += parts[-1] elif parts[-1]: line = cspan + parts[-1] lspan = cspan # else we neither have to open a new span nor set lspan if line: yield 1, line + (lspan and '</span>') + lsep