]> jfr.im git - yt-dlp.git/blob - devscripts/prepare_manpage.py
[lazy_extractors] Fix for search IEs
[yt-dlp.git] / devscripts / prepare_manpage.py
1 #!/usr/bin/env python3
2 from __future__ import unicode_literals
3
4 import io
5 import optparse
6 import os.path
7 import re
8
9 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
10 README_FILE = os.path.join(ROOT_DIR, 'README.md')
11
12 PREFIX = r'''%yt-dlp(1)
13
14 # NAME
15
16 youtube\-dl \- download videos from youtube.com or other video platforms
17
18 # SYNOPSIS
19
20 **yt-dlp** \[OPTIONS\] URL [URL...]
21
22 '''
23
24
25 def main():
26 parser = optparse.OptionParser(usage='%prog OUTFILE.md')
27 options, args = parser.parse_args()
28 if len(args) != 1:
29 parser.error('Expected an output filename')
30
31 outfile, = args
32
33 with io.open(README_FILE, encoding='utf-8') as f:
34 readme = f.read()
35
36 readme = re.sub(r'(?s)^.*?(?=# DESCRIPTION)', '', readme)
37 readme = re.sub(r'\s+yt-dlp \[OPTIONS\] URL \[URL\.\.\.\]', '', readme)
38 readme = PREFIX + readme
39
40 readme = filter_options(readme)
41
42 with io.open(outfile, 'w', encoding='utf-8') as outf:
43 outf.write(readme)
44
45
46 def filter_options(readme):
47 ret = ''
48 in_options = False
49 for line in readme.split('\n'):
50 if line.startswith('# '):
51 if line[2:].startswith('OPTIONS'):
52 in_options = True
53 else:
54 in_options = False
55
56 if in_options:
57 if line.lstrip().startswith('-'):
58 split = re.split(r'\s{2,}', line.lstrip())
59 # Description string may start with `-` as well. If there is
60 # only one piece then it's a description bit not an option.
61 if len(split) > 1:
62 option, description = split
63 split_option = option.split(' ')
64
65 if not split_option[-1].startswith('-'): # metavar
66 option = ' '.join(split_option[:-1] + ['*%s*' % split_option[-1]])
67
68 # Pandoc's definition_lists. See http://pandoc.org/README.html
69 # for more information.
70 ret += '\n%s\n: %s\n' % (option, description)
71 continue
72 ret += line.lstrip() + '\n'
73 else:
74 ret += line + '\n'
75
76 return ret
77
78
79 if __name__ == '__main__':
80 main()