]>
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 | ||
ec2e44fc | 16 | yt\-dlp \- A youtube-dl fork with additional features and patches |
44c88923 S |
17 | |
18 | # SYNOPSIS | |
19 | ||
7a5c1cfe | 20 | **yt-dlp** \[OPTIONS\] URL [URL...] |
44c88923 | 21 | |
ec2e44fc | 22 | # DESCRIPTION |
23 | ||
44c88923 S |
24 | ''' |
25 | ||
26 | ||
27 | def main(): | |
28 | parser = optparse.OptionParser(usage='%prog OUTFILE.md') | |
29 | options, args = parser.parse_args() | |
30 | if len(args) != 1: | |
31 | parser.error('Expected an output filename') | |
32 | ||
33 | outfile, = args | |
34 | ||
35 | with io.open(README_FILE, encoding='utf-8') as f: | |
36 | readme = f.read() | |
37 | ||
ec2e44fc | 38 | readme = filter_excluded_sections(readme) |
39 | readme = move_sections(readme) | |
44c88923 S |
40 | readme = filter_options(readme) |
41 | ||
42 | with io.open(outfile, 'w', encoding='utf-8') as outf: | |
ec2e44fc | 43 | outf.write(PREFIX + readme) |
44 | ||
45 | ||
46 | def filter_excluded_sections(readme): | |
47 | EXCLUDED_SECTION_BEGIN_STRING = re.escape('<!-- MANPAGE: BEGIN EXCLUDED SECTION -->') | |
48 | EXCLUDED_SECTION_END_STRING = re.escape('<!-- MANPAGE: END EXCLUDED SECTION -->') | |
49 | return re.sub( | |
50 | rf'(?s){EXCLUDED_SECTION_BEGIN_STRING}.+?{EXCLUDED_SECTION_END_STRING}\n', | |
51 | '', readme) | |
52 | ||
53 | ||
54 | def move_sections(readme): | |
55 | MOVE_TAG_TEMPLATE = '<!-- MANPAGE: MOVE "%s" SECTION HERE -->' | |
0fcba15d | 56 | sections = re.findall(r'(?m)^%s$' % ( |
57 | re.escape(MOVE_TAG_TEMPLATE).replace(r'\%', '%') % '(.+)'), readme) | |
ec2e44fc | 58 | |
59 | for section_name in sections: | |
60 | move_tag = MOVE_TAG_TEMPLATE % section_name | |
61 | if readme.count(move_tag) > 1: | |
62 | raise Exception(f'There is more than one occurrence of "{move_tag}". This is unexpected') | |
63 | ||
64 | sections = re.findall(rf'(?sm)(^# {re.escape(section_name)}.+?)(?=^# )', readme) | |
65 | if len(sections) < 1: | |
66 | raise Exception(f'The section {section_name} does not exist') | |
67 | elif len(sections) > 1: | |
68 | raise Exception(f'There are multiple occurrences of section {section_name}, this is unhandled') | |
69 | ||
70 | readme = readme.replace(sections[0], '', 1).replace(move_tag, sections[0], 1) | |
71 | return readme | |
44c88923 | 72 | |
bad84757 YCH |
73 | |
74 | def filter_options(readme): | |
ec2e44fc | 75 | section = re.search(r'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme).group(0) |
76 | options = '# OPTIONS\n' | |
77 | for line in section.split('\n')[1:]: | |
78 | if line.lstrip().startswith('-'): | |
79 | split = re.split(r'\s{2,}', line.lstrip()) | |
80 | # Description string may start with `-` as well. If there is | |
81 | # only one piece then it's a description bit not an option. | |
82 | if len(split) > 1: | |
83 | option, description = split | |
84 | split_option = option.split(' ') | |
85 | ||
86 | if not split_option[-1].startswith('-'): # metavar | |
87 | option = ' '.join(split_option[:-1] + [f'*{split_option[-1]}*']) | |
88 | ||
89 | # Pandoc's definition_lists. See http://pandoc.org/README.html | |
90 | options += f'\n{option}\n: {description}\n' | |
91 | continue | |
92 | options += line.lstrip() + '\n' | |
93 | ||
94 | return readme.replace(section, options, 1) | |
bad84757 | 95 | |
582be358 | 96 | |
44c88923 S |
97 | if __name__ == '__main__': |
98 | main() |