|
@@ -3,9 +3,12 @@ import ast
|
|
import json
|
|
import json
|
|
import os
|
|
import os
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
+from typing import Union
|
|
|
|
|
|
from icecream import ic
|
|
from icecream import ic
|
|
|
|
|
|
|
|
+from nicegui import app, ui
|
|
|
|
+
|
|
dir_path = os.path.dirname(os.path.abspath(__file__))
|
|
dir_path = os.path.dirname(os.path.abspath(__file__))
|
|
os.chdir(dir_path)
|
|
os.chdir(dir_path)
|
|
|
|
|
|
@@ -16,22 +19,41 @@ class DemoVisitor(ast.NodeVisitor):
|
|
super().__init__()
|
|
super().__init__()
|
|
self.topic = topic
|
|
self.topic = topic
|
|
|
|
|
|
- def visit_FunctionDef(self, node):
|
|
|
|
|
|
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
|
|
|
|
+ if node.name == 'main_demo':
|
|
|
|
+ docstring = ast.get_docstring(node)
|
|
|
|
+ if docstring is None:
|
|
|
|
+ api = getattr(ui, self.topic) if hasattr(ui, self.topic) else getattr(app, self.topic)
|
|
|
|
+ docstring = api.__doc__ or api.__init__.__doc__
|
|
|
|
+ lines = docstring.splitlines()
|
|
|
|
+ self.add_to_search_index(lines[0], lines[1:], main=True)
|
|
|
|
+
|
|
for decorator in node.decorator_list:
|
|
for decorator in node.decorator_list:
|
|
if isinstance(decorator, ast.Call):
|
|
if isinstance(decorator, ast.Call):
|
|
function = decorator.func
|
|
function = decorator.func
|
|
if isinstance(function, ast.Name) and function.id == 'text_demo':
|
|
if isinstance(function, ast.Name) and function.id == 'text_demo':
|
|
title = decorator.args[0].s
|
|
title = decorator.args[0].s
|
|
- content = ' '.join([l.strip() for l in decorator.args[1].s.splitlines()]).strip()
|
|
|
|
- anchor = title.lower().replace(' ', '_')
|
|
|
|
- url = f'/documentation/{self.topic}#{anchor}'
|
|
|
|
- documents.append({
|
|
|
|
- 'title': f'{self.topic.replace("_", " ").title()}: {title}',
|
|
|
|
- 'content': content,
|
|
|
|
- 'url': url
|
|
|
|
- })
|
|
|
|
|
|
+ content = decorator.args[1].s.splitlines()
|
|
|
|
+ self.add_to_search_index(title, content)
|
|
self.generic_visit(node)
|
|
self.generic_visit(node)
|
|
|
|
|
|
|
|
+ def add_to_search_index(self, title: str, content: Union[str, list], main: bool = False) -> None:
|
|
|
|
+ if isinstance(content, list):
|
|
|
|
+ content_str = ' '.join([l.strip() for l in content]).strip()
|
|
|
|
+ else:
|
|
|
|
+ content_str = content
|
|
|
|
+
|
|
|
|
+ anchor = title.lower().replace(' ', '_')
|
|
|
|
+ url = f'/documentation/{self.topic}'
|
|
|
|
+ if not main:
|
|
|
|
+ url += f'#{anchor}'
|
|
|
|
+ title = f'{self.topic.replace("_", " ").title()}: {title}'
|
|
|
|
+ documents.append({
|
|
|
|
+ 'title': title,
|
|
|
|
+ 'content': content_str,
|
|
|
|
+ 'url': url
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
|
|
documents = []
|
|
documents = []
|
|
for file in Path('./more_documentation').glob('*.py'):
|
|
for file in Path('./more_documentation').glob('*.py'):
|