diff --git a/mkquartodocs/extension.py b/mkquartodocs/extension.py index 0ef8352..e06e997 100644 --- a/mkquartodocs/extension.py +++ b/mkquartodocs/extension.py @@ -24,14 +24,14 @@ # `:::: {.cell execution_count="1"}` # `:::::: {.cell layout-align="default"}` <- happens in mermaid diagrams -CELL_REGEX: Final = re.compile(r"^(:{3,}) \{\.cell .*}\s*$") +CELL_REGEX: Final = re.compile(r"^(:{3,}) \{(#[-\w_]+)?\.cell .*}\s*$") CELL_END: Final = re.compile(r"^(:{3,})$") # In theory it would be a 'cell-output' but there are other kinds of out # Cells contain multiple cell elements. # `::: {.cell-output .cell-output-stdout}` CELL_ELEM_REGEX: Final = re.compile( - r"^(:{3,}) \{(.cell-\w+)\s?(\.cell-[\w-]+)?( execution_count=\"\d+\")?\}$" + r"^(:{3,}) \{(#[-\w_]+)?\s*(.cell-\w+)\s?(\.cell-[\w-]+)?( execution_count=\"\d+\")?\}$" ) # `::::: cell-output-display` CELL_ELEM_ALT_REGEX: Final = re.compile(r"^(:{3,})\s*(cell-output-display\s*)$") @@ -136,6 +136,7 @@ class BlockContext: attributes: list[str] start: Cursor end: Cursor | UnknownEnd + label: str | None = None def __post_init__(self): log.info(f"BlockContext: {self}") @@ -149,12 +150,18 @@ def _from_cell_start_line(cls, line: str, line_number: int) -> BlockContext: sr = CELL_REGEX.search(line) grp = sr.groups() assert all(w == ":" for w in grp[0]), f"{grp[0]} should be :" + first_cell_grp = 1 + label = None + if len(grp) > 1 and grp [1] and grp[1].startswith("#"): + label = grp[1][1:] + first_cell_grp = 2 return BlockContext( block_type=BlockType.CELL, delimiter=grp[0], - attributes=grp[1:], + attributes=grp[first_cell_grp:], start=Cursor(line=line_number, col=0), end=UnknownEnd(), + label=label, ) @classmethod @@ -162,12 +169,18 @@ def _from_cell_element_line(cls, line: str, line_number: int) -> BlockContext: """This function assumes that you already checked that the line matches CELL_ELEM_REGEX""" sr = CELL_ELEM_REGEX.search(line) grp = sr.groups() + first_cell_grp = 1 + label = None + if len(grp) > 1 and grp [1] and grp[1].startswith("#"): + label = grp[1][1:] + first_cell_grp = 2 return BlockContext( block_type=BlockType.CELL_ELEM, delimiter=grp[0], - attributes=grp[1:], + attributes=grp[first_cell_grp:], start=Cursor(line=line_number, col=0), end=UnknownEnd(), + label=label, ) @classmethod @@ -240,6 +253,7 @@ def with_end(self, end: Cursor | UnknownEnd) -> BlockContext: attributes=self.attributes, start=self.start, end=end, + label=self.label, ) def find_end( @@ -358,8 +372,16 @@ def _into_output_lines_cell_elem(self, file_content: FileContent) -> list[str]: # markdown in html attribute and leave as is. non_empty = [line for line in internal if line.strip()] if internal[0].startswith("