Falko Schindler 1 рік тому
батько
коміт
b6262e612b
4 змінених файлів з 15 додано та 24 видалено
  1. 1 1
      main.py
  2. 14 21
      website/build_search_index.py
  3. 0 2
      website/search.py
  4. 0 0
      website/search.vue

+ 1 - 1
main.py

@@ -20,10 +20,10 @@ import prometheus
 from nicegui import Client, app
 from nicegui import globals as nicegui_globals
 from nicegui import ui
-from search import search
 from website import documentation, example_card, svg
 from website.demo import bash_window, browser_window, python_window
 from website.documentation_tools import create_anchor_name, element_demo, generate_class_doc
+from website.search import search
 from website.star import add_star
 from website.style import example_link, features, heading, link_target, section_heading, side_menu, subtitle, title
 

+ 14 - 21
website/build_search_index.py

@@ -5,13 +5,11 @@ import os
 import re
 from _ast import AsyncFunctionDef
 from pathlib import Path
-from typing import Any, List, Optional, Union
-
-from icecream import ic
+from typing import List, Optional, Union
 
 from nicegui import app, ui
 
-dir_path = os.path.dirname(os.path.abspath(__file__))
+dir_path = Path(__file__).parent
 os.chdir(dir_path)
 
 
@@ -24,7 +22,7 @@ def ast_string_node_to_string(node):
         return str(ast.unparse(node))
 
 
-def cleanup(markdown_string):
+def cleanup(markdown_string: str) -> str:
     # Remove link URLs but keep the description
     markdown_string = re.sub(r'\[([^\[]+)\]\([^\)]+\)', r'\1', markdown_string)
     # Remove inline code ticks
@@ -58,7 +56,7 @@ class DocVisitor(ast.NodeVisitor):
         elif function_name == 'markdown':
             if node.args:
                 raw = ast_string_node_to_string(node.args[0]).splitlines()
-                raw = ' '.join([l.strip() for l in raw]).strip()
+                raw = ' '.join(l.strip() for l in raw).strip()
                 self.current_content.append(cleanup(raw))
         self.generic_visit(node)
 
@@ -81,18 +79,16 @@ class DocVisitor(ast.NodeVisitor):
 
         for decorator in node.decorator_list:
             if isinstance(decorator, ast.Call):
-                ic(decorator.func)
                 function = decorator.func
                 if isinstance(function, ast.Name) and function.id == 'text_demo':
                     title = decorator.args[0].s
                     content = cleanup(decorator.args[1].s).splitlines()
                     self.add_to_search_index(title, content)
-        ic(node.name)
         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()
+            content_str = ' '.join(l.strip() for l in content).strip()
         else:
             content_str = content
 
@@ -105,7 +101,7 @@ class DocVisitor(ast.NodeVisitor):
         documents.append({
             'title': title,
             'content': content_str,
-            'url': url
+            'url': url,
         })
 
 
@@ -124,24 +120,21 @@ class MainVisitor(ast.NodeVisitor):
             documents.append({
                 'title': 'Example: ' + title,
                 'content': ast_string_node_to_string(node.args[1]),
-                'url': f'https://github.com/zauberzeug/nicegui/tree/main/examples/{name}/main.py'
+                'url': f'https://github.com/zauberzeug/nicegui/tree/main/examples/{name}/main.py',
             })
 
 
 def generate_for(file: Path, topic: Optional[str] = None) -> None:
-    with open(file, 'r') as source:
-        ic(file)
-        tree = ast.parse(source.read())
-        doc_visitor = DocVisitor(topic)
-        doc_visitor.visit(tree)
-        if doc_visitor.current_title:
-            doc_visitor.on_new_heading()  # to finalize the last heading
+    tree = ast.parse(file.read_text())
+    doc_visitor = DocVisitor(topic)
+    doc_visitor.visit(tree)
+    if doc_visitor.current_title:
+        doc_visitor.on_new_heading()  # to finalize the last heading
 
 
 documents = []
-with open(Path('../main.py'), 'r') as source:
-    tree = ast.parse(source.read())
-    MainVisitor().visit(tree)
+tree = ast.parse(Path('../main.py').read_text())
+MainVisitor().visit(tree)
 
 generate_for(Path('./documentation.py'))
 for file in Path('./more_documentation').glob('*.py'):

+ 0 - 2
search.py → website/search.py

@@ -1,5 +1,3 @@
-from typing import Any, Callable, Optional
-
 from nicegui.dependencies import register_component
 from nicegui.element import Element
 

+ 0 - 0
search.vue → website/search.vue