more development to coloring
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
# Python library for ansi colorization
|
# Python library for ansi colorization
|
||||||
|
import re
|
||||||
|
|
||||||
class ansi:
|
class ansi:
|
||||||
K="\033[1;30m"
|
K="\033[1;30m"
|
||||||
@@ -29,14 +30,21 @@ class ansi:
|
|||||||
bw="\033[47m"
|
bw="\033[47m"
|
||||||
|
|
||||||
S = '\033[1m'#strong
|
S = '\033[1m'#strong
|
||||||
|
s = '\033[2m'#strong off
|
||||||
U = '\033[4m'#underline
|
U = '\033[4m'#underline
|
||||||
|
u = '\033[24m'#underline off
|
||||||
Z = '\033[0m'#zero colors
|
Z = '\033[0m'#zero colors
|
||||||
|
ic = '\033[7m'#inverse colors
|
||||||
|
io = '\033[27m'#inverse off
|
||||||
|
st = '\033[9m'#strike on
|
||||||
|
so = '\033[29m'#strike off
|
||||||
CLRLIN = '\033[2J'
|
CLRLIN = '\033[2J'
|
||||||
CLREND = '\033[K'
|
CLREND = '\033[K'
|
||||||
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,U,Z,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,CLRSCR".split(",")
|
||||||
color_list=[K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,U,Z,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,CLRSCR]
|
||||||
|
custom_match=re.compile(r'(\${)([0-9;]*[ABCDEFGHJKSTfminsu]+)(})')
|
||||||
|
|
||||||
def pos(self,y,x):
|
def pos(self,y,x):
|
||||||
return "\033["+str(y)+";"+str(x)+"H"
|
return "\033["+str(y)+";"+str(x)+"H"
|
||||||
@@ -52,9 +60,12 @@ class ansi:
|
|||||||
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):
|
||||||
s=s.replace("${"+c+"}",self.color_list[i])
|
s=s.replace("${"+c+"}",self.color_list[i])
|
||||||
return s
|
return self.custom_color(s)
|
||||||
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 s
|
||||||
|
|
||||||
|
def custom_color(self,s):
|
||||||
|
return self.custom_match.sub('\033[\\2',s)
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import bc
|
|||||||
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
|
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
|
||||||
__version__ = "0.1"
|
__version__ = "0.1"
|
||||||
|
|
||||||
|
''' Rules modified from mistune project '''
|
||||||
|
|
||||||
def setup_options():
|
def setup_options():
|
||||||
''' Create command line options '''
|
''' Create command line options '''
|
||||||
@@ -32,23 +33,29 @@ Special syntaxes:
|
|||||||
return opts
|
return opts
|
||||||
|
|
||||||
block_match={
|
block_match={
|
||||||
'block_code': (re.compile(r'^( {4}[^*])(.*)$'),'{}\\1\\2','${c}'),
|
'block_code': (re.compile(r'^( {4}[^*])(.*)$'),'${c}\\1\\2','${Z}${c}'),
|
||||||
'multiline_code' : (re.compile(r'^ *(`{3,}|~{3,}) *(\S*)?'),'${c}\\1\\2','${c}'), # ```lang
|
'multiline_code' : (re.compile(r'^ *(`{3,}|~{3,}) *(\S*)?'),'${c}\\1\\2','${Z}${c}'), # ```lang
|
||||||
'block_quote': (re.compile(r'^ *>'),False),
|
'block_quote': (re.compile(r'^(>[ >]* )'),'${K}\\1${Z}','${Z}'),
|
||||||
|
|
||||||
'hrule': (re.compile(r'^ {0,3}[-*_]([-*_]){2,}$'),False),
|
'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 \\2','${W}'),
|
||||||
'lheading' : (re.compile(r'^(=+|-+)$'),'${W}\\1','${W}'),
|
'lheading' : (re.compile(r'^(=+|-+)$'),'${W}\\1','${W}'),
|
||||||
'list_bullet':(re.compile(r'^( *)([*+-]|\d+\.)( +)'),'\\1${y}\\2${Z}\\3'),
|
'list_bullet':(re.compile(r'^( *)([*+-]|\d+\.)( +)'),'\\1${y}\\2${Z}\\3','${Z}'),
|
||||||
'text': (re.compile(r'^[^\n]+'),False),
|
'text': (re.compile(r'^[^\n]+'),False,'${Z}'),
|
||||||
'empty': (re.compile(r'^$'),False)
|
'empty': (re.compile(r'^$'),False,'${Z}'),
|
||||||
}
|
}
|
||||||
blocks=['block_code', 'block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'text', 'empty']
|
blocks=['block_code', 'block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'text', 'empty']
|
||||||
|
|
||||||
inline_match={
|
inline_match={
|
||||||
'code': (re.compile(r'(`+)([^`]+)(`+)'),'${c}\\1\\2\\3') # `code`
|
'bold1': (re.compile(r'(^| )(_[^_]+_)'), '${W}\\1\\2'), # _bold_
|
||||||
|
'bold2': (re.compile(r'(^| )(\*{1,2}[^\*]+\*{1,2})'), '${W}\\1\\2'), # **bold**
|
||||||
|
'code': (re.compile(r'([`]+[^`]+[`]+)'),'${c}\\1'), # `code`
|
||||||
|
'link': (re.compile(r'(\[[^\]]+\]\([^\)]+\))'),'${B}${U}\\1'), # [text](link)
|
||||||
|
'image': (re.compile(r'(!\[[^\]]+\]\([^\)]+\))'),'${r}\\1'), # 
|
||||||
|
'underline': (re.compile(r'(^|\W)(__)([^_]+)(__)'), '\\1\\2${U}\\3${Z}\\4'), # __underline__
|
||||||
|
'strikethrough': (re.compile(r'(~~)([^~]+)(~~)'),'\\1${st}\\2${so}\\3'), # ~~strike~~
|
||||||
}
|
}
|
||||||
inlines=['code']
|
inlines=['bold1','bold2','code','image','link','underline','strikethrough']
|
||||||
|
|
||||||
|
|
||||||
bc=bc.ansi()
|
bc=bc.ansi()
|
||||||
@@ -80,12 +87,14 @@ for row in f:
|
|||||||
if not multiline_block:
|
if not multiline_block:
|
||||||
sys.stdout.write(bc.Z)
|
sys.stdout.write(bc.Z)
|
||||||
#print(multiline_block)
|
#print(multiline_block)
|
||||||
#print(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)
|
||||||
for match in inlines:
|
if not (multiline_block or match=='block_code'):
|
||||||
if inline_match[match][0].match(row):
|
for match in inlines:
|
||||||
pass
|
if inline_match[match][0].search(row):
|
||||||
|
row=inline_match[match][0].sub(inline_match[match][1]+block_match[block][2],row)
|
||||||
|
|
||||||
#print(row)
|
#print(row)
|
||||||
if opts.color:
|
if opts.color:
|
||||||
colored=bc.color_string(row)
|
colored=bc.color_string(row)
|
||||||
|
|||||||
Reference in New Issue
Block a user