#!/usr/bin/env python3 """将HTML表单转换为JSON Schema""" import asyncio from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy import text import re DB_CONFIG = { 'host': '123.57.81.127', 'port': 3306, 'user': 'cpy_admin', 'password': 'Tgzz2025+', 'database': 'ruoyi-fastapi' } DATABASE_URL = ( f"mysql+asyncmy://{DB_CONFIG['user']}:{DB_CONFIG['password']}@" f"{DB_CONFIG['host']}:{DB_CONFIG['port']}/{DB_CONFIG['database']}" ) def parse_html_form(html): """解析HTML表单,提取字段定义""" fields = [] # 匹配 el-form-item pattern = r']*label="([^"]*)"[^>]*prop="([^"]*)"[^>]*>(.*?)' matches = re.findall(pattern, html, re.DOTALL) for label, prop, content in matches: field = { 'prop': prop, 'label': label, 'type': 'input', # 默认 'required': 'required' in content } # 判断输入类型 if 'el-input-number' in content: field['type'] = 'number' elif 'el-upload' in content: field['type'] = 'upload' elif 'type="textarea"' in content: field['type'] = 'textarea' elif 'show-password' in content: field['type'] = 'password' else: field['type'] = 'input' # 提取placeholder placeholder_match = re.search(r'placeholder="([^"]*)"', content) if placeholder_match: field['placeholder'] = placeholder_match.group(1) # 提取maxlength maxlength_match = re.search(r':maxlength="(\d+)"', content) if maxlength_match: field['maxlength'] = int(maxlength_match.group(1)) fields.append(field) return fields async def main(): engine = create_async_engine(DATABASE_URL, echo=False) async with engine.connect() as conn: # 获取你刚才添加的表单 result = await conn.execute(text(""" SELECT id, name, form_json FROM test_form WHERE id = 1 """)) row = result.fetchone() if row: print(f"Form: {row[1]}") print(f"HTML:\n{row[2][:500]}...") # 解析HTML fields = parse_html_form(row[2]) print(f"\nParsed fields:") for field in fields: print(f" - {field['label']} ({field['prop']}): {field['type']}") # 生成JSON Schema schema = { 'fields': fields } print(f"\nJSON Schema:\n{schema}") await engine.dispose() if __name__ == '__main__': asyncio.run(main())