marimo-md-export 0.5.2__tar.gz → 0.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: marimo-md-export
3
- Version: 0.5.2
3
+ Version: 0.7.0
4
4
  Summary: A minimal solution for exporting marimo notebooks to markdown *with* rendered HTML outputs, designed for mkdocs/zensical sites.
5
5
  Keywords: marimo,markdown,documentation
6
6
  Author: Joe Marsh Rossney
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "marimo-md-export"
7
- version = "0.5.2"
7
+ version = "0.7.0"
8
8
  description = "A minimal solution for exporting marimo notebooks to markdown *with* rendered HTML outputs, designed for mkdocs/zensical sites."
9
9
  authors = [
10
10
  { name = "Joe Marsh Rossney", email = "17361029+jmarshrossney@users.noreply.github.com" }
@@ -51,6 +51,7 @@ docs = [
51
51
  "zensical>=0.0.37",
52
52
  ]
53
53
  examples = [
54
+ "graphviz>=0.21",
54
55
  "matplotlib>=3.10.9",
55
56
  "numpy>=2.4.4",
56
57
  "pandas>=3.0.2",
@@ -70,4 +71,7 @@ include = ["src/", "tests/"]
70
71
  [tool.ruff]
71
72
  line-length = 88
72
73
  indent-width = 4
73
- target-version = "py313"
74
+ target-version = "py312"
75
+
76
+ [tool.uv]
77
+ exclude-newer = "1 week"
@@ -11,6 +11,7 @@ from .inject import (
11
11
  )
12
12
  from .parse_html import extract_outputs
13
13
  from .parse_md import collect_cells
14
+ from .transform import convert_admonitions
14
15
 
15
16
  _err_console = Console(stderr=True)
16
17
 
@@ -85,6 +86,7 @@ def main(
85
86
  typer.echo(f"marimo export md failed:\n{exc}", err=True)
86
87
  raise typer.Exit(1)
87
88
 
89
+ md = convert_admonitions(md)
88
90
  cells = collect_cells(md)
89
91
 
90
92
  if verbose:
@@ -93,12 +93,13 @@ def export_html(
93
93
  ]
94
94
  if sandbox:
95
95
  cmd.append("--sandbox")
96
+ else:
97
+ cmd.append("--no-sandbox")
96
98
  if extra_args:
97
99
  cmd.append("--")
98
100
  cmd.extend(extra_args)
99
101
  env = os.environ.copy()
100
102
  env["MPLBACKEND"] = "Agg"
101
- env["MARIMO_MANAGE_SCRIPT_METADATA"] = "true"
102
103
  try:
103
104
  result = _run_with_visible_output(cmd, env=env, timeout=timeout)
104
105
  except subprocess.TimeoutExpired:
@@ -127,9 +128,10 @@ def export_md(
127
128
  cmd = ["marimo", "export", "md", str(notebook), "-o", str(tmp), "--force"]
128
129
  if sandbox:
129
130
  cmd.append("--sandbox")
131
+ else:
132
+ cmd.append("--no-sandbox")
130
133
  env = os.environ.copy()
131
134
  env["MPLBACKEND"] = "Agg"
132
- env["MARIMO_MANAGE_SCRIPT_METADATA"] = "true"
133
135
  try:
134
136
  result = _run_with_visible_output(cmd, env=env, timeout=timeout)
135
137
  except subprocess.TimeoutExpired:
@@ -66,6 +66,15 @@ def _escape_brackets_in_pre(html: str) -> str:
66
66
  )
67
67
 
68
68
 
69
+ def _escape_brackets_in_html(html: str) -> str:
70
+ return re.sub(
71
+ r">([^<]+)<",
72
+ lambda m: ">" + _escape_brackets(m.group(1)) + "<",
73
+ html,
74
+ flags=re.DOTALL,
75
+ )
76
+
77
+
69
78
  def _format_output(output: ExtractedOutput, pre_style: str) -> str:
70
79
  comment = f"<!-- @output:{output.cell_id} -->"
71
80
 
@@ -90,6 +99,8 @@ def _format_output(output: ExtractedOutput, pre_style: str) -> str:
90
99
  _inject_pre_style(output.raw_html, pre_style)
91
100
  )
92
101
  )
102
+ elif output.output_type in ("html", "figure"):
103
+ parts.append(_escape_brackets_in_html(output.raw_html))
93
104
  else:
94
105
  parts.append(
95
106
  _escape_brackets_in_pre(_inject_pre_style(output.raw_html, pre_style))
@@ -24,7 +24,7 @@
24
24
  # text/latex — LaTeX math (wrapped in $$ delimiters)
25
25
  # text/csv — CSV data (rendered as code block)
26
26
  # text/html — may contain <marimo-table> web component
27
- # text/markdown — rendered markdown (placeholder comment; already in MD export)
27
+ # text/markdown — rendered markdown (text/html output from mo.md)
28
28
  # text/plain — plain text
29
29
  # Unsupported types (vega, jupyter widgets, etc.) produce a placeholder comment.
30
30
  #
@@ -36,6 +36,7 @@ import json
36
36
  import pprint
37
37
  import re
38
38
  from html import escape, unescape
39
+ from urllib.parse import quote as _url_quote
39
40
 
40
41
  from bs4 import BeautifulSoup
41
42
 
@@ -196,6 +197,8 @@ def _classify_and_build(
196
197
  val = bundle.get(mime_key)
197
198
  if val:
198
199
  if mime_key.startswith("image/"):
200
+ if mime_key == "image/svg+xml" and not val.startswith("data:"):
201
+ val = f"data:image/svg+xml,{_url_quote(val, safe='')}"
199
202
  return (
200
203
  "figure",
201
204
  f'<img src="{escape(val, quote=True)}" alt="{escape(mime_key.split("/")[1], quote=True)}">',
@@ -220,6 +223,8 @@ def _classify_and_build(
220
223
  ):
221
224
  img_val = data.get(mime_type)
222
225
  if img_val:
226
+ if mime_type == "image/svg+xml" and not img_val.startswith("data:"):
227
+ img_val = f"data:image/svg+xml,{_url_quote(img_val, safe='')}"
223
228
  fmt = mime_type.split("/")[1]
224
229
  return (
225
230
  "figure",
@@ -306,13 +311,16 @@ def _classify_and_build(
306
311
  f"<pre>{escape(plain)}</pre>",
307
312
  )
308
313
 
314
+ md_val = data.get("text/markdown")
315
+ if md_val:
316
+ return "html", unescape(md_val)
317
+
309
318
  unsupported = {
310
319
  "application/vnd.vega.v5+json",
311
320
  "application/vnd.vega.v6+json",
312
321
  "application/vnd.vegalite.v5+json",
313
322
  "application/vnd.vegalite.v6+json",
314
323
  "application/vnd.jupyter.widget-view+json",
315
- "text/markdown",
316
324
  "text/password",
317
325
  }
318
326
  for mime_type in unsupported:
@@ -3,7 +3,7 @@ import re
3
3
 
4
4
  from .models import Cell
5
5
 
6
- _BLOCK_RE = re.compile(r"(```python \{\.marimo\}\n(.*?)```)", re.DOTALL)
6
+ _BLOCK_RE = re.compile(r"(```python \{\.marimo[^}]*\}\n(.*?)```)", re.DOTALL)
7
7
  _SUPPRESS_RE = re.compile(r"#\s*@suppress")
8
8
  _SCROLL_RE = re.compile(r"#\s*@scroll")
9
9
  _WRAP_RE = re.compile(r"#\s*@wrap")
@@ -0,0 +1,41 @@
1
+ """Markdown-to-markdown transformations for exported notebooks.
2
+
3
+ Functions in this module take the markdown produced by ``marimo export md``
4
+ and transform it for compatibility with downstream renderers like MkDocs
5
+ or Zensical.
6
+ """
7
+
8
+ import re
9
+
10
+ _ADN_RE = re.compile(
11
+ r"^///\s+(\w+)(?:\s*\|\s*(.*?))?\s*$\n(.*?)^///\s*$",
12
+ re.MULTILINE | re.DOTALL,
13
+ )
14
+
15
+
16
+ def _convert_one(match: re.Match) -> str:
17
+ admon_type = match.group(1)
18
+ title = match.group(2)
19
+ content = match.group(3)
20
+
21
+ if title is not None:
22
+ title = title.strip().replace('"', '\\"')
23
+ header = f'!!! {admon_type} "{title}"'
24
+ else:
25
+ header = f"!!! {admon_type}"
26
+
27
+ if not content.strip():
28
+ return header + "\n"
29
+
30
+ indented = "\n".join(" " + line for line in content.splitlines())
31
+ return f"{header}\n{indented}\n"
32
+
33
+
34
+ def convert_admonitions(md: str) -> str:
35
+ """Convert marimo-style admonitions to MkDocs/Zensical format.
36
+
37
+ Transforms ``/// type | Title ... ///`` blocks into
38
+ ``!!! type "Title"`` with 4-space-indented content.
39
+ Omits the title (and quotes) when no title is provided.
40
+ """
41
+ return _ADN_RE.sub(_convert_one, md)