def extratabs(self, line): tabcount = 0 for c in line: if c <> '\t': break tabcount = tabcount + 1 last = 0 cleanline = '' tags = PyFontify.fontify(line) # strip comments and strings for tag, start, end, sublist in tags: if tag in ('string', 'comment'): cleanline = cleanline + line[last:start] last = end cleanline = cleanline + line[last:] cleanline = string.strip(cleanline) if cleanline and cleanline[-1] == ':': tabcount = tabcount + 1 else: # extra indent after unbalanced (, [ or { for open, close in (('(', ')'), ('[', ']'), ('{', '}')): count = string.count(cleanline, open) if count and count > string.count(cleanline, close): tabcount = tabcount + 2 break return tabcount
def extratabs(self, line): tabcount = 0 for c in line: if c <> "\t": break tabcount = tabcount + 1 last = 0 cleanline = "" tags = PyFontify.fontify(line) # strip comments and strings for tag, start, end, sublist in tags: if tag in ("string", "comment"): cleanline = cleanline + line[last:start] last = end cleanline = cleanline + line[last:] cleanline = string.strip(cleanline) if cleanline and cleanline[-1] == ":": tabcount = tabcount + 1 else: # extra indent after unbalanced (, [ or { for open, close in (("(", ")"), ("[", "]"), ("{", "}")): count = string.count(cleanline, open) if count and count > string.count(cleanline, close): tabcount = tabcount + 2 break return tabcount
def py2html(source): f = open(source) text = f.read() f.close() tags = PyFontify.fontify(text) done = 0 chunks = [] for tag, start, end, sublist in tags: chunks.append(escape_html(text[done:start])) chunks.append(formats[tag] % escape_html(text[start:end])) done = end chunks.append(escape_html(text[done:])) dict = { 'source' : source, 'target' : ''.join(chunks) } f = open(source + '.html', 'w') f.write(pattern % dict) f.close()
def py2html(source): f = open(source) text = f.read() f.close() tags = PyFontify.fontify(text) done = 0 chunks = [] for tag, start, end, sublist in tags: chunks.append(escape_html(text[done:start])) chunks.append(formats[tag] % escape_html(text[start:end])) done = end chunks.append(escape_html(text[done:])) dict = {'source': source, 'target': ''.join(chunks)} f = open(source + '.html', 'w') f.write(pattern % dict) f.close()
def fontify(self, start=0, end=None): #W.SetCursor('watch') if self.readonly: self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 0) self.ted.WEFeatureFlag(WASTEconst.weFOutlineHilite, 0) self.ted.WEDeactivate() self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 0) self.ted.WEFeatureFlag(WASTEconst.weFUndo, 0) pytext = self.get().replace("\r", "\n") if end is None: end = len(pytext) else: end = min(end, len(pytext)) selstart, selend = self.ted.WEGetSelection() self.ted.WESetSelection(start, end) self.ted.WESetStyle(WASTEconst.weDoFace | WASTEconst.weDoColor, (0, 0, 12, (0, 0, 0))) tags = PyFontify.fontify(pytext, start, end) styles = { 'string': (WASTEconst.weDoColor, (0, 0, 0, kStringColor)), 'keyword': (WASTEconst.weDoFace, (0, 1, 0, (0, 0, 0))), 'comment': (WASTEconst.weDoFace | WASTEconst.weDoColor, (0, 0, 0, kCommentColor)), 'identifier': (WASTEconst.weDoColor, (0, 0, 0, (0xbfff, 0, 0))) } setselection = self.ted.WESetSelection setstyle = self.ted.WESetStyle for tag, start, end, sublist in tags: setselection(start, end) mode, style = styles[tag] setstyle(mode, style) self.ted.WESetSelection(selstart, selend) self.SetPort() self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 1) self.ted.WEFeatureFlag(WASTEconst.weFUndo, 1) self.ted.WEActivate() self.ted.WEFeatureFlag(WASTEconst.weFOutlineHilite, 1) if self.readonly: self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 1)
def fontify(self, start=0, end=None): # W.SetCursor('watch') if self.readonly: self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 0) self.ted.WEFeatureFlag(WASTEconst.weFOutlineHilite, 0) self.ted.WEDeactivate() self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 0) self.ted.WEFeatureFlag(WASTEconst.weFUndo, 0) pytext = self.get().replace("\r", "\n") if end is None: end = len(pytext) else: end = min(end, len(pytext)) selstart, selend = self.ted.WEGetSelection() self.ted.WESetSelection(start, end) self.ted.WESetStyle(WASTEconst.weDoFace | WASTEconst.weDoColor, (0, 0, 12, (0, 0, 0))) tags = PyFontify.fontify(pytext, start, end) styles = { "string": (WASTEconst.weDoColor, (0, 0, 0, kStringColor)), "keyword": (WASTEconst.weDoFace, (0, 1, 0, (0, 0, 0))), "comment": (WASTEconst.weDoFace | WASTEconst.weDoColor, (0, 0, 0, kCommentColor)), "identifier": (WASTEconst.weDoColor, (0, 0, 0, (0xBFFF, 0, 0))), } setselection = self.ted.WESetSelection setstyle = self.ted.WESetStyle for tag, start, end, sublist in tags: setselection(start, end) mode, style = styles[tag] setstyle(mode, style) self.ted.WESetSelection(selstart, selend) self.SetPort() self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 1) self.ted.WEFeatureFlag(WASTEconst.weFUndo, 1) self.ted.WEActivate() self.ted.WEFeatureFlag(WASTEconst.weFOutlineHilite, 1) if self.readonly: self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 1)
### testing import sys # open file text = open(sys.argv[-1]).read() print 'read',len(text),'bytes...' taglist = [] t = TextTools._timer() # parse file t.start() result, taglist, next = tag(text,python_script,0,len(text),taglist) print 'tag:',t.stop(),'sec.' # run against Just's PyFontify if available try: import PyFontify # URL: ftp://starship.python.net/pub/crew/just/PyFontify.py t.start() PyFontify.fontify(text) print 'PyFontify:',t.stop(),'sec.' except ImportError: pass print 'hit return...' raw_input() print result if result: print_tags(text,taglist)
### testing import sys # open file text = open(sys.argv[-1]).read() print 'read', len(text), 'bytes...' taglist = [] t = TextTools._timer() # parse file t.start() result, taglist, next = tag(text, python_script, 0, len(text), taglist) print 'tag:', t.stop(), 'sec.' # run against Just's PyFontify if available try: import PyFontify # URL: ftp://starship.python.net/pub/crew/just/PyFontify.py t.start() PyFontify.fontify(text) print 'PyFontify:', t.stop(), 'sec.' except ImportError: pass print 'hit return...' raw_input() print result if result: print_tags(text, taglist)