1
0

build_search_index.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env python3
  2. import ast
  3. import json
  4. import os
  5. import re
  6. from _ast import AsyncFunctionDef
  7. from pathlib import Path
  8. from typing import List, Optional, Union
  9. from nicegui import app, ui
  10. dir_path = Path(__file__).parent
  11. os.chdir(dir_path)
  12. def ast_string_node_to_string(node):
  13. if isinstance(node, ast.Str):
  14. return node.s
  15. elif isinstance(node, ast.JoinedStr):
  16. return ''.join(ast_string_node_to_string(part) for part in node.values)
  17. else:
  18. return str(ast.unparse(node))
  19. def cleanup(markdown_string: str) -> str:
  20. # Remove link URLs but keep the description
  21. markdown_string = re.sub(r'\[([^\[]+)\]\([^\)]+\)', r'\1', markdown_string)
  22. # Remove inline code ticks
  23. markdown_string = re.sub(r'`([^`]+)`', r'\1', markdown_string)
  24. # Remove code blocks
  25. markdown_string = re.sub(r'```([^`]+)```', r'\1', markdown_string)
  26. markdown_string = re.sub(r'``([^`]+)``', r'\1', markdown_string)
  27. # Remove braces
  28. markdown_string = re.sub(r'\{([^\}]+)\}', r'\1', markdown_string)
  29. return markdown_string
  30. class DocVisitor(ast.NodeVisitor):
  31. def __init__(self, topic: Optional[str] = None) -> None:
  32. super().__init__()
  33. self.topic = topic
  34. self.current_title = None
  35. self.current_content: List[str] = []
  36. def visit_Call(self, node: ast.Call):
  37. if isinstance(node.func, ast.Name):
  38. function_name = node.func.id
  39. elif isinstance(node.func, ast.Attribute):
  40. function_name = node.func.attr
  41. else:
  42. raise NotImplementedError(f'Unknown function type: {node.func}')
  43. if function_name in ['heading', 'subheading']:
  44. self.on_new_heading()
  45. self.current_title = node.args[0].s
  46. elif function_name == 'markdown':
  47. if node.args:
  48. raw = ast_string_node_to_string(node.args[0]).splitlines()
  49. raw = ' '.join(l.strip() for l in raw).strip()
  50. self.current_content.append(cleanup(raw))
  51. self.generic_visit(node)
  52. def on_new_heading(self) -> None:
  53. if self.current_title:
  54. self.add_to_search_index(self.current_title, self.current_content if self.current_content else 'Overview')
  55. self.current_content = []
  56. def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> None:
  57. self.visit_FunctionDef(node)
  58. def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
  59. if node.name == 'main_demo':
  60. docstring = ast.get_docstring(node)
  61. if docstring is None:
  62. api = getattr(ui, self.topic) if hasattr(ui, self.topic) else getattr(app, self.topic)
  63. docstring = api.__doc__ or api.__init__.__doc__
  64. lines = cleanup(docstring).splitlines()
  65. self.add_to_search_index(lines[0], lines[1:], main=True)
  66. for decorator in node.decorator_list:
  67. if isinstance(decorator, ast.Call):
  68. function = decorator.func
  69. if isinstance(function, ast.Name) and function.id == 'text_demo':
  70. title = decorator.args[0].s
  71. content = cleanup(decorator.args[1].s).splitlines()
  72. self.add_to_search_index(title, content)
  73. if isinstance(function, ast.Name) and function.id == 'element_demo':
  74. attr_name = decorator.args[0].attr
  75. obj_name = decorator.args[0].value.id
  76. if obj_name == 'app':
  77. docstring: str = getattr(app, attr_name).__doc__
  78. docstring = ' '.join(l.strip() for l in docstring.splitlines()).strip()
  79. self.current_content.append(cleanup(docstring))
  80. else:
  81. print(f'Unknown object: {obj_name} for element_demo', flush=True)
  82. self.generic_visit(node)
  83. def add_to_search_index(self, title: str, content: Union[str, list], main: bool = False) -> None:
  84. if isinstance(content, list):
  85. content_str = ' '.join(l.strip() for l in content).strip()
  86. else:
  87. content_str = content
  88. anchor = title.lower().replace(' ', '_')
  89. url = f'/documentation/{self.topic or ""}'
  90. if not main:
  91. url += f'#{anchor}'
  92. if self.topic:
  93. title = f'{self.topic.replace("_", " ").title()}: {title}'
  94. documents.append({
  95. 'title': title,
  96. 'content': content_str,
  97. 'url': url,
  98. })
  99. class MainVisitor(ast.NodeVisitor):
  100. def visit_Call(self, node: ast.Call):
  101. if isinstance(node.func, ast.Name):
  102. function_name = node.func.id
  103. elif isinstance(node.func, ast.Attribute):
  104. function_name = node.func.attr
  105. else:
  106. return
  107. if function_name == 'example_link':
  108. title = ast_string_node_to_string(node.args[0])
  109. name = name = title.lower().replace(' ', '_')
  110. documents.append({
  111. 'title': 'Example: ' + title,
  112. 'content': ast_string_node_to_string(node.args[1]),
  113. 'url': f'https://github.com/zauberzeug/nicegui/tree/main/examples/{name}/main.py',
  114. })
  115. def generate_for(file: Path, topic: Optional[str] = None) -> None:
  116. tree = ast.parse(file.read_text())
  117. doc_visitor = DocVisitor(topic)
  118. doc_visitor.visit(tree)
  119. if doc_visitor.current_title:
  120. doc_visitor.on_new_heading() # to finalize the last heading
  121. documents = []
  122. tree = ast.parse(Path('../main.py').read_text())
  123. MainVisitor().visit(tree)
  124. generate_for(Path('./documentation.py'))
  125. for file in Path('./more_documentation').glob('*.py'):
  126. generate_for(file, file.stem.removesuffix('_documentation'))
  127. with open('static/search_index.json', 'w') as f:
  128. json.dump(documents, f, indent=2)