Merge pull request #6928 from remitamine/cnet
[youtube-dl] / devscripts / prepare_manpage.py
1 from __future__ import unicode_literals
2
3 import io
4 import os.path
5 import sys
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
11
12 def filter_options(readme):
13     ret = ''
14     in_options = False
15     for line in readme.split('\n'):
16         if line.startswith('# '):
17             if line[2:].startswith('OPTIONS'):
18                 in_options = True
19             else:
20                 in_options = False
21
22         if in_options:
23             if line.lstrip().startswith('-'):
24                 option, description = re.split(r'\s{2,}', line.lstrip())
25                 split_option = option.split(' ')
26
27                 if not split_option[-1].startswith('-'):  # metavar
28                     option = ' '.join(split_option[:-1] + ['*%s*' % split_option[-1]])
29
30                 # Pandoc's definition_lists. See http://pandoc.org/README.html
31                 # for more information.
32                 ret += '\n%s\n:   %s\n' % (option, description)
33             else:
34                 ret += line.lstrip() + '\n'
35         else:
36             ret += line + '\n'
37
38     return ret
39
40 with io.open(README_FILE, encoding='utf-8') as f:
41     readme = f.read()
42
43 PREFIX = '''%YOUTUBE-DL(1)
44
45 # NAME
46
47 youtube\-dl \- download videos from youtube.com or other video platforms
48
49 # SYNOPSIS
50
51 **youtube-dl** \[OPTIONS\] URL [URL...]
52
53 '''
54 readme = re.sub(r'(?s)^.*?(?=# DESCRIPTION)', '', readme)
55 readme = re.sub(r'\s+youtube-dl \[OPTIONS\] URL \[URL\.\.\.\]', '', readme)
56 readme = PREFIX + readme
57
58 readme = filter_options(readme)
59
60 if sys.version_info < (3, 0):
61     print(readme.encode('utf-8'))
62 else:
63     print(readme)