-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2md.py
More file actions
452 lines (367 loc) · 15.4 KB
/
Copy pathhtml2md.py
File metadata and controls
452 lines (367 loc) · 15.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python3
"""
html2md.py — One-time converter: existing index.html pages → src/**/*.md
Run once to populate src/ from the current HTML files.
After that, edit only the .md files and run build.py to regenerate HTML.
Usage:
python html2md.py
"""
import re
import sys
import html as html_lib
import yaml
from pathlib import Path
from bs4 import BeautifulSoup, Tag, NavigableString, Comment
ROOT = Path(__file__).parent
SRC = ROOT / 'src'
# Pages that use a completely different layout — skip
SKIP_FILES = {'tree.html', 'tags.html'}
# ── PAGE_CONFIG extractor ─────────────────────────────────────────────────────
def _extract_config(html_text):
"""Return (breadcrumb, subpages) lists from inline PAGE_CONFIG script."""
script_m = re.search(r'PAGE_CONFIG\s*=\s*\{(.*?)\};', html_text, re.DOTALL)
if not script_m:
return [], []
cfg = script_m.group(1)
def _links(key):
sec_m = re.search(rf'{key}\s*:\s*\[(.*?)\]', cfg, re.DOTALL)
if not sec_m:
return []
return [
{'label': m.group(1), 'href': m.group(2)}
for m in re.finditer(r'\{label:\s*"([^"]*)",\s*href:\s*"([^"]*)"\}',
sec_m.group(1))
]
return _links('breadcrumb'), _links('subpages')
# ── HTML → Markdown converter ─────────────────────────────────────────────────
def _is_complex_table(tag):
"""Tables that cannot be expressed as Markdown pipe syntax."""
if 'infobox' in (tag.get('class') or []):
return True
if tag.find(attrs={'colspan': True}) or tag.find(attrs={'rowspan': True}):
return True
return False
def _raw(el):
"""Return the element as a raw HTML string (stripped of excess whitespace)."""
return str(el).strip()
# ── Inline-run grouping ───────────────────────────────────────────────────────
_INLINE_TAGS = frozenset({
'a', 'abbr', 'b', 'br', 'cite', 'code', 'em', 'i', 'img',
'kbd', 'mark', 'q', 's', 'samp', 'small', 'span', 'strong',
'sub', 'sup', 'u', 'var',
})
def _is_inline_node(node):
"""True for text nodes and inline HTML elements."""
if isinstance(node, Comment):
return False
if isinstance(node, NavigableString):
return True
return isinstance(node, Tag) and node.name in _INLINE_TAGS
def _block_children_md(children, skip_fn=None):
"""Convert block-container children to a list of Markdown strings.
Consecutive inline nodes (text + inline tags) are grouped into one paragraph
so un-wrapped inline content in <section>/<div> is not split up."""
parts = []
buf = []
def flush():
if buf:
raw = ''.join(_inline(n) for n in buf)
# Collapse whitespace runs (incl. newlines) to single space so that
# indented continuation lines don't become Markdown code blocks.
text = re.sub(r'\s+', ' ', raw).strip()
if text:
parts.append(text)
buf.clear()
for child in children:
if skip_fn is not None and skip_fn(child):
flush()
continue
if _is_inline_node(child):
buf.append(child)
else:
flush()
md = _block(child)
if md and md.strip():
parts.append(md.strip())
flush()
return parts
# ── Inline conversion ─────────────────────────────────────────────────────────
def _inline(node):
"""Convert an inline node (text or inline HTML) to Markdown string."""
if isinstance(node, Comment):
return ''
if isinstance(node, NavigableString):
return str(node)
tag = node.name
children = ''.join(_inline(c) for c in node.children)
if tag in ('b', 'strong'):
if node.get('style') or node.get('class'):
return _raw(node)
s = children.strip()
if not s:
return children
# preserve surrounding whitespace
pre = children[: len(children) - len(children.lstrip())]
post = children[len(children.rstrip()):]
return f'{pre}**{s}**{post}'
if tag in ('i', 'em'):
if node.get('style') or node.get('class'):
return _raw(node)
s = children.strip()
if not s:
return children
pre = children[: len(children) - len(children.lstrip())]
post = children[len(children.rstrip()):]
return f'{pre}*{s}*{post}'
if tag == 's':
return f'~~{children}~~'
if tag == 'code':
text = node.get_text()
return f'`{text}`'
if tag == 'a':
href = node.get('href', '')
cls = node.get('class') or []
title = node.get('title', '')
# keep <a class="ref"> as raw HTML — it has special CSS styling
if 'ref' in cls:
return _raw(node)
# external links with target=_blank: plain Markdown link is fine
t_part = f' "{title}"' if title else ''
return f'[{children}]({href}{t_part})'
if tag == 'br':
return '<br>\n'
if tag in ('span', 'sub', 'sup'):
# span with style: keep as HTML
if node.get('style') or node.get('class'):
return _raw(node)
return children
if tag == 'img':
src = node.get('src', '')
alt = node.get('alt', '')
style = node.get('style', '')
width = node.get('width', '')
extra = ''
if style:
extra += f' style="{style}"'
if width:
extra += f' width="{width}"'
if extra:
return f'<img src="{src}" alt="{alt}"{extra}>'
return f''
# Any other inline tag: keep as raw HTML
return _raw(node)
# ── Block conversion ──────────────────────────────────────────────────────────
def _list_to_md(tag, depth=0):
is_ol = tag.name == 'ol'
lines = []
num = 1
for item in tag.find_all('li', recursive=False):
prefix = f'{num}. ' if is_ol else '- '
inline_parts = []
nested_lists = []
nested_others = []
for child in item.children:
if isinstance(child, Tag) and child.name in ('ul', 'ol'):
nested_lists.append(child)
elif isinstance(child, Tag) and child.name in ('p', 'div'):
nested_others.append(child)
else:
inline_parts.append(_inline(child))
raw = ''.join(inline_parts)
text = re.sub(r'\s+', ' ', raw).strip()
indent = ' ' * depth
lines.append(f'{indent}{prefix}{text}')
for n in nested_lists:
lines.append(_list_to_md(n, depth + 1))
for b in nested_others:
# nested block inside li
lines.append(_block(b, depth + 1))
num += 1
return '\n'.join(lines)
def _table_to_md(tag):
"""Convert a simple table (no colspan/rowspan) to Markdown."""
rows = tag.find_all('tr')
if not rows:
return _raw(tag)
md_rows = []
for i, row in enumerate(rows):
cells = row.find_all(['th', 'td'])
values = []
for cell in cells:
raw = ''.join(_inline(c) for c in cell.children)
# Pipe-table cells cannot contain newlines — collapse all whitespace
cell_text = re.sub(r'\s+', ' ', raw).strip()
values.append(cell_text)
md_rows.append('| ' + ' | '.join(values) + ' |')
if i == 0:
md_rows.append('|' + ' --- |' * len(values))
return '\n'.join(md_rows)
def _block(el, _depth=0):
"""Convert a block-level element to a Markdown string (may contain raw HTML)."""
if isinstance(el, Comment):
return ''
if isinstance(el, NavigableString):
text = str(el)
return text if text.strip() else ''
tag = el.name
# ── Headings ──
if tag in ('h2', 'h3', 'h4', 'h5', 'h6'):
level = int(tag[1])
text = re.sub(r'\s+', ' ', el.get_text()).strip()
return '#' * level + ' ' + text
# ── Paragraphs ──
if tag == 'p':
raw = ''.join(_inline(c) for c in el.children)
# Collapse internal whitespace to prevent indented Markdown code blocks
return re.sub(r'\s+', ' ', raw).strip()
# ── Lists ──
if tag in ('ul', 'ol'):
if el.get('id') == 'tags':
return '' # handled separately via front-matter
return _list_to_md(el)
# ── Tables ──
if tag == 'table':
if _is_complex_table(el):
return _raw(el) # keep infobox / colspan tables verbatim
return _table_to_md(el)
# ── Code ──
if tag == 'pre':
code = el.find('code')
src = code or el
lang = ''
if src.get('class'):
for c in src.get('class'):
if c.startswith('language-'):
lang = c[9:]
text = src.get_text()
return f'```{lang}\n{text}\n```'
# ── Blockquote ──
if tag == 'blockquote':
inner = '\n\n'.join(_block_children_md(el.children))
return '\n'.join('> ' + line for line in inner.splitlines())
# ── HR ──
if tag == 'hr':
return '---'
# ── Figure ──
if tag == 'figure':
return _raw(el)
# ── BR at block level ──
if tag == 'br':
return '<br>'
# ── Images at block level ──
if tag == 'img':
return _inline(el)
# ── DIV ──
if tag == 'div':
div_id = el.get('id', '')
div_style = el.get('style', '')
# Skip TOC placeholders — they are re-added by the template
if div_id in ('tocButton', 'toc'):
return ''
# Styled divs: keep as raw HTML
if div_style:
return _raw(el)
# Generic div: group inline runs into paragraphs, then block elements
return '\n\n'.join(_block_children_md(el.children))
# ── Section wrapper ──
if tag == 'section':
return '\n\n'.join(_block_children_md(el.children))
# ── Inline tags appearing at block level ──
if tag in ('b', 'strong', 'i', 'em', 'a', 's', 'span', 'code', 'u'):
return _inline(el)
# ── Any unknown tag: keep as raw HTML ──
return _raw(el)
def _section_to_md(container):
"""
Convert the contents of <main> (or <main><section>) to Markdown.
Skips h1 (becomes front-matter title), tocButton/toc divs, and tags ul.
"""
def _skip(child):
if not isinstance(child, Tag):
return False
if child.name == 'h1':
return True
if child.get('id') in ('tocButton', 'toc'):
return True
if child.name == 'ul' and child.get('id') == 'tags':
return True
return False
return '\n\n'.join(_block_children_md(container.children, skip_fn=_skip))
# ── Convert one HTML file ─────────────────────────────────────────────────────
def convert_file(html_path):
raw_html = html_path.read_text(encoding='utf-8')
# Only process pages that use our shared template
if 'script.js' not in raw_html:
return None
soup = BeautifulSoup(raw_html, 'html.parser')
# ── Title ──────────────────────────────────────────────────────────────
title_tag = soup.find('title')
title = ''
if title_tag:
raw_title = title_tag.get_text()
title = re.sub(r'^Open Eggbert\s*[-–]\s*', '', raw_title).strip()
# ── Navigation data ─────────────────────────────────────────────────────
breadcrumb, subpages = _extract_config(raw_html)
# ── Tags ────────────────────────────────────────────────────────────────
tags_ul = soup.find('ul', id='tags')
tags = [li.get_text(strip=True) for li in tags_ul.find_all('li')] \
if tags_ul else []
# ── Main content ─────────────────────────────────────────────────────────
main_el = soup.find('main')
if not main_el:
return None
main_style = main_el.get('style', '')
section = main_el.find('section')
content_container = section if section else main_el
md_body = _section_to_md(content_container)
# ── Build YAML front-matter ───────────────────────────────────────────────
front = {'title': title}
if breadcrumb:
front['breadcrumb'] = breadcrumb
if subpages:
front['subpages'] = subpages
if tags:
front['tags'] = tags
if main_style:
front['main_style'] = main_style
front_str = yaml.dump(front, allow_unicode=True, sort_keys=False,
default_flow_style=False)
md_out = f'---\n{front_str}---\n\n{md_body}\n'
# ── Output path ───────────────────────────────────────────────────────────
rel = html_path.relative_to(ROOT)
md_rel = rel.with_suffix('.md') # About/index.html → About/index.md
out = SRC / md_rel
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(md_out, encoding='utf-8')
return out
# ── Main ──────────────────────────────────────────────────────────────────────
def main():
SRC.mkdir(exist_ok=True)
errors = []
converted = 0
for html_file in sorted(ROOT.rglob('*.html')):
if '.git' in html_file.parts:
continue
if 'src' in html_file.parts: # skip already-generated files
continue
if html_file.name in SKIP_FILES:
print(f'SKIP {html_file.relative_to(ROOT)}')
continue
try:
out = convert_file(html_file)
if out:
print(f'OK {html_file.relative_to(ROOT)} → {out.relative_to(ROOT)}')
converted += 1
else:
print(f'-- {html_file.relative_to(ROOT)} (no template, skipped)')
except Exception as e:
errors.append((html_file, e))
import traceback
print(f'ERR {html_file.relative_to(ROOT)}: {e}', file=sys.stderr)
traceback.print_exc(file=sys.stderr)
print(f'\nConverted {converted} files, {len(errors)} errors')
for f, e in errors:
print(f' {f.relative_to(ROOT)}: {e}', file=sys.stderr)
sys.exit(1 if errors else 0)
if __name__ == '__main__':
main()