Repository: yt-dlp/yt-dlp
File: devscripts/prepare_manpage.py (Python)

Code before:
        parser.error('Expected an output filename')

    outfile, = args

    with io.open(README_FILE, encoding='utf-8') as f:
        readme = f.read()

    readme = re.sub(r'(?s)^.*?(?=# DESCRIPTION)', '', readme)
    readme = re.sub(r'\s+yt-dlp \[OPTIONS\] URL \[URL\.\.\.\]', '', readme)
    readme = filter_excluded_sections(readme)
    readme = move_section('usage and options', readme)
    readme = move_section('installation', readme)
    readme = re.sub(r'^# USAGE AND OPTIONS$', '# OPTIONS', readme, 1, flags=re.M)
    readme = PREFIX + readme

    readme = filter_options(readme)

    with io.open(outfile, 'w', encoding='utf-8') as outf:
        outf.write(readme)


def filter_excluded_sections(readme):
    EXCLUDED_SECTION_BEGIN_STRING = '<!-- MANPAGE: BEGIN EXCLUDED SECTION -->'
    EXCLUDED_SECTION_END_STRING = '<!-- MANPAGE: END EXCLUDED SECTION -->'
    return re.sub(
        '(?s)%s.+?%s\n' % (EXCLUDED_SECTION_BEGIN_STRING, EXCLUDED_SECTION_END_STRING),
        '', readme)


def move_section(section_name, readme):
    section_name = section_name.upper()
    move_tag = '<!-- MANPAGE: MOVE \"%s\" SECTION HERE -->' % section_name
    section_pattern = '(?sm)(^# %s.+?)^# ' % section_name
    section = re.findall(section_pattern, readme)
    if len(section) < 1:
        raise Exception("The section %s does not exist" % section_name)
    elif len(section) > 1:
        raise Exception("There are multiple occurrences of section %s, this is unhandled" % section_name)
    else:
        pass
    readme_without_section = re.sub(section_pattern, '', readme, 1)
    if readme_without_section.count(move_tag) < 1:
        raise Exception('Moving the "%s" section was requested, but no "%s" marker was found.' % (section_name, move_tag))
    elif readme_without_section.count(move_tag) > 1:
        raise Warning('There is more than one occurrence of "%s". This is probably an accident.' % move_tag)
    return readme_without_section.replace(move_tag, section[0])


def filter_options(readme):
    ret = ''
    in_options = False

Review comment (security):
```suggestion
        rf'(?s){re.escape(EXCLUDED_SECTION_BEGIN_STRING)}.+?{re.escape(EXCLUDED_SECTION_END_STRING)}\n'
```
do `re.escape` when putting literal string inside regex

Code after:
        parser.error('Expected an output filename')

    outfile, = args

    with io.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:
        outf.write(PREFIX + readme)


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)


def move_sections(readme):
    MOVE_TAG_TEMPLATE = '<!-- MANPAGE: MOVE "%s" SECTION HERE -->'
    sections = re.findall(rf'(?m)^{re.escape(MOVE_TAG_TEMPLATE) % "(.+)"}$', readme)

    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


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: