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