11"""Welcome to Reflex! This file outlines the steps to create a basic app."""
22
33import os
4- from pathlib import Path
54from collections import defaultdict
5+ from pathlib import Path
66from types import SimpleNamespace
77
8- import reflex as rx
9- import reflex_chakra as rc
108import flexdown
9+ import reflex as rx
10+ from reflex .utils .format import to_kebab_case , to_snake_case
1111
12- from .utils .flexdown import xd
13- from .utils .docpage import multi_docs
14- from .constants import css
15-
16-
17- def should_skip_compile (doc : flexdown .Document ):
18- """Skip compilation if the markdown file has not been modified since the last compilation."""
19- if not os .environ .get ("REFLEX_PERSIST_WEB_DIR" , False ):
20- return False
12+ import reflex_chakra as rc # noqa: F401
2113
22- # Check if the doc has been compiled already.
23- compiled_output = f".web/pages/{ doc .replace ('.md' , '.js' )} "
24- # Get the timestamp of the compiled file.
25- compiled_time = (
26- os .path .getmtime (compiled_output ) if os .path .exists (compiled_output ) else 0
27- )
28- # Get the timestamp of the source file.
29- source_time = os .path .getmtime (doc )
30- return compiled_time > source_time
14+ from .constants import css
15+ from .utils .docpage import Route , multi_docs
16+ from .utils .flexdown import xd
3117
3218
3319def find_flexdown_files (directory : str ) -> list [str ]:
@@ -40,14 +26,16 @@ def find_flexdown_files(directory: str) -> list[str]:
4026 A list of paths to Flexdown files.
4127 """
4228 return [
43- os . path . join ( root , file ).replace ( " \\ " , "/" )
29+ ( Path ( root ) / file ).as_posix ( )
4430 for root , _ , files in os .walk (directory )
4531 for file in files
4632 if file .endswith (".md" )
4733 ]
4834
4935
50- def extract_components_from_metadata (document ) -> list :
36+ def extract_components_from_metadata (
37+ document : flexdown .Document ,
38+ ) -> list [tuple [type , str ]]:
5139 """Extract components from the document metadata.
5240
5341 Args:
@@ -56,14 +44,14 @@ def extract_components_from_metadata(document) -> list:
5644 Returns:
5745 A list of tuples containing component instances and their string representation.
5846 """
59- components = []
47+ components : list [ tuple [ type , str ]] = []
6048 for comp_str in document .metadata .get ("components" , []):
6149 component = eval (comp_str )
6250 if isinstance (component , type ):
6351 components .append ((component , comp_str ))
6452 elif hasattr (component , "__self__" ):
6553 components .append ((component .__self__ , comp_str ))
66- elif isinstance (component , SimpleNamespace ) and hasattr (component , "__call__" ):
54+ elif isinstance (component , SimpleNamespace ) and hasattr (component , "__call__" ): # noqa: B004
6755 components .append ((component .__call__ .__self__ , comp_str ))
6856 return components
6957
@@ -99,8 +87,10 @@ def convert_to_title_case(text: str) -> str:
9987
10088
10189def create_doc_component (
102- doc_path : str , base_dir : str , component_registry : defaultdict
103- ) -> rx .Component :
90+ doc_path : str ,
91+ base_dir : str ,
92+ component_registry : defaultdict [str , list [tuple [str , list [tuple [type , str ]]]]],
93+ ) -> Route :
10494 """Create a document component for a given file path.
10595
10696 Args:
@@ -112,13 +102,14 @@ def create_doc_component(
112102 A component object representing the document page.
113103 """
114104 doc_path = doc_path .replace ("\\ " , "/" )
115- route_path = rx .utils .format .to_kebab_case (
105+ doc_path_path = Path (doc_path )
106+ route_path = to_kebab_case (
116107 f"/{ doc_path .removeprefix (base_dir ).replace ('.md' , '/' )} "
117108 ).replace ("//" , "/" )
118- category = os . path . basename ( os . path . dirname ( doc_path )) .title ()
109+ category = doc_path_path . parent . name .title ()
119110 document = flexdown .parse_file (doc_path )
120- title = rx . utils . format . to_snake_case (os . path . basename ( doc_path ) .replace (".md" , "" ))
121- component_list = [ title , * extract_components_from_metadata (document )]
111+ title = to_snake_case (doc_path_path . name .replace (".md" , "" ))
112+ component_list = ( title , extract_components_from_metadata (document ))
122113 component_registry [category ].append (component_list )
123114 return multi_docs (
124115 path = route_path ,
@@ -129,7 +120,7 @@ def create_doc_component(
129120 )
130121
131122
132- def generate_document_routes (doc_paths : list [str ], base_dir : str ) -> list [rx . Component ]:
123+ def generate_document_routes (doc_paths : list [str ], base_dir : str ) -> list [Route ]:
133124 """Generate document components and routes from Flexdown file paths.
134125
135126 Args:
@@ -146,9 +137,7 @@ def generate_document_routes(doc_paths: list[str], base_dir: str) -> list[rx.Com
146137 ]
147138
148139
149- def setup_application_routes (
150- app : rx .App , doc_routes : list [rx .Component ], base_path : str
151- ):
140+ def setup_application_routes (app : rx .App , doc_routes : list [Route ], base_path : str ):
152141 """Set up application routes based on document components.
153142
154143 Args:
0 commit comments