Files
python/VsCode配置/Other/WatchClipChangeLatex.py
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

54 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
import sys
import os
import re
# pip3 install install pyperclip
sys.path.append(os.path.abspath("SO_site-packages"))
import pyperclip # 引入模块
recent_value = ""
tmp_value = "" # 初始化应该也可以没有这一行感觉意义不大。但是对recent_value的初始化是必须的
def add_dollars(text):
pattern = r'(?<!\$)\b([a-zA-Z0-9.+*/-]+)\b(?!\$)'
replace_text = r'$\1$'
result = re.sub(pattern, replace_text, text)
return result
while True:
tmp_value = pyperclip.paste() # 读取剪切板复制的内容
if tmp_value != recent_value: # 如果检测到剪切板内容有改动,那么就进入文本的修改
recent_value = tmp_value
lines = recent_value.split('\n')
changed = "### 一、题目描述\n"
flag = False
for line in lines:
if not flag:
line = add_dollars(line)
line = line.replace("$\r", "$")
line = line.replace("$\n", "$")
if "输入样例" in line:
flag = True
line = line.replace("输入格式", "**输入格式**")
line = line.replace("输出格式", "**输出格式**")
line = line.replace("数据范围", "**数据范围**")
if "输入样例" in line:
line = "\r\n\r\n" + line.replace("输入样例", "**输入样例**")
line = line + "\r\n" + "```cpp {.line-numbers}\r\n"
if "输出样例" in line:
line = "```\r\n" + line.replace("输出样例", "**输出样例**")
line = line + "\r\n" + "```cpp {.line-numbers}\r\n"
changed = changed + line
changed = changed + "\n" + "```"
recent_value = changed
pyperclip.copy(changed) # 将修改后的文本写入系统剪切板中
print("\n Value changed: %s" % str(changed)) # 输出已经去除换行符的文本
time.sleep(0.1)