diff -ur v7/ezmd.py v8/ezmd.py --- v7/ezmd.py 2019-09-27 08:13:21.000000000 +0900 +++ v8/ezmd.py 2019-09-27 09:23:15.000000000 +0900 @@ -5,7 +5,7 @@ import nkf heads = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9' ] -modes = heads + [ 'p', 'pre', 'ul' ] +modes = heads + [ 'p', 'pre', 'ul', 'index' ] is_all_ge_n = lambda s, c, n: all( map( lambda c_: c_ == c, s ) ) and len(s) >= n @@ -61,7 +61,7 @@ if v1 == 'img': return { 'img src="{}"'.format(v2): '/' } if v1 == 'video': - return { 'video src="{}" controls playsinline'.format(v2): '' } + return { 'video src="{}" controls playsinline'.format(v2): '' } if v1 == '-': return { 's': v2 } return { 'a href="{}"'.format(v1): v2 } if v1 else v2 @@ -115,12 +115,16 @@ res.extend( list( map(lst_to, lsts) ) ) -def do_mode(mode, buf, res): +def do_mode(mode, buf, res, hd_names): if mode in heads: buf = lst_strip(buf) buf = list( filter( lambda s: s!='', buf ) ) s = ''.join(buf) - res.append( { mode: s } ) + (h_i, n) = ( int(mode[1:]), len(hd_names) ) + name = 'c_{}_{}'.format(h_i, n) + res.append( { mode: { 'a name="{}"'.format(name): s } } ) + hd_names.append( (h_i, n, name, s) ) + elif mode == 'p': buf = lst_strip(buf) @@ -148,20 +152,62 @@ do_mode_ul(buf, res) elif mode == 'hr': res.extend( list( map( lambda s: { 'p': { 'hr': '/' } }, buf ) ) ) + elif mode == 'index': + buf = lst_strip(buf) + buf = list( filter( lambda s: s!='', buf ) ) + res.append( { 'index': buf } ) # continue to 2 pass + +def do_index(lst, hd_names): + + def slice_get(buf): + slc = ':' + for s in buf: + if ':' in s: + slc = s + elif s.isdigit(): + slc = s + ':{}'.format( int(s)+1 ) + if slc: + break + return slc + + def names_get(hd_names, slc): + sel = range(10) + try: + sel = eval( str(sel) + '[{}]'.format(slc) ) + except: + pass + return list( filter( lambda vs: vs[0] in sel, hd_names ) ) + + res = [] + for d in lst: + if 'index' in d: + slc =slice_get( d.get('index') ) + names = names_get(hd_names, slc) + + func = lambda h_i, n, name, s: ' ' * h_i + '[[#{}|'.format(name) + s + ']]' + buf = list( map( lambda vs: func(*vs), names ) ) + ul_res = [] + do_mode_ul(buf, ul_res) + + res.extend(ul_res) + else: + res.append(d) + return res def ezmd(lst): lst = ['p'] + lst - (buf, res) = ( [], [] ) + (buf, res, hd_names) = ( [], [], [] ) mode = '' while lst: s = lst.pop(0) (s, nmd) = next_mode_switch(s, mode); if nmd != mode: - do_mode(mode, buf, res) + do_mode(mode, buf, res, hd_names) (mode, buf) = (nmd, []) if s != None: buf.append(s) - do_mode(mode, buf, res) + do_mode(mode, buf, res, hd_names) + res = do_index(res, hd_names) return res def yaml_dump(o):