prepare_manpage.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. import os.path
  7. import re
  8. from devscripts.utils import (
  9. compose_functions,
  10. get_filename_args,
  11. read_file,
  12. write_file,
  13. )
  14. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  15. README_FILE = os.path.join(ROOT_DIR, 'README.md')
  16. PREFIX = r'''%HYPERVIDEO(1)
  17. # NAME
  18. yt\-dlp \- A youtube-dl fork with additional features and patches
  19. # SYNOPSIS
  20. **hypervideo** \[OPTIONS\] URL [URL...]
  21. # DESCRIPTION
  22. '''
  23. def filter_excluded_sections(readme):
  24. EXCLUDED_SECTION_BEGIN_STRING = re.escape('<!-- MANPAGE: BEGIN EXCLUDED SECTION -->')
  25. EXCLUDED_SECTION_END_STRING = re.escape('<!-- MANPAGE: END EXCLUDED SECTION -->')
  26. return re.sub(
  27. rf'(?s){EXCLUDED_SECTION_BEGIN_STRING}.+?{EXCLUDED_SECTION_END_STRING}\n',
  28. '', readme)
  29. def move_sections(readme):
  30. MOVE_TAG_TEMPLATE = '<!-- MANPAGE: MOVE "%s" SECTION HERE -->'
  31. sections = re.findall(r'(?m)^%s$' % (
  32. re.escape(MOVE_TAG_TEMPLATE).replace(r'\%', '%') % '(.+)'), readme)
  33. for section_name in sections:
  34. move_tag = MOVE_TAG_TEMPLATE % section_name
  35. if readme.count(move_tag) > 1:
  36. raise Exception(f'There is more than one occurrence of "{move_tag}". This is unexpected')
  37. sections = re.findall(rf'(?sm)(^# {re.escape(section_name)}.+?)(?=^# )', readme)
  38. if len(sections) < 1:
  39. raise Exception(f'The section {section_name} does not exist')
  40. elif len(sections) > 1:
  41. raise Exception(f'There are multiple occurrences of section {section_name}, this is unhandled')
  42. readme = readme.replace(sections[0], '', 1).replace(move_tag, sections[0], 1)
  43. return readme
  44. def filter_options(readme):
  45. section = re.search(r'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme).group(0)
  46. options = '# OPTIONS\n'
  47. for line in section.split('\n')[1:]:
  48. mobj = re.fullmatch(r'''(?x)
  49. \s{4}(?P<opt>-(?:,\s|[^\s])+)
  50. (?:\s(?P<meta>(?:[^\s]|\s(?!\s))+))?
  51. (\s{2,}(?P<desc>.+))?
  52. ''', line)
  53. if not mobj:
  54. options += f'{line.lstrip()}\n'
  55. continue
  56. option, metavar, description = mobj.group('opt', 'meta', 'desc')
  57. # Pandoc's definition_lists. See http://pandoc.org/README.html
  58. option = f'{option} *{metavar}*' if metavar else option
  59. description = f'{description}\n' if description else ''
  60. options += f'\n{option}\n: {description}'
  61. continue
  62. return readme.replace(section, options, 1)
  63. TRANSFORM = compose_functions(filter_excluded_sections, move_sections, filter_options)
  64. def main():
  65. write_file(get_filename_args(), PREFIX + TRANSFORM(read_file(README_FILE)))
  66. if __name__ == '__main__':
  67. main()