Spaces:
Sleeping
Sleeping
| import re | |
| from collections import defaultdict | |
| def analyze_tags(data): | |
| lines = data.split('\n') | |
| all_open = [] | |
| all_close = [] | |
| all_corresponding_close_tag = [] | |
| all_reference = [] | |
| unclosed_tag = [] | |
| ref_not_impl = [] | |
| open_tag_pattern = re.compile(r'.*\s*<([A-Za-z0-9_-]+)>\s*.*') | |
| close_tag_pattern = re.compile(r'.*</([A-Za-z0-9_-]+)>\s*$') | |
| reference_pattern = re.compile(r'<([A-Za-z0-9_-]+)>') | |
| analysis = { | |
| 'opened_tags': defaultdict(list), | |
| 'closed_pairs': defaultdict(list), | |
| 'unclosed_tags': defaultdict(list), | |
| 'referenced_tags': defaultdict(list), | |
| 'referenced_not_implemented': defaultdict(list), | |
| 'stack': [] | |
| } | |
| for line_num, line in enumerate(lines, 1): | |
| open_match = open_tag_pattern.match(line) | |
| if open_match: | |
| tag = open_match.group(1) | |
| if not re.search(r'<<.*>>', line): | |
| print(f"opening tag {tag} on line {line_num}") | |
| open_tag = "<"+tag+">" | |
| actual_close = "</"+tag+">" | |
| if open_tag not in all_open: | |
| all_open.append(open_tag) | |
| all_corresponding_close_tag.append(actual_close) | |
| close_match = close_tag_pattern.match(line) | |
| if close_match: | |
| tag = close_match.group(1) | |
| if not re.search(r'<<.*>>', line): | |
| print(f"closing tag {tag} on line {line_num}") | |
| tag = "</"+tag+">" | |
| if tag not in all_close: | |
| all_close.append(tag) | |
| for ref_match in reference_pattern.finditer(line): | |
| ref_tag = ref_match.group(1) | |
| if not re.search(r'<<.*>>', line): | |
| print(f"found reference to {ref_tag} on line {line_num}") | |
| ref_tag = "<"+ref_tag+">" | |
| if ref_tag not in all_reference: | |
| all_reference.append(ref_tag) | |
| for i, open_tag in enumerate(all_open): | |
| if all_corresponding_close_tag[i] not in all_close: | |
| unclosed_tag.append(all_corresponding_close_tag[i]) | |
| for ref_tag in all_reference: | |
| if ref_tag not in all_open: | |
| ref_not_impl.append(ref_tag) | |
| metrics = { | |
| "Total Open Tags": len(all_open), | |
| "Total Closed Tags": len(all_close), | |
| "Unclosed Tags": len(unclosed_tag), | |
| "Referenced But Not Implemented": len(ref_not_impl), | |
| "All Open Tags": all_open, | |
| "Unclosed Tags List": unclosed_tag, | |
| "All Close ":all_close, | |
| "Referenced But Not Implemented List": ref_not_impl | |
| } | |
| metric_summary = f""" | |
| Total Open Tags: {metrics['Total Open Tags']} | |
| Total Closed Tags: {metrics['Total Closed Tags']} | |
| Unclosed Tags: {metrics['Unclosed Tags']} | |
| Referenced But Not Implemented: {metrics['Referenced But Not Implemented']} | |
| All Open Tags: {', '.join(metrics['All Open Tags'])} | |
| Unclosed Tags List: {', '.join(metrics['Unclosed Tags List'])} | |
| Referenced But Not Implemented List: {', '.join(metrics['Referenced But Not Implemented List'])} | |
| """ | |
| print(all_reference) | |
| return metric_summary | |