Dark`Kris

Private blog

I'm spark , hope you fire


为GFM生成目录

在前面的C++算法及数据结构模板一文中提到过GFM和普通的Markdown不一样,GFM不能自动生成TOC目录,而博主的那篇文章又巨长,每次翻看的时候心都很累,于是博主手撸了一个菜鸡都会写的py小工具来帮助GFM生成TOC目录

源码如下(Python3):

def work(string1):
    ret = ''
    string1 = string1.strip()
    for i in range(len(string1)):
        if string1[i] in ['-', '_'] or string1[i].isalnum():
            ret += (string1[i])
    return ret


url = input()
file = open(url)
line = file.readline()
flag = False
while line:
    if line[0:3] == '```':
        if not flag:
            flag = True
        else:
            flag = False
    if flag:
        line = file.readline()
        continue
    p = 0
    while line[p] == '#':
        p += 1
    str = line[p:len(line) - 1].strip()
    str1 = work(str)
    if p > 0:
        print('> ', end="")
        for index in range(p-1):
            print('\t', end="")
        print('* ['+str+'](#'+str1.lower()+')')
    line = file.readline()

使用方法:

$ python3 py.py
>>> (在这里输入.md文件的绝对路径)

Tips

Cancel

Thanks a lot,I will be better!

scan possible
scan possible
Scan and give tips as you wish

OpenAlipayScan it and give tips as you wish

Wait a minute,Loading