|
15 | 15 | import re |
16 | 16 | from typing import TYPE_CHECKING |
17 | 17 |
|
| 18 | +from marimo import _loggers |
18 | 19 | from marimo._convert.markdown.flavor.base import ( |
19 | 20 | CodeCellBlock, |
20 | 21 | MarkdownCellBlock, |
21 | 22 | MarkdownExportDocument, |
22 | 23 | MarkdownFlavor, |
| 24 | + MarkdownFlavorName, |
| 25 | + MarkdownImportContext, |
| 26 | + _escape_attribute, |
23 | 27 | ) |
24 | 28 |
|
25 | 29 | if TYPE_CHECKING: |
26 | 30 | from collections.abc import Mapping |
27 | 31 |
|
| 32 | +LOGGER = _loggers.marimo_logger() |
| 33 | + |
28 | 34 | # Metadata emitted through the `{marimo-config}` directive. |
29 | 35 | _CONFIG_KEYS = {"header", "pyproject"} |
30 | 36 | # marimo-specific metadata filtered before writing MyST frontmatter. |
31 | 37 | _MARIMO_METADATA_KEYS = {"width"} |
| 38 | +# MyST marimo executable directive headers. |
| 39 | +_MARIMO_DIRECTIVE_HEADER_RE = re.compile( |
| 40 | + r"^(?P<fence>`{3,})\{marimo\}(?:\s+(?P<language>\w+))?\s*$" |
| 41 | +) |
| 42 | +# MyST marimo page-level configuration directive headers. |
| 43 | +_MARIMO_CONFIG_HEADER_RE = re.compile( |
| 44 | + r"^(?P<fence>`{3,})\{marimo-config\}\s*$" |
| 45 | +) |
| 46 | +# MyST directive options, e.g. `:hide-code: true`. |
| 47 | +_DIRECTIVE_OPTION_RE = re.compile(r"^:([A-Za-z0-9_-]+):(?:\s+(.*))?$") |
32 | 48 | # PEP 723 script metadata blocks embedded in exported notebook headers. |
33 | 49 | _SCRIPT_METADATA_RE = re.compile( |
34 | 50 | r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s" |
35 | 51 | r"(?P<content>(^#(| .*)$\s)+)^# ///$" |
36 | 52 | ) |
37 | 53 |
|
38 | 54 |
|
| 55 | +class _MystmdMarkdownImportDialect: |
| 56 | + name: MarkdownFlavorName = "mystmd" |
| 57 | + |
| 58 | + def matches(self, text: str, filepath: str | None) -> bool: |
| 59 | + del filepath |
| 60 | + return any(_is_marimo_header(line) for line in text.splitlines()) |
| 61 | + |
| 62 | + def preprocess( |
| 63 | + self, lines: list[str], context: MarkdownImportContext |
| 64 | + ) -> list[str]: |
| 65 | + normalized: list[str] = [] |
| 66 | + index = 0 |
| 67 | + |
| 68 | + while index < len(lines): |
| 69 | + config_match = _MARIMO_CONFIG_HEADER_RE.match(lines[index]) |
| 70 | + if config_match is not None: |
| 71 | + closing_index = _find_closing_fence( |
| 72 | + lines, index + 1, config_match.group("fence") |
| 73 | + ) |
| 74 | + if closing_index is None: |
| 75 | + normalized.extend(lines[index:]) |
| 76 | + break |
| 77 | + |
| 78 | + context.metadata.update( |
| 79 | + _extract_config_metadata(lines[index + 1 : closing_index]) |
| 80 | + ) |
| 81 | + index = closing_index + 1 |
| 82 | + continue |
| 83 | + |
| 84 | + match = _MARIMO_DIRECTIVE_HEADER_RE.match(lines[index]) |
| 85 | + if match is None: |
| 86 | + normalized.append(lines[index]) |
| 87 | + index += 1 |
| 88 | + continue |
| 89 | + |
| 90 | + closing_index = _find_closing_fence( |
| 91 | + lines, index + 1, match.group("fence") |
| 92 | + ) |
| 93 | + if closing_index is None: |
| 94 | + normalized.extend(lines[index:]) |
| 95 | + break |
| 96 | + |
| 97 | + options, body_lines = _extract_directive_options( |
| 98 | + lines[index + 1 : closing_index] |
| 99 | + ) |
| 100 | + normalized.append(_canonical_code_fence_head(match, options)) |
| 101 | + normalized.extend(body_lines) |
| 102 | + normalized.append(lines[closing_index]) |
| 103 | + index = closing_index + 1 |
| 104 | + |
| 105 | + return normalized |
| 106 | + |
| 107 | + |
39 | 108 | class MystmdMarkdownFlavor(MarkdownFlavor): |
40 | 109 | """Render marimo notebooks as mystmd markdown. |
41 | 110 |
|
@@ -164,3 +233,82 @@ def _uncomment_script_metadata(content: str) -> str: |
164 | 233 |
|
165 | 234 | def _mystmd_option_name(key: str) -> str: |
166 | 235 | return key.replace("_", "-") |
| 236 | + |
| 237 | + |
| 238 | +def _is_marimo_header(line: str) -> bool: |
| 239 | + return bool( |
| 240 | + _MARIMO_DIRECTIVE_HEADER_RE.match(line) |
| 241 | + or _MARIMO_CONFIG_HEADER_RE.match(line) |
| 242 | + ) |
| 243 | + |
| 244 | + |
| 245 | +def _is_closing_fence(line: str, opening_fence: str) -> bool: |
| 246 | + stripped = line.strip() |
| 247 | + return len(stripped) >= len(opening_fence) and set(stripped) == {"`"} |
| 248 | + |
| 249 | + |
| 250 | +def _find_closing_fence( |
| 251 | + lines: list[str], start: int, opening_fence: str |
| 252 | +) -> int | None: |
| 253 | + for index in range(start, len(lines)): |
| 254 | + if _is_closing_fence(lines[index], opening_fence): |
| 255 | + return index |
| 256 | + return None |
| 257 | + |
| 258 | + |
| 259 | +def _extract_directive_options( |
| 260 | + lines: list[str], |
| 261 | +) -> tuple[dict[str, str], list[str]]: |
| 262 | + options: dict[str, str] = {} |
| 263 | + body_start = 0 |
| 264 | + |
| 265 | + for index, line in enumerate(lines): |
| 266 | + match = _DIRECTIVE_OPTION_RE.match(line) |
| 267 | + if match is None: |
| 268 | + break |
| 269 | + options[match.group(1).replace("-", "_")] = match.group(2) or "true" |
| 270 | + body_start = index + 1 |
| 271 | + |
| 272 | + if body_start and body_start < len(lines) and lines[body_start] == "": |
| 273 | + body_start += 1 |
| 274 | + |
| 275 | + return options, lines[body_start:] |
| 276 | + |
| 277 | + |
| 278 | +def _canonical_code_fence_head( |
| 279 | + match: re.Match[str], options: dict[str, str] |
| 280 | +) -> str: |
| 281 | + attribute_str = "".join( |
| 282 | + f' {key}="{_escape_attribute(value)}"' |
| 283 | + for key, value in options.items() |
| 284 | + ) |
| 285 | + return "{fence}{language} {{.marimo{attributes}}}".format( |
| 286 | + fence=match.group("fence"), |
| 287 | + language=match.group("language") or "python", |
| 288 | + attributes=attribute_str, |
| 289 | + ) |
| 290 | + |
| 291 | + |
| 292 | +def _extract_config_metadata(lines: list[str]) -> dict[str, str]: |
| 293 | + from marimo._utils import yaml |
| 294 | + |
| 295 | + if lines and lines[0] == "---": |
| 296 | + for index, line in enumerate(lines[1:], start=1): |
| 297 | + if line == "---": |
| 298 | + lines = lines[1:index] |
| 299 | + break |
| 300 | + |
| 301 | + try: |
| 302 | + metadata = yaml.load("\n".join(lines)) |
| 303 | + except yaml.YAMLError: |
| 304 | + LOGGER.warning("Error parsing marimo-config YAML. Ignoring config.") |
| 305 | + return {} |
| 306 | + |
| 307 | + if not isinstance(metadata, dict): |
| 308 | + return {} |
| 309 | + |
| 310 | + return { |
| 311 | + key: value |
| 312 | + for key, value in metadata.items() |
| 313 | + if key in _CONFIG_KEYS and isinstance(value, str) |
| 314 | + } |
0 commit comments