still developing new md-coloring
This commit is contained in:
@@ -40,22 +40,50 @@ class ansi:
|
|||||||
so = '\033[29m'#strike off
|
so = '\033[29m'#strike off
|
||||||
CLRLIN = '\033[2J'
|
CLRLIN = '\033[2J'
|
||||||
CLREND = '\033[K'
|
CLREND = '\033[K'
|
||||||
|
CLRBEG = '\033[1K'
|
||||||
CLRSCR = CLRLIN+"\033[0;0H"
|
CLRSCR = CLRLIN+"\033[0;0H"
|
||||||
|
|
||||||
color_keys="K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLRLIN,CLREND,CLRSCR".split(",")
|
color_keys="K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLRLIN,CLREND,CLRBEG,CLRSCR".split(",")
|
||||||
color_list=[K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLRLIN,CLREND,CLRSCR]
|
color_list=[K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLRLIN,CLREND,CLRBEG,CLRSCR]
|
||||||
custom_match=re.compile(r'(\${)([0-9;]*[ABCDEFGHJKSTfminsu]+)(})')
|
custom_match=re.compile(r'(\${)([0-9;]*[ABCDEFGHJKSTfminsu]+)(})')
|
||||||
|
|
||||||
def pos(self,y,x):
|
def pos(self,y,x):
|
||||||
|
""" Go to absolute position """
|
||||||
return "\033["+str(y)+";"+str(x)+"H"
|
return "\033["+str(y)+";"+str(x)+"H"
|
||||||
|
def column(self,x):
|
||||||
|
""" Go to absolute column """
|
||||||
|
return "\033["+str(x)+"G"
|
||||||
|
|
||||||
def posprint(self, y,x,s):
|
def posprint(self, y,x,s):
|
||||||
|
""" Print string at a location """
|
||||||
sys.stdout.write( self.pos(y,x) + str(s) )
|
sys.stdout.write( self.pos(y,x) + str(s) )
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
sys.stdout.write( self.CLR+self.pos(0,0) )
|
sys.stdout.write( self.CLR+self.pos(0,0) )
|
||||||
def clear_to_end(self):
|
def clear_to_end(self):
|
||||||
sys.stdout.write( self.CLREND )
|
sys.stdout.write( self.CLREND )
|
||||||
|
def clear_to_beginning(self):
|
||||||
|
sys.stdout.write( self.CLRBEG )
|
||||||
|
|
||||||
|
def up(self,n=1):
|
||||||
|
sys.stdout.write( "\033["+str(n)+"A" )
|
||||||
|
def down(self,n=1):
|
||||||
|
sys.stdout.write( "\033["+str(n)+"B" )
|
||||||
|
def right(self,n=1):
|
||||||
|
sys.stdout.write( "\033["+str(n)+"C" )
|
||||||
|
def left(self,n=1):
|
||||||
|
sys.stdout.write( "\033["+str(n)+"D" )
|
||||||
|
def up_line(self,n=1):
|
||||||
|
sys.stdout.write( "\033["+str(n)+"F" )
|
||||||
|
def down_line(self,n=1):
|
||||||
|
sys.stdout.write( "\033["+str(n)+"E" )
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
""" Save cursor position """
|
||||||
|
sys.stdout.write( "\033[s" )
|
||||||
|
def restore(self):
|
||||||
|
""" Restore cursor position """
|
||||||
|
sys.stdout.write( "\033[u" )
|
||||||
|
|
||||||
def color_string(self,s):
|
def color_string(self,s):
|
||||||
for i,c in enumerate(self.color_keys):
|
for i,c in enumerate(self.color_keys):
|
||||||
@@ -64,8 +92,9 @@ class ansi:
|
|||||||
def nocolor_string(self,s):
|
def nocolor_string(self,s):
|
||||||
for i,c in enumerate(self.color_keys):
|
for i,c in enumerate(self.color_keys):
|
||||||
s=s.replace("${"+c+"}","")
|
s=s.replace("${"+c+"}","")
|
||||||
return s
|
return self.custom_nocolor(s)
|
||||||
|
|
||||||
def custom_color(self,s):
|
def custom_color(self,s):
|
||||||
return self.custom_match.sub('\033[\\2',s)
|
return self.custom_match.sub('\033[\\2',s)
|
||||||
|
def custom_nocolor(self,s):
|
||||||
|
return self.custom_match.sub('',s)
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ Special syntaxes:
|
|||||||
epilog=__author__)
|
epilog=__author__)
|
||||||
|
|
||||||
parser.add_argument("-v","--version",action="version",version=__version__)
|
parser.add_argument("-v","--version",action="version",version=__version__)
|
||||||
|
parser.add_argument("-D",action="store_true",dest="debug",default=False,
|
||||||
|
help="Debug mode")
|
||||||
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
|
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
|
||||||
help="Disable color.")
|
help="Disable color.")
|
||||||
parser.add_argument("-z",action="store_true",dest="zero",default=False,
|
parser.add_argument("-z",action="store_true",dest="zero",default=False,
|
||||||
@@ -38,13 +40,14 @@ block_match={
|
|||||||
'block_quote': (re.compile(r'^(>[ >]* )'),'${K}\\1${Z}','${Z}'),
|
'block_quote': (re.compile(r'^(>[ >]* )'),'${K}\\1${Z}','${Z}'),
|
||||||
|
|
||||||
'hrule': (re.compile(r'^ {0,3}[-*_]([-*_]){2,}$'),False,'${Z}'),
|
'hrule': (re.compile(r'^ {0,3}[-*_]([-*_]){2,}$'),False,'${Z}'),
|
||||||
'heading' : (re.compile(r'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)'),'${W}\\1 \\2','${W}'),
|
'heading' : (re.compile(r'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)'),'${W}\\1 ${U}\\2','${W}${U}'),
|
||||||
'lheading' : (re.compile(r'^(=+|-+)$'),'${W}\\1','${W}'),
|
'lheading' : (re.compile(r'^(=+|-+)$'),'${W}\\1','${W}',(-1,re.compile(r'^([^\n]+)'),'${W}\\1')),
|
||||||
'list_bullet':(re.compile(r'^( *)([*+-]|\d+\.)( +)'),'\\1${y}\\2${Z}\\3','${Z}'),
|
'list_bullet':(re.compile(r'^( *)([*+-]|[\d\.]+)( +)'),'\\1${y}\\2${Z}\\3','${Z}'),
|
||||||
|
'list_loose': (None,False,'${Z}'),
|
||||||
'text': (re.compile(r'^[^\n]+'),False,'${Z}'),
|
'text': (re.compile(r'^[^\n]+'),False,'${Z}'),
|
||||||
'empty': (re.compile(r'^$'),False,'${Z}'),
|
'empty': (re.compile(r'^$'),False,'${Z}'),
|
||||||
}
|
}
|
||||||
blocks=['block_code', 'block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'text', 'empty']
|
blocks=['block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'block_code', 'text', 'empty']
|
||||||
|
|
||||||
inline_match={
|
inline_match={
|
||||||
'bold1': (re.compile(r'(^| )(_[^_]+_)'), '${W}\\1\\2'), # _bold_
|
'bold1': (re.compile(r'(^| )(_[^_]+_)'), '${W}\\1\\2'), # _bold_
|
||||||
@@ -64,13 +67,23 @@ if opts.filename=="-":
|
|||||||
f=sys.stdin
|
f=sys.stdin
|
||||||
else:
|
else:
|
||||||
f=open(opts.filename,'r')
|
f=open(opts.filename,'r')
|
||||||
block='text'
|
data=[]
|
||||||
new_block='text'
|
# Read data
|
||||||
multiline_block=False
|
|
||||||
for row in f:
|
for row in f:
|
||||||
if not row:
|
if not row:
|
||||||
continue
|
continue
|
||||||
row=row.decode('utf-8').rstrip("\n\r ")
|
row=row.decode('utf-8').rstrip("\n\r ")
|
||||||
|
data.append([None,row])
|
||||||
|
|
||||||
|
block='text'
|
||||||
|
new_block='text'
|
||||||
|
multiline_block=False
|
||||||
|
# Parse styles
|
||||||
|
for i,line in enumerate(data):
|
||||||
|
row=line[1]
|
||||||
|
if line[0] is not None:
|
||||||
|
# Previous lines have set the style already
|
||||||
|
continue
|
||||||
for match in blocks:
|
for match in blocks:
|
||||||
if block_match[match][0].match(row):
|
if block_match[match][0].match(row):
|
||||||
new_block=match
|
new_block=match
|
||||||
@@ -81,25 +94,41 @@ for row in f:
|
|||||||
multiline_block=match
|
multiline_block=match
|
||||||
break
|
break
|
||||||
if multiline_block:
|
if multiline_block:
|
||||||
new_block=multiline_block
|
new_block=multiline_block
|
||||||
|
# Lists must end with empty line
|
||||||
|
if new_block not in ('empty','list_bullet') and block.startswith('list_'):
|
||||||
|
new_block='list_loose'
|
||||||
|
|
||||||
|
if len(block_match[match])>3:
|
||||||
|
# Style sets block in previous or next lines
|
||||||
|
data[i+block_match[match][3][0]][0]=new_block
|
||||||
|
data[i+block_match[match][3][0]][1]=block_match[match][3][1].sub(
|
||||||
|
block_match[match][3][2], data[i+block_match[match][3][0]][1])
|
||||||
|
data[i][0]=new_block
|
||||||
if new_block!=block:
|
if new_block!=block:
|
||||||
block=new_block
|
block=new_block
|
||||||
if not multiline_block:
|
|
||||||
sys.stdout.write(bc.Z)
|
# Start inserting colors, and printing
|
||||||
|
for i,line in enumerate(data):
|
||||||
|
row=line[1]
|
||||||
|
block=line[0]
|
||||||
|
multiline_block=block.startswith('multiline')
|
||||||
|
if not multiline_block:
|
||||||
|
sys.stdout.write(bc.Z)
|
||||||
#print(multiline_block)
|
#print(multiline_block)
|
||||||
#~ print(block)
|
|
||||||
if block_match[block][1]:
|
if block_match[block][1]:
|
||||||
row=block_match[block][0].sub(block_match[block][1],row)
|
row=block_match[block][0].sub(block_match[block][1],row)
|
||||||
if not (multiline_block or match=='block_code'):
|
if not (multiline_block or match=='block_code'):
|
||||||
for match in inlines:
|
for match in inlines:
|
||||||
if inline_match[match][0].search(row):
|
if inline_match[match][0].search(row):
|
||||||
row=inline_match[match][0].sub(inline_match[match][1]+block_match[block][2],row)
|
row=inline_match[match][0].sub(inline_match[match][1]+block_match[block][2],row)
|
||||||
|
|
||||||
#print(row)
|
|
||||||
if opts.color:
|
if opts.color:
|
||||||
colored=bc.color_string(row)
|
colored=bc.color_string(row)
|
||||||
else:
|
else:
|
||||||
colored=bc.nocolor_string(row)
|
colored=bc.nocolor_string(row)
|
||||||
|
if opts.debug:
|
||||||
|
multistr="*" if multiline_block else " "
|
||||||
|
colored="{:<18}{:}:".format(data[i][0],multistr)+colored
|
||||||
sys.stdout.write(colored.encode('utf-8'))
|
sys.stdout.write(colored.encode('utf-8'))
|
||||||
if opts.zero:
|
if opts.zero:
|
||||||
sys.stdout.write(bc.Z)
|
sys.stdout.write(bc.Z)
|
||||||
|
|||||||
Reference in New Issue
Block a user