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