File size: 3,218 Bytes
c56870a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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