-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplit.py
More file actions
executable file
·89 lines (77 loc) · 2.96 KB
/
Copy pathsplit.py
File metadata and controls
executable file
·89 lines (77 loc) · 2.96 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
import argparse
import os
import re
def slugify(text):
"""Simple slugify: translate German umlauts and lowercase, replace non-alphanumerics with hyphens."""
text = text.lower()
# basic translation for German characters
replacements = {'ä': 'ae', 'ö': 'oe', 'ü': 'ue', 'ß': 'ss'}
for orig, repl in replacements.items():
text = text.replace(orig, repl)
# replace non-alphanumeric with hyphens
text = re.sub(r'[^a-z0-9]+', '-', text)
text = text.strip('-')
return text
def split_markdown(input_path, output_dir):
# Read the entire file
with open(input_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Extract title and date
title = ''
date_line = ''
for line in lines:
if line.startswith('# '):
title = line.strip()
elif line.lower().startswith('datum:'):
date_line = line.strip()
if title and date_line:
break
if not title:
raise ValueError("Konnte den Haupttitel (Zeile mit '# ') nicht finden.")
if not date_line:
raise ValueError("Konnte die Datumszeile (beginnt mit 'Datum:') nicht finden.")
# Prepare output directory
os.makedirs(output_dir, exist_ok=True)
# Find sections
sections = [] # list of (section_title, content_lines)
current_section = None
for line in lines:
match = re.match(r'##\s+(.+)', line)
if match:
if current_section:
sections.append(current_section)
sec_title = match.group(1).strip()
current_section = (sec_title, [line])
else:
if current_section:
current_section[1].append(line)
if current_section:
sections.append(current_section)
# Base slug for filename
base_slug = slugify(title.lstrip('# '))
# Write each section with numbering
chapter_counter = 1
for sec_title, content_lines in sections:
# Assign number: 'Präambel' gets 00, others sequential
if sec_title.lower() == 'präambel':
num = 0
else:
num = chapter_counter
chapter_counter += 1
num_str = f"{num:02d}"
sec_slug = slugify(sec_title)
filename = f"{num_str}-{base_slug}-{sec_slug}.md"
out_path = os.path.join(output_dir, filename)
with open(out_path, 'w', encoding='utf-8') as out_file:
out_file.write(f"{title}\n")
out_file.write(f"{date_line}\n\n")
out_file.writelines(content_lines)
print(f"Erstellt: {out_path}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Teile ein Markdown-Dokument an den H2-Überschriften in einzelne Dateien und nummeriere sie.'
)
parser.add_argument('input_file', help='Pfad zur Eingabe-Markdown-Datei')
parser.add_argument('output_dir', help='Verzeichnis für die Ausgabe-Dateien')
args = parser.parse_args()
split_markdown(args.input_file, args.output_dir)