|
4 | 4 | from .user_exception import UserException |
5 | 5 | from .download_help import DownloadHelp |
6 | 6 | from .time_helper import TimeHelper |
| 7 | +import re |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class Main(): |
@@ -42,27 +43,53 @@ def getCopyFilePath(self, fileName: str): |
42 | 43 | SysConfig = Config() |
43 | 44 | return SysConfig.getMarkdownImgDirPath()+'\\'+copyFileName |
44 | 45 |
|
| 46 | + def isImgFileName(self, file): |
| 47 | + imgExts = ('.png', '.gif', '.jpg', '.jpeg', '.svg', '.bmp') |
| 48 | + for imgExt in imgExts: |
| 49 | + if file.endswith(imgExt): |
| 50 | + return True |
| 51 | + return False |
| 52 | + |
| 53 | + def findHtmlImg(self, line, onlyLocal) -> list: |
| 54 | + '''从字符串中查找html标签中的本地图片''' |
| 55 | + findImgs = [] |
| 56 | + pattern = re.compile(r"<img src=\"(.+?)\"(.*)/>") |
| 57 | + for matched in pattern.finditer(line): |
| 58 | + imgFile = matched.group(1) |
| 59 | + if self.isImgFileName(imgFile): |
| 60 | + if not onlyLocal: |
| 61 | + findImgs.append(imgFile) |
| 62 | + elif os.path.exists(imgFile): |
| 63 | + findImgs.append(imgFile) |
| 64 | + else: |
| 65 | + pass |
| 66 | + return findImgs |
| 67 | + |
45 | 68 | def findLocalImageFile(self, line: str, localImages: set): |
46 | 69 | '''递归查找某段字符串中中括号包裹的内容是否为本地图片''' |
| 70 | + htmlImgs = self.findHtmlImg(line,True) |
| 71 | + if htmlImgs: |
| 72 | + localImages.update(htmlImgs) |
47 | 73 | linePart = line.partition('(') |
48 | 74 | if len(linePart[2]) > 0: |
49 | 75 | secondPart = linePart[2].partition(')') |
50 | 76 | content = secondPart[0] |
51 | 77 | if len(content) > 0: |
52 | | - # print(content) |
53 | | - if content.endswith('.png') and os.path.exists(content): |
| 78 | + if self.isImgFileName(content) and os.path.exists(content): |
54 | 79 | localImages.add(content) |
55 | 80 | self.findLocalImageFile(content, localImages) |
56 | 81 |
|
57 | 82 | def findImagesInStr(self, line: str, localImages: list): |
58 | 83 | '''递归查找某段字符串中中括号包裹的内容是否为本地图片''' |
| 84 | + htmlImgs = self.findHtmlImg(line,False) |
| 85 | + if htmlImgs: |
| 86 | + localImages.update(htmlImgs) |
59 | 87 | linePart = line.partition('(') |
60 | 88 | if len(linePart[2]) > 0: |
61 | 89 | secondPart = linePart[2].partition(')') |
62 | 90 | content = secondPart[0] |
63 | 91 | if len(content) > 0: |
64 | | - # print(content) |
65 | | - if content.endswith('.png'): |
| 92 | + if self.isImgFileName(content): |
66 | 93 | localImages.append(content) |
67 | 94 | self.findImagesInStr(content, localImages) |
68 | 95 |
|
|
0 commit comments