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