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.

79 lines
2.1 KiB

  1. from __future__ import unicode_literals
  2. import io
  3. import optparse
  4. import os.path
  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. PREFIX = '''%YOUTUBE-DL(1)
  9. # NAME
  10. youtube\-dl \- download videos from youtube.com or other video platforms
  11. # SYNOPSIS
  12. **youtube-dl** \[OPTIONS\] URL [URL...]
  13. '''
  14. def main():
  15. parser = optparse.OptionParser(usage='%prog OUTFILE.md')
  16. options, args = parser.parse_args()
  17. if len(args) != 1:
  18. parser.error('Expected an output filename')
  19. outfile, = args
  20. with io.open(README_FILE, encoding='utf-8') as f:
  21. readme = f.read()
  22. readme = re.sub(r'(?s)^.*?(?=# DESCRIPTION)', '', readme)
  23. readme = re.sub(r'\s+youtube-dl \[OPTIONS\] URL \[URL\.\.\.\]', '', readme)
  24. readme = PREFIX + readme
  25. readme = filter_options(readme)
  26. with io.open(outfile, 'w', encoding='utf-8') as outf:
  27. outf.write(readme)
  28. def filter_options(readme):
  29. ret = ''
  30. in_options = False
  31. for line in readme.split('\n'):
  32. if line.startswith('# '):
  33. if line[2:].startswith('OPTIONS'):
  34. in_options = True
  35. else:
  36. in_options = False
  37. if in_options:
  38. if line.lstrip().startswith('-'):
  39. split = re.split(r'\s{2,}', line.lstrip())
  40. # Description string may start with `-` as well. If there is
  41. # only one piece then it's a description bit not an option.
  42. if len(split) > 1:
  43. option, description = split
  44. split_option = option.split(' ')
  45. if not split_option[-1].startswith('-'): # metavar
  46. option = ' '.join(split_option[:-1] + ['*%s*' % split_option[-1]])
  47. # Pandoc's definition_lists. See http://pandoc.org/README.html
  48. # for more information.
  49. ret += '\n%s\n: %s\n' % (option, description)
  50. continue
  51. ret += line.lstrip() + '\n'
  52. else:
  53. ret += line + '\n'
  54. return ret
  55. if __name__ == '__main__':
  56. main()