Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions mkquartodocs/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*)$")
Expand Down Expand Up @@ -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}")
Expand All @@ -149,25 +150,37 @@ 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
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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("<div>") and non_empty[-1].endswith("</div>"):
print("FOO")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove these lines

print(self.label)
if "style" in internal[1]:
internal[0] = '<div markdown="block">'
if self.label:
internal[0] = f'<div markdown="block" id="{self.label}">'
else:
internal[0] = '<div markdown="block">'
else:
if self.label:
internal[0] = f'<div id="{self.label}">'

last_end = self.end

Expand Down