#!/usr/bin/env python # (works with either Python 2 or Python 3) # TermLayout v0.15 (c) 2014-15,2020,2023-24 Silas S. Brown # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # If you want to compare this code to old versions, the old # versions are being kept in the E-GuideDog SVN repository on # http://svn.code.sf.net/p/e-guidedog/code/ssb22/adjuster/ # and on GitHub at https://github.com/ssb22/adjuster # and on GitLab at https://gitlab.com/ssb22/adjuster # and on BitBucket https://bitbucket.org/ssb22/adjuster # and at https://gitlab.developers.cam.ac.uk/ssb22/adjuster # and in China: https://gitee.com/ssb22/adjuster import re, unicodedata, os, sys if type("")==type(u""): # Python 3 unichr,unicode,xrange = chr,str,range from functools import reduce class ANSIfiedText: "Small piece of text with its own ANSI attributes, which is self-contained so can be moved around into other contexts as needed. Should not change attributes mid-text though." def __init__(self,txt,attrList): "text, and ordered list of ANSI attribute sequences. If any of the attribute sequences change colour, only the last colour is kept. After this, colour is thrown away if it's black or white, since this is often used for terminal backgrounds." # TODO: customise which colours are thrown away? self.txt = txt self.attrList = [] ; prevColour = None for a in attrList: if any(a in ("\x1b[%dm"%i) for i in range(30,38)): # it's a colour change if prevColour: self.attrList.remove(prevColour) prevColour = None if a in ["\x1b[30m","\x1b[37m"]: continue # leave us as the terminal's 'normal' colour if we're supposed to be black or white prevColour = a self.attrList.append(a) def __repr__(self): return 'a(%s)' % repr(self.txt) # for debugging def printUsingState(self,curAttrList): "Returns the string needed to change any necessary ANSI codes and print this text, updating curAttrList in-place" ret = [] if set(curAttrList) > set(self.attrList): # there's attributes out there we don't want ret.append("\x1b[0m") # reset the state ret += self.attrList # and print all our own while curAttrList: del curAttrList[0] for x in self.attrList: curAttrList.append(x) else: for a in self.attrList: if not a in curAttrList: curAttrList.append(a) ret.append(a) return "".join(ret) + self.txt class ANSIfiedLineDrawing(ANSIfiedText): "Derivative of ANSIfiedText meant for simple ASCII art line drawing. ANSI codes are substituted for the ASCII during draw, but internally the ASCII-only version is kept (so stripANSI below results in ASCII lines). Use characters - | for lines, / \\ L J for corners, and T U ] [ + for intersections (U is upside-down T, and ] and [ are T's with the bargoing to left or right - sorry but it's only ASCII)." def printUsingState(self,curAttrList): oldTxt = self.txt self.txt = '\x1b(0' + str(self.txt).translate(ansiLineTable) + '\x1b(B' ret = ANSIfiedText.printUsingState(self,curAttrList) self.txt = oldTxt ; return ret ansiLineTable = [chr(c) for c in range(256)] for p,q in {'-':'q', '|':'x', '/':'l', '\\':'k', 'L':'m', 'J':'j', 'T':'w', 'U':'v', '[':'t', ']':'u', '+':'n' }.items(): ansiLineTable[ord(p)]=q ansiLineTable = "".join(ansiLineTable) class ANSIfiedNewline: #"Since we don't have to care about ANSI attributes when outputting a newline, this will not try to change them" #def printUsingState(self,curAttrList): return "\n" "actually we'd better reset all ANSI attributes on each new line, for use with a pager like 'less'" def printUsingState(self,curAttrList): if curAttrList: while curAttrList: del curAttrList[0] return "\x1b[0m\n" else: return "\n" class ANSIfiedSpacer: "A 'spacer' object that prints spaces without attributes, but does not bother to clear attributes that wouldn't show up on spaces anyway" def __init__(self,numSpaces=1): self.ns = numSpaces def __repr__(self): return 's(%d)' % self.ns # for debugging def printUsingState(self,curAttrList): ret = "" if "\x1b[4m" in curAttrList: # oops, underline is set - we don't want that for our spacer. (TODO: check for background colours also, if adding them) ret = "\x1b[0m" # reset the state while curAttrList: del curAttrList[0] return ret + " "*self.ns def ansifiedLineLen(atList): s = 0 for a in atList: if hasattr(a,'ns'): s += a.ns elif hasattr(a,'txt'): s += widthOf(a.txt) return s def mergeAnsifiedTexts(atList,stripANSI=False): "Assembles a complete list of ANSIfiedText objects for final output" curAttrList = [] ; ret = [] for a in atList: if stripANSI and hasattr(a,'txt'): ret.append(a.txt) else: ret += a.printUsingState(curAttrList) if curAttrList: ret.append("\x1b[0m") # final reset return "".join(ret) def mergeAnsifiedLines(atListList,stripANSI=False): "Assembles a complete list of lists of ANSIfiedText objects, one per line, for final output with newlines between. Oblivious to the wrapping. Strips trailing ANSIfiedSpacer objects at the end of each line." r = [] ; anl = ANSIfiedNewline() # 1 instance justHadBlankLine = False # collapse any mult. blanks for atList in atListList: for i in range(len(atList),-1,-1): if i==0 or not (atList[i-1].__class__ == ANSIfiedSpacer or not atList[i-1].txt.strip()): break if i: r += atList[:i] ; justHadBlankLine = False else: if justHadBlankLine: continue # no anl justHadBlankLine = True r.append(anl) return mergeAnsifiedTexts(r,stripANSI) class Strut: def __init__(self,direction='x',length=0): self.direction = direction self.length = length ; self.width = 0 def getSize(self,direction): if self.direction == direction: return self.length else: return self.width def padToSize(self,direction='x',desired=0,align='c', absoluteMaximum = None): if self.direction == direction: self.length = max(desired,self.length) else: self.width = max(desired,self.width) def getLine(self,N): if self.direction=='x': numSpaces = self.length else: numSpaces = self.width if numSpaces: return [ANSIfiedSpacer(numSpaces)] else: return [] def canSubstituteLineBreak(self): return True def isHardLineBreak(self): return False def __repr__(self): # for debugging r = 'S'+self.direction if self.length or self.width: r += "%d" % self.length if self.width: r += "(w%d)" % self.width return r class StackingRectangle: def __init__(self,stackingDir='y',initItems=None,name=""): if not initItems: initItems = [] self.items = initItems self.stackingDir = stackingDir self.name = name # for debugging & introspection def getSize(self,direction='x'): if direction==self.stackingDir: func = sum else: if not self.items: return 0 func = max return func(i.getSize(direction) for i in self.items) def padToSize(self,direction='x',desired=0,align='c', absoluteMaximum = None): if direction == self.stackingDir: if desired: before,after = centreCalc( self.getSize(direction),desired,align) if before: self.items.insert(0,Strut(direction,before)) if after: self.items.append(Strut(direction,after)) # else we can't make it any bigger than the existing size, but anyway just check everything inside's "padded out" (so we can put 'x' and 'y' stacks together in any order without worrying about strict alternation). E.g. a collection of cols is told "make all your rows as wide as your widest", so says "I'm cols not rows, but I can ask any collections of rows inside to make sure each of THEIR rows is as wide as the collection" for r in self.items: r.padToSize(direction,0,align,absoluteMaximum) else: if not desired: # make all the size of largest desired = self.getSize(direction) if absoluteMaximum: # if one item exceeds absoluteMaximum, don't make the others exceed it also desired=min(desired, absoluteMaximum) for r in self.items: r.padToSize(direction,desired,align) def tabulate(self): "Pad the children of children as in a table. Assumes each cell takes exactly 1 row or column and there are no borders except a gap between columns. For more complex tables (borders, rowspan/colspan etc), please use an XYGrid instead, which automatically tabulates (although it's probably slower than the more simplistic approach used here, and the simpler version is also more flexible at reflowing overly-wide tables)." rows = [i for i in self.items if not i.__class__ == Strut] numCols = max(len([c for c in r.items if not c.__class__ == Strut]) for r in rows) ; numRows = len(rows) if self.stackingDir=='y': colWidthDir='x' else: colWidthDir='y' # (and var names all wrong) def xy(x,y): its = rows[y].items ; i = 0 while True: while i < len(its) and its[i].__class__ == Strut: i+=1 if i == len(its): return Strut() # size 0 if not x: return its[i] x -= 1 ; i+=1 for colNo in xrange(numCols): wid = max(xy(colNo,r).getSize(colWidthDir) for r in xrange(numRows)) if colNo < numCols-1 and colWidthDir=='x': wid += 1 # gap between cols for r in xrange(numRows): xy(colNo,r).padToSize(colWidthDir,wid,'top') # TODO: currently an XYGrid is used if we have align= or valign= , but we could probably get those attributes here (but would also need to make sure that any 'docs' contained in the cell have been padded using it) for rowNo in xrange(numRows): hi = max(xy(c,rowNo).getSize(self.stackingDir) for c in xrange(numCols)) if rowNo < numRows-1 and self.stackingDir=='x': hi += 1 # gap between cols for c in xrange(numCols): xy(c,rowNo).padToSize(self.stackingDir,hi,'top') # TODO: alignment as above TODO (currently handled by XYGrid) def lineBreakAndPadLeading(self,maxSize,ourDirectionRunsFromEnd=False,orthogonalDirectionRunsFromEnd=False,baselineAlign='bottom',callback=None): # ( 'bottom' = 'right' for columns) "When processing a stack of lines (or, for vertical text, a bunch of columns), interprets hard line breaks, adds soft line breaks, and ensures all lines are consistent 'height' (or width if they are columns), using baselineAlign to align any that were out of place. Flags: False,False for a StackingRectangle('y') containing StackingRectangle('x') lines with left-to-right top-to-bottom line wrapping; True,False for a StackingRectangle('x') containing StackingRectangle('y') columns when we want to wrap in columns from right to left (and top to bottom within each column; True,True would give bottom to top), etc. It's assumed that all relevant immediate children are other StackingRectangles; if any of these have a direction the same as ours, we'll recurse on them. If callback is not None, we're the top-level document and callback is called to 'flush' lines from it as we go (so don't need to build up entire doc in memory before starting to print); in this case the document is left empty at the end." if callback: assert self.stackingDir=='y' _callback = callback if ourDirectionRunsFromEnd: self.items.reverse() ; callback = None if self.stackingDir=='y': wrappingDir = 'x' else: wrappingDir = 'y' i = 0 while i < len(self.items): if not self.items[i].__class__ == StackingRectangle: i += 1 ; continue if self.items[i].stackingDir == self.stackingDir: self.items[i].lineBreakAndPadLeading(maxSize,ourDirectionRunsFromEnd,orthogonalDirectionRunsFromEnd,baselineAlign,callback=callback) i += 1 ; continue if orthogonalDirectionRunsFromEnd: self.items[i].items.reverse() itemNo, breakAt, size = 0,None,0 for ii in self.items[i].items: if ii.isHardLineBreak() or ii.canSubstituteLineBreak(): breakAt = itemNo size += ii.getSize(wrappingDir) if ii.isHardLineBreak() or (size > maxSize and (breakAt or itemNo)): if breakAt or ii.isHardLineBreak(): self.items[i].items, newItems = self.items[i].items[:breakAt], self.items[i].items[breakAt+1:] if not breakAt: self.items[i].items=[Strut(self.stackingDir,1)] # if a hard line-break left an empty line, make sure it has some height else: self.items[i].items, newItems = self.items[i].items[:itemNo], self.items[i].items[itemNo:] # "emergency mode" for very long words - break just before the item we just looked at, even though no breakpoint nsr = StackingRectangle(wrappingDir,name='continuation') if orthogonalDirectionRunsFromEnd: newItems.reverse() # undo damage nsr.items = newItems self.items.insert(i+1,nsr) break itemNo += 1 if orthogonalDirectionRunsFromEnd: self.items[i].items.reverse() # undo damage for ii in self.items[i].items[:]: if not ii.getSize(wrappingDir): self.items[i].items.remove(ii) # (no point keeping 0-width spaces and things around once the line has been wrapped) self.items[i].padToSize(self.stackingDir,align=baselineAlign) # (if horizontal wrap, pad-Y makes sure all cols on the items[i] row are the same height; if vertical wrap, similar for width) for it in self.items[i].items: it.padToSize(wrappingDir) # (so any extra 'rows' we just added to those 'columns' are padded out as well) # note however that we DON'T pad our ROWS to size in wrappingDir (that's done separately if centering etc is desired) i += 1 if callback: self.items,it0 = self.items[:i],self.items[i:] callback(self.getLines()) self.items = it0 ; i = 0 if ourDirectionRunsFromEnd: self.items.reverse() if _callback and self.items: _callback(self.getLines()) self.items = [] def getLines(self): "assumes all needed padding is already done" return [self.getLine(N) for N in range(self.getSize('y'))] def getLine(self,N): if self.stackingDir == 'x': return reduce(lambda a,b:a+b, (i.getLine(N) for i in self.items)) if N < 0: return [] lNo = 0 for r in self.items: l0,lNo = lNo,lNo+r.getSize('y') if lNo > N: return r.getLine(N-l0) return [] def canSubstituteLineBreak(self): if not self.items: return False # empty container boxes are not necessarily valid line-break points return all( i.canSubstituteLineBreak() for i in self.items) def isHardLineBreak(self): items = [i for i in self.items if not i.__class__ == Strut] if not items: return False return all(i.isHardLineBreak() for i in items) def isConsistent(self,seenList=None): # for debugging if not seenList: seenList = [] if self in seenList: return False seenList.append(self) return all(x.isConsistent(seenList) for x in self.items if x.__class__ == StackingRectangle) def __repr__(self): # for debugging if self.stackingDir=='x': r = 'cols' else: r = 'rows' if self.name: r += '('+self.name+')' return "%d %s %s" % (self.getSize(self.stackingDir),r,repr(self.items)) class XYGrid: def __init__(self,drawBorder): self.items = {} # (x,y) : ((colspan,rowspan),item) self.drawBorder = drawBorder def getNumRowsOrCols(self,axis): return max(k[axis]+v[0][axis] for k,v in self.items.iteritems()) def getSizeOfRowOrCol(self,N,nEnd=None,direction='x'): "Returns the size of a particular row or column (or range of them), not including borders (except does include internal borders when returning a range)" # If there's a border of size 1, the amount of space a cell contributes to each of its columns is (cell size - (colspan-1)) / colspan, but taking care to ensure that surplus space is allocated if this does not result in an integer (TODO: this need be only the average and it doesn't have to contribute equally to all) spaceForBorder = (direction=='x') # TODO: or True if we want line borders in Y direction also if not nEnd: nEnd = N+1 def contribOf(itemSize,span,makeUp): if spaceForBorder: itemSize += 1-span # subtract some which will be provided anyway by internal borders of other cells (and is added in below) each = int(itemSize/span) if makeUp: return itemSize-(each*(span-1)) else: return each if direction=='x': axis = 0 else: axis = 1 if spaceForBorder: border = nEnd-N-1 # the internal borders in the range else: border = 0 return border + sum(max([0]+[contribOf(v[1].getSize(direction),v[0][axis],n == k[axis]+v[0][axis]-1) for k,v in self.items.iteritems() if k[axis] <= n < k[axis]+v[0][axis]]) for n in xrange(N,nEnd)) def getSize(self,direction='x'): if direction=='x': axis = 0 else: axis = 1 return self.getSizeOfRowOrCol(0,self.getNumRowsOrCols(axis),direction) # TODO: +2 if external borders def padToSize(self,direction='x',desired=0,align='c', absoluteMaximum = None): "XYGrid itself doesn't pad - it expects to be placed in containers that deal with padding" def getLine(self,N): if N < 0: return [] rowsSoFar = [0] # start line of table row N for row in xrange(self.getNumRowsOrCols(1)): rowsSoFar.append(self.getSizeOfRowOrCol(row,direction='y')+rowsSoFar[-1]) # TODO: if drawing borders between rows, may need another +1 somewhere (and +1 on first if top border also), plus detect when we're on a border line and draw it (except where rowspan is in effect) if rowsSoFar[-1] > N: # it's this row cells = range(self.getNumRowsOrCols(0)) for k,v in self.items.iteritems(): if not k[1] <= row < k[1]+v[0][1]: continue # wrong row assert cells[k[0]:k[0]+v[0][0]] == range(k[0],k[0]+v[0][0]), "overlapping cells!" if hasattr(v[1],'valign'): valign = v[1].valign else: valign='top' itsLine = N-rowsSoFar[k[1]] if not valign=='top': itsLine -= centreCalc(v[1].getSize('y'),rowsSoFar[k[1]+v[0][1]]-rowsSoFar[k[1]],valign)[0] cells[k[0]] = v[1].getLine(itsLine) for o in xrange(k[0]+1,k[0]+v[0][0]): cells[o] = None cellWid = ansifiedLineLen(cells[k[0]]) needWid = self.getSizeOfRowOrCol(k[0],k[0]+v[0][0],direction='x') if needWid > cellWid: if hasattr(v[1],'align'): align = v[1].align else: align='left' before,after = centreCalc(cellWid,needWid,align) if before: cells[k[0]].insert(0,ANSIfiedSpacer(before)) if after: cells[k[0]].append(ANSIfiedSpacer(after)) else: assert needWid==cellWid, "getSizeOfRowOrCol(%d:%d) returned %d but actual cell width %d" % (k[0],k[0]+v[0][0],needWid,cellWid) for i in xrange(len(cells)): if type(cells[i]) == int: cells[i] = [ANSIfiedSpacer(self.getSizeOfRowOrCol(i))] if self.drawBorder: mid = [ANSIfiedLineDrawing('|',[])] # TODO: more complex if drawing horizontal borders also: it might need to intersect them; TODO: external borders before/after the table? else: mid = [ANSIfiedSpacer(1)] return reduce(lambda a,b:a+mid+b, [x for x in cells if not x==None]) # (None = placeholder for colspans) return [] def canSubstituteLineBreak(self): return False def isHardLineBreak(self): return False def ANSIfiedTextToStackingRectangle(at): "Make an ANSIfiedText 'know' how to run getSize etc" width = widthOf(at.txt) if width: def getSize(direction='x'): if direction=='x': return width else: return 1 def getLine(N): return [at] # (can assume N==0 here, because the enclosing StackingRectangle('y') will handle any out-of-bounds requests) else: # zero-width space or something - don't output def getSize(_): return 0 def getLine(_): return [] at.getSize,at.getLine = getSize,getLine at.padToSize = lambda *args: True # no need to do anything because the surrounding objects will add struts if at.txt in lineBreakCharList: at.canSubstituteLineBreak = lambda *args: True else: at.canSubstituteLineBreak = lambda *args: False if at.txt in ['\n']: # TODO: more? at.isHardLineBreak = lambda *args: True else: at.isHardLineBreak = lambda *args: False return StackingRectangle('x',[StackingRectangle('y',[at])],name='obj') lineBreakCharList = [u' ',u'\u200b'] notAllowedToBreakHanziBefore = u'.,;?!:' notAllowedToBreakHanziBefore = u'[\u2019\u201d\u3001\u3002' + u''.join(unichr(x) for x in range(0x3009,0x3020,2)) + notAllowedToBreakHanziBefore + u''.join(unichr(ord(c)+0xfee0) for c in notAllowedToBreakHanziBefore) +u']' def textIntoWordsAndSpaces(text): text = re.sub(u'([\u4e00-\ua700])(?!'+notAllowedToBreakHanziBefore+u')',r'\1'+u'\u200b',text) # TODO: + kana ? i = 0 for w in re.finditer('|'.join(re.escape(w) for w in lineBreakCharList), text): if w.start() > i: yield text[i:w.start()] yield w.group() i = w.end() if i]*)( [^>]*)?>',inStr) if not tagName: return tStart = tagName.end() tagName = tagName.group(1).lower() ; numOpen = 0 if inStr[tStart-2]=='/' or tagName in ['br','hr']: # TODO: more self-closing tags? return tagName,tStart,tStart,tStart for t in re.finditer('(?i)]', inStr): # (ONLY the named tag, not any intervening ones. This won't work with tags like
  • , which are implicitly closed if another one starts with no intervening