]> jfr.im git - yt-dlp.git/blame - devscripts/make_readme.py
[devscripts] Create `utils` and refactor
[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
115add43 8# Allow direct execution
9import os
10import sys
11
12sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13
54007a45 14
c487cf00 15import functools
4bb028f4 16import re
115add43 17
18from devscripts.utils import read_file, write_file
4bb028f4 19
682407f2 20README_FILE = 'README.md'
4bb028f4 21
19a03940 22OPTIONS_START = 'General Options:'
23OPTIONS_END = 'CONFIGURATION'
24EPILOG_START = 'See full documentation'
64fa820c 25ALLOWED_OVERSHOOT = 2
19a03940 26
8a82af35 27DISABLE_PATCH = object()
28
19a03940 29
c487cf00 30def take_section(text, start=None, end=None, *, shift=0):
31 return text[
32 text.index(start) + shift if start else None:
33 text.index(end) + shift if end else None
34 ]
49f2bf76 35
c487cf00 36
37def apply_patch(text, patch):
8a82af35 38 return text if patch[0] is DISABLE_PATCH else re.sub(*patch, text)
c487cf00 39
40
41options = take_section(sys.stdin.read(), f'\n {OPTIONS_START}', f'\n{EPILOG_START}', shift=1)
42
64fa820c 43max_width = max(map(len, options.split('\n')))
c487cf00 44switch_col_width = len(re.search(r'(?m)^\s{5,}', options).group())
45delim = f'\n{" " * switch_col_width}'
46
47PATCHES = (
48 ( # Headings
49 r'(?m)^ (\w.+\n)( (?=\w))?',
50 r'## \1'
51 ),
52 ( # Do not split URLs
53 rf'({delim[:-1]})? (?P<label>\[\S+\] )?(?P<url>https?({delim})?:({delim})?/({delim})?/(({delim})?\S+)+)\s',
54 lambda mobj: ''.join((delim, mobj.group('label') or '', re.sub(r'\s+', '', mobj.group('url')), '\n'))
55 ),
8a82af35 56 ( # Do not split "words"
57 rf'(?m)({delim}\S+)+$',
58 lambda mobj: ''.join((delim, mobj.group(0).replace(delim, '')))
59 ),
64fa820c 60 ( # Allow overshooting last line
61 rf'(?m)^(?P<prev>.+)${delim}(?P<current>.+)$(?!{delim})',
62 lambda mobj: (mobj.group().replace(delim, ' ')
63 if len(mobj.group()) - len(delim) + 1 <= max_width + ALLOWED_OVERSHOOT
64 else mobj.group())
65 ),
8a82af35 66 ( # Avoid newline when a space is available b/w switch and description
67 DISABLE_PATCH, # This creates issues with prepare_manpage
68 r'(?m)^(\s{4}-.{%d})(%s)' % (switch_col_width - 6, delim),
69 r'\1 '
70 ),
c487cf00 71)
4bb028f4 72
115add43 73readme = read_file(README_FILE)
4bb028f4 74
115add43 75write_file(README_FILE, ''.join((
76 take_section(readme, end=f'## {OPTIONS_START}'),
77 functools.reduce(apply_patch, PATCHES, options),
78 take_section(readme, f'# {OPTIONS_END}'),
79)))