You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.6 KiB

  1. from __future__ import unicode_literals
  2. import io
  3. import os.path
  4. import sys
  5. import re
  6. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  7. README_FILE = os.path.join(ROOT_DIR, 'README.md')
  8. def filter_options(readme):
  9. ret = ''
  10. in_options = False
  11. for line in readme.split('\n'):
  12. if line.startswith('# '):
  13. if line[2:].startswith('OPTIONS'):
  14. in_options = True
  15. else:
  16. in_options = False
  17. if in_options:
  18. if line.lstrip().startswith('-'):
  19. option, description = re.split(r'\s{2,}', line.lstrip())
  20. split_option = option.split(' ')
  21. if not split_option[-1].startswith('-'): # metavar
  22. option = ' '.join(split_option[:-1] + ['*%s*' % split_option[-1]])
  23. # Pandoc's definition_lists. See http://pandoc.org/README.html
  24. # for more information.
  25. ret += '\n%s\n: %s\n' % (option, description)
  26. else:
  27. ret += line.lstrip() + '\n'
  28. else:
  29. ret += line + '\n'
  30. return ret
  31. with io.open(README_FILE, encoding='utf-8') as f:
  32. readme = f.read()
  33. PREFIX = '''%YOUTUBE-DL(1)
  34. # NAME
  35. youtube\-dl \- download videos from youtube.com or other video platforms
  36. # SYNOPSIS
  37. **youtube-dl** \[OPTIONS\] URL [URL...]
  38. '''
  39. readme = re.sub(r'(?s)^.*?(?=# DESCRIPTION)', '', readme)
  40. readme = re.sub(r'\s+youtube-dl \[OPTIONS\] URL \[URL\.\.\.\]', '', readme)
  41. readme = PREFIX + readme
  42. readme = filter_options(readme)
  43. if sys.version_info < (3, 0):
  44. print(readme.encode('utf-8'))
  45. else:
  46. print(readme)