def dbify(text): marker = 0 for tag, start, end, sublist in fontify(text): if marker < start: yield escape(text[marker:start]) yield ("""<phrase role="py_%s">%s</phrase>""" % (tag, escape(text[start:end]))) marker = end if marker < len(text): yield escape(text[marker:])
def htmlify(text): marker = 0 for tag, start, end, sublist in fontify(text): if marker < start: yield escape(text[marker:start]) yield """<span class="py_%s">%s</span>""" % (tag, escape(text[start:end])) marker = end if marker < len(text): yield escape(text[marker:])
def _colorize(self): if not self._dirty: return storage = self._storage source = self.getSource() sourceLen = len(source) dirtyStart = self._dirty.pop() getColor = self._syntaxColors.get setAttrs = storage.setAttributes_range_ getAttrs = storage.attributesAtIndex_effectiveRange_ basicAttrs = getBasicTextAttributes() lastEnd = end = dirtyStart count = 0 sameCount = 0 for tag, start, end, sublist in fontify(source, dirtyStart): end = min(end, sourceLen) rng = (start, end - start) attrs = getColor(tag) oldAttrs, oldRng = getAttrs(rng[0]) if attrs is not None: clearRng = (lastEnd, start - lastEnd) if clearRng[1]: setAttrs(basicAttrs, clearRng) setAttrs(attrs, rng) if rng == oldRng and attrs == oldAttrs: sameCount += 1 if sameCount > 4: # due to backtracking we have to account for a few more # tokens, but if we've seen a few tokens that were already # colorized the way we want, we're done return else: sameCount = 0 else: rng = (lastEnd, end - lastEnd) if rng[1]: setAttrs(basicAttrs, rng) count += 1 if count > 200: # enough for now, schedule a new chunk self._dirty.append(end) self.scheduleColorize() break lastEnd = end else: # reset coloring at the end end = min(sourceLen, end) rng = (end, sourceLen - end) if rng[1]: setAttrs(basicAttrs, rng)
def render(self, context): from PyFontify import fontify output = unicode(self.nodelist.render(context)) tags = fontify(output) html = output d = 0 for tag, start, end, sublist in tags: html = ( html[: start + d] + """<span class="%s">%s</span>""" % (tag, html[start + d : end + d]) + html[end + d :] ) d += len(tag) + 22 # 22 is the length of the span HTML code return """<div class="python">""" + html + "</div>"