]> jfr.im git - yt-dlp.git/blob - devscripts/make_readme.py
[ie/youtube] Fix comments extraction (#9775)
[yt-dlp.git] / devscripts / make_readme.py
1 #!/usr/bin/env python3
2
3 """
4 yt-dlp --help | make_readme.py
5 This must be run in a console of correct width
6 """
7
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
14
15 import functools
16 import re
17
18 from devscripts.utils import read_file, write_file
19
20 README_FILE = 'README.md'
21
22 OPTIONS_START = 'General Options:'
23 OPTIONS_END = 'CONFIGURATION'
24 EPILOG_START = 'See full documentation'
25 ALLOWED_OVERSHOOT = 2
26
27 DISABLE_PATCH = object()
28
29
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 ]
35
36
37 def apply_patch(text, patch):
38 return text if patch[0] is DISABLE_PATCH else re.sub(*patch, text)
39
40
41 options = take_section(sys.stdin.read(), f'\n {OPTIONS_START}', f'\n{EPILOG_START}', shift=1)
42
43 max_width = max(map(len, options.split('\n')))
44 switch_col_width = len(re.search(r'(?m)^\s{5,}', options).group())
45 delim = f'\n{" " * switch_col_width}'
46
47 PATCHES = (
48 ( # Standardize `--update` message
49 r'(?m)^( -U, --update\s+).+(\n \s.+)*$',
50 r'\1Update this program to the latest version',
51 ),
52 ( # Headings
53 r'(?m)^ (\w.+\n)( (?=\w))?',
54 r'## \1'
55 ),
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
63 rf'({delim[:-1]})? (?P<label>\[\S+\] )?(?P<url>https?({delim})?:({delim})?/({delim})?/(({delim})?\S+)+)\s',
64 lambda mobj: ''.join((delim, mobj.group('label') or '', re.sub(r'\s+', '', mobj.group('url')), '\n'))
65 ),
66 ( # Do not split "words"
67 rf'(?m)({delim}\S+)+$',
68 lambda mobj: ''.join((delim, mobj.group(0).replace(delim, '')))
69 ),
70 ( # Allow overshooting last line
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
74 else mobj.group())
75 ),
76 ( # Avoid newline when a space is available b/w switch and description
77 DISABLE_PATCH, # This creates issues with prepare_manpage
78 r'(?m)^(\s{4}-.{%d})(%s)' % (switch_col_width - 6, delim),
79 r'\1 '
80 ),
81 ( # Replace brackets with a Markdown link
82 r'SponsorBlock API \((http.+)\)',
83 r'[SponsorBlock API](\1)'
84 ),
85 )
86
87 readme = read_file(README_FILE)
88
89 write_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 )))