-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgen.py
More file actions
393 lines (333 loc) · 12.4 KB
/
Copy pathgen.py
File metadata and controls
393 lines (333 loc) · 12.4 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python3
"""
Generate C++ header for parsing OSM amenity categories from HTML table.
"""
from bs4 import BeautifulSoup, NavigableString
import re
from pathlib import Path
from jinja2 import Template
from typing import List, Dict, Tuple, Optional, Set
def sanitize_enum_name(name: str) -> str:
"""Convert category name to valid C++ enum name."""
# Remove special characters and convert to snake_case
name = re.sub(r'[^\w\s-]', '', name)
name = re.sub(r'[-\s]+', '_', name)
return 'k' + ''.join(word.capitalize() for word in name.split('_'))
def make_string_name(name: str) -> str:
"""Derive lower-case identifier for enum string lookup."""
slug = name.lower()
slug = re.sub(r'[^a-z0-9]+', '_', slug)
slug = re.sub(r'_+', '_', slug).strip('_')
return slug or name.lower()
def normalize_icon_name(raw: str) -> Tuple[str, Optional[str]]:
"""Normalize wiki icon reference and capture trailing numeric suffix if present."""
if not raw:
return '', None
name = raw.split('/')[-1]
name = name.split('?', 1)[0]
name = re.sub(r'^\d+px-', '', name)
if name.startswith('File:'):
name = name[len('File:'):]
# Strip known extensions repeatedly (e.g., .svg.png)
while True:
stripped = re.sub(r'\.(svg|png|gif)$', '', name, flags=re.IGNORECASE)
if stripped == name:
break
name = stripped
suffix_digits: Optional[str] = None
pattern = re.compile(r'[-_\.\s](\d+)(px)?$', flags=re.IGNORECASE)
# Drop trailing size suffixes like -14, _14, or -14px, but remember first occurrence
while True:
match = pattern.search(name)
if not match:
break
if suffix_digits is None:
suffix_digits = match.group(1)
name = name[:match.start()]
return name, suffix_digits
def parse_tag_combinations(tag_cell) -> List[List[str]]:
"""Capture tag combinations preserving '+' as AND and '/' or <br> as OR."""
combos: List[List[str]] = []
current_combo: List[str] = []
pending_sep: Optional[str] = None
def flush_current() -> None:
nonlocal current_combo
if current_combo:
combos.append(current_combo)
current_combo = []
for node in tag_cell.children:
if isinstance(node, NavigableString):
text = str(node)
if not text.strip():
continue
if '/' in text:
pending_sep = '/'
elif '+' in text:
pending_sep = '+'
continue
if getattr(node, "name", None) == "br":
flush_current()
pending_sep = None
continue
if node.name == "code":
tag_text = node.get_text(strip=True)
if not tag_text or '=' not in tag_text:
continue
if pending_sep == '/':
flush_current()
current_combo.append(tag_text)
pending_sep = None
continue
# Some cells wrap <code> inside spans; attempt to drill down once.
inner_code = node.find('code')
if inner_code:
tag_text = inner_code.get_text(strip=True)
if tag_text and '=' in tag_text:
if pending_sep == '/':
flush_current()
current_combo.append(tag_text)
pending_sep = None
flush_current()
return combos
def extract_categories(html_content: str) -> List[Dict]:
"""Extract one category entry per SVG/icon row from HTML table."""
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find('table', class_='wikitable')
if not table:
raise ValueError("Could not find wikitable")
entries = []
for row in table.find_all('tr'):
cells = row.find_all('td')
if len(cells) >= 3:
# Extract icon filename
icon_cell = cells[0]
icon_link = icon_cell.find('a', class_='mw-file-description')
icon_src = ''
if icon_link and icon_link.has_attr('href'):
icon_src = icon_link['href']
else:
img = icon_cell.find('img')
if img and 'src' in img.attrs:
icon_src = img['src']
icon_name, icon_suffix = normalize_icon_name(icon_src)
# Extract description
description = cells[1].get_text(strip=True)
# Extract tags
tag_cell = cells[2]
tag_combos = parse_tag_combinations(tag_cell)
flat_tags = [tag for combo in tag_combos for tag in combo]
if flat_tags:
entries.append({
'icon': icon_name,
'icon_suffix': icon_suffix,
'description': description,
'tag_combos': tag_combos
})
for entry in entries:
icon = entry.get('icon')
suffix = entry.pop('icon_suffix', None)
if icon and suffix:
entry['icon'] = f"{icon}-{suffix}"
return entries
def extract_place_types(html_content: str) -> List[Dict]:
"""Extract place=* values from Key:place tables."""
soup = BeautifulSoup(html_content, 'html.parser')
tables = soup.find_all(
'table',
class_=lambda c: c and 'wikitable' in c and 'taginfo-taglist' in c
)
if not tables:
raise ValueError("Could not find place tag list tables")
entries: List[Dict] = []
seen_values: Set[str] = set()
for table in tables:
rows = table.find_all('tr')
for row in rows[1:]:
cells = row.find_all('td')
if len(cells) < 2:
continue
key = cells[0].get_text(strip=True)
value = cells[1].get_text(strip=True)
description = cells[3].get_text(strip=True) if len(cells) > 3 else ""
if key != "place" or not value or value in seen_values:
continue
seen_values.add(value)
entries.append({
'icon': value,
'description': description or value,
'tag_combos': [[f"place={value}"]]
})
return entries
def parse_tag(tag_str: str) -> Tuple[str, str]:
"""Parse OSM tag string into key and value."""
if '=' not in tag_str:
return None, None
key, value = tag_str.split('=', 1)
return key.strip(), value.strip()
def generate_cpp_header(entries: List[Dict]) -> str:
"""Generate C++ header file using Jinja2 template, one rule per SVG/icon row."""
template = Template(
'''// WARNING: This file is auto-generated. Do not edit manually.
#pragma once
#include "cista/hash.h"
#include "osmium/osm/object.hpp"
#include <array>
#include <cstdint>
#include <string_view>
namespace adr {
enum class amenity_category : std::uint16_t {
kNone = 0,
{% for entry in unique_entries %}
{{ entry.enum_name }},
{% endfor %}
kExtra
};
constexpr std::array<char const*, {{ unique_entries|length + 2 }}> amenity_category_names = {
"none",
{% for entry in unique_entries %}
"{{ entry.string_name }}",
{% endfor %}
"extra"
};
inline constexpr char const* to_str(amenity_category const cat) {
return amenity_category_names[static_cast<std::size_t>(cat)];
}
struct amenity_tags {
explicit amenity_tags(osmium::TagList const& tags) {
using namespace std::string_view_literals;
// Single pass over all tags
for (auto const& t : tags) {
switch (cista::hash(std::string_view{t.key()})) {
{% for key, var_name in tag_keys.items() %}
case cista::hash("{{ key }}"): {{ var_name }}_ = t.value(); break;
{% endfor %}
default: break;
}
}
}
amenity_category get_category() const {
using namespace std::string_view_literals;
{% for entry in condition_entries %}
// {{ entry.name_source }}
{% for condition in entry.conditions %}
{{ condition }}
{% endfor %}
{% endfor %}
return amenity_category::kNone;
}
private:
{% for var_decl in member_vars %}
{{ var_decl }};
{% endfor %}
};
} // namespace osr
''',
trim_blocks=True,
lstrip_blocks=True,
)
# Collect all unique keys and create member variables
tag_keys = {}
member_vars = []
all_keys = set()
for entry in entries:
for combo in entry['tag_combos']:
for tag_str in combo:
key, _ = parse_tag(tag_str)
if key:
all_keys.add(key)
for key in sorted(all_keys):
var_name = key.replace(':', '_').replace('-', '_')
tag_keys[key] = var_name
member_vars.append(f"std::string_view {var_name}_")
condition_entries = []
unique_entries = []
seen_enum_names = set()
for entry in entries:
name_source = entry['icon'] or entry['description']
if not name_source:
name_source = entry['description']
entry['name_source'] = name_source
entry['enum_name'] = sanitize_enum_name(name_source)
entry['string_name'] = make_string_name(name_source)
condition_entries.append(entry)
if entry['enum_name'] in seen_enum_names:
continue
seen_enum_names.add(entry['enum_name'])
unique_entries.append(entry)
# Build conditions for each entry
for entry in condition_entries:
conditions: List[str] = []
for combo in entry['tag_combos']:
combo_conditions: List[str] = []
for tag_str in combo:
parts = [p.strip() for p in tag_str.split('+')] if '+' in tag_str else [tag_str]
for part in parts:
key, value = parse_tag(part)
if not key:
continue
var_name = tag_keys[key]
if not value:
continue
if value == '*' or value.startswith('*'):
combo_conditions.append(f"!{var_name}_.empty()")
else:
value_clean = value.replace('*', '')
if value_clean:
combo_conditions.append(f'{var_name}_ == "{value_clean}"sv')
if combo_conditions:
if len(combo_conditions) == 1:
cond_expr = combo_conditions[0]
else:
cond_expr = ' && '.join(combo_conditions)
conditions.append(
f"if ({cond_expr}) return amenity_category::{entry['enum_name']};"
)
entry['conditions'] = conditions
return template.render(
unique_entries=unique_entries,
condition_entries=condition_entries,
sanitize_enum_name=sanitize_enum_name,
tag_keys=tag_keys,
member_vars=member_vars
)
def main():
import sys
if len(sys.argv) < 2:
print("Usage: python generate_osm_parser.py <html_file> [output_file]")
print(" html_file: Path to OSM wiki HTML table file")
print(" output_file: Output C++ header file (default: amenity_category.h)")
sys.exit(1)
html_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else "amenity_category.h"
# Read amenities HTML content
print(f"Reading {html_file}...")
with open(html_file, 'r', encoding='utf-8') as f:
html_content = f.read()
# Extract entries (one per SVG/icon row)
print("Parsing HTML...")
entries = extract_categories(html_content)
print(f"\nFound {len(entries)} amenity entries (one per SVG/icon row)\n")
places_file = Path(html_file).with_name("places.html")
if places_file.exists():
print(f"Reading {places_file}...")
with open(places_file, 'r', encoding='utf-8') as f:
places_html_content = f.read()
print("Parsing place types...")
place_entries = extract_place_types(places_html_content)
entries.extend(place_entries)
print(f"Found {len(place_entries)} place type entries\n")
else:
print(f"Skipping place types, file not found: {places_file}\n")
# Generate C++ header
print("Generating C++ code...")
cpp_code = generate_cpp_header(entries)
# Write to file
with open(output_file, 'w') as f:
f.write(cpp_code)
print(f"✓ Generated {output_file}")
print(f"\nUsage in C++:")
print(f" osr::amenity_tags tags(osm_object);")
print(f" auto category = tags.get_category();")
print(f" std::cout << osr::to_str(category) << '\\n';")
if __name__ == "__main__":
main()