common replace rule, new coloring library
This commit is contained in:
@@ -9,6 +9,7 @@ function helpexit() {
|
|||||||
echo 'Modes:'
|
echo 'Modes:'
|
||||||
echo ' -n to match non-ascii and non-printable characters, and replace to [arg1]'
|
echo ' -n to match non-ascii and non-printable characters, and replace to [arg1]'
|
||||||
echo ' -f to replace match [arg1] with format [arg2], ex: -f "[0-9]\+" "%04d"'
|
echo ' -f to replace match [arg1] with format [arg2], ex: -f "[0-9]\+" "%04d"'
|
||||||
|
echo ' -p to replace problematic characters [^\w()[]-.] with [arg1]'
|
||||||
|
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
@@ -39,6 +40,10 @@ do [[ "${!i}" = "-h" ]] && helpexit
|
|||||||
MODE=printf
|
MODE=printf
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
[[ "${!i}" = "-p" ]] && {
|
||||||
|
SRC='[^]\[0-9a-zA-Z_.()-]'
|
||||||
|
continue
|
||||||
|
}
|
||||||
[[ -z "$SRC" ]] && {
|
[[ -z "$SRC" ]] && {
|
||||||
SRC="${!i}"
|
SRC="${!i}"
|
||||||
continue
|
continue
|
||||||
|
|||||||
60
reporting/bc.py
Normal file
60
reporting/bc.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# Python library for ansi colorization
|
||||||
|
|
||||||
|
class ansi:
|
||||||
|
K="\033[1;30m"
|
||||||
|
R="\033[1;31m"
|
||||||
|
G="\033[1;32m"
|
||||||
|
B="\033[1;34m"
|
||||||
|
Y="\033[1;33m"
|
||||||
|
M="\033[1;35m"
|
||||||
|
C="\033[1;36m"
|
||||||
|
W="\033[1;37m"
|
||||||
|
|
||||||
|
k="\033[0;30m"
|
||||||
|
r="\033[0;31m"
|
||||||
|
g="\033[0;32m"
|
||||||
|
b="\033[0;34m"
|
||||||
|
y="\033[0;33m"
|
||||||
|
m="\033[0;35m"
|
||||||
|
c="\033[0;36m"
|
||||||
|
w="\033[0;37m"
|
||||||
|
|
||||||
|
bk="\033[40m"
|
||||||
|
br="\033[41m"
|
||||||
|
bg="\033[42m"
|
||||||
|
by="\033[43m"
|
||||||
|
bb="\033[44m"
|
||||||
|
bm="\033[45m"
|
||||||
|
bc="\033[46m"
|
||||||
|
bw="\033[47m"
|
||||||
|
|
||||||
|
S = '\033[1m'#strong
|
||||||
|
U = '\033[4m'#underline
|
||||||
|
Z = '\033[0m'#zero colors
|
||||||
|
CLRLIN = '\033[2J'
|
||||||
|
CLREND = '\033[K'
|
||||||
|
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_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]
|
||||||
|
|
||||||
|
def pos(self,y,x):
|
||||||
|
return "\033["+str(y)+";"+str(x)+"H"
|
||||||
|
|
||||||
|
def posprint(self, y,x,s):
|
||||||
|
sys.stdout.write( self.pos(y,x) + str(s) )
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
sys.stdout.write( self.CLR+self.pos(0,0) )
|
||||||
|
def clear_to_end(self):
|
||||||
|
sys.stdout.write( self.CLREND )
|
||||||
|
|
||||||
|
def color_string(self,s):
|
||||||
|
for i,c in enumerate(self.color_keys):
|
||||||
|
s=s.replace("${"+c+"}",self.color_list[i])
|
||||||
|
return s
|
||||||
|
def nocolor_string(self,s):
|
||||||
|
for i,c in enumerate(self.color_keys):
|
||||||
|
s=s.replace("${"+c+"}","")
|
||||||
|
return s
|
||||||
|
|
||||||
97
reporting/md-color.py
Executable file
97
reporting/md-color.py
Executable file
@@ -0,0 +1,97 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys,os,re
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
||||||
|
import bc
|
||||||
|
|
||||||
|
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
|
||||||
|
__version__ = "0.1"
|
||||||
|
|
||||||
|
|
||||||
|
def setup_options():
|
||||||
|
''' Create command line options '''
|
||||||
|
usage='''
|
||||||
|
Color notation renderer in ANSI codes
|
||||||
|
Special syntaxes:
|
||||||
|
* Colors: insert string ${C}, where C is one of KRGBYMCWkrgbymcwSUZ
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
parser=ArgumentParser(description=usage,
|
||||||
|
epilog=__author__)
|
||||||
|
|
||||||
|
parser.add_argument("-v","--version",action="version",version=__version__)
|
||||||
|
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
|
||||||
|
help="Disable color.")
|
||||||
|
parser.add_argument("-z",action="store_true",dest="zero",default=False,
|
||||||
|
help="Reset coloring at the end of each line.")
|
||||||
|
parser.add_argument("filename",type=str,
|
||||||
|
help="File to show, - for stdin")
|
||||||
|
opts=parser.parse_args()
|
||||||
|
return opts
|
||||||
|
|
||||||
|
block_match={
|
||||||
|
'block_code': (re.compile(r'^( {4}[^*])(.*)$'),'{}\\1\\2','${c}'),
|
||||||
|
'multiline_code' : (re.compile(r'^ *(`{3,}|~{3,}) *(\S*)?'),'${c}\\1\\2','${c}'), # ```lang
|
||||||
|
'block_quote': (re.compile(r'^ *>'),False),
|
||||||
|
|
||||||
|
'hrule': (re.compile(r'^ {0,3}[-*_]([-*_]){2,}$'),False),
|
||||||
|
'heading' : (re.compile(r'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)'),'${W}\\1 \\2','${W}'),
|
||||||
|
'lheading' : (re.compile(r'^(=+|-+)$'),'${W}\\1','${W}'),
|
||||||
|
'list_bullet':(re.compile(r'^( *)([*+-]|\d+\.)( +)'),'\\1${y}\\2${Z}\\3'),
|
||||||
|
'text': (re.compile(r'^[^\n]+'),False),
|
||||||
|
'empty': (re.compile(r'^$'),False)
|
||||||
|
}
|
||||||
|
blocks=['block_code', 'block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'text', 'empty']
|
||||||
|
|
||||||
|
inline_match={
|
||||||
|
'code': (re.compile(r'(`+)([^`]+)(`+)'),'${c}\\1\\2\\3') # `code`
|
||||||
|
}
|
||||||
|
inlines=['code']
|
||||||
|
|
||||||
|
|
||||||
|
bc=bc.ansi()
|
||||||
|
opts=setup_options()
|
||||||
|
if opts.filename=="-":
|
||||||
|
f=sys.stdin
|
||||||
|
else:
|
||||||
|
f=open(opts.filename,'r')
|
||||||
|
block='text'
|
||||||
|
new_block='text'
|
||||||
|
multiline_block=False
|
||||||
|
for row in f:
|
||||||
|
if not row:
|
||||||
|
continue
|
||||||
|
row=row.decode('utf-8').rstrip("\n\r ")
|
||||||
|
for match in blocks:
|
||||||
|
if block_match[match][0].match(row):
|
||||||
|
new_block=match
|
||||||
|
if match.startswith('multiline'):
|
||||||
|
if multiline_block:
|
||||||
|
multiline_block=False
|
||||||
|
else:
|
||||||
|
multiline_block=match
|
||||||
|
break
|
||||||
|
if multiline_block:
|
||||||
|
new_block=multiline_block
|
||||||
|
|
||||||
|
if new_block!=block:
|
||||||
|
block=new_block
|
||||||
|
if not multiline_block:
|
||||||
|
sys.stdout.write(bc.Z)
|
||||||
|
#print(multiline_block)
|
||||||
|
#print(block)
|
||||||
|
if block_match[block][1]:
|
||||||
|
row=block_match[block][0].sub(block_match[block][1],row)
|
||||||
|
#print(row)
|
||||||
|
if opts.color:
|
||||||
|
colored=bc.color_string(row)
|
||||||
|
else:
|
||||||
|
colored=bc.nocolor_string(row)
|
||||||
|
sys.stdout.write(colored.encode('utf-8'))
|
||||||
|
if opts.zero:
|
||||||
|
sys.stdout.write(bc.Z)
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user