lib/gittoc.py
def detectHeadLines(f):
    '''detact headline and return inserted string.
    params:
        f: Markdown file
    '''
    f.seek(0)

    insert_str = ""
    org_str = ""

    last_status = -1
    c_status = -1

    headline_counter = 0
    iscode = False

    # 处理目录内容
    for line in f.readlines():
        # 如果是代码块
        if(line[:3] == '```'):
            iscode = not iscode

        # fix code indent bug and fix other indentation bug. 2020/7/3  缩进问题
        if not iscode:
            temp_line = line.strip(' ')
        ls = temp_line.split(' ')
        # '## 标题' -> ls[0]:'##' ls[1]:'标题'
        if len(ls) > 1 and ls[0] in headline_dic.keys() and not iscode:
            headline_counter += 1
            c_status = headline_dic[ls[0]]
            #header_number_array[c_status] = header_number_array[c_status] + 1
            # find first rank headline
            if last_status == -1 or c_status == 0 or suojin[c_status] == 0:
                # init suojin
                for key in suojin.keys():
                    suojin[key] = -1
                suojin[c_status] = 0
            elif c_status > last_status:
                suojin[c_status] = suojin[last_status]+1

            # update headline text
            headtext = (' '.join(ls[1:-1]))
            spaceText = '' if headtext == '' else ' '
            if ls[-1][-1] == '\n':
                headtext += (spaceText+ls[-1][:-1])
            else:
                headtext += (spaceText+ls[-1])

            if headtext.find('资料') != -1:
                continue

            headid = '{}{}'.format('head', headline_counter)
            headline = ls[0] + \
                ' <span id=\"{}\"'.format(headid)+'>' + headtext+'</span>'+'\n'
            org_str += headline

            jump_str = '- [{}](#{}{})'.format(headtext,
                                              'head', headline_counter)
            insert_str += ('\t'*suojin[c_status]+jump_str+'\n')

            last_status = c_status
        else:
            org_str += line

    # 真实的文件名
    realFileName = f.name.split('.')[0].split('\\')[-1]

    # 前缀
    prefix = ''
    prefix += realFileName
    prefix += '\n'
    prefix += '---'
    prefix += '\r\n'
    prefix += '#### 目录'
    prefix += '\n'

    # 后缀
    afterFix = ''
    afterFix += '\n'
    afterFix += '---'
    afterFix += '\r\n'

    insert_str = prefix + insert_str + afterFix

    return insert_str + org_str
detact headline and return inserted string.
    params:
        f: Markdown file
    
detact headline and return inserted string.
params:
f: Markdown file
detact headline and return inserted string . params : f : Markdown file
def detectHeadLines(f):
    f.seek(0)
    insert_str = ""
    org_str = ""
    last_status = -1
    c_status = -1
    headline_counter = 0
    iscode = False
    for line in f.readlines():
        if(line[:3] == '```'):
            iscode = not iscode
        if not iscode:
            temp_line = line.strip(' ')
        ls = temp_line.split(' ')
        if len(ls) > 1 and ls[0] in headline_dic.keys() and not iscode:
            headline_counter += 1
            c_status = headline_dic[ls[0]]
            if last_status == -1 or c_status == 0 or suojin[c_status] == 0:
                for key in suojin.keys():
                    suojin[key] = -1
                suojin[c_status] = 0
            elif c_status > last_status:
                suojin[c_status] = suojin[last_status]+1
            headtext = (' '.join(ls[1:-1]))
            spaceText = '' if headtext == '' else ' '
            if ls[-1][-1] == '\n':
                headtext += (spaceText+ls[-1][:-1])
            else:
                headtext += (spaceText+ls[-1])
            if headtext.find('资料') != -1:
                continue
            headid = '{}{}'.format('head', headline_counter)
            headline = ls[0] + \
                ' <span id=\"{}\"'.format(headid)+'>' + headtext+'</span>'+'\n'
            org_str += headline
            jump_str = '- [{}](#{}{})'.format(headtext,
                                              'head', headline_counter)
            insert_str += ('\t'*suojin[c_status]+jump_str+'\n')
            last_status = c_status
        else:
            org_str += line
    realFileName = f.name.split('.')[0].split('\\')[-1]
    prefix = ''
    prefix += realFileName
    prefix += '\n'
    prefix += '---'
    prefix += '\r\n'
    prefix += '#### 目录'
    prefix += '\n'
    afterFix = ''
    afterFix += '\n'
    afterFix += '---'
    afterFix += '\r\n'
    insert_str = prefix + insert_str + afterFix
    return insert_str + org_str
def detectHeadLines ( f ) : f . seek ( 0 ) insert_str = "" org_str = "" last_status = - 1 c_status = - 1 headline_counter = 0 iscode = False for line in f . readlines ( ) : if ( line [ : 3 ] == '```' ) : iscode = not iscode if not iscode : temp_line = line . strip ( ' ' ) ls = temp_line . split ( ' ' ) if len ( ls ) > 1 and ls [ 0 ] in headline_dic . keys ( ) and not iscode : headline_counter += 1 c_status = headline_dic [ ls [ 0 ] ] if last_status == - 1 or c_status == 0 or suojin [ c_status ] == 0 : for key in suojin . keys ( ) : suojin [ key ] = - 1 suojin [ c_status ] = 0 elif c_status > last_status : suojin [ c_status ] = suojin [ last_status ] + 1 headtext = ( ' ' . join ( ls [ 1 : - 1 ] ) ) spaceText = '' if headtext == '' else ' ' if ls [ - 1 ] [ - 1 ] == '\n' : headtext += ( spaceText + ls [ - 1 ] [ : - 1 ] ) else : headtext += ( spaceText + ls [ - 1 ] ) if headtext . find ( '资料') != 1: continue headid = '{}{}' . format ( 'head' , headline_counter ) headline = ls [ 0 ] + ' <span id=\"{}\"' . format ( headid ) + '>' + headtext + '</span>' + '\n' org_str += headline jump_str = '- [{}](#{}{})' . format ( headtext , 'head' , headline_counter ) insert_str += ( '\t' * suojin [ c_status ] + jump_str + '\n' ) last_status = c_status else : org_str += line realFileName = f . name . split ( '.' ) [ 0 ] . split ( '\\' ) [ - 1 ] prefix = '' prefix += realFileName prefix += '\n' prefix += '---' prefix += '\r\n' prefix += '#### 目录' prefix += '\n' afterFix = '' afterFix += '\n' afterFix += '---' afterFix += '\r\n' insert_str = prefix + insert_str + afterFix return insert_str + org_str
detact headline and return inserted string.
detact headline and return inserted string .
'''detact headline and return inserted string.
    params:
        f: Markdown file
    ''' # 处理目录内容 # 如果是代码块 # fix code indent bug and fix other indentation bug. 2020/7/3  缩进问题 # '## 标题' -> ls[0]:'##' ls[1]:'标题' #header_number_array[c_status] = header_number_array[c_status] + 1 # find first rank headline # init suojin # update headline text # 真实的文件名 # 前缀 # 后缀
f