提交 e7edce45 编写于 作者: H horsesword

右键新建笔记的时候,可以更方便地选择笔记类型了。

上级 d71dfce1
"""
本文件用于处理markdown文件的移动问题,尤其是对于相对路径的移动有一定的自动化处理能力。
思路是:如果有md文件需要移动/复制的话,
- 检索md文件中的绝对链接和相对链接;
- 将这些相对链接的文件放在新的目标位置;
- 如果目标位置已经有同名文件+同文件大小,跳过;否则提醒是跳过、保留且修改还是怎样。
"""
import re
import os
import shutil
def find_links(filepath: str):
"""
查找md文件中的超链接,并返回绝对链接和相对链接(列表)。
其中的相对路径开头没有斜杠,所以要手动补充斜杠获取绝对路径。
:param filepath: 文件完整路径
:return: Tuple(res_abs,res_rel): 绝对链接(列表),相对链接(列表)
"""
pattern1 = re.compile(r'<img src=".+"?')
pattern11 = re.compile(r'src=".+?"')
pattern2 = re.compile(r'\[.*]\(.+\)?')
pattern21 = re.compile(r'\(.+?\)+?')
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = f.readlines()
except UnicodeDecodeError:
with open(filepath, 'r', encoding='gbk') as f:
data = f.readlines()
except Exception as e:
print(e)
finally:
pass
lst1 = []
lst2 = []
for i in data:
for j in pattern1.findall(i):
lst1 += pattern11.findall(j)
for j in pattern2.findall(i):
lst2 += pattern21.findall(j)
res = []
for i in lst1:
res.append(i[5:-1])
for i in lst2:
res.append(i[1:-1])
res_abs = []
res_rel = []
pattern_head = re.compile(r'^(.+?)(/+?)')
for p in res:
p = str(p).replace('\\', '/')
try:
if str(p).find('/') >= 0:
if str(pattern_head.findall(p)[0]).find(':') >= 0:
res_abs.append(p)
else:
res_rel.append(p)
else:
res_rel.append(p)
except Exception as e:
print(p)
print(e)
return res_abs, res_rel
def copy_md_linked_files(file_path_old: str, new_path, mode='copy'):
"""
:param file_path_old: md文件所在完整路径(包括文件名)
:param new_path: md文件目标位置(文件夹)
:param mode: 移动或者复制
:return:
"""
lst_links_abs, lst_links_rel = find_links(file_path_old)
fpath, fname = os.path.split(file_path_old)
for p in lst_links_rel:
old_pth = fpath.replace('\\', '/') + '/' + p # 原位置
print(old_pth)
tar_pth = new_path.replace('\\', '/')
if tar_pth.endswith('/'):
tar_pth = tar_pth + p
else:
tar_pth = tar_pth + '/' + p
print(tar_pth)
tarp, tarfn = os.path.split(tar_pth)
if not os.path.exists(tarp):
os.makedirs(tarp)
try:
shutil.copyfile(old_pth, tar_pth) # 目前,附件永远是复制,因为移动可能存在其他问题
except Exception as e:
print(e)
def copy_md(file_path_old: str, file_path_new: str, mode='copy'):
"""
复制md文件到指定位置。
:param file_path_old:
:param file_path_new: 目标位置(文件夹 + 文件名)
:param mode: 移动或者复制
:return: 无
"""
new_path, new_filename = os.path.split(file_path_new)
copy_md_linked_files(file_path_old, new_path)
if len(new_filename) == 0:
fpath, fname = os.path.split(file_path_old)
new_full_path = new_path + '/' + fname
else:
new_full_path = file_path_new
try:
if mode == 'copy':
shutil.copyfile(file_path_old, new_full_path)
elif mode == 'move':
shutil.move(file_path_old, new_full_path)
except Exception as e:
print('复制文件出错:')
print(e)
if __name__ == '__main__':
file_old = r"D:\MaJian\Desktop\@短文剪辑\Python 图像库 PIL 的类 Image 及其方法介绍_leemboy 的博客 - CSDN 博客_pil.md"
file_new = r'd:/to_delete/a/b/c/d\\e/PIL.md'
# pth_abs, pth_rel = find_links(fip)
# print(pth_abs)
# print(pth_rel)
#
# copy_md_linked_files(fip, 'd:/to_delete')
copy_md(file_old, file_new)
......@@ -40,6 +40,8 @@ from my_scripts.widgets.my_tk_widgets import my_progress_window
from my_scripts.widgets.my_tk_widgets import my_input_window
from my_scripts.widgets.my_tk_widgets import my_space_window
from my_scripts.markdown import MarkdownRel # 对 markdown 的特殊处理
# import my_logger
# import send2trash # 回收站(目前作废)
......@@ -47,10 +49,13 @@ URL_HELP = 'https://gitee.com/horse_sword/my-local-library' # 帮助的超链
URL_ADV = 'https://gitee.com/horse_sword/my-local-library/issues' # 提建议的位置
URL_CHK_UPDATE = 'https://gitee.com/horse_sword/my-local-library/releases' # 检查更新的位置
TAR = 'Tagdox / 标签文库' # 程序名称
VER = 'v0.21.1.2' # 版本号
VER = 'v0.21.2.0' # 版本号
"""
## 近期更新说明
#### v0.21.2.0 2021年10月21日
右键新建笔记的时候,可以更方便地选择笔记类型了。
#### v0.21.1.2 2021年10月6日
修复了在非NTFS磁盘上的兼容性bug。
文件夹区域也增加了鼠标指向效果。
......@@ -4493,6 +4498,14 @@ def show_popup_menu_file(event):
#
menu_tags_to_drop = tk.Menu(window, tearoff=0)
menu_tags_to_add = tk.Menu(window, tearoff=0)
menu_create_note = tk.Menu(window, tearoff=0) # 新建笔记
menu_create_note.add_command(label='.docx', command=lambda x=1: exec_create_note(None, '.docx'))
menu_create_note.add_command(label='.md', command=lambda x=1: exec_create_note(None, '.md'))
menu_create_note.add_command(label='.txt', command=lambda x=1: exec_create_note(None, '.txt'))
menu_create_note.add_command(label='.rtf', command=lambda x=1: exec_create_note(None, '.rtf'))
#
if len(QUICK_TAGS) > 0:
for i in QUICK_TAGS:
menu_tags_to_add.add_command(label=i, command=lambda x=i: exec_fast_add_tag(x))
......@@ -4505,6 +4518,7 @@ def show_popup_menu_file(event):
menu_file.add_separator()
if len(lst_my_path_long_selected) == 1:
menu_file.add_command(label="新建笔记", command=exec_create_note, accelerator='Ctrl+N')
menu_file.add_cascade(label="新建更多格式的笔记", menu=menu_create_note)
else:
menu_file.add_command(label="新建笔记", state=tk.DISABLED, command=exec_create_note, accelerator='Ctrl+N')
menu_file.add_separator()
......@@ -4549,6 +4563,7 @@ def show_popup_menu_file(event):
menu_file_no_selection.add_separator()
if len(lst_my_path_long_selected) == 1:
menu_file_no_selection.add_command(label="新建笔记", command=exec_create_note, accelerator='Ctrl+N')
menu_file_no_selection.add_cascade(label="新建更多格式的笔记", menu=menu_create_note)
else:
menu_file_no_selection.add_command(label="新建笔记", state=tk.DISABLED, command=exec_create_note,
accelerator='Ctrl+N')
......@@ -5684,42 +5699,55 @@ class main_app:
###########################################################
###########################################################
# 主程序开始
###########################################################
###########################################################
# 检查是否已经运行;
'''
import win32gui
import win32con
wd_name = TAR + ' ' + VER
pr_name = '我的文库.exe'
have_exe = 0
try:
win =win32gui.FindWindow(wd_name,None)
print(win)
if win:
have_exe = 1
win.ShowWindow(win32con.SW_SHOWNORMAL)
print('\n已经存在打开的实例\n')
else:
print('\n不存在打开的实例\n')
except Exception as e:
print(e)
"""
def check_single_instance(): # 检查是否已经运行;
import win32gui
import win32com.client
import sys
wd_name = TAR + ' ' + VER
pr_name = 'tagdox'
have_exe = 0
try:
# win32gui.GetWindow()
shell = win32com.client.Dispatch("WScript.Shell") # 未找到函数
shell.AppActivate(wd_name)
# print('\n已经存在打开的实例\n')
have_exe = 1
win = win32gui.FindWindow(None,wd_name)
print(win)
if win:
have_exe = 1
root = tk.Tk()
t = tk.messagebox.showerror(title='ERROR',
message='本程序已经在运行。')
root.destroy()
# win.ShowWindow(win32con.SW_SHOWNORMAL)
print('\n已经存在打开的实例\n')
else:
print('\n不存在打开的实例\n')
except Exception as e:
print(e)
have_exe = 0
return have_exe
"""
# from tendo import singleton
# me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running
'''
# have_exe = check_single_instance()
# if __name__ == '__main__' and have_exe==0:
if __name__ == '__main__':
# if True:
# 变量 ###########################################################
# from tendo import singleton
# import sys
# try:
# me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running
# except:
# t = tk.messagebox.showerror(title='ERROR',
# message='文件夹重命名失败,可能是有内部文件正在被访问,或没有操作权限。')
# sys.exit(-1)
#
#
q = queue.Queue()
#
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册