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.

51 lines
1.4 KiB

  1. #!/usr/bin/env python3
  2. import json
  3. import sys
  4. def markdown_to_json(filename, anchor):
  5. """Convert a Markdown file into a JSON string"""
  6. category = ""
  7. entries = []
  8. with open(filename) as fp:
  9. lines = (line.rstrip() for line in fp)
  10. lines = list(line for line in lines if line and
  11. line.startswith(anchor) or line.startswith('| '))
  12. for line in lines:
  13. if line.startswith(anchor):
  14. category = line.split(anchor)[1].strip()
  15. continue
  16. chunks = [x.strip() for x in line.split('|')[1:-1]]
  17. entry = {
  18. 'API': chunks[0],
  19. 'Description': chunks[1],
  20. 'Auth': None if chunks[2].upper() == 'NO' else chunks[2].strip('`'),
  21. 'HTTPS': True if chunks[3].upper() == 'YES' else False,
  22. 'CORS': chunks[4].strip('`'),
  23. 'Link': chunks[5].replace('[Go!]', '')[1:-1],
  24. 'Category': category,
  25. }
  26. entries.append(entry)
  27. final = {
  28. 'count': len(entries),
  29. 'entries': entries,
  30. }
  31. return json.dumps(final)
  32. def main():
  33. num_args = len(sys.argv)
  34. if num_args < 2:
  35. print("No .md file passed")
  36. sys.exit(1)
  37. if num_args < 3:
  38. anchor = '###'
  39. else:
  40. anchor = sys.argv[2]
  41. print(markdown_to_json(sys.argv[1], anchor))
  42. if __name__ == "__main__":
  43. main()