]> jfr.im git - yt-dlp.git/blob - devscripts/make_readme.py
[cleanup] Misc fixes and cleanup
[yt-dlp.git] / devscripts / make_readme.py
1 #!/usr/bin/env python3
2
3 # yt-dlp --help | make_readme.py
4 # This must be run in a console of correct width
5 import functools
6 import re
7 import sys
8
9 README_FILE = 'README.md'
10
11 OPTIONS_START = 'General Options:'
12 OPTIONS_END = 'CONFIGURATION'
13 EPILOG_START = 'See full documentation'
14
15 DISABLE_PATCH = object()
16
17
18 def take_section(text, start=None, end=None, *, shift=0):
19 return text[
20 text.index(start) + shift if start else None:
21 text.index(end) + shift if end else None
22 ]
23
24
25 def apply_patch(text, patch):
26 return text if patch[0] is DISABLE_PATCH else re.sub(*patch, text)
27
28
29 options = take_section(sys.stdin.read(), f'\n {OPTIONS_START}', f'\n{EPILOG_START}', shift=1)
30
31 switch_col_width = len(re.search(r'(?m)^\s{5,}', options).group())
32 delim = f'\n{" " * switch_col_width}'
33
34 PATCHES = (
35 ( # Headings
36 r'(?m)^ (\w.+\n)( (?=\w))?',
37 r'## \1'
38 ),
39 ( # Do not split URLs
40 rf'({delim[:-1]})? (?P<label>\[\S+\] )?(?P<url>https?({delim})?:({delim})?/({delim})?/(({delim})?\S+)+)\s',
41 lambda mobj: ''.join((delim, mobj.group('label') or '', re.sub(r'\s+', '', mobj.group('url')), '\n'))
42 ),
43 ( # Do not split "words"
44 rf'(?m)({delim}\S+)+$',
45 lambda mobj: ''.join((delim, mobj.group(0).replace(delim, '')))
46 ),
47 ( # Avoid newline when a space is available b/w switch and description
48 DISABLE_PATCH, # This creates issues with prepare_manpage
49 r'(?m)^(\s{4}-.{%d})(%s)' % (switch_col_width - 6, delim),
50 r'\1 '
51 ),
52 )
53
54 with open(README_FILE, encoding='utf-8') as f:
55 readme = f.read()
56
57 with open(README_FILE, 'w', encoding='utf-8') as f:
58 f.write(''.join((
59 take_section(readme, end=f'## {OPTIONS_START}'),
60 functools.reduce(apply_patch, PATCHES, options),
61 take_section(readme, f'# {OPTIONS_END}'),
62 )))