def __call__(self, s): if not s: return '' self.s = s r = "" b = 0 e = 0 for i in SEQUENCE_RE.finditer(s): b, e1 = i.span() r += html_quote(str(s[e:b])) r += self.repl(i) e = e1 r += html_quote(str(s[e:])) return r
def __call__(self, s, enable_topic=False): self.enable_topic = enable_topic ret = {} r = "" pos = [] b = 0 e = 0 for i in MENTION_RE.finditer(s): b, e1 = i.span() r += html_quote(s[e:b]) id, replaced, kind = self.repl(i) if id and kind: pos.append((b, e1, id, kind)) r += replaced e = e1 r += html_quote(s[e:]) ret["origin"] = s ret["postions"] = pos ret["html"] = covert_space_and_newline(r) return ret
def repl(self, match): ret = "" text = "" del_code = match.group(1) add_code = match.group(2) rep_code = match.group(3) color_code = match.group(4) if del_code: text = html_quote(str(del_code)) ret = '<span class="x">%s</span>' % text elif add_code: text = html_quote(str(add_code)) ret = '<span class="i">%s</span>' % text elif rep_code: text = html_quote(str(rep_code)) ret = '<span class="c">%s</span>' % text elif color_code: color_code = re.sub(r'[\+\-\^](.*?)', r'\1', color_code) text = html_quote(str(color_code)) ret = '<span class="color">%s</span>' % text return str(ret)
def repl(self, match): at, name, protocol, link, topic = match.groups() # print 'repl enable_topic=', self.enable_topic # print 'mention', at, name, protocol, link, topic if at and name: user_id = self.users_dict.get(name.lower(), None) card = user_id and Card.get(user_id) if card: return user_id, '<a href="%s">@%s</a>' % (card.path, card.screen_name), "card" if protocol and link: url = protocol + link if url.startswith(SITE): return None, '<a href="%s">%s</a>' % (url, url), "url" else: return None, '<a href="%s" target="_blank">%s</a>' % (url, url), "url" if self.enable_topic and topic: return ( topic, '<a href="/topic/%s">#%s#</a>' % (html_quote(topic.upper()), html_quote(topic.upper())), "topic", ) return None, at or topic, None
def __call__(self, s): if not s: return '' self.s = str(s) self.run_plugins() self.collect_pairs() self.decross_pairs() html_by_idx = self.merge_pairs() parts = [] last_end = 0 for i in sorted(html_by_idx): html = html_by_idx[i] parts.append(html_quote(self.s[last_end:i])) parts.append(html) last_end = i parts.append(html_quote(self.s[last_end:])) self.s = ''.join(parts) return self.s
def dump_html(self): row_fmt = '<tr valign="top"><th align="left">%s</th><td>%s</td></tr>' lines = ["<h3>form</h3>", "<table>"] for k, v in self.form.items(): lines.append(row_fmt % (html_quote(k), html_quote(v))) lines += ["</table>", "<h3>cookies</h3>", "<table>"] for k, v in self.cookies.items(): lines.append(row_fmt % (html_quote(k), html_quote(v))) lines += ["</table>", "<h3>environ</h3>" "<table>"] for k, v in self.environ.items(): lines.append(row_fmt % (html_quote(k), html_quote(str(v)))) lines.append("</table>") return "\n".join(lines)
def dump_html(self): row_fmt = ('<tr valign="top"><th align="left">%s</th><td>%s</td></tr>') lines = ["<h3>form</h3>", "<table>"] for k, v in self.form.items(): lines.append(row_fmt % (html_quote(k), html_quote(v))) lines += ["</table>", "<h3>cookies</h3>", "<table>"] for k, v in self.cookies.items(): lines.append(row_fmt % (html_quote(k), html_quote(v))) lines += ["</table>", "<h3>environ</h3>" "<table>"] for k, v in self.environ.items(): lines.append(row_fmt % (html_quote(k), html_quote(str(v)))) lines.append("</table>") return "\n".join(lines)
def span(self, code, cls): text = html_quote(str(code)) return '<span class="%s">%s</span>' % (cls, text)
def url(self): return '%s/topic/%s' % (SITE, html_quote(self.name))
def path(self): return '/topic/%s' % html_quote(self.name)