markslider python3 upgrades
This commit is contained in:
@@ -8,21 +8,23 @@ import ansi
|
||||
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
|
||||
__version__ = "0.3"
|
||||
|
||||
''' Rules modified from mistune project '''
|
||||
''' Rules modified from mistune project '''
|
||||
|
||||
def setup_options():
|
||||
bc=ansi.code()
|
||||
bc = ansi.code()
|
||||
''' Create command line options '''
|
||||
usage='''
|
||||
usage = '''
|
||||
Markdown syntax color in ansi codes.
|
||||
Special syntaxes:
|
||||
Colors: insert string ${C}, where C is one of %s.
|
||||
Any ANSI control code: ${3A}, ${1;34;42m}, etc..
|
||||
|
||||
'''%(" ".join(bc.get_keys()))
|
||||
|
||||
parser=ArgumentParser(description=usage,
|
||||
epilog=__author__)
|
||||
|
||||
parser = ArgumentParser(
|
||||
description = usage,
|
||||
epilog = __author__
|
||||
)
|
||||
|
||||
parser.add_argument("-v","--version",action="version",version=__version__)
|
||||
parser.add_argument("-D",action="store_true",dest="debug",default=False,
|
||||
@@ -64,19 +66,19 @@ def parse(data):
|
||||
else:
|
||||
multiline_block=match
|
||||
break
|
||||
if multiline_block:
|
||||
if 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 'mod' in block_match[match]:
|
||||
# Style sets block in previous or next lines
|
||||
data[i+block_match[match]['mod']['pos']][0]=block_match[match]['mod']['name']
|
||||
data[i][0]=new_block
|
||||
if new_block!=block:
|
||||
block=new_block
|
||||
return data
|
||||
block=new_block
|
||||
return data
|
||||
|
||||
def colorize(data,remove_colors=False,dark_colors=False,debug=False):
|
||||
# Start inserting colors, and printing
|
||||
@@ -86,7 +88,7 @@ def colorize(data,remove_colors=False,dark_colors=False,debug=False):
|
||||
csc=cs+'c'
|
||||
for i,line in enumerate(data):
|
||||
row=line[1]
|
||||
block=line[0]
|
||||
block=line[0]
|
||||
multiline_block=block.startswith('multiline')
|
||||
if multiline_block:
|
||||
row=block_match[block][csc]+row
|
||||
@@ -122,14 +124,53 @@ def md_re_compile(d):
|
||||
sys.exit(1)
|
||||
return n
|
||||
|
||||
def read_data2(fp):
|
||||
data = []
|
||||
# Read data
|
||||
for row in f:
|
||||
if not row:
|
||||
continue
|
||||
row = row.decode('utf-8').rstrip("\n\r ")
|
||||
data.append(row)
|
||||
return data
|
||||
|
||||
def read_data3(fp):
|
||||
data = []
|
||||
# Read data
|
||||
for row in f:
|
||||
if not row:
|
||||
continue
|
||||
row = row.rstrip("\n\r ")
|
||||
data.append(row)
|
||||
return data
|
||||
|
||||
def write_colored2(colored, opts):
|
||||
for c in colored:
|
||||
sys.stdout.write(c.encode('utf-8'))
|
||||
if opts.zero:
|
||||
sys.stdout.write(bc.Z)
|
||||
sys.stdout.write("\n")
|
||||
if opts.zero_final:
|
||||
sys.stdout.write(bc.Z.encode('utf-8'))
|
||||
|
||||
def write_colored3(colored, opts):
|
||||
for c in colored:
|
||||
sys.stdout.write(c)
|
||||
if opts.zero:
|
||||
sys.stdout.write(bc.Z)
|
||||
sys.stdout.write("\n")
|
||||
if opts.zero_final:
|
||||
sys.stdout.write(bc.Z)
|
||||
|
||||
|
||||
|
||||
# re: regular expression, bc: bright colors, bcc: continue with this color after inline
|
||||
# dc: dark colors, dcc: continued color after inline
|
||||
block_match_str={
|
||||
'block_code': {'re':'^( {4}[^*])(.*)$',
|
||||
'bc' :'${Z}${c}\\1\\2', 'bcc':'${Z}${c}',
|
||||
'block_code': {'re':'^( {4}[^*])(.*)$',
|
||||
'bc' :'${Z}${c}\\1\\2', 'bcc':'${Z}${c}',
|
||||
'dc' :'${Z}${m}\\1\\2', 'dcc':'${Z}${m}'}, # code
|
||||
'multiline_code' : {'re':'^ *(`{3,}|~{3,}) *(\S*)',
|
||||
'multiline_code' : {'re':'^ *(`{3,}|~{3,}) *(\S*)',
|
||||
'bc' :'${Z}${c}\\1\\2', 'bcc':'${Z}${c}',
|
||||
'dc' :'${Z}${m}\\1\\2', 'dcc':'${Z}${m}'}, # ```lang
|
||||
'block_quote': {'re':'^(>[ >]* )',
|
||||
@@ -165,9 +206,9 @@ block_match=md_re_compile(block_match_str)
|
||||
blocks=['block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'block_code', 'text', 'empty']
|
||||
|
||||
inline_match_str={
|
||||
'bold1': {'re':r'(^| |})(_[^_]+_)',
|
||||
'bold1': {'re':r'(^| |})(_[^_]+_)',
|
||||
'bc':'\\1${W}\\2','dc':'\\1${W}\\2'}, # _bold_
|
||||
'bold2': {'re':r'(^| |})(\*{1,2}[^\*]+\*{1,2})',
|
||||
'bold2': {'re':r'(^| |})(\*{1,2}[^\*]+\*{1,2})',
|
||||
'bc':'\\1${W}\\2','dc':'\\1${W}\\2'}, # **bold**
|
||||
'code': {'re':r'([`]+[^`]+[`]+)',
|
||||
'bc':'${c}\\1','dc':'${m}\\1'}, # `code`
|
||||
@@ -178,15 +219,15 @@ inline_match_str={
|
||||
'dc':'${b}\\1${Z}\\2${b}\\3(${U}\\4${u})'}, # [text](link)
|
||||
'image': {'re':r'(!\[[^\]]+\]\([^\)]+\))',
|
||||
'bc':'${r}\\1','dc':'${g}\\1'}, # 
|
||||
'underline': {'re':r'(^|\W)(__)([^_]+)(__)',
|
||||
'underline': {'re':r'(^|\W)(__)([^_]+)(__)',
|
||||
'bc':'\\1\\2${U}\\3${Z}\\4','dc':'\\1\\2${U}\\3${Z}\\4'}, # __underline__
|
||||
'strikethrough': {'re':r'(~~)([^~]+)(~~)',
|
||||
'bc':'\\1${st}\\2${so}\\3','dc':'\\1${st}\\2${so}\\3'}, # ~~strike~~
|
||||
'checkbox': {'re':r'(\[[x ]\])',
|
||||
'bc':'${y}\\1','dc':'${r}\\1'}, # [x] [ ]
|
||||
}
|
||||
inline_match=md_re_compile(inline_match_str)
|
||||
inlines=['bold1','bold2','code_special','code','image','link','underline','strikethrough', 'checkbox']
|
||||
inline_match = md_re_compile(inline_match_str)
|
||||
inlines = ['bold1','bold2','code_special','code','image','link','underline','strikethrough', 'checkbox']
|
||||
|
||||
if __name__ == "__main__":
|
||||
opts=setup_options()
|
||||
@@ -204,33 +245,27 @@ if __name__ == "__main__":
|
||||
template=json.load(open(opts.template,'r'))
|
||||
if 'inlines' in template: inlines=template['inlines']
|
||||
if 'blocks' in template: blocks=template['blocks']
|
||||
if 'block_match' in template:
|
||||
if 'block_match' in template:
|
||||
block_match_str=template['block_match']
|
||||
block_match=md_re_compile(block_match_str)
|
||||
if 'inline_match' in template:
|
||||
if 'inline_match' in template:
|
||||
inline_match_str=template['inline_match']
|
||||
inline_match=md_re_compile(inline_match_str)
|
||||
|
||||
bc=ansi.code()
|
||||
if opts.filename=="-" or opts.filename==None:
|
||||
f=sys.stdin
|
||||
bc = ansi.code()
|
||||
if opts.filename == "-" or opts.filename == None:
|
||||
f = sys.stdin
|
||||
else:
|
||||
f=open(opts.filename,'r')
|
||||
data=[]
|
||||
# Read data
|
||||
for row in f:
|
||||
if not row:
|
||||
continue
|
||||
row=row.decode('utf-8').rstrip("\n\r ")
|
||||
data.append(row)
|
||||
f = open(opts.filename, 'r')
|
||||
if (sys.version_info > (3, 0)):
|
||||
data = read_data3(f)
|
||||
else:
|
||||
data = read_data2(f)
|
||||
|
||||
data=parse(data)
|
||||
colored=colorize(data,not opts.color,opts.dark_colors,opts.debug)
|
||||
for c in colored:
|
||||
sys.stdout.write(c.encode('utf-8'))
|
||||
if opts.zero:
|
||||
sys.stdout.write(bc.Z)
|
||||
sys.stdout.write("\n")
|
||||
if opts.zero_final:
|
||||
sys.stdout.write(bc.Z.encode('utf-8'))
|
||||
data = parse(data)
|
||||
colored = colorize(data, not opts.color, opts.dark_colors, opts.debug)
|
||||
if (sys.version_info > (3, 0)):
|
||||
write_colored3(colored, opts)
|
||||
else:
|
||||
write_colored2(colored, opts)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user