[docs]defget_markdown(self,generate_toc=False):"""Returns markdown string from joined self.sections Args: generate_toc (bool): Generate table of contents bool. Returns: str: Markdown string """ifgenerate_toc:self.generate_toc()return"".join(self.sections)
def_slugify(self,title):"""Make title strings slugified (transform to URL friendly string) Args: title (str): Original Title str Returns: slug_title (str): Slugified Title str """slug=re.sub(r'[^\w\s-]','',title).strip().lower()slug=re.sub(r'\s+','-',slug)returnslug
[docs]defgenerate_toc(self,):"""Generate a table of contents from markdown string Returns: toc (str): Table of contents markdown string """markdown_text="".join(self.sections)lines=markdown_text.split('\n')toc=[]slug_counts=defaultdict(int)forlineinlines:match=re.match(r'^(#{1,6})\s+(.*)',line)ifmatch:level=len(match.group(1))-1title=match.group(2).strip()base_slug=self._slugify(title)slug_counts[base_slug]+=1anchor=base_slugifslug_counts[base_slug]==1elsef"{base_slug}-{slug_counts[base_slug]-1}"toc.append(f"{' '*level}- [{title}](#{anchor})")self.sections=["## Table of Contents\n\n"+"\n".join(toc)+"\n\n"]+self.sections
[docs]defadd_header(self,title,level=1):"""Adds header to markdown Args: title (Any): Title string level (int): Markdown header level. Defaults to 1. """self.sections.append(f"{'#'*level}{title}\n")
[docs]defadd_definition(self,title,content):"""Adds definition line to markdown in the following format **title**: content Args: title (Any): Name content (Any): Description """self.sections.append(f"**{title}**: {content}\n\n")
[docs]defadd_table(self,headers,rows):"""Adds a table to markdown with headers and rows Args: headers (List[str]): Headers rows (List[str]): Rows """iflen(rows)>0:assertlen(rows[0])==len(headers), \
f"Length of rows ({len(rows[0])}) \ does not equal length of headers \ ({len(headers)}) for headers:\n{headers}\n"table="| "+" | ".join(headers)+" |\n"table+="|"+"|".join(["---"]*len(headers))+"|\n"forrowinrows:table+="| "+" | ".join(row)+" |\n"self.sections.append(table)self.sections.append("\n")
[docs]defadd_list(self,items):"""Adds list to markdown Args: items (List[str]): List of items """foriteminitems:self.sections.append(f"- {item}\n")self.sections.append("\n")
[docs]defadd_collapsible_section(self,text,title="Root"):"""Adds collapsible section to markdown Args: text (str): contents of collapsible section title (str, optional): Collapsible section title text. Defaults to "Root". """self.add_text(f"<details><summary>{title}</summary>\n\n```json\n"+text+"\n```\n</details>")
[docs]defadd_text(self,content):"""Adds text to markdown Args: content (Any): Text content """self.sections.append(f"{content}\n\n")