Pygments support for markslider

This commit is contained in:
ville rantanen
2019-06-16 12:00:36 +03:00
parent 768ee7773b
commit 50d9b96f00
3 changed files with 140 additions and 26 deletions

View File

@@ -81,32 +81,36 @@ def parse(data):
block=new_block
return data
def colorize(data,remove_colors=False,dark_colors=False,debug=False):
def colorize(data, remove_colors = False, dark_colors = False, debug = False):
# Start inserting colors, and printing
bc=ansi.code()
colorized=[]
cs='dc' if dark_colors else 'bc'
csc=cs+'c'
for i,line in enumerate(data):
row=line[1]
block=line[0]
multiline_block=block.startswith('multiline')
bc = ansi.code()
colorized = []
cs = 'dc' if dark_colors else 'bc'
csc = cs + 'c'
for i, line in enumerate(data):
row = line[1]
block = line[0]
multiline_block = block.startswith('multiline')
if multiline_block:
row=block_match[block][csc]+row
row = block_match[block][csc] + row
if block_match[block][cs]:
row=block_match[block]['re'].sub(block_match[block][cs],row)
row = block_match[block]['re'].sub(block_match[block][cs], row)
# No coloring inside block_code, nor multiline
if not (multiline_block or block=='block_code'):
if not (multiline_block or block == 'block_code'):
for match in inlines:
if inline_match[match]['re'].search(row):
row=inline_match[match]['re'].sub(inline_match[match][cs]+block_match[block][csc],row)
row = inline_match[match]['re'].sub(
inline_match[match][cs] + block_match[block][csc],
row
)
if remove_colors:
colored=bc.nocolor_string(row)
colored = bc.nocolor_string(row)
else:
colored=bc.color_string(row)
colored = bc.color_string(row)
if debug:
multistr="*" if multiline_block else " "
colored="{:<18}{:}:".format(data[i][0],multistr)+colored
multistr = "*" if multiline_block else " "
colored = "{:<18}{:}:".format(data[i][0], multistr) + colored
colorized.append(colored)
return colorized
@@ -202,6 +206,9 @@ block_match_str={
'empty': {'re':'(^$)',
'bc':'${Z}\\1','bcc':'${Z}',
'dc':'${Z}\\1','dcc':'${Z}'},
'preformatted': {'re': 'a^', # Never matches anything
'bc':'','bcc':'',
'dc':'','dcc':''},
}
block_match=md_re_compile(block_match_str)
blocks=['block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'block_code', 'text', 'empty']