|
| 1 | +from config import Config |
| 2 | +import os |
| 3 | +from smms_img import SmmsImg |
| 4 | +from user_exception import UserException |
| 5 | + |
| 6 | + |
| 7 | +class Main(): |
| 8 | + |
| 9 | + def __init__(self): |
| 10 | + pass |
| 11 | + |
| 12 | + def isMarkdownFile(self, fileName: str): |
| 13 | + filePart = fileName.rpartition('.') |
| 14 | + fileExt = filePart[-1] |
| 15 | + return fileExt == 'md' |
| 16 | + |
| 17 | + def isOrigMdFile(self, fileName: str): |
| 18 | + '''判断是否为原始markdown文件''' |
| 19 | + if not self.isMarkdownFile(fileName): |
| 20 | + return False |
| 21 | + # 检查文件后缀是否为_image |
| 22 | + specialMark = "_image" |
| 23 | + filePart = fileName.rpartition('.') |
| 24 | + # 如果文件名长度过小,肯定是原始文件 |
| 25 | + if len(filePart[0]) <= len(specialMark): |
| 26 | + return True |
| 27 | + # 后缀是否能完全匹配 |
| 28 | + if filePart[0].endswith(specialMark): |
| 29 | + return False |
| 30 | + return True |
| 31 | + |
| 32 | + def getCopyFileName(self, fileName: str): |
| 33 | + filePart = fileName.rpartition('.') |
| 34 | + fileExt = filePart[-1] |
| 35 | + newFileName = filePart[0]+'_image.'+fileExt |
| 36 | + return newFileName |
| 37 | + |
| 38 | + def getCopyFilePath(self, fileName: str): |
| 39 | + copyFileName = self.getCopyFileName(fileName) |
| 40 | + SysConfig = Config() |
| 41 | + return SysConfig.getMarkdownImgDirPath()+'\\'+copyFileName |
| 42 | + |
| 43 | + def findLocalImageFile(self, line: str, localImages: set): |
| 44 | + '''递归查找某段字符串中中括号包裹的内容是否为本地图片''' |
| 45 | + linePart = line.partition('(') |
| 46 | + if len(linePart[2]) > 0: |
| 47 | + secondPart = linePart[2].partition(')') |
| 48 | + content = secondPart[0] |
| 49 | + if len(content) > 0: |
| 50 | + # print(content) |
| 51 | + if content.endswith('.png') and os.path.exists(content): |
| 52 | + localImages.add(content) |
| 53 | + self.findLocalImageFile(content, localImages) |
| 54 | + |
| 55 | + def dealMdFile(self, mdFile: str): |
| 56 | + imgDict = dict() |
| 57 | + localImages = set() |
| 58 | + # 逐行扫描,查找本地图片 |
| 59 | + with open(file=mdFile, mode='r', encoding='UTF-8') as fopen: |
| 60 | + for line in fopen: |
| 61 | + # 去除行尾的换行 |
| 62 | + subLine = line[0:len(line)-1] |
| 63 | + self.findLocalImageFile(subLine, localImages) |
| 64 | + # 上传本地图片,建立图片映射表 |
| 65 | + imgServer = SmmsImg() |
| 66 | + imgServer.multiUploadImage(list(localImages), imgDict) |
| 67 | + # 替换本地图片 |
| 68 | + # copyFileName = self.getCopyFileName(mdFile) |
| 69 | + copyFilePath = self.getCopyFilePath(mdFile) |
| 70 | + copyFileOpen = open(file=copyFilePath, mode='w', encoding='UTF-8') |
| 71 | + with open(file=mdFile, mode='r', encoding='UTF-8') as fwrite: |
| 72 | + for line in fwrite: |
| 73 | + for localImg, webImg in imgDict.items(): |
| 74 | + line = line.replace(localImg, webImg) |
| 75 | + copyFileOpen.write(line) |
| 76 | + copyFileOpen.close() |
| 77 | + |
| 78 | + def dealUserException(self, userExp: UserException): |
| 79 | + sysConfig = Config() |
| 80 | + if userExp.getErrorCode() == UserException.CODE_NO_CONFIG: |
| 81 | + token = input("缺少你的sm.ms访问令牌,请输入:") |
| 82 | + sysConfig.writeSmmsToken(token) |
| 83 | + print("访问令牌已保存,请重新运行程序") |
| 84 | + exit() |
| 85 | + elif userExp.getErrorCode() == UserException.CODE_UPLOAD_ERROR: |
| 86 | + print("上传图片到sm.ms失败,请检查日志文件", sysConfig.getErrorLogFilePath()) |
| 87 | + exit() |
| 88 | + else: |
| 89 | + print("未定义错误,请联系开发者") |
| 90 | + exit() |
| 91 | + |
| 92 | + def main(self): |
| 93 | + # 检索当前目录中的markdown文件 |
| 94 | + for dir in os.listdir(): |
| 95 | + # print(dir) |
| 96 | + if os.path.isfile(dir): |
| 97 | + if self.isOrigMdFile(dir): |
| 98 | + # 复制一份拷贝,如果有,则不覆盖 |
| 99 | + # copyFileName = self.getCopyFileName(dir) |
| 100 | + copyFilePath = self.getCopyFilePath(dir) |
| 101 | + if not os.path.exists(copyFilePath): |
| 102 | + # 对拷贝进行处理 |
| 103 | + try: |
| 104 | + self.dealMdFile(dir) |
| 105 | + except UserException as e: |
| 106 | + self.dealUserException(e) |
| 107 | + print("已成功处理markdown文件", dir) |
| 108 | + print("所有markdown文档已处理完毕") |
| 109 | + exit() |
0 commit comments