-
Notifications
You must be signed in to change notification settings - Fork 0
/
ob2tid.py
134 lines (116 loc) · 4.19 KB
/
ob2tid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import datetime
# 读取md文件内容
def extract_code(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
in_code_block = False
for i in range(len(lines)):
line = lines[i]
# check if in code block
if line.startswith('```'):
in_code_block = not in_code_block
# process headers
if not in_code_block and line.startswith('#'):
level = line.count('#')
lines[i] = '!' * level + line[level:]
# process unordered lists
if not in_code_block and line.startswith('-'):
lines[i] = '#' + line[2:]
# process ordered lists
if not in_code_block and line[0].isdigit() and line[1] == '.':
lines[i] = '*' + line[2:]
return ''.join(lines)
def add_head(file_path,tags) :
# 获取不带后缀的文件名
basename = os.path.splitext(file_path)[0]
result = []
for s in basename.split('\\'):
for ss in s.split('/'):
result.append(ss)
basename = result[-1]
# 获取创建和修改日期
now = datetime.datetime.now()
timestamp = str(now.year) + str(now.month).zfill(2) + str(now.day).zfill(2) + str(now.hour).zfill(2) + str(
now.minute).zfill(2) + str(now.second).zfill(2) + str(now.microsecond).zfill(6)
# 输出结果
title="title:"+basename
creat="created:"+str(timestamp)
modified="modified:"+str(timestamp)
return creat+'\n'+modified+'\n'+'tags:'+tags+'\n'+"title:"+basename+"\n"+"type: text/vnd.tiddlywiki"+"\n\n"
def allinone(file_path,tags):
output=add_head(file_path,tags)+extract_code(file_path)
# print(output)
basename = os.path.splitext(file_path)[0]
result = []
for s in basename.split('\\'):
for ss in s.split('/'):
result.append(ss)
basename = result[-1]
print(basename)
filename=str(basename)+".tid"
#下面这个创建完就不能重新创建了
# os.makedirs(file_output)
with open(file_output+"/"+filename, 'w', encoding='utf-8') as f:
f.write(output)
def process_folder(folder_path):
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
if os.path.isdir(file_path):
process_folder(file_path)
elif file_name.endswith(".md"):
tags = os.path.basename(folder_path)
allinone(file_path, tags)
#allinone('example.md','love')
#process_folder("C:/Users/别正/Desktop/个人")
import tkinter as tk
from tkinter import filedialog
import os
def browse_file():
global file_input
file_input = filedialog.askdirectory()
folder_name.set(os.path.basename(file_input))
def browse_output():
global file_output
file_output = filedialog.askdirectory()
output_name.set(os.path.basename(file_output))
def run_all():
process_folder(file_input)
#文件夹模式
def run_allin():
allinone(file_input,"")
#单文件模式
# 创建GUI窗口
root = tk.Tk()
root.title("ob2md")
root.geometry("400x200")
# 创建文件夹选择区域
folder_frame = tk.Frame(root)
folder_frame.pack(pady=10)
folder_label = tk.Label(folder_frame, text="请选择文件夹:")
folder_label.pack(side=tk.LEFT)
folder_btn = tk.Button(folder_frame, text="选择", command=browse_file)
folder_btn.pack(side=tk.LEFT)
folder_name = tk.StringVar()
folder_name.set("")
folder_display = tk.Label(folder_frame, textvariable=folder_name)
folder_display.pack(side=tk.LEFT)
# 创建输出路径选择区域
output_frame = tk.Frame(root)
output_frame.pack(pady=10)
output_label = tk.Label(output_frame, text="请选择输出路径:")
output_label.pack(side=tk.LEFT)
output_btn = tk.Button(output_frame, text="选择", command=browse_output)
output_btn.pack(side=tk.LEFT)
output_name = tk.StringVar()
output_name.set("")
output_display = tk.Label(output_frame, textvariable=output_name)
output_display.pack(side=tk.LEFT)
# 创建运行按钮
run_frame = tk.Frame(root)
run_frame.pack(pady=10)
run_all_btn = tk.Button(run_frame, text="文件夹运行", command=run_all)
run_all_btn.pack(side=tk.LEFT, padx=10)
run_allin_btn = tk.Button(run_frame, text="单文件运行", command=run_allin)
run_allin_btn.pack(side=tk.LEFT, padx=10)
root.mainloop()