code2markdown

发布时间 2023-04-04 19:10:03作者: 虎虎生威啊

python代码 main.py

import os
import re
import shutil
import sys

import easygui
from easygui import *

from pathlib import Path


class User(EgStore):
    def __init__(self, filename):
        self.path = ''
        EgStore.__init__(self, filename)


def get_dir_path_gui():
    # 创建存储对象
    user = User("settings.txt")
    # 取出以前保存的文件
    user.restore()
    file_path = easygui.diropenbox(default=user.path)
    user.path = file_path
    user.store()
    return file_path


def get_root_dir(dir_path):
    file_list = os.listdir(dir_path)
    path_list = []
    root_file_list = []
    for file in file_list:
        print(file)
        # 过滤隐藏文件
        if file.startswith('.'):
            continue
        # 过滤所有的文件
        is_file = re.findall(r'\.[^.\\/:*?"<>|\r\n]+$', file)
        if len(is_file):
            # 反向过滤,后缀文件
            res_abort = re.findall(re.compile(
                r'(\.json|d\.ts|config\.ts|config\.js)$'), file)
            if res_abort:
                continue
            # 保留根文件夹的(\.py|vue|js|ts)$ 结尾的文件
            res_save = re.findall(re.compile(r'(\.py|vue|js|ts|html)$'), file)
            if len(res_save):
                root_file_list.append(file)
            continue
        # 过滤node_modules
        res_abort = re.findall(re.compile(
            r'(__pycache__|venv|build|dist|node_modules|public|LICENSE)'), file)
        if len(res_abort):
            continue
        # 拼接成路径
        file_path = os.path.join(dir_path, file)
        path_list.append(file_path)
    return path_list, root_file_list


def get_deep_dirs(path):
    file_path = []
    for root, dirs, files in os.walk(path):

        # 过滤不符合的文件夹------------------------------------------------------------------------
        del_dir_index = []
        for i, dir in enumerate(dirs):
            # 过滤隐藏文件
            if dir.startswith('.'):
                del_dir_index.append(i)
            # 过滤掉所有不符合的文件夹
            res_abort = re.findall(re.compile(
                r'(__pycache__|venv|build|dist|node_modules|public|LICENSE|assets)'), dir)
            if len(res_abort):
                del_dir_index.append(i)

        # 去重,排序,过滤文件夹
        del_dir_index = list(set(del_dir_index))
        del_dir_index.sort()
        for counter, index in enumerate(del_dir_index):
            index = index - counter
            dirs.pop(index)

        # 过滤不符合的文件-----------------------------------------------------------------------------
        del_file_index = []
        for i, file in enumerate(files):
            # 过滤隐藏文件
            # (\.gitignore)|(\.prettierrc)
            if file.startswith('.'):
                del_file_index.append(i)
            # 过滤掉所有不符合的文件
            res_abort = re.findall(re.compile(
                r'(\.json|\.d\.ts|\.lock|\.config\.ts|\.config\.js|\.png|\.woff2|\.ttf|\.woff|\.css|README\.md|\.toml|swagger-ui-bundle.js)$'),
                file)
            if len(res_abort):
                del_file_index.append(i)

        # 去重排序,过滤文件
        del_file_index = list(set(del_file_index))
        del_file_index.sort()
        for counter, index in enumerate(del_file_index):
            index = index - counter
            files.pop(index)

        # 筛选所有符合后缀的文件------------------------------------------------------------------------
        for file in files:
            # 正向过滤含有(\.py|vue|js|ts)$ 结尾的文件
            res_save = re.findall(re.compile(r'(\.py|vue|js|ts|html)$'), file)
            if len(res_save):
                file_path.append(os.path.join(root, file))
    return file_path


def readcode_writemd(file_path, root_path):
    suffix = re.findall(r'\.[^.\\/:*?"<>|\r\n]+$', file_path)
    if len(suffix):
        suffix = suffix[0][1:]
    with open(file_path, "r", encoding='utf-8') as f:  # 打开文件
        head_line = f.readline()
        rest_line = f.read()
        write2md(head_line, head_line + rest_line,
                 suffix, file_path, root_path)


def write2md(head, content, suffix, file_path, root_path):
    with open(root_path + '/NOTE.md', "a", encoding='utf-8') as f:  # 打开文件
        f.write(f"# `{file_path}`\n\n")
        # f.write(f"# {head}\n\n")
        f.write(f"```{suffix}\n")
        f.write(content)
        f.write(f"\n")
        f.write(f"```\n")


if __name__ == '__main__':
    if len(sys.argv) == 1:
        print('请随便拖进来一个文件夹里面的文件')
        exit(-1)

    file_path = sys.argv[1:][0]  # markdown路径
    root_path = os.path.dirname(file_path)

    md_file = os.path.join(root_path, 'NOTE.md')

    # 清楚上一次的文件
    if os.path.exists(md_file):
        os.remove(md_file)

    file_path_list = get_deep_dirs(root_path)
    for file_path in file_path_list:
        print(file_path)
        readcode_writemd(file_path, root_path)
    print('!!!complete!!!')

bat脚本

@echo off
:start
set filePath=
set /p filePath=Please input file path:
python main.py  %filePath%
goto start