prepare_manpage.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(rf'(?m)^{re.escape(MOVE_TAG_TEMPLATE) % "(.+)"}$', readme)
  38. for section_name in sections:
  39. move_tag = MOVE_TAG_TEMPLATE % section_name
  40. if readme.count(move_tag) > 1:
  41. raise Exception(f'There is more than one occurrence of "{move_tag}". This is unexpected')
  42. sections = re.findall(rf'(?sm)(^# {re.escape(section_name)}.+?)(?=^# )', readme)
  43. if len(sections) < 1:
  44. raise Exception(f'The section {section_name} does not exist')
  45. elif len(sections) > 1:
  46. raise Exception(f'There are multiple occurrences of section {section_name}, this is unhandled')
  47. readme = readme.replace(sections[0], '', 1).replace(move_tag, sections[0], 1)
  48. return readme
  49. def filter_options(readme):
  50. section = re.search(r'(?sm)^# USAGE AND OPTIONS\n.+?(?=^# )', readme).group(0)
  51. options = '# OPTIONS\n'
  52. for line in section.split('\n')[1:]:
  53. if line.lstrip().startswith('-'):
  54. split = re.split(r'\s{2,}', line.lstrip())
  55. # Description string may start with `-` as well. If there is
  56. # only one piece then it's a description bit not an option.
  57. if len(split) > 1:
  58. option, description = split
  59. split_option = option.split(' ')
  60. if not split_option[-1].startswith('-'): # metavar
  61. option = ' '.join(split_option[:-1] + [f'*{split_option[-1]}*'])
  62. # Pandoc's definition_lists. See http://pandoc.org/README.html
  63. options += f'\n{option}\n: {description}\n'
  64. continue
  65. options += line.lstrip() + '\n'
  66. return readme.replace(section, options, 1)
  67. if __name__ == '__main__':
  68. main()