python生成gitbook中SUMMARY.md文件

今天用gitbook搭建零组的漏洞文库,需要生成SUMMARY.md文件,做目录树

网上找了自动生成的

插件生成

用的插件gitbook-plugin-summary,看使用 方法需要本地下载gitbook,就没去下载搭建

参考https://www.jianshu.com/p/2160f1ba68a0?utm_campaign

gitbook-summary

安装

npm install -g gitbook-summary

使用

cd /path/to/your/book/

book sm

这样生成有个缺陷,就是文件或者文件夹有英文括号()会在gitbook中解析出错,需要用反斜杠转义\

原文件
demo_(rce)/(demo-rce).md

生成
* [(demo-rce)](demo_(rce)/(demo-rce).md)

gitbook格式需要转义

* [\(demo-rce\)](demo_\(rce\)/\(demo-rce\).md)

因为漏洞库目录及文件较多,所有如果手动修改的话,很费时间,就用python写了个生成的脚本,只针对漏洞文库,但是可以做参考

python代码

# /usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File  :   gitbook.py
@Time  :   2020/12/16 14:17:12
@Author:   Morker
@Blog  :   https://96.mk/
@Email :   [email protected]

If you don't go through the cold, you can't get the fragrant plum blossom.
'''

import os

path = os.getcwd()
dirs = os.listdir(path)
writeMD = open('SUMMARY.md', 'a+', encoding='utf-8')
writeMD.write("# Wiki\n\n")
writeMD.write("* [前言](README.md)\n")

for vuDir in dirs:
    if "gitbook.py" == vuDir:
        pass
    elif "README.md" == vuDir:
        pass
    elif "SUMMARY.md" == vuDir:
        pass
    else:
        print("- {}".format(vuDir))
        writeMD.write("- {}\n".format(vuDir))
    if os.path.isdir(vuDir):
        cmsDirs = os.listdir(path + "\\" + vuDir)
        for cmsDir in cmsDirs:
            print("\t- {}".format(cmsDir))
            writeMD.write("\t- {}\n".format(cmsDir))
            filesDir = os.listdir(path + "\\" + vuDir + "\\" + cmsDir)
            for files in filesDir:
                if ".resource" == files:
                    pass
                if files.endswith('md'):
                    files = files.replace("(", "\(")
                    files = files.replace(")", "\)")
                    newcmsDir = cmsDir.replace("(", "\(")
                    newcmsDir = newcmsDir.replace(")", "\)")
                    print(
                        "\t\t* [{}]({}/{}/{})".format(files.rstrip(".md"), vuDir, newcmsDir, files))
                    writeMD.write(
                        "\t\t* [{}]({}/{}/{})\n".format(files.rstrip(".md"), vuDir, newcmsDir, files))
                else:
                    if ".resource" == files:
                        pass
                    else:
                        print("\t\t- {}".format(files))
                        writeMD.write("\t\t- {}\n".format(files))
                        llDirs = os.listdir(
                            path + "\\" + vuDir + "\\" + cmsDir + "\\" + files)
                        for llDir in llDirs:
                            if ".resource" == llDir:
                                pass
                            else:
                                if llDir.endswith('md'):
                                    llDir = llDir.replace("(", "\(")
                                    llDir = llDir.replace(")", "\)")
                                    print(
                                        "\t\t\t* [{}]({}/{}/{}/{})".format(llDir.rstrip(".md"), vuDir, newcmsDir, files, llDir))
                                    writeMD.write(
                                        "\t\t\t* [{}]({}/{}/{}/{})\n".format(llDir.rstrip(".md"), vuDir, newcmsDir, files, llDir))

writeMD.close()

放在文库的根目录运行即可,运行完后会在根目录生成 SUMMARY.md文件

附图:

Snipaste_2020-12-17_00-02-20

Snipaste_2020-12-17_00-03-46

我做了实时输出,所有命令行上看着比较乱

Snipaste_2020-12-17_00-05-02

在根目录生成SUMMARY.md文件,打开复制或者直接上传到giuhub都可以

Snipaste_2020-12-17_00-07-03

将带有英文括号的目录和文件都转义了

Snipaste_2020-12-17_00-09-25

访问一切正常

最后修改:2020 年 12 月 17 日 12 : 11 AM