2021-06-03 03:43:42 -06:00
|
|
|
#!/usr/bin/env python3
|
2022-06-24 05:06:16 -06:00
|
|
|
|
2022-08-08 13:38:47 -06:00
|
|
|
# Allow direct execution
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
|
2014-05-13 06:21:21 -06:00
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
|
2022-08-08 13:38:47 -06:00
|
|
|
from devscripts.utils import (
|
|
|
|
compose_functions,
|
|
|
|
get_filename_args,
|
|
|
|
read_file,
|
|
|
|
write_file,
|
|
|
|
)
|
|
|
|
|
2014-05-13 06:21:21 -06:00
|
|
|
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
README_FILE = os.path.join(ROOT_DIR, 'README.md')
|
|
|
|
|
2021-02-24 11:45:56 -07:00
|
|
|
PREFIX = r'''%yt-dlp(1)
|
2016-05-28 21:06:10 -06:00
|
|
|
|
|
|
|
# NAME
|
|
|
|
|
2024-03-19 11:14:04 -06:00
|
|
|
yt\-dlp \- A feature\-rich command\-line audio/video downloader
|
2016-05-28 21:06:10 -06:00
|
|
|
|
|
|
|
# SYNOPSIS
|
|
|
|
|
2021-02-24 11:45:56 -07:00
|
|
|
**yt-dlp** \[OPTIONS\] URL [URL...]
|
2016-05-28 21:06:10 -06:00
|
|
|
|
2021-12-16 18:23:04 -07:00
|
|
|
# DESCRIPTION
|
|
|
|
|
2016-05-28 21:06:10 -06:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2021-12-16 18:23:04 -07:00
|
|
|
def filter_excluded_sections(readme):
|
|
|
|
EXCLUDED_SECTION_BEGIN_STRING = re.escape('<!-- MANPAGE: BEGIN EXCLUDED SECTION -->')
|
|
|
|
EXCLUDED_SECTION_END_STRING = re.escape('<!-- MANPAGE: END EXCLUDED SECTION -->')
|
|
|
|
return re.sub(
|
|
|
|
rf'(?s){EXCLUDED_SECTION_BEGIN_STRING}.+?{EXCLUDED_SECTION_END_STRING}\n',
|
|
|
|
'', readme)
|
|
|
|
|
|
|
|
|
2024-04-08 13:18:04 -06:00
|
|
|
def _convert_code_blocks(readme):
|
|
|
|
current_code_block = None
|
|
|
|
|
|
|
|
for line in readme.splitlines(True):
|
|
|
|
if current_code_block:
|
|
|
|
if line == current_code_block:
|
|
|
|
current_code_block = None
|
|
|
|
yield '\n'
|
|
|
|
else:
|
|
|
|
yield f' {line}'
|
|
|
|
elif line.startswith('```'):
|
|
|
|
current_code_block = line.count('`') * '`' + '\n'
|
|
|
|
yield '\n'
|
|
|
|
else:
|
|
|
|
yield line
|
|
|
|
|
|
|
|
|
|
|
|
def convert_code_blocks(readme):
|
|
|
|
return ''.join(_convert_code_blocks(readme))
|
|
|
|
|
|
|
|
|
2021-12-16 18:23:04 -07:00
|
|
|
def move_sections(readme):
|
|
|
|
MOVE_TAG_TEMPLATE = '<!-- MANPAGE: MOVE "%s" SECTION HERE -->'
|
2021-12-20 06:13:33 -07:00
|
|
|
sections = re.findall(r'(?m)^%s$' % (
|
|
|
|
re.escape(MOVE_TAG_TEMPLATE).replace(r'\%', '%') % '(.+)'), readme)
|
2021-12-16 18:23:04 -07:00
|
|
|
|
|
|
|
for section_name in sections:
|
|
|
|
move_tag = MOVE_TAG_TEMPLATE % section_name
|
|
|
|
if readme.count(move_tag) > 1:
|
|
|
|
raise Exception(f'There is more than one occurrence of "{move_tag}". This is unexpected')
|
|
|
|
|
|
|
|
sections = re.findall(rf'(?sm)(^# {re.escape(section_name)}.+?)(?=^# )', readme)
|
|
|
|
if len(sections) < 1:
|
|
|
|
raise Exception(f'The section {section_name} does not exist')
|
|
|
|
elif len(sections) > 1:
|
|
|
|
raise Exception(f'There are multiple occurrences of section {section_name}, this is unhandled')
|
|
|
|
|
|
|
|
readme = readme.replace(sections[0], '', 1).replace(move_tag, sections[0], 1)
|
|
|
|
return readme
|
2016-05-28 21:06:10 -06:00
|
|
|
|
2015-09-13 06:10:23 -06:00
|
|
|
|
|
|
|
def filter_options(readme):
|
2021-12-16 18:23:04 -07:00
|
|
|
section = re.search(r'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme).group(0)
|
2024-04-08 13:18:04 -06:00
|
|
|
section_new = section.replace('*', R'\*')
|
|
|
|
|
2021-12-16 18:23:04 -07:00
|
|
|
options = '# OPTIONS\n'
|
2024-04-08 13:18:04 -06:00
|
|
|
for line in section_new.split('\n')[1:]:
|
2022-03-04 07:08:55 -07:00
|
|
|
mobj = re.fullmatch(r'''(?x)
|
|
|
|
\s{4}(?P<opt>-(?:,\s|[^\s])+)
|
|
|
|
(?:\s(?P<meta>(?:[^\s]|\s(?!\s))+))?
|
|
|
|
(\s{2,}(?P<desc>.+))?
|
|
|
|
''', line)
|
2022-02-24 04:32:19 -07:00
|
|
|
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
|
2021-12-16 18:23:04 -07:00
|
|
|
|
|
|
|
return readme.replace(section, options, 1)
|
2015-09-13 06:10:23 -06:00
|
|
|
|
2016-11-17 04:42:56 -07:00
|
|
|
|
2024-04-08 13:18:04 -06:00
|
|
|
TRANSFORM = compose_functions(filter_excluded_sections, convert_code_blocks, move_sections, filter_options)
|
2022-08-08 13:38:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
write_file(get_filename_args(), PREFIX + TRANSFORM(read_file(README_FILE)))
|
|
|
|
|
|
|
|
|
2016-05-28 21:06:10 -06:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|