18classMkLink(mknode.MkNode): 19"""A simple Link (with optional icon and option to show up as a button). 20 21 If no title is given, the URL is used as a title. 22 """ 23 24ICON="octicons/link-24" 25ATTR_LIST_SEPARATOR="" 26 27def__init__( 28self, 29target:linkprovider.LinkableType, 30title:str|None=None, 31*, 32tooltip:str|None=None, 33icon:str|None=None, 34as_button:bool=False, 35primary_color:bool=False, 36**kwargs:Any, 37)->None: 38"""Constructor. 39 40 Args: 41 target: Link target 42 title: Title used for link 43 tooltip: Tooltip for the link 44 icon: Optional icon to be displayed in front of title 45 as_button: Whether link should be rendered as button 46 primary_color: If rendered as button, use primary color as background. 47 kwargs: keyword arguments passed to parent 48 """ 49super().__init__(**kwargs) 50self.target=target 51self._title=title 52self.tooltip=tooltip 53self.as_button=as_button 54self.primary_color=primary_color 55self._icon=icon 56ifas_button: 57self.add_css_class("md-button") 58ifprimary_color: 59self.add_css_class("md-button--primary") 60 61@property 62deficon(self)->str: 63returnicons.get_emoji_slug(self._icon)ifself._iconelse"" 64 65defget_url(self)->str: 66ifisinstance(self.target,str)andhelpers.is_url(self.target): 67returnself.target# no project context needed 68returnself.ctx.links.get_url(self.target) 69 70defget_title(self)->str: 71returnself._titleorself.get_url() 72 73@classmethod 74deffor_pydantic_playground( 75cls, 76files:Mapping[str,str]|list[types.ModuleType]|Sequence[str|os.PathLike[str]], 77title:str="Open in Pydantic Playground", 78active_index:int=0, 79**kwargs:Any, 80)->MkLink: 81"""Create a link to Pydantic playground with pre-populated files. 82 83 Args: 84 files: The files to include in the playground. Can be: 85 - A mapping of filenames to code content 86 - A list of modules 87 - A list of file paths 88 title: The title of the link 89 active_index: The index of the active file in the playground 90 **kwargs: Additional keyword arguments to pass to the Pydantic playground 91 92 Returns: 93 An MkLink instance pointing to the Pydantic playground 94 """ 95fromurllib.parseimportquote 96 97matchfiles: 98caseMapping(): 99file_data:list[dict[str,Any]]=[100{"name":name,"content":content}forname,contentinfiles.items()101]102case[types.ModuleType(),*_]:103file_data=[104{"name":f"{mod.__name__}.py","content":inspect.getsource(mod)}# type: ignore105formodinfiles106]107case[str()|os.PathLike(),*_]:108file_data=[]109forpathinfiles:110file=to_upath(path)111file_data.append({112"name":file.name,113"content":file.read_text("utf-8"),114})115case_:116raiseTypeError(files)117118# Add activeIndex to first file119iffile_data:120file_data[active_index]["activeIndex"]=1121122json_str=json.dumps(file_data)123encoded=quote(json_str)124125url=f"https://pydantic.run/new?files={encoded}"126returncls(url,title,**kwargs)127128defto_md_unprocessed(self)->str:129prefix=f"{self.icon} "ifself.iconelse""130tooltip=f" {self.tooltip!r}"ifself.tooltipelse""131returnf"[{prefix}{self.get_title()}]({self.get_url()}{tooltip})"