prepare_manpage.py 2.9 KB

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