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