prepare_manpage.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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'''%yt-dlp(1)
  17. # NAME
  18. yt\-dlp \- A feature\-rich command\-line audio/video downloader
  19. # SYNOPSIS
  20. **yt-dlp** \[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 _convert_code_blocks(readme):
  30. current_code_block = None
  31. for line in readme.splitlines(True):
  32. if current_code_block:
  33. if line == current_code_block:
  34. current_code_block = None
  35. yield '\n'
  36. else:
  37. yield f' {line}'
  38. elif line.startswith('```'):
  39. current_code_block = line.count('`') * '`' + '\n'
  40. yield '\n'
  41. else:
  42. yield line
  43. def convert_code_blocks(readme):
  44. return ''.join(_convert_code_blocks(readme))
  45. def move_sections(readme):
  46. MOVE_TAG_TEMPLATE = '<!-- MANPAGE: MOVE "%s" SECTION HERE -->'
  47. sections = re.findall(r'(?m)^%s$' % (
  48. re.escape(MOVE_TAG_TEMPLATE).replace(r'\%', '%') % '(.+)'), readme)
  49. for section_name in sections:
  50. move_tag = MOVE_TAG_TEMPLATE % section_name
  51. if readme.count(move_tag) > 1:
  52. raise Exception(f'There is more than one occurrence of "{move_tag}". This is unexpected')
  53. sections = re.findall(rf'(?sm)(^# {re.escape(section_name)}.+?)(?=^# )', readme)
  54. if len(sections) < 1:
  55. raise Exception(f'The section {section_name} does not exist')
  56. elif len(sections) > 1:
  57. raise Exception(f'There are multiple occurrences of section {section_name}, this is unhandled')
  58. readme = readme.replace(sections[0], '', 1).replace(move_tag, sections[0], 1)
  59. return readme
  60. def filter_options(readme):
  61. section = re.search(r'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme).group(0)
  62. section_new = section.replace('*', R'\*')
  63. options = '# OPTIONS\n'
  64. for line in section_new.split('\n')[1:]:
  65. mobj = re.fullmatch(r'''(?x)
  66. \s{4}(?P<opt>-(?:,\s|[^\s])+)
  67. (?:\s(?P<meta>(?:[^\s]|\s(?!\s))+))?
  68. (\s{2,}(?P<desc>.+))?
  69. ''', line)
  70. if not mobj:
  71. options += f'{line.lstrip()}\n'
  72. continue
  73. option, metavar, description = mobj.group('opt', 'meta', 'desc')
  74. # Pandoc's definition_lists. See http://pandoc.org/README.html
  75. option = f'{option} *{metavar}*' if metavar else option
  76. description = f'{description}\n' if description else ''
  77. options += f'\n{option}\n: {description}'
  78. continue
  79. return readme.replace(section, options, 1)
  80. TRANSFORM = compose_functions(filter_excluded_sections, convert_code_blocks, move_sections, filter_options)
  81. def main():
  82. write_file(get_filename_args(), PREFIX + TRANSFORM(read_file(README_FILE)))
  83. if __name__ == '__main__':
  84. main()