def format(chunk): if colorizer: # pass stdin as fake file object, but... hc = colorizer.HtmlColorizer(sys.stdin, sys.stdout) # really just use the lines: hc.readline = [x + "\n" for x in chunk].__iter__().next # disable line numbers: hc.lineno = lambda x: None # and turn off his pre/code tags: hc.begin = lambda: None hc.end = lambda: None hc.colorize() else: for line in chunk: print xmlEncode(line)
def x2e(data): if data is None: return None # convert back to etree: try: data = etree.fromstring(data) except (ExpatError, etree.ParseError): try: data = etree.fromstring('<null>' + data + '</null>') except (ExpatError, etree.ParseError): data = etree.fromstring('<null>' + handy.xmlEncode(data) + '</null>') return data
html = highlight(code, get_lexer_for_filename(trail[path.split(".")[0]].outFile), HTMLformat) # remove the fake <? php if snip: match = '<span class="cp"><?php</span>' point = html.find(match) html = html[:point] + html[point+len(match):] print >> out, html elif name.startswith(hideBlaze): path = parseBlaze(name, hideBlaze) print >> out, '<pre class="hide">%s</pre>' % xmlEncode(trail[path].snapShot()) trail[path].hide() elif name.startswith("> "): html = highlight(htmlDecode(name[5:]), JavascriptLexer(), HTMLformat) cutPoint = html.find("<span") print >> out, html[:cutPoint] + prompt + html[cutPoint:] elif name.startswith(snapBlaze): # make snapshots in directory snap/snapName snapName = parseBlaze(name, snapBlaze) snapPath = os.path.join(snapDir, snapName) os.mkdir(snapPath) for item in trail.blazes.values(): if item.visible: fileName = item.fileName
def textarea(name, value, attrs=''): ''' An html TextArea tag ''' return '<textarea name="%s"%s>%s</textarea>' \ % (name, attrs, xmlEncode(deNone(value)))
def _deBlock(self, lines, tag, attrs="", left=0): "Recursive routine to handle chunks of ZBR code" if attrs: res = "<%s %s>\n" % (tag, attrs) else: res = "<%s>\n" % tag x = 0 while x < len(lines): lineNo = x + 1 ## only look from the leftmost position onwards. ## (it's okay because we make sure theres' nothing ## but space in there a little later.. line = lines[x][left:] ## comments are single lines starting with * # if string.lstrip(string.lstrip(line[1:])).startswith("#"): # this little mess just strips off the * and # res = res + "<rem>%s</rem>\n" \ % xmlEncode(string.lstrip(line[1:])[2:]) ## zebra commands begin with * elif string.lstrip(line).startswith("*"): line = string.strip(line) ## .. and end with : or ; if line[-1] == ":": isBlock = 1 elif line[-1] == ";": isBlock = 0 else: raise SyntaxError, \ "* tag without ':' or ';' on line %i\n[%s]" \ % (lineNo, line) line = line[:-1] ## get the tokens after *: toks = string.split(line, " ")[1:] tok = toks[0] ## see if we can parse that token: if hasattr(self, "parse_"+tok): attrs = apply(getattr(self,"parse_"+tok), (toks,)) else: raise NameError, \ "Don't know how to handle '%s' on line %i" \ % (line, lineNo) ## find the new left edge: if isBlock: newleft = 0 topx = x = x + 1 if topx >= len(lines): newleft = left - 1 else: for i in range(len(lines[topx])): if lines[topx][i]!=" ": newleft = i break ## find the end of the block, which should be ## less indented than the inside of the block. if newleft <= left: ## the block is empty pass else: while (x<len(lines)): if (string.strip(lines[x])=="") \ or (lines[x][:newleft])==(" " * newleft): x = x + 1 else: break ## run this routine recursively on the inner block: res = res + self._deBlock(lines[topx:x], tok, attrs, newleft) ## we've already added 1, so jump back to the top: continue else: # not a block... res = res + "<%s %s/>\n" % (tok, attrs) ## just a normal line.. else: if tag == "exec": end = "\n" elif (line.endswith(chr(92))): line = line[:-1] end = "" else: end = "<nl/>\n" res = res + deCurl(xmlEncode(line)) + end ## move on to the next line and continue with the while() loop: x = x + 1 ## cap off the current tag and get out of here! res = res + "</%s>\n" % tag return res
def _deBlock(self, lines, tag, attrs="", left=0): "Recursive routine to handle chunks of ZBR code" if attrs: res = "<%s %s>\n" % (tag, attrs) else: res = "<%s>\n" % tag x = 0 while x < len(lines): lineNo = x + 1 ## only look from the leftmost position onwards. ## (it's okay because we make sure theres' nothing ## but space in there a little later.. line = lines[x][left:] ## comments are single lines starting with * # if string.lstrip(string.lstrip(line[1:])).startswith("#"): # this little mess just strips off the * and # res = res + "<rem>%s</rem>\n" \ % xmlEncode(string.lstrip(line[1:])[2:]) ## zebra commands begin with * elif string.lstrip(line).startswith("*"): line = string.strip(line) ## .. and end with : or ; if line[-1] == ":": isBlock = 1 elif line[-1] == ";": isBlock = 0 else: raise SyntaxError, \ "* tag without ':' or ';' on line %i\n[%s]" \ % (lineNo, line) line = line[:-1] ## get the tokens after *: toks = string.split(line, " ")[1:] tok = toks[0] ## see if we can parse that token: if hasattr(self, "parse_" + tok): attrs = apply(getattr(self, "parse_" + tok), (toks, )) else: raise NameError, \ "Don't know how to handle '%s' on line %i" \ % (line, lineNo) ## find the new left edge: if isBlock: newleft = 0 topx = x = x + 1 if topx >= len(lines): newleft = left - 1 else: for i in range(len(lines[topx])): if lines[topx][i] != " ": newleft = i break ## find the end of the block, which should be ## less indented than the inside of the block. if newleft <= left: ## the block is empty pass else: while (x < len(lines)): if (string.strip(lines[x])=="") \ or (lines[x][:newleft])==(" " * newleft): x = x + 1 else: break ## run this routine recursively on the inner block: res = res + self._deBlock(lines[topx:x], tok, attrs, newleft) ## we've already added 1, so jump back to the top: continue else: # not a block... res = res + "<%s %s/>\n" % (tok, attrs) ## just a normal line.. else: if tag == "exec": end = "\n" elif (line.endswith(chr(92))): line = line[:-1] end = "" else: end = "<nl/>\n" res = res + deCurl(xmlEncode(line)) + end ## move on to the next line and continue with the while() loop: x = x + 1 ## cap off the current tag and get out of here! res = res + "</%s>\n" % tag return res
html = highlight( code, get_lexer_for_filename(trail[path.split(".")[0]].outFile), HTMLformat) # remove the fake <? php if snip: match = '<span class="cp"><?php</span>' point = html.find(match) html = html[:point] + html[point + len(match):] print >> out, html elif name.startswith(hideBlaze): path = parseBlaze(name, hideBlaze) print >> out, '<pre class="hide">%s</pre>' % xmlEncode( trail[path].snapShot()) trail[path].hide() elif name.startswith("> "): html = highlight(htmlDecode(name[5:]), JavascriptLexer(), HTMLformat) cutPoint = html.find("<span") print >> out, html[:cutPoint] + prompt + html[cutPoint:] elif name.startswith(snapBlaze): # make snapshots in directory snap/snapName snapName = parseBlaze(name, snapBlaze) snapPath = os.path.join(snapDir, snapName) os.mkdir(snapPath) for item in trail.blazes.values(): if item.visible: