prepare_manpage.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. if line.lstrip().startswith('-'):
  55. split = re.split(r'\s{2,}', line.lstrip())
  56. # Description string may start with `-` as well. If there is
  57. # only one piece then it's a description bit not an option.
  58. if len(split) > 1:
  59. option, description = split
  60. split_option = option.split(' ')
  61. if not split_option[-1].startswith('-'): # metavar
  62. option = ' '.join(split_option[:-1] + [f'*{split_option[-1]}*'])
  63. # Pandoc's definition_lists. See http://pandoc.org/README.html
  64. options += f'\n{option}\n: {description}\n'
  65. continue
  66. options += line.lstrip() + '\n'
  67. return readme.replace(section, options, 1)
  68. if __name__ == '__main__':
  69. main()