@@ -25,6 +25,11 @@ class ASTNode(object):
2525 def __init__ (self , node : dict ):
2626 self .node = node
2727 self .children = []
28+ self ._local_vars = {}
29+ for candidate in self ._get_annotations ():
30+ split_list = candidate .split ("=" , 1 )
31+ if len (split_list ) == 2 :
32+ self ._local_vars [split_list [0 ]] = split_list [1 ]
2833 if 'children' in node :
2934 for child in node ['children' ]:
3035 self .children .append (ASTNode (child ))
@@ -39,7 +44,7 @@ def is_executable(self) -> bool:
3944 return self .get_type () == 'block_code'
4045
4146 def _get_annotations (self ) -> List [str ]:
42- return self .node .get ('attrs' ).get ('info' , '' ).split (' ' )
47+ return self .node .get ('attrs' , {} ).get ('info' , '' ).split (' ' )
4348
4449 def is_stackql_shell_invocation (self ) -> bool :
4550 return self ._STACKQL_SHELL_INVOCATION in self ._get_annotations ()
@@ -55,22 +60,38 @@ def is_teardown(self) -> bool:
5560
5661 def get_execution_language (self ) -> str :
5762 return self .node .get ('lang' , '' )
63+
64+ def expand (self ) -> str :
65+ return self .get_text ().replace ("<" , "{" ).replace (">" , "}" ).format (** self ._local_vars )
5866
5967 def __str__ (self ):
6068 return json .dumps (self .node , indent = 2 )
6169
6270 def __repr__ (self ):
6371 return self .__str__ ()
72+
73+
74+ class MdAST (object ):
75+
76+ def __init__ (self , ast : List [ASTNode ]):
77+ self ._ast : List [ASTNode ] = ast
78+
79+ def get_ordered (self ) -> List [ASTNode ]:
80+ return self ._ast
81+
82+ def expand (self , node : ASTNode ) -> str :
83+ return node .expand ()
84+
85+
6486
6587class MdParser (object ):
6688
67- def parse_markdown_file (self , file_path : str , lang = None ) -> List [ ASTNode ] :
89+ def parse_markdown_file (self , file_path : str , lang = None ) -> MdAST :
6890 markdown : mistune .Markdown = mistune .create_markdown (renderer = 'ast' )
6991 with open (file_path , 'r' ) as f :
7092 txt = f .read ()
7193 raw_list : List [dict ] = markdown (txt )
72- return [ASTNode (node ) for node in raw_list ]
73-
94+ return MdAST ([ASTNode (node ) for node in raw_list ])
7495
7596class WorkloadDTO (object ):
7697
@@ -119,23 +140,23 @@ def orchestrate(self, file_path: str) -> WorkloadDTO:
119140 setup_str : str = f'cd { _REPOSITORY_ROOT_PATH } ;\n '
120141 in_session_commands : List [str ] = []
121142 teardown_str : str = f'cd { _REPOSITORY_ROOT_PATH } ;\n '
122- for node in ast :
143+ for node in ast . get_ordered () :
123144 if node .is_executable ():
124145 if node .is_setup ():
125146 if setup_count < self ._max_setup_blocks :
126- setup_str += f' { node . get_text () } '
147+ setup_str += ast . expand ( node )
127148 setup_count += 1
128149 else :
129150 raise KeyError (f'Maximum setup blocks exceeded: { self ._max_setup_blocks } ' )
130151 elif node .is_teardown ():
131152 if teardown_count < self ._max_teardown_blocks :
132- teardown_str += f' { node . get_text () } '
153+ teardown_str += ast . expand ( node )
133154 teardown_count += 1
134155 else :
135156 raise KeyError (f'Maximum teardown blocks exceeded: { self ._max_teardown_blocks } ' )
136157 elif node .is_stackql_shell_invocation ():
137158 if invocation_count < self ._max_invocations_blocks :
138- all_commands : str = node . get_text ( ).split ('\n \n ' )
159+ all_commands : str = ast . expand ( node ).split ('\n \n ' )
139160 in_session_commands += all_commands
140161 invocation_count += 1
141162 else :
0 commit comments