|
以下是修复后的HTML编译器代码,主要修复了以下几个问题:
1. 修复了`handle_return`方法中计算缩进的逻辑错误
2. 修复了未完成的`highlight`方法
3. 添加了完整的HTML语法高亮功能
4. 优化了注释自动补全逻辑
- from tkinter import *
- from tkinter import scrolledtext, filedialog, messagebox
- import re
- import tempfile
- import webbrowser
- import os
- class HTMLCompiler:
- def __init__(self, root):
- self.root = root
- self.root.title("HTML 编译器")
- self.root.configure(bg='black')
- self.current_file = None
-
- # 创建菜单栏
- self.menu_bar = Menu(root)
-
- # 创建工具栏
- self.toolbar = Frame(root, bg='#333')
-
- self.new_btn = Button(self.toolbar, text="新建", command=self.new_file, bg='#333', fg='white')
- self.open_btn = Button(self.toolbar, text="打开", command=self.open_file, bg='#333', fg='white')
- self.save_btn = Button(self.toolbar, text="保存", command=self.save_file, bg='#333', fg='white')
- self.run_btn = Button(self.toolbar, text="运行", command=self.run_html, bg='#333', fg='white')
- self.help_btn = Button(self.toolbar, text='帮助', command=self.help_file, bg='#333', fg='white')
- self.new_btn.pack(side=LEFT, padx=2, pady=2)
- self.open_btn.pack(side=LEFT, padx=2, pady=2)
- self.save_btn.pack(side=LEFT, padx=2, pady=2)
- self.run_btn.pack(side=LEFT, padx=2, pady=2)
- self.help_btn.pack(side=LEFT, padx=2, pady=2)
-
- self.toolbar.pack(fill=X)
-
- # 创建文本编辑器
- self.text_area = scrolledtext.ScrolledText(
- root, wrap=WORD, bg='black', fg='white',
- insertbackground='white', font=('Consolas', 12)
- self.text_area.pack(fill=BOTH, expand=True)
-
- # 绑定事件
- self.text_area.bind('<KeyRelease>', self.on_key_release)
- self.text_area.bind('<Tab>', self.handle_tab)
- self.text_area.bind('<Return>', self.handle_return)
-
- # 绑定快捷键
- self.root.bind('<Control-n>', lambda event: self.new_file())
- self.root.bind('<Control-o>', lambda event: self.open_file())
- self.root.bind('<Control-s>', lambda event: self.save_file())
- self.root.bind('<Control-r>', lambda event: self.run_html())
-
- # 语法高亮颜色配置
- self.tag_colors = {
- 'tag': '#569CD6',
- 'attribute': '#9CDCFE',
- 'value': '#CE9178',
- 'comment': '#6A9955',
- 'doctype': '#569CD6',
- 'string': '#CE9178',
- 'symbol': '#D4D4D4'
- }
-
- # 配置标签样式
- for tag, color in self.tag_colors.items():
- self.text_area.tag_config(tag, foreground=color)
-
- # 初始高亮
- self.highlight()
- self.new_file()
-
- def new_file(self, event=None):
- self.text_area.delete('1.0', END)
- self.current_file = None
- self.root.title("HTML 编译器 - 未命名")
-
- def open_file(self, event=None):
- file_path = filedialog.askopenfilename(
- filetypes=[("HTML文件", "*.html *.htm"), ("所有文件", "*.*")]
- )
- if file_path:
- try:
- with open(file_path, 'r', encoding='utf-8') as file:
- content = file.read()
- self.text_area.delete('1.0', END)
- self.text_area.insert('1.0', content)
- self.current_file = file_path
- self.root.title(f"HTML 编译器 - {os.path.basename(file_path)}")
- self.highlight()
- except Exception as e:
- messagebox.showerror("错误", f"无法打开文件:\n{str(e)}")
-
- def save_file(self, event=None):
- if self.current_file:
- try:
- with open(self.current_file, 'w', encoding='utf-8') as file:
- file.write(self.text_area.get('1.0', END))
- messagebox.showinfo("保存", "文件保存成功!")
- except Exception as e:
- messagebox.showerror("错误", f"无法保存文件:\n{str(e)}")
- else:
- self.save_as_file()
-
- def save_as_file(self):
- file_path = filedialog.asksaveasfilename(
- defaultextension=".html",
- filetypes=[("HTML文件", "*.html *.htm"), ("所有文件", "*.*")]
- )
- if file_path:
- try:
- with open(file_path, 'w', encoding='utf-8') as file:
- file.write(self.text_area.get('1.0', END))
- self.current_file = file_path
- self.root.title(f"HTML 编译器 - {os.path.basename(file_path)}")
- messagebox.showinfo("保存", "文件保存成功!")
- except Exception as e:
- messagebox.showerror("错误", f"无法保存文件:\n{str(e)}")
-
- def run_html(self, event=None):
- html_code = self.text_area.get("1.0", END)
-
- if not html_code.strip():
- messagebox.showwarning("警告", "没有内容可运行!")
- return
-
- try:
- with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.html') as f:
- f.write(html_code)
- temp_path = f.name
-
- webbrowser.open('file://' + os.path.abspath(temp_path))
- # 延迟删除临时文件
- self.root.after(5000, lambda: os.unlink(temp_path) if os.path.exists(temp_path) else None)
- except Exception as e:
- messagebox.showerror("错误", f"无法运行HTML:\n{str(e)}")
- if 'temp_path' in locals() and os.path.exists(temp_path):
- os.unlink(temp_path)
- def help_file(self):
- messagebox.showinfo('提示','''ctrl+n 新建
- ctrl+o 打开
- ctrl+s 保存
- ctrl+r 运行''')
-
- def on_key_release(self, event):
- self.highlight()
-
- def handle_tab(self, event):
- current_pos = self.text_area.index(INSERT)
- line, col = map(int, current_pos.split('.'))
-
- space_count = 4 - (col % 4)
- self.text_area.insert(INSERT, ' ' * space_count)
- return 'break'
-
- def handle_return(self, event):
- current_pos = self.text_area.index(INSERT)
- line, col = map(int, current_pos.split('.'))
- line_text = self.text_area.get(f'{line}.0', f'{line}.end')
-
- # 修复缩进计算逻辑
- indent = 0
- while indent < len(line_text) and line_text[indent] == ' ':
- indent += 1
-
- # 自动添加注释结束标记
- if '<!--' in line_text and '-->' not in line_text:
- self.text_area.insert(INSERT, '\n' + ' ' * indent + '-->')
- self.text_area.mark_set(INSERT, f"{line+1}.{indent+4}")
- return 'break'
-
- self.text_area.insert(INSERT, '\n' + ' ' * indent)
- return 'break'
-
- def highlight_pattern(self, pattern, tag, regexp=True):
- start = "1.0"
- end = "end"
- self.text_area.tag_remove(tag, start, end)
-
- count = IntVar()
- idx = self.text_area.search(pattern, start, end, count=count, regexp=regexp)
-
- while idx:
- end_idx = f"{idx}+{count.get()}c"
- self.text_area.tag_add(tag, idx, end_idx)
- start = end_idx
- idx = self.text_area.search(pattern, start, end, count=count, regexp=regexp)
-
- def highlight(self):
- # 清除所有高亮
- for tag in self.tag_colors:
- self.text_area.tag_remove(tag, "1.0", END)
-
- # 高亮注释
- self.highlight_pattern(r"<!--.*?-->", "comment")
-
- # 高亮DOCTYPE
- self.highlight_pattern(r"<!DOCTYPE.*?>", "doctype")
-
- # 高亮标签
- self.highlight_pattern(r"</?\w+\b", "tag")
-
- # 高亮属性名
- self.highlight_pattern(r'\b\w+(?=\s*=)', "attribute")
-
- # 高亮属性值
- self.highlight_pattern(r'"[^"]*"', "value")
- self.highlight_pattern(r"'[^']*'", "value")
-
- # 高亮自闭合标签
- self.highlight_pattern(r'/>', "symbol")
- if __name__ == "__main__":
- root = Tk()
- editor = HTMLCompiler(root)
- root.mainloop()
复制代码
### 主要修复说明:
1. **缩进计算修复**:
- 修复了`handle_return`方法中的缩进计算逻辑
- 原错误:`while indent < len(line_text) and line_text == ' '`
- 修复为:`while indent < len(line_text) and line_text == ' '`
2. **语法高亮功能完善**:
- 添加了完整的`highlight_pattern`方法
- 实现了多元素高亮:
- 标签:蓝色
- 属性名:浅蓝色
- 属性值:橙色
- 注释:绿色
- DOCTYPE:蓝色
- 自闭合标签:灰色
3. **注释自动补全**:
- 当在未闭合的注释行按回车时,自动补全`-->`
- 光标定位到注释结束符后
4. **其他优化**:
- 添加了运行快捷键提示(Ctrl+R)
- 修正了文件类型过滤器的元组格式
- 优化了临时文件删除逻辑
### 使用说明:
1. 按回车自动保持当前缩进
2. 在未闭合的注释行按回车自动补全`-->`
3. 按Tab键插入4个空格
4. 支持语法高亮显示
5. 快捷键:
- Ctrl+N 新建
- Ctrl+O 打开
- Ctrl+S 保存
- Ctrl+R 运行
这个版本已经修复了主要问题,运行更稳定,编辑器功能更完整。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|