"""YAML requirements renderer — for request_*.yaml style files.""" from html import escape def render(filepath): """Render YAML requirements as grouped tables (F_/P_/S_ sections).""" try: import yaml except ImportError: return '
pyyaml not installed. Run: pip install pyyaml
' with open(str(filepath), encoding='utf-8') as f: data = yaml.safe_load(f) if not isinstance(data, dict): return f'
{escape(str(data))}
' sections = { 'F_': ('功能需求', []), 'P_': ('性能需求', []), 'S_': ('规格需求', []), } other = [] for key, val in data.items(): if not isinstance(val, dict): continue name = val.get('name', key) desc = val.get('description', '') limit = val.get('limit', None) limit_str = '' if limit and isinstance(limit, dict): parts = [] if 'min' in limit and limit['min'] is not None: parts.append(f'>= {limit["min"]}') if 'max' in limit and limit['max'] is not None: parts.append(f'<= {limit["max"]}') if 'value' in limit: parts.append(str(limit['value'])) if 'unit' in limit: parts.append(limit['unit']) if 'count' in limit: parts.append(f'x{limit["count"]}') limit_str = ' '.join(parts) entry = {'id': key, 'name': name, 'desc': desc, 'limit': limit_str} placed = False for prefix in sections: if key.startswith(prefix): sections[prefix][1].append(entry) placed = True break if not placed: other.append(entry) html = [] for prefix, (label, items) in sections.items(): if not items: continue html.append(f'

{label}({len(items)} 项)

') html.append( '' '' '' '' '' '' ) for e in items: html.append( f'' f'' f'' f'' f'' f'' ) html.append('
ID名称描述指标
{escape(e["id"])}{escape(e["name"])}{escape(e["desc"])}{escape(e["limit"])}
') return '\n'.join(html)