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.

49 regels
1.3 KiB

  1. #!/usr/bin/env python3
  2. import sys
  3. import json
  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 \
  11. and 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. 'Link': chunks[4].replace('[Go!]', '')[1:-1],
  23. 'Category': category,
  24. }
  25. entries.append(entry)
  26. final = {
  27. 'count': len(entries),
  28. 'entries': entries,
  29. }
  30. return json.dumps(final)
  31. def main():
  32. num_args = len(sys.argv)
  33. if num_args < 2:
  34. print("No .md file passed")
  35. sys.exit(1)
  36. if num_args < 3:
  37. anchor = '###'
  38. else:
  39. anchor = sys.argv[2]
  40. print(markdown_to_json(sys.argv[1], anchor))
  41. if __name__ == "__main__":
  42. main()