-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_articles.py
More file actions
179 lines (150 loc) · 5.3 KB
/
view_articles.py
File metadata and controls
179 lines (150 loc) · 5.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import http.server
import socketserver
import webbrowser
import argparse
from pathlib import Path
def start_server(directory, port=8000):
"""Start a simple HTTP server to view the HTML files."""
# Change to the specified directory
os.chdir(directory)
# Create a simple HTTP server
handler = http.server.SimpleHTTPRequestHandler
# Allow the server to be reused
socketserver.TCPServer.allow_reuse_address = True
# Create the server
with socketserver.TCPServer(("", port), handler) as httpd:
print(f"Server started at http://localhost:{port}")
print(f"Serving files from: {os.path.abspath(directory)}")
print("Press Ctrl+C to stop the server")
# Open the browser
webbrowser.open(f"http://localhost:{port}")
# Serve until interrupted
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
def list_articles(directory):
"""List all HTML files in the directory."""
html_files = sorted([f for f in os.listdir(directory) if f.endswith('.html')])
if not html_files:
print(f"No HTML files found in {directory}")
return
print(f"Found {len(html_files)} HTML files in {directory}:")
for i, file in enumerate(html_files[:20], 1):
print(f"{i}. {file}")
if len(html_files) > 20:
print(f"... and {len(html_files) - 20} more files")
def create_index_page(directory):
"""Create an index.html file with links to all articles."""
html_files = sorted([f for f in os.listdir(directory) if f.endswith('.html')])
if not html_files:
print(f"No HTML files found in {directory}")
return
# Create the index.html file
index_path = os.path.join(directory, "index.html")
with open(index_path, 'w', encoding='utf-8') as f:
f.write("""<!DOCTYPE html>
<html>
<head>
<title>Civilization 5 Wiki Articles</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
max-width: 800px;
margin: 0 auto;
padding: 20px;
color: #333;
}
h1 {
color: #444;
border-bottom: 1px solid #ddd;
padding-bottom: 10px;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 8px;
padding: 8px;
border-bottom: 1px solid #eee;
}
li:hover {
background-color: #f9f9f9;
}
a {
text-decoration: none;
color: #0066cc;
}
a:hover {
text-decoration: underline;
}
.search-container {
margin: 20px 0;
}
#search {
padding: 8px;
width: 100%;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Civilization 5 Wiki Articles</h1>
<div class="search-container">
<input type="text" id="search" placeholder="Search articles..." onkeyup="filterArticles()">
</div>
<ul id="article-list">
""")
# Add links to all HTML files
for file in html_files:
if file != "index.html":
title = file.replace('.html', '').replace('_', ' ')
f.write(f' <li><a href="{file}">{title}</a></li>\n')
f.write(""" </ul>
<script>
function filterArticles() {
// Get the search term
var input = document.getElementById('search');
var filter = input.value.toUpperCase();
// Get the list of articles
var ul = document.getElementById('article-list');
var li = ul.getElementsByTagName('li');
// Loop through all list items, and hide those that don't match the search query
for (var i = 0; i < li.length; i++) {
var a = li[i].getElementsByTagName('a')[0];
var txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = '';
} else {
li[i].style.display = 'none';
}
}
}
</script>
</body>
</html>""")
print(f"Created index.html with links to {len(html_files)} articles")
return index_path
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="View minimal HTML files in a browser")
parser.add_argument("--dir", default="./minimal_html", help="Directory containing the HTML files")
parser.add_argument("--port", type=int, default=8000, help="Port to run the server on")
parser.add_argument("--list", action="store_true", help="List available HTML files")
args = parser.parse_args()
# Check if the directory exists
if not os.path.exists(args.dir):
print(f"Directory {args.dir} does not exist")
exit(1)
# List articles if requested
if args.list:
list_articles(args.dir)
exit(0)
# Create an index.html file
create_index_page(args.dir)
# Start the server
start_server(args.dir, args.port)