-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot.py
More file actions
99 lines (84 loc) · 2.82 KB
/
dot.py
File metadata and controls
99 lines (84 loc) · 2.82 KB
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
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""
NOTE: This file was generated by GPT-4o.
Generate a DOT graph of HTML file link structure.
Usage:
html_to_dot.py [-d DIRECTORY] [-o OUTPUT]
Requirements:
pip install beautifulsoup4 networkx pydot
"""
print("graph [ overlap=false ];\nnode [shape=record,height=1];")
import os
import argparse
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import networkx as nx
def find_html_files(directory):
"""
Walk through `directory`, collecting all .html/.htm files.
Returns a dict mapping relative path -> absolute path.
"""
html_files = {}
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith(('.html', '.htm')):
rel = os.path.relpath(os.path.join(root, file), directory)
html_files[rel] = os.path.join(root, file)
return html_files
def extract_links(file_path):
"""
Parse HTML file and extract internal links to other .html/.htm files.
Returns a set of normalized relative paths.
"""
links = set()
with open(file_path, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f, 'html.parser')
for a in soup.find_all('a', href=True):
href = a['href']
parsed = urlparse(href)
# Skip external or fragment-only links
if parsed.scheme or parsed.netloc:
continue
path = parsed.path
if path.lower().endswith(('.html', '.htm')):
norm = os.path.normpath(path)
links.add(norm)
return links
def build_graph(html_files):
"""
Build a directed graph where nodes are HTML files and edges
represent links between them.
"""
G = nx.DiGraph()
# Add all files as nodes
for node in html_files:
G.add_node(node)
# Extract and add edges
for node, full_path in html_files.items():
targets = extract_links(full_path)
for tgt in targets:
if tgt in html_files:
G.add_edge(node, tgt)
return G
def main():
parser = argparse.ArgumentParser(
description='Generate DOT graph of HTML links.')
parser.add_argument('-d',
'--directory',
default='.',
help='Directory to scan for HTML files.')
parser.add_argument('-o',
'--output',
default='graph.dot',
help='Path to output DOT file.')
args = parser.parse_args()
html_files = find_html_files(args.directory)
if not html_files:
print(f"No HTML files found in {args.directory}")
return
G = build_graph(html_files)
# Write to DOT format using pydot via networkx
nx.drawing.nx_pydot.write_dot(G, args.output)
print(f"DOT graph written to {args.output}")
if __name__ == '__main__':
main()