1010from pygments import highlight
1111from pygments .formatters import HtmlFormatter
1212from pygments .lexers import PythonLexer
13- from unitxt .artifact import Artifact
13+ from unitxt .artifact import (
14+ get_class_or_function_from_artifact_type ,
15+ get_module_class ,
16+ )
1417from unitxt .text_utils import print_dict_as_python
1518from unitxt .utils import load_json
1619
@@ -51,8 +54,8 @@ def imports_to_syntax_highlighted_html(subtypes: List[str]) -> str:
5154 return ""
5255 module_to_class_names = defaultdict (list )
5356 for subtype in subtypes :
54- subtype_class = Artifact . _class_register . get (subtype )
55- module_to_class_names [subtype_class . __module__ ].append (subtype_class . __name__ )
57+ ( module , class_name ) = get_module_class (subtype )
58+ module_to_class_names [module ].append (class_name )
5659
5760 imports_txt = ""
5861 for modu in sorted (module_to_class_names .keys ()):
@@ -103,31 +106,6 @@ def custom_walk(top):
103106 yield entry
104107
105108
106- def all_subtypes_of_artifact (artifact ):
107- if (
108- artifact is None
109- or isinstance (artifact , str )
110- or isinstance (artifact , bool )
111- or isinstance (artifact , int )
112- or isinstance (artifact , float )
113- ):
114- return []
115- if isinstance (artifact , list ):
116- to_return = []
117- for art in artifact :
118- to_return .extend (all_subtypes_of_artifact (art ))
119- return to_return
120- # artifact is a dict
121- to_return = []
122- for key , value in artifact .items ():
123- if isinstance (value , str ):
124- if key == "__type__" :
125- to_return .append (value )
126- else :
127- to_return .extend (all_subtypes_of_artifact (value ))
128- return to_return
129-
130-
131109def get_all_type_elements (nested_dict ):
132110 type_elements = set ()
133111
@@ -137,32 +115,31 @@ def recursive_search(d):
137115 d .pop ("__tags__" , None )
138116 for key , value in d .items ():
139117 if key == "__type__" :
140- type_elements .add (value )
118+ type_elements .add (json . dumps ( value ) )
141119 elif isinstance (value , dict ):
142120 recursive_search (value )
143121 elif isinstance (value , list ):
144122 for item in value :
145123 recursive_search (item )
146124
147125 recursive_search (nested_dict )
148- return list ( type_elements )
126+ return [ json . loads ( type_element ) for type_element in type_elements ]
149127
150128
151129@lru_cache (maxsize = None )
152130def artifact_type_to_link (artifact_type ):
153- artifact_class = Artifact ._class_register .get (artifact_type )
154- type_class_name = artifact_class .__name__
155- artifact_class_id = f"{ artifact_class .__module__ } .{ type_class_name } "
156- return f'<a class="reference internal" href="../{ artifact_class .__module__ } .html#{ artifact_class_id } " title="{ artifact_class_id } "><code class="xref py py-class docutils literal notranslate"><span class="pre">{ type_class_name } </span></code></a>'
131+ artifact_module , artifact_class_name = get_module_class (json .loads (artifact_type ))
132+ return f'<a class="reference internal" href="../{ artifact_module } .html#{ artifact_module } .{ artifact_class_name } " title="{ artifact_module } .{ artifact_class_name } "><code class="xref py py-class docutils literal notranslate"><span class="pre">{ artifact_class_name } </span></code></a>'
157133
158134
159135# flake8: noqa: C901
136+
137+
160138def make_content (artifact , label , all_labels ):
161- artifact_type = artifact ["__type__" ]
162- artifact_class = Artifact ._class_register .get (artifact_type )
163- type_class_name = artifact_class .__name__
164- catalog_id = label .replace ("catalog." , "" )
139+ artifact_type = artifact ["__type__" ] # dict with fields "module" and "name"
140+ artifact_class = get_class_or_function_from_artifact_type (artifact_type )
165141
142+ catalog_id = label .replace ("catalog." , "" )
166143 result = ""
167144
168145 if "__description__" in artifact and artifact ["__description__" ] is not None :
@@ -205,25 +182,22 @@ def make_content(artifact, label, all_labels):
205182 )
206183
207184 for type_name in type_elements :
208- # source = f'<span class="nt">__type__</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">{type_name}</span>'
209- source = f'<span class="n">__type__{ type_name } </span><span class="p">'
210- target = artifact_type_to_link (type_name )
211- html_for_dict = html_for_dict .replace (
212- source ,
213- f'<span class="n" STYLE="font-size:108%">{ target } </span><span class="p">' ,
214- # '<span class="nt">"type"</span><span class="p">:</span><span class="w"> </span>'
215- # + target,
185+ artifact_module , artifact_class_name = get_module_class (type_name )
186+ pattern = re .compile (
187+ f'<span class="n">__type__(.*?)<span class="n">{ artifact_class_name } </span>'
216188 )
189+ repl = (
190+ '<span class="n" STYLE="font-size:108%">'
191+ + artifact_type_to_link (json .dumps (type_name ))
192+ + "</span>"
193+ )
194+ html_for_dict = pattern .sub (repl , html_for_dict )
217195
218- pattern = r'(<span class="nt">)"(.*?)"(</span>)'
219-
220- # Replacement function
221- html_for_dict = re .sub (pattern , r"\1\2\3" , html_for_dict )
222-
223- subtypes = all_subtypes_of_artifact (artifact )
224- subtypes = list (set (subtypes ))
196+ subtypes = type_elements
225197 subtypes .remove (artifact_type ) # this was already documented
226- html_for_imports = imports_to_syntax_highlighted_html (subtypes )
198+ html_for_imports = imports_to_syntax_highlighted_html (
199+ get_all_type_elements (artifact )
200+ )
227201
228202 source_link = f"""<a class="reference external" href="https://github.com/IBM/unitxt/blob/main/src/unitxt/catalog/{ catalog_id .replace ("." , "/" )} .json"><span class="viewcode-link"><span class="pre">[source]</span></span></a>"""
229203 html_for_element = f"""<div class="admonition note">
@@ -237,13 +211,13 @@ def make_content(artifact, label, all_labels):
237211 result += " " + html_for_element + "\n "
238212
239213 if artifact_class .__doc__ :
240- explanation_str = f"Explanation about `{ type_class_name } `"
214+ explanation_str = f"Explanation about `{ artifact_class . __name__ } `"
241215 result += f"\n { explanation_str } \n "
242216 result += "+" * len (explanation_str ) + "\n \n "
243217 result += artifact_class .__doc__ + "\n "
244218
245219 for subtype in subtypes :
246- subtype_class = Artifact . _class_register . get (subtype )
220+ subtype_class = get_class_or_function_from_artifact_type (subtype )
247221 subtype_class_name = subtype_class .__name__
248222 if subtype_class .__doc__ :
249223 explanation_str = f"Explanation about `{ subtype_class_name } `"
0 commit comments