diff -ur v4/ezmd.py v5/ezmd.py --- v4/ezmd.py 2019-09-25 00:23:47.000000000 +0900 +++ v5/ezmd.py 2019-09-26 21:40:15.000000000 +0900 @@ -7,12 +7,24 @@ heads = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9' ] modes = heads + [ 'p', 'pre', 'ul' ] -def is_mode_switch(s, mode): +is_all_ge_n = lambda s, c, n: all( map( lambda c_: c_ == c, s ) ) and len(s) >= n + +def next_mode_switch(s, mode): if mode == 'pre': - return s == '/' - return s in modes and s != mode + return (None, 'p') if s == '/' else (s, mode) + + if s in modes: + return (None, s) if s != mode else (None, mode) + + if is_all_ge_n(s, '-', 4): + return (s, 'hr') + + if mode == 'hr': + if is_all_ge_n(s, '-', 4): + return (s, 'hr') + return (s, 'p') -next_mode = lambda s: 'p' if s == '/' else s + return (s, mode) lst_strip = lambda lst: list( map( lambda s: s.strip(), lst ) ) @@ -125,6 +137,8 @@ res.append( { 'pre': s } ) elif mode == 'ul': do_mode_ul(buf, res) + elif mode == 'hr': + res.extend( list( map( lambda s: { 'p': { 'hr': '/' } }, buf ) ) ) def ezmd(lst): lst = ['p'] + lst @@ -132,11 +146,11 @@ mode = '' while lst: s = lst.pop(0) - if is_mode_switch(s, mode): + (s, nmd) = next_mode_switch(s, mode); + if nmd != mode: do_mode(mode, buf, res) - mode = next_mode(s) - buf = [] - elif s != mode: + (mode, buf) = (nmd, []) + if s != None: buf.append(s) do_mode(mode, buf, res) return res