1
0

build_search_data.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. import ast
  3. import json
  4. import os
  5. from pathlib import Path
  6. from icecream import ic
  7. dir_path = os.path.dirname(os.path.abspath(__file__))
  8. os.chdir(dir_path)
  9. class DemoVisitor(ast.NodeVisitor):
  10. def __init__(self, topic: str) -> None:
  11. super().__init__()
  12. self.topic = topic
  13. def visit_FunctionDef(self, node):
  14. for decorator in node.decorator_list:
  15. if isinstance(decorator, ast.Call):
  16. function = decorator.func
  17. if isinstance(function, ast.Name) and function.id == 'text_demo':
  18. title = decorator.args[0].s
  19. content = ' '.join([l.strip() for l in decorator.args[1].s.splitlines()]).strip()
  20. anchor = title.lower().replace(' ', '_')
  21. url = f'/documentation/{self.topic}#{anchor}'
  22. documents.append({
  23. 'title': f'{self.topic}: {title}',
  24. 'content': content,
  25. 'url': url
  26. })
  27. self.generic_visit(node)
  28. documents = []
  29. for file in Path('./more_documentation').glob('*.py'):
  30. with open(file, 'r') as source:
  31. tree = ast.parse(source.read())
  32. DemoVisitor('_'.join(file.stem.split('_')[:-1])).visit(tree)
  33. with open('static/search_data.json', 'w') as f:
  34. json.dump(documents, f, indent=2)