]> jfr.im git - yt-dlp.git/blame - devscripts/prepare_manpage.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / devscripts / prepare_manpage.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
54007a45 2
115add43 3# Allow direct execution
4import os
5import sys
6
7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9
1800eeef 10import os.path
1800eeef
PH
11import re
12
115add43 13from devscripts.utils import (
14 compose_functions,
15 get_filename_args,
16 read_file,
17 write_file,
18)
19
1800eeef
PH
20ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
21README_FILE = os.path.join(ROOT_DIR, 'README.md')
22
7a5c1cfe 23PREFIX = r'''%yt-dlp(1)
44c88923
S
24
25# NAME
26
388c979a 27yt\-dlp \- A feature\-rich command\-line audio/video downloader
44c88923
S
28
29# SYNOPSIS
30
7a5c1cfe 31**yt-dlp** \[OPTIONS\] URL [URL...]
44c88923 32
ec2e44fc 33# DESCRIPTION
34
44c88923
S
35'''
36
37
ec2e44fc 38def filter_excluded_sections(readme):
39 EXCLUDED_SECTION_BEGIN_STRING = re.escape('<!-- MANPAGE: BEGIN EXCLUDED SECTION -->')
40 EXCLUDED_SECTION_END_STRING = re.escape('<!-- MANPAGE: END EXCLUDED SECTION -->')
41 return re.sub(
42 rf'(?s){EXCLUDED_SECTION_BEGIN_STRING}.+?{EXCLUDED_SECTION_END_STRING}\n',
43 '', readme)
44
45
df0e138f
LHR
46def _convert_code_blocks(readme):
47 current_code_block = None
48
49 for line in readme.splitlines(True):
50 if current_code_block:
51 if line == current_code_block:
52 current_code_block = None
53 yield '\n'
54 else:
55 yield f' {line}'
56 elif line.startswith('```'):
57 current_code_block = line.count('`') * '`' + '\n'
58 yield '\n'
59 else:
60 yield line
61
62
63def convert_code_blocks(readme):
64 return ''.join(_convert_code_blocks(readme))
65
66
ec2e44fc 67def move_sections(readme):
68 MOVE_TAG_TEMPLATE = '<!-- MANPAGE: MOVE "%s" SECTION HERE -->'
0fcba15d 69 sections = re.findall(r'(?m)^%s$' % (
70 re.escape(MOVE_TAG_TEMPLATE).replace(r'\%', '%') % '(.+)'), readme)
ec2e44fc 71
72 for section_name in sections:
73 move_tag = MOVE_TAG_TEMPLATE % section_name
74 if readme.count(move_tag) > 1:
75 raise Exception(f'There is more than one occurrence of "{move_tag}". This is unexpected')
76
77 sections = re.findall(rf'(?sm)(^# {re.escape(section_name)}.+?)(?=^# )', readme)
78 if len(sections) < 1:
79 raise Exception(f'The section {section_name} does not exist')
80 elif len(sections) > 1:
81 raise Exception(f'There are multiple occurrences of section {section_name}, this is unhandled')
82
83 readme = readme.replace(sections[0], '', 1).replace(move_tag, sections[0], 1)
84 return readme
44c88923 85
bad84757
YCH
86
87def filter_options(readme):
ec2e44fc 88 section = re.search(r'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme).group(0)
df0e138f
LHR
89 section_new = section.replace('*', R'\*')
90
ec2e44fc 91 options = '# OPTIONS\n'
df0e138f 92 for line in section_new.split('\n')[1:]:
08d30158 93 mobj = re.fullmatch(r'''(?x)
94 \s{4}(?P<opt>-(?:,\s|[^\s])+)
95 (?:\s(?P<meta>(?:[^\s]|\s(?!\s))+))?
96 (\s{2,}(?P<desc>.+))?
97 ''', line)
b440e1bb 98 if not mobj:
99 options += f'{line.lstrip()}\n'
100 continue
101 option, metavar, description = mobj.group('opt', 'meta', 'desc')
102
103 # Pandoc's definition_lists. See http://pandoc.org/README.html
104 option = f'{option} *{metavar}*' if metavar else option
105 description = f'{description}\n' if description else ''
106 options += f'\n{option}\n: {description}'
107 continue
ec2e44fc 108
109 return readme.replace(section, options, 1)
bad84757 110
582be358 111
df0e138f 112TRANSFORM = compose_functions(filter_excluded_sections, convert_code_blocks, move_sections, filter_options)
115add43 113
114
115def main():
116 write_file(get_filename_args(), PREFIX + TRANSFORM(read_file(README_FILE)))
117
118
44c88923
S
119if __name__ == '__main__':
120 main()