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.

45 lines
1.2 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()[1]
  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. return json.dumps(entries)
  27. def main():
  28. num_args = len(sys.argv)
  29. if num_args < 2:
  30. print("No .md file passed")
  31. sys.exit(1)
  32. if num_args < 3:
  33. anchor = '###'
  34. else:
  35. anchor = sys.argv[2]
  36. print(markdown_to_json(sys.argv[1], anchor))
  37. if __name__ == "__main__":
  38. main()