api.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import json
  4. def parse_line(line, category):
  5. name = line.split('](')[0][3:]
  6. description = line.split(') ')[1][2:]
  7. url = line.split('](')[1].split(') ')[0]
  8. return {
  9. 'name': name,
  10. 'description': description,
  11. 'url': url,
  12. 'category': category,
  13. }
  14. def parse_apps(readme):
  15. app_section = readme.split('## Apps')[1].split('## Tools')[0]
  16. app_sections = app_section.split('### ')[1:]
  17. apps = []
  18. for section in app_sections:
  19. split_section = section.split('\n\n')
  20. section_title = split_section[0]
  21. for line in split_section[1].split('\n'):
  22. app = parse_line(line, section_title)
  23. apps.append(app)
  24. return apps
  25. def parse_readme(readme):
  26. return {
  27. 'apps': parse_apps(readme),
  28. }
  29. if __name__ == '__main__':
  30. readme = open('README.md', 'r').read()
  31. readme_payload = parse_readme(readme)
  32. with open('./dist/api/v1/readme.json', mode='w+', encoding='utf-8') as f:
  33. f.write(json.dumps(readme_payload, indent=True))