]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
dcddc10a | 2 | from __future__ import unicode_literals |
1800eeef PH |
3 | |
4 | import io | |
44c88923 | 5 | import optparse |
1800eeef | 6 | import os.path |
1800eeef PH |
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 | ||
7a5c1cfe | 12 | PREFIX = r'''%yt-dlp(1) |
44c88923 S |
13 | |
14 | # NAME | |
15 | ||
16 | youtube\-dl \- download videos from youtube.com or other video platforms | |
17 | ||
18 | # SYNOPSIS | |
19 | ||
7a5c1cfe | 20 | **yt-dlp** \[OPTIONS\] URL [URL...] |
44c88923 S |
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) | |
7a5c1cfe | 37 | readme = re.sub(r'\s+yt-dlp \[OPTIONS\] URL \[URL\.\.\.\]', '', readme) |
44c88923 S |
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 | ||
bad84757 YCH |
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('-'): | |
cc9c8ce5 S |
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' | |
bad84757 YCH |
73 | else: |
74 | ret += line + '\n' | |
75 | ||
76 | return ret | |
77 | ||
582be358 | 78 | |
44c88923 S |
79 | if __name__ == '__main__': |
80 | main() |