]> jfr.im git - yt-dlp.git/blame - devscripts/make_readme.py
[ie/brightcove] Upgrade requests to HTTPS (#10202)
[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 = (
29cb20bd 48 ( # Standardize `--update` message
70b23409 49 r'(?m)^( -U, --update\s+).+(\n \s.+)*$',
50 r'\1Update this program to the latest version',
51 ),
29cb20bd 52 ( # Headings
c487cf00 53 r'(?m)^ (\w.+\n)( (?=\w))?',
add96eb9 54 r'## \1',
c487cf00 55 ),
29cb20bd
SS
56 ( # Fixup `--date` formatting
57 rf'(?m)( --date DATE.+({delim}[^\[]+)*)\[.+({delim}.+)*$',
58 (rf'\1[now|today|yesterday][-N[day|week|month|year]].{delim}'
59 f'E.g. "--date today-2weeks" downloads only{delim}'
60 'videos uploaded on the same day two weeks ago'),
61 ),
62 ( # Do not split URLs
c487cf00 63 rf'({delim[:-1]})? (?P<label>\[\S+\] )?(?P<url>https?({delim})?:({delim})?/({delim})?/(({delim})?\S+)+)\s',
add96eb9 64 lambda mobj: ''.join((delim, mobj.group('label') or '', re.sub(r'\s+', '', mobj.group('url')), '\n')),
c487cf00 65 ),
29cb20bd 66 ( # Do not split "words"
8a82af35 67 rf'(?m)({delim}\S+)+$',
add96eb9 68 lambda mobj: ''.join((delim, mobj.group(0).replace(delim, ''))),
8a82af35 69 ),
29cb20bd 70 ( # Allow overshooting last line
64fa820c 71 rf'(?m)^(?P<prev>.+)${delim}(?P<current>.+)$(?!{delim})',
72 lambda mobj: (mobj.group().replace(delim, ' ')
73 if len(mobj.group()) - len(delim) + 1 <= max_width + ALLOWED_OVERSHOOT
add96eb9 74 else mobj.group()),
64fa820c 75 ),
29cb20bd 76 ( # Avoid newline when a space is available b/w switch and description
8a82af35 77 DISABLE_PATCH, # This creates issues with prepare_manpage
78 r'(?m)^(\s{4}-.{%d})(%s)' % (switch_col_width - 6, delim),
add96eb9 79 r'\1 ',
8a82af35 80 ),
29cb20bd
SS
81 ( # Replace brackets with a Markdown link
82 r'SponsorBlock API \((http.+)\)',
add96eb9 83 r'[SponsorBlock API](\1)',
29cb20bd 84 ),
c487cf00 85)
4bb028f4 86
115add43 87readme = read_file(README_FILE)
4bb028f4 88
115add43 89write_file(README_FILE, ''.join((
90 take_section(readme, end=f'## {OPTIONS_START}'),
91 functools.reduce(apply_patch, PATCHES, options),
92 take_section(readme, f'# {OPTIONS_END}'),
93)))