]> jfr.im git - yt-dlp.git/blobdiff - devscripts/prepare_manpage.py
[test] Split download tests so they can be more easily run in CI
[yt-dlp.git] / devscripts / prepare_manpage.py
index b4446a36880ecc7b24a149f42ce381826e7d52f2..cea934949972f426b84ed384e3cce58881202777 100644 (file)
@@ -1,7 +1,5 @@
 #!/usr/bin/env python3
-from __future__ import unicode_literals
 
-import io
 import optparse
 import os.path
 import re
 
 def main():
     parser = optparse.OptionParser(usage='%prog OUTFILE.md')
-    options, args = parser.parse_args()
+    _, args = parser.parse_args()
     if len(args) != 1:
         parser.error('Expected an output filename')
 
     outfile, = args
 
-    with io.open(README_FILE, encoding='utf-8') as f:
+    with open(README_FILE, encoding='utf-8') as f:
         readme = f.read()
 
     readme = filter_excluded_sections(readme)
     readme = move_sections(readme)
     readme = filter_options(readme)
 
-    with io.open(outfile, 'w', encoding='utf-8') as outf:
+    with open(outfile, 'w', encoding='utf-8') as outf:
         outf.write(PREFIX + readme)
 
 
@@ -53,7 +51,8 @@ def filter_excluded_sections(readme):
 
 def move_sections(readme):
     MOVE_TAG_TEMPLATE = '<!-- MANPAGE: MOVE "%s" SECTION HERE -->'
-    sections = re.findall(rf'(?m)^{re.escape(MOVE_TAG_TEMPLATE) % "(.+)"}$', readme)
+    sections = re.findall(r'(?m)^%s$' % (
+        re.escape(MOVE_TAG_TEMPLATE).replace(r'\%', '%') % '(.+)'), readme)
 
     for section_name in sections:
         move_tag = MOVE_TAG_TEMPLATE % section_name
@@ -74,21 +73,21 @@ def filter_options(readme):
     section = re.search(r'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme).group(0)
     options = '# OPTIONS\n'
     for line in section.split('\n')[1:]:
-        if line.lstrip().startswith('-'):
-            split = re.split(r'\s{2,}', line.lstrip())
-            # Description string may start with `-` as well. If there is
-            # only one piece then it's a description bit not an option.
-            if len(split) > 1:
-                option, description = split
-                split_option = option.split(' ')
-
-                if not split_option[-1].startswith('-'):  # metavar
-                    option = ' '.join(split_option[:-1] + [f'*{split_option[-1]}*'])
-
-                # Pandoc's definition_lists. See http://pandoc.org/README.html
-                options += f'\n{option}\n:   {description}\n'
-                continue
-        options += line.lstrip() + '\n'
+        mobj = re.fullmatch(r'''(?x)
+                \s{4}(?P<opt>-(?:,\s|[^\s])+)
+                (?:\s(?P<meta>(?:[^\s]|\s(?!\s))+))?
+                (\s{2,}(?P<desc>.+))?
+            ''', line)
+        if not mobj:
+            options += f'{line.lstrip()}\n'
+            continue
+        option, metavar, description = mobj.group('opt', 'meta', 'desc')
+
+        # Pandoc's definition_lists. See http://pandoc.org/README.html
+        option = f'{option} *{metavar}*' if metavar else option
+        description = f'{description}\n' if description else ''
+        options += f'\n{option}\n:   {description}'
+        continue
 
     return readme.replace(section, options, 1)