prepare_manpage.py 2.9 KB

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