This commit is contained in:
fengbohan 2024-01-12 13:46:57 +08:00
commit 0407d02129
6 changed files with 67 additions and 0 deletions

19
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"${workspaceFolder}/test.md"
]
}
]
}

45
remove.py Normal file
View File

@ -0,0 +1,45 @@
# auto remove unuse img
from pathlib import Path
import argparse
import chardet
if __name__ == "__main__":
# get opt
parser = argparse.ArgumentParser()
parser.add_argument("file", help="specifi the markdown file", type=str)
parser.add_argument("-a", "--assets", help="specifi the img dir", type=str)
args = parser.parse_args()
if args.assets:
img_dir = Path(args.assets)
else:
img_dir = Path(args.file).with_suffix('.assets')
with open(args.file, 'rb') as file:
result = chardet.detect(file.read())
print("{} encoding is {}, confidence: {}".format(args.file, result['encoding'], result['confidence']))
with open(args.file, 'r', encoding=result['encoding']) as file:
content = file.read()
img_list = list(img_dir.glob('**/*'))
unuse_img_list = []
for file_name in img_list:
img_name = Path(file_name).name
if img_name not in content:
print("img:{}".format(img_name))
unuse_img_list.append(file_name)
if len(unuse_img_list) == 0:
print("not find unuse img file")
else:
print("find {} unuse img files:".format(len(unuse_img_list)))
for file_name in unuse_img_list:
print("{}".format(file_name))
usr_input = input("would you want to delete them?(y or n):")
if usr_input == "y":
for file_name in unuse_img_list:
print("delete {}".format(file_name))
Path(file_name).unlink()
else:
print("unused img will not remove!")

BIN
test.assets/img.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 KiB

3
test.md Normal file
View File

@ -0,0 +1,3 @@
![萌萌的伊雷娜](./test.assets/萌萌的伊雷娜.jpg)
![写周报了](./test.assets/写周报了.png)