mdcolor template support
This commit is contained in:
@@ -6,7 +6,7 @@ sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
||||
import ansi
|
||||
|
||||
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
|
||||
__version__ = "0.2"
|
||||
__version__ = "0.3"
|
||||
|
||||
''' Rules modified from mistune project '''
|
||||
|
||||
@@ -31,11 +31,15 @@ Any ANSI control code: ${3A}, ${1;34;42m}, etc..
|
||||
help="Use dark colorscheme, better for white background terminals.")
|
||||
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
|
||||
help="Disable color.")
|
||||
parser.add_argument("--print-template",action="store_true",dest="print_template",default=False,
|
||||
help="Print customizable color template.")
|
||||
parser.add_argument("--template",action="store",type=str,dest="template",default=None,
|
||||
help="Use a custom template file for colorization.")
|
||||
parser.add_argument("-z",action="store_true",dest="zero",default=False,
|
||||
help="Reset coloring at the end of each line.")
|
||||
parser.add_argument("-Z",action="store_false",dest="zero_final",default=True,
|
||||
help="Disable reset of colors at the end of file.")
|
||||
parser.add_argument("filename",type=str,
|
||||
parser.add_argument("filename",type=str, nargs='?',
|
||||
help="File to show, - for stdin")
|
||||
opts=parser.parse_args()
|
||||
return opts
|
||||
@@ -103,72 +107,112 @@ def colorize(data,remove_colors=False,dark_colors=False,debug=False):
|
||||
colorized.append(colored)
|
||||
return colorized
|
||||
|
||||
# re: regular expression, bc: subract value bright colors, bcc: continue with this color after inline
|
||||
block_match={
|
||||
'block_code': {'re':re.compile(r'^( {4}[^*])(.*)$'),
|
||||
def md_re_compile(d):
|
||||
''' Returns a re.compiled dict '''
|
||||
n={}
|
||||
for t in d:
|
||||
n[t]={}
|
||||
for i in d[t]:
|
||||
n[t][i]=d[t][i]
|
||||
try:
|
||||
if n[t]['re']:
|
||||
n[t]['re']=re.compile(n[t]['re'])
|
||||
except err:
|
||||
print("Error compiling: %s"%(n[t]['re']))
|
||||
sys.exit(1)
|
||||
return n
|
||||
|
||||
|
||||
# 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}',
|
||||
'dc' :'${Z}${m}\\1\\2', 'dcc':'${Z}${m}'}, # code
|
||||
'multiline_code' : {'re':re.compile(r'^ *(`{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':re.compile(r'^(>[ >]* )'),
|
||||
'block_quote': {'re':'^(>[ >]* )',
|
||||
'bc':'${K}\\1${Z}','bcc':'${Z}',
|
||||
'dc':'${Y}\\1${Z}','dcc':'${Z}'}, # > > quote
|
||||
'hrule': {'re':re.compile(r'^ {0,3}[-*_]([-*_]){2,}$'),
|
||||
'bc':False,'bcc':'${Z}',
|
||||
'dc':False,'dcc':'${Z}'}, # ----
|
||||
'heading' : {'re':re.compile(r'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)'),
|
||||
'hrule': {'re':'^ {0,3}[-*_]([-*_]){2,}$',
|
||||
'bc':'False','bcc':'${Z}',
|
||||
'dc':'False','dcc':'${Z}'}, # ----
|
||||
'heading' : {'re':'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)',
|
||||
'bc':'${W}\\1 ${U}\\2','bcc':'${W}${U}',
|
||||
'dc':'${B}\\1 ${U}\\2','dcc':'${B}${U}'}, # # heading
|
||||
'lheading' : {'re':re.compile(r'^(=+|-+)$'),
|
||||
'lheading' : {'re':'^(=+|-+)$',
|
||||
'bc':'${W}\\1', 'bcc':'${W}',
|
||||
'dc':'${B}\\1', 'dcc':'${B}',
|
||||
'mod':{'pos':-1,'name':'lheading.mod'}}, # ======= under header
|
||||
'lheading.mod' : {'re':re.compile(r'^([^\n]+)'),
|
||||
'lheading.mod' : {'re':'^([^\n]+)',
|
||||
'bc':'${W}\\1', 'bcc':'${W}',
|
||||
'dc':'${B}\\1', 'dcc':'${B}'}, # over the ======= under header
|
||||
'list_bullet': {'re':re.compile(r'^( *)([*+-]|[\d\.]+)( +)'),
|
||||
'list_bullet': {'re':'^( *)([*+-]|[\d\.]+)( +)',
|
||||
'bc':'\\1${y}\\2${Z}\\3','bcc':'${Z}',
|
||||
'dc':'\\1${r}\\2${Z}\\3','dcc':'${Z}'}, # * or 1.
|
||||
'list_loose': {'re':None,
|
||||
'bc':False,'bcc':'${Z}',
|
||||
'dc':False,'dcc':'${Z}'},
|
||||
'text': {'re':re.compile(r'^([^\n]+)'),
|
||||
'list_loose': {'re':'None',
|
||||
'bc':'False','bcc':'${Z}',
|
||||
'dc':'False','dcc':'${Z}'},
|
||||
'text': {'re':'^([^\n]+)',
|
||||
'bc':'${Z}\\1','bcc':'${Z}',
|
||||
'dc':'${Z}\\1','dcc':'${Z}'},
|
||||
'empty': {'re':re.compile(r'(^$)'),
|
||||
'empty': {'re':'(^$)',
|
||||
'bc':'${Z}\\1','bcc':'${Z}',
|
||||
'dc':'${Z}\\1','dcc':'${Z}'},
|
||||
}
|
||||
block_match=md_re_compile(block_match_str)
|
||||
blocks=['block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'block_code', 'text', 'empty']
|
||||
|
||||
inline_match={
|
||||
'bold1': {'re':re.compile(r'(^| |})(_[^_]+_)'),
|
||||
inline_match_str={
|
||||
'bold1': {'re':r'(^| |})(_[^_]+_)',
|
||||
'bc':'\\1${W}\\2','dc':'\\1${W}\\2'}, # _bold_
|
||||
'bold2': {'re':re.compile(r'(^| |})(\*{1,2}[^\*]+\*{1,2})'),
|
||||
'bold2': {'re':r'(^| |})(\*{1,2}[^\*]+\*{1,2})',
|
||||
'bc':'\\1${W}\\2','dc':'\\1${W}\\2'}, # **bold**
|
||||
'code': {'re':re.compile(r'([`]+[^`]+[`]+)'),
|
||||
'code': {'re':r'([`]+[^`]+[`]+)',
|
||||
'bc':'${c}\\1','dc':'${m}\\1'}, # `code`
|
||||
'code_special': {'re':re.compile(r'([`]+[^`]+[`]+)([!>])'),
|
||||
'code_special': {'re':r'([`]+[^`]+[`]+)([!>])',
|
||||
'bc':'${c}\\1${g}\\2','dc':'${m}\\1${r}\\2'}, # `code`! or `code`> for markslider
|
||||
'link': {'re':re.compile(r'(\[)([^\]]+)(\])\(([^\)]+)\)'),
|
||||
'link': {'re':r'(\[)([^\]]+)(\])\(([^\)]+)\)',
|
||||
'bc':'${B}\\1${Z}\\2${B}\\3(${U}\\4${u})',
|
||||
'dc':'${b}\\1${Z}\\2${b}\\3(${U}\\4${u})'}, # [text](link)
|
||||
'image': {'re':re.compile(r'(!\[[^\]]+\]\([^\)]+\))'),
|
||||
'image': {'re':r'(!\[[^\]]+\]\([^\)]+\))',
|
||||
'bc':'${r}\\1','dc':'${g}\\1'}, # 
|
||||
'underline': {'re':re.compile(r'(^|\W)(__)([^_]+)(__)'),
|
||||
'underline': {'re':r'(^|\W)(__)([^_]+)(__)',
|
||||
'bc':'\\1\\2${U}\\3${Z}\\4','dc':'\\1\\2${U}\\3${Z}\\4'}, # __underline__
|
||||
'strikethrough': {'re':re.compile(r'(~~)([^~]+)(~~)'),
|
||||
'strikethrough': {'re':r'(~~)([^~]+)(~~)',
|
||||
'bc':'\\1${st}\\2${so}\\3','dc':'\\1${st}\\2${so}\\3'}, # ~~strike~~
|
||||
'checkbox': {'re':re.compile(r'(\[[x ]\])'),
|
||||
'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']
|
||||
|
||||
if __name__ == "__main__":
|
||||
opts=setup_options()
|
||||
if opts.print_template:
|
||||
import json
|
||||
print(json.dumps({'about':'re: regular expression, bc: bright colors, bcc: continue with this color after inline, dc: dark colors, dcc: continued color after inline. "blocks" and "inlines" list keys of matchers. ',
|
||||
'blocks':blocks,
|
||||
'block_match':block_match_str,
|
||||
'inline_match':inline_match_str,
|
||||
'inlines':inlines},
|
||||
indent=2,sort_keys=True))
|
||||
sys.exit(0)
|
||||
if opts.template:
|
||||
import json
|
||||
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:
|
||||
block_match_str=template['block_match']
|
||||
block_match=md_re_compile(block_match_str)
|
||||
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=="-":
|
||||
if opts.filename=="-" or opts.filename==None:
|
||||
f=sys.stdin
|
||||
else:
|
||||
f=open(opts.filename,'r')
|
||||
|
||||
Reference in New Issue
Block a user