| """convert code to markdown |
| |
| """ |
| import os |
| import re |
| from datetime import datetime |
| |
| |
| exclude_dirs = [ |
| "__pycache__", |
| "venv", |
| "build", |
| "dist", |
| "node_modules", |
| "public", |
| "LICENSE", |
| "assets", |
| "vendor", |
| "tmp", |
| "static", |
| "templates", |
| "bin", |
| "obj", |
| "Migrations", |
| "Properties", |
| "packages", |
| ] |
| |
| |
| exclude_files = [ |
| "_NOTE.md", |
| ".d.ts", |
| ".lock", |
| ".png", |
| ".woff2", |
| ".ttf", |
| ".woff", |
| ".css", |
| "README.md", |
| ".toml", |
| "swagger-ui-bundle.js", |
| "-lock.json", |
| "zz_code2md.py", |
| "temp.md", |
| ] |
| |
| |
| include_exts = [ |
| ".py", |
| ".vue", |
| ".js", |
| ".ts", |
| ".html", |
| ".go", |
| ".mod", |
| ".json", |
| ".txt", |
| ".sh", |
| ".command", |
| ".cs", |
| "csproj", |
| ".jsx", |
| ".sln", |
| ".sh", |
| ".bat", |
| ] |
| |
| |
| md_suffix_table = {"command": "sh", "csproj": "xml"} |
| |
| |
| class CodeToMarkDown: |
| """_summary_""" |
| |
| __slots__ = ["path", "md_path", "code_file_path"] |
| |
| def __init__(self, path: str = None) -> None: |
| if path: |
| self.path = path |
| else: |
| self.path = os.getcwd() |
| |
| def generate_md(self): |
| self.__generate_md_file_path() |
| self.__collect_code_files() |
| self.__generate_md_file() |
| |
| def __generate_md_file_path(self): |
| cur_time_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| md_name = f"Z_{cur_time_str}_NOTE.md" |
| self.md_path = os.path.join(self.path, md_name) |
| |
| def __collect_code_files(self): |
| """_summary_ |
| Returns: |
| _type_: _description_ |
| """ |
| self.code_file_path = [] |
| for root, dirs, files in os.walk(self.path): |
| |
| dirs[:] = [d for d in dirs if not d.startswith(".") and not any(ex in d for ex in exclude_dirs)] |
| |
| files[:] = [f for f in files if not f.startswith(".") and not any(ex in f for ex in exclude_files)] |
| |
| for file in files: |
| |
| if any(file.endswith(ext) for ext in include_exts): |
| self.code_file_path.append(os.path.join(root, file)) |
| |
| def __generate_md_file(self): |
| for i, code_file_path in enumerate(self.code_file_path): |
| print(i + 1, "->", self.__get_md_title_level_one(code_file_path)) |
| self.__readcode_writemd(code_file_path) |
| |
| def __get_md_title_level_one(self, code_file_path): |
| """获取每个代码文件的md标题,去掉项目之前的文件路径 |
| Args: |
| code_file_path (_type_): 代码路径 |
| project_path (_type_): 项目根路径 |
| Returns: |
| _type_: 每个代码文件的md标题 |
| """ |
| |
| common_prefix = os.path.commonprefix([code_file_path, self.path]) |
| |
| diff1 = code_file_path[len(common_prefix) + 1 :] |
| md_title = os.path.join(os.path.basename(self.path), diff1) |
| return md_title |
| |
| def __readcode_writemd(self, code_file_path): |
| """_summary_ |
| Args: |
| code_file_path (_type_): _description_ |
| """ |
| with open(code_file_path, "r", encoding="utf-8") as f: |
| try: |
| content = f.read() |
| except Exception as e: |
| print(f"{code_file_path}{e}文件编码读取错误") |
| content = "" |
| self.__write2md(content, code_file_path) |
| |
| def __write2md( |
| self, |
| content, |
| code_file_path, |
| ): |
| """_summary_ |
| Args: |
| content (_type_): _description_ |
| suffix (_type_): _description_ |
| code_file_path (_type_): _description_ |
| """ |
| with open(self.md_path, "a", encoding="utf-8") as f: |
| md_title_level_one = self.__get_md_title_level_one(code_file_path) |
| code_label = self.__get_code_md_lable_by_suffix(code_file_path) |
| f.write("\n") |
| f.write(f"# `{md_title_level_one}`\n\n") |
| f.write(f"```{code_label}\n") |
| f.write(content) |
| f.write("\n") |
| f.write("```\n\n\n") |
| |
| def __get_code_md_lable_by_suffix(self, code_file_path): |
| suffix = re.findall(r'\.[^.\\/:*?"<>|\r\n]+$', code_file_path) |
| if len(suffix): |
| suffix = suffix[0][1:] |
| if md_suffix_table.get(suffix) is not None: |
| return md_suffix_table.get(suffix) |
| return suffix |
| |
| |
| if __name__ == "__main__": |
| print("====================start====================") |
| root_path = """F:\\song\\dotnet_efcore_two_database_learn\\SqliteToOracle""" |
| code2md = CodeToMarkDown(root_path) |
| code2md.generate_md() |
| print("====================done====================") |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战