-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
171 lines (135 loc) · 5.04 KB
/
Copy pathsetup.py
File metadata and controls
171 lines (135 loc) · 5.04 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
设置 Windows 文件关联:双击 .md 文件自动转换并打开 HTML
用法: python setup.py [--uninstall]
"""
import sys
import os
import winreg
from pathlib import Path
def get_script_dir():
"""获取脚本所在目录"""
return Path(__file__).parent
def create_bat_file():
"""创建 render.bat 文件"""
script_dir = get_script_dir()
bat_path = script_dir / 'render.bat'
bat_content = f'''@echo off
chcp 65001 >nul
python "{script_dir / 'render.py'}" "%~1"
'''
bat_path.write_text(bat_content, encoding='utf-8')
print(f'✅ 已创建: {bat_path}')
return bat_path
def create_html_launcher():
"""创建 HTML 启动器(双击 .md 文件时调用)"""
script_dir = get_script_dir()
launcher_path = script_dir / 'open_md.bat'
launcher_content = f'''@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
REM 获取输入文件路径
set "input_file=%~1"
REM 生成临时 HTML 文件名
set "timestamp=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%"
set "timestamp=!timestamp: =0!"
set "html_file=%TEMP%\\agent_!timestamp!.html"
REM 转换
python "{script_dir / 'render.py'}" "%input_file%" -o "!html_file!" -q
REM 打开浏览器
start "" "!html_file!"
'''
launcher_path.write_text(launcher_content, encoding='utf-8')
print(f'✅ 已创建: {launcher_path}')
return launcher_path
def register_file_association():
"""注册 Windows 文件关联"""
script_dir = get_script_dir()
bat_path = script_dir / 'open_md.bat'
try:
# 创建 .md 文件关联
# HKEY_CLASSES_ROOT\.md
key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, '.md')
winreg.SetValueEx(key, '', 0, winreg.REG_SZ, 'MarkdownFile')
winreg.CloseKey(key)
# HKEY_CLASSES_ROOT\MarkdownFile
key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, 'MarkdownFile')
winreg.SetValueEx(key, '', 0, winreg.REG_SZ, 'Markdown Document')
winreg.CloseKey(key)
# HKEY_CLASSES_ROOT\MarkdownFile\shell\open\command
key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, r'MarkdownFile\shell\open\command')
winreg.SetValueEx(key, '', 0, winreg.REG_SZ, f'"{bat_path}" "%1"')
winreg.CloseKey(key)
print('✅ 已注册文件关联')
print(f' 双击 .md 文件将使用: {bat_path}')
return True
except PermissionError:
print('❌ 需要管理员权限来注册文件关联')
print(' 请右键点击此脚本,选择"以管理员身份运行"')
return False
except Exception as e:
print(f'❌ 注册失败: {e}')
return False
def unregister_file_association():
"""取消 Windows 文件关联"""
try:
# 删除 .md 关联
try:
winreg.DeleteKey(winreg.HKEY_CLASSES_ROOT, r'MarkdownFile\shell\open\command')
winreg.DeleteKey(winreg.HKEY_CLASSES_ROOT, r'MarkdownFile\shell\open')
winreg.DeleteKey(winreg.HKEY_CLASSES_ROOT, r'MarkdownFile\shell')
winreg.DeleteKey(winreg.HKEY_CLASSES_ROOT, 'MarkdownFile')
except FileNotFoundError:
pass
# 恢复 .md 默认关联
try:
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, '.md', 0, winreg.KEY_SET_VALUE)
winreg.DeleteValue(key, '')
winreg.CloseKey(key)
except FileNotFoundError:
pass
print('✅ 已取消文件关联')
return True
except PermissionError:
print('❌ 需要管理员权限来取消文件关联')
return False
except Exception as e:
print(f'❌ 取消失败: {e}')
return False
def main():
import argparse
parser = argparse.ArgumentParser(description='设置 MD 文件关联')
parser.add_argument('--uninstall', action='store_true', help='取消文件关联')
parser.add_argument('--force', action='store_true', help='强制覆盖现有关联')
args = parser.parse_args()
print('=' * 50)
print('MD → HTML 文件关联设置')
print('=' * 50)
print()
if args.uninstall:
unregister_file_association()
return
# 创建必要的文件
print('📁 创建启动脚本...')
create_bat_file()
create_html_launcher()
print()
# 注册文件关联
print('🔗 注册文件关联...')
if register_file_association():
print()
print('=' * 50)
print('✅ 设置完成!')
print('=' * 50)
print()
print('现在你可以:')
print(' 1. 双击 .md 文件 → 自动转换并打开 HTML')
print(' 2. 右键 .md 文件 → 打开方式 → 选择 "open_md.bat"')
print()
print('如需取消关联,运行: python setup.py --uninstall')
else:
print()
print('⚠️ 文件关联注册失败,但启动脚本已创建')
print(' 你可以手动右键 .md 文件 → 打开方式 → 选择 open_md.bat')
if __name__ == '__main__':
main()