From e530e85939af4e9e6d42c42e2fc153e40fa81bb5 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer <8102638+jpfeuffer@users.noreply.github.com> Date: Tue, 1 Apr 2025 14:38:46 +0200 Subject: [PATCH 1/4] support reading labelled code cells --- mkquartodocs/extension.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/mkquartodocs/extension.py b/mkquartodocs/extension.py index 0ef8352..23ee233 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+\s+)?\.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 = 0 + label = None + if grp[0].startswith("#"): + label = grp[0][1:] + first_cell_grp = 1 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 = 0 + label = None + if grp[0].startswith("#"): + label = grp[0][1:] + first_cell_grp = 1 return BlockContext( - block_type=BlockType.CELL_ELEM, + 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 From d19b3fd504932cee90f924993c99b7a0cf79d07e Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer <8102638+jpfeuffer@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:13:41 +0200 Subject: [PATCH 2/4] little fixes --- mkquartodocs/extension.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/mkquartodocs/extension.py b/mkquartodocs/extension.py index 23ee233..3349fbe 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,}) \{(?:#\w+\s+)?\.cell .*}\s*$") +CELL_REGEX: Final = re.compile(r"^(:{3,}) \{(#[-\w_]+)?\s*\.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,}) \{(?:#[-\w_]+\s+)?(.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*)$") @@ -150,11 +150,11 @@ 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 = 0 + first_cell_grp = 1 label = None - if grp[0].startswith("#"): - label = grp[0][1:] - first_cell_grp = 1 + 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], @@ -169,11 +169,11 @@ 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 = 0 + first_cell_grp = 1 label = None - if grp[0].startswith("#"): - label = grp[0][1:] - first_cell_grp = 1 + 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], @@ -253,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( From 816f7dfb979b9319624557f7b98e74cb83440c85 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer <8102638+jpfeuffer@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:15:42 +0200 Subject: [PATCH 3/4] fix: oops copypasta --- mkquartodocs/extension.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkquartodocs/extension.py b/mkquartodocs/extension.py index 3349fbe..f367e71 100644 --- a/mkquartodocs/extension.py +++ b/mkquartodocs/extension.py @@ -175,7 +175,7 @@ def _from_cell_element_line(cls, line: str, line_number: int) -> BlockContext: label = grp[1][1:] first_cell_grp = 2 return BlockContext( - block_type=BlockType.CELL, + block_type=BlockType.CELL_ELEM, delimiter=grp[0], attributes=grp[first_cell_grp:], start=Cursor(line=line_number, col=0), From 652f232e4a4f696e4fcb4a4c956b9623ecebfd17 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer <8102638+jpfeuffer@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:46:44 +0200 Subject: [PATCH 4/4] hack: add label as id to div --- mkquartodocs/extension.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mkquartodocs/extension.py b/mkquartodocs/extension.py index f367e71..e06e997 100644 --- a/mkquartodocs/extension.py +++ b/mkquartodocs/extension.py @@ -24,7 +24,7 @@ # `:::: {.cell execution_count="1"}` # `:::::: {.cell layout-align="default"}` <- happens in mermaid diagrams -CELL_REGEX: Final = re.compile(r"^(:{3,}) \{(#[-\w_]+)?\s*\.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 @@ -372,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("