88from mkdocs .structure .pages import Page
99from mkdocs .structure .files import Files
1010
11- from entangled .document import ReferenceMap , Content , PlainText , ReferenceId , CodeBlock , document_to_text
12- from entangled .properties import get_attribute , Id , Attribute , Class
13- from entangled .hooks import get_hooks
14- from entangled .config import config
11+ from entangled .model import ReferenceMap , Content , PlainText , ReferenceId , CodeBlock , content_to_text
12+ from entangled .model . properties import get_attribute , Id , Attribute , Class
13+ from entangled .config import ConfigUpdate , read_config
14+ from entangled .interface import Context , read_markdown
1515from repl_session import read_session
1616
17- config .read ()
18-
19- # The Entangled config needs to be read before importing the Markdown reader
20- # This is very bad I know, and should be fixed in some future version.
21- from entangled .markdown_reader import read_markdown_string
22-
2317
2418type ContentFilter = Callable [[ReferenceMap , Content ], Iterable [Content ]]
2519type CodeBlockFilter = Callable [[ReferenceMap , ReferenceId ], Iterable [Content ]]
@@ -35,6 +29,10 @@ def _foo(rm: ReferenceMap, c: Content) -> Iterable[Content]:
3529 return _foo
3630
3731
32+ def document_to_text (refs : ReferenceMap , content : Iterable [Content ]) -> str :
33+ return "" .join (map (lambda c : content_to_text (refs , c )[0 ], content ))
34+
35+
3836def iter_bind [T , U ](lst : Iterable [T ], f : Callable [[T ], Iterable [U ]]) -> Iterable [U ]:
3937 return chain (* map (f , lst ))
4038
@@ -49,9 +47,10 @@ def compose_filters(*args: ContentFilter) -> ContentFilter:
4947 return reduce (compose_filter , args , lambda _ , c : [c ])
5048
5149
52- def read_markdown (text : str ) -> tuple [ReferenceMap , list [Content ]]:
53- hooks = get_hooks ()
54- return read_markdown_string (text , hooks = hooks )
50+ def read_single_markdown (ctx : Context , text : str ) -> tuple [ReferenceMap , list [Content ]]:
51+ refs = ReferenceMap ()
52+ content , _ = read_markdown (ctx , refs , text )
53+ return refs , content
5554
5655
5756@codeblock_filter
@@ -60,7 +59,7 @@ def add_title(reference_map: ReferenceMap, r: ReferenceId) -> list[Content]:
6059 Changes the `open_line` member of a `CodeBlock` to reflect accepted
6160 MkDocs syntax, adding a `title` attribute.
6261 """
63- codeblock : CodeBlock = reference_map . get_codeblock ( r )
62+ codeblock : CodeBlock = reference_map [ r ]
6463
6564 ids = [p .value for p in codeblock .properties if isinstance (p , Id )]
6665 classes = [p .value for p in codeblock .properties if isinstance (p , Class )]
@@ -93,7 +92,7 @@ def add_title(reference_map: ReferenceMap, r: ReferenceId) -> list[Content]:
9392 if title :
9493 open_line += " " + str (Attribute ("title" , title ))
9594
96- open_line += "}"
95+ open_line += "}\n "
9796
9897 codeblock .open_line = open_line
9998 return [r ]
@@ -104,11 +103,12 @@ def include_repl_output(reference_map: ReferenceMap, r: ReferenceId) -> list[Con
104103 """
105104 Takes any codeblock that has the `repl` class and append its pre-computed output.
106105 """
107- codeblock : CodeBlock = reference_map . get_codeblock ( r )
106+ codeblock : CodeBlock = reference_map [ r ]
108107 if Class ("repl" ) not in codeblock .properties :
109108 return [r ]
110109
111- first : CodeBlock = next (iter (reference_map .by_name (r .name )))
110+ ref = next (iter (reference_map .select_by_name (r .name )))
111+ first : CodeBlock = reference_map [ref ]
112112 session_filename = get_attribute (first .properties , "session" )
113113 assert session_filename is not None
114114 session_path : Path = Path (session_filename )
@@ -132,6 +132,7 @@ def include_repl_output(reference_map: ReferenceMap, r: ReferenceId) -> list[Con
132132
133133
134134def on_page_markdown (markdown : str , * , page : Page , config : MkDocsConfig , files : Files ) -> str :
135- reference_map , content = read_markdown (markdown )
135+ context = Context () | read_config ()
136+ reference_map , content = read_single_markdown (context , markdown )
136137 filtered_content = iter_bind (content , partial (compose_filters (add_title , include_repl_output ), reference_map ))
137138 return document_to_text (reference_map , filtered_content )
0 commit comments