nbsync 0.3.9__tar.gz → 0.3.10__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.
- {nbsync-0.3.9 → nbsync-0.3.10}/PKG-INFO +1 -1
- {nbsync-0.3.9 → nbsync-0.3.10}/pyproject.toml +1 -1
- {nbsync-0.3.9 → nbsync-0.3.10}/src/nbsync/cell.py +64 -30
- {nbsync-0.3.9 → nbsync-0.3.10}/LICENSE +0 -0
- {nbsync-0.3.9 → nbsync-0.3.10}/README.md +0 -0
- {nbsync-0.3.9 → nbsync-0.3.10}/src/nbsync/__init__.py +0 -0
- {nbsync-0.3.9 → nbsync-0.3.10}/src/nbsync/logger.py +0 -0
- {nbsync-0.3.9 → nbsync-0.3.10}/src/nbsync/markdown.py +0 -0
- {nbsync-0.3.9 → nbsync-0.3.10}/src/nbsync/notebook.py +0 -0
- {nbsync-0.3.9 → nbsync-0.3.10}/src/nbsync/py.typed +0 -0
- {nbsync-0.3.9 → nbsync-0.3.10}/src/nbsync/sync.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: nbsync
|
3
|
-
Version: 0.3.
|
3
|
+
Version: 0.3.10
|
4
4
|
Summary: A core library to synchronize Jupyter notebooks and Markdown documents, enabling seamless integration and dynamic content execution
|
5
5
|
Keywords: jupyter,notebook,documentation,markdown,python,visualization,dynamic-execution,real-time-sync
|
6
6
|
Author: daizutabi
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = "nbsync"
|
7
|
-
version = "0.3.
|
7
|
+
version = "0.3.10"
|
8
8
|
description = "A core library to synchronize Jupyter notebooks and Markdown documents, enabling seamless integration and dynamic content execution"
|
9
9
|
readme = "README.md"
|
10
10
|
license = { file = "LICENSE" }
|
@@ -13,6 +13,23 @@ if TYPE_CHECKING:
|
|
13
13
|
from nbstore.markdown import Image
|
14
14
|
|
15
15
|
|
16
|
+
@dataclass
|
17
|
+
class Attributes:
|
18
|
+
source: str
|
19
|
+
tabs: str
|
20
|
+
identifier: str
|
21
|
+
result: str
|
22
|
+
|
23
|
+
@classmethod
|
24
|
+
def pop(cls, attrs: dict[str, str]) -> Attributes:
|
25
|
+
return cls(
|
26
|
+
source=attrs.pop("source", ""),
|
27
|
+
tabs=attrs.pop("tabs", ""),
|
28
|
+
identifier=attrs.pop("identifier", ""),
|
29
|
+
result=attrs.pop("result", ""),
|
30
|
+
)
|
31
|
+
|
32
|
+
|
16
33
|
@dataclass
|
17
34
|
class Cell:
|
18
35
|
image: Image
|
@@ -28,64 +45,77 @@ class Cell:
|
|
28
45
|
"""The content of the image."""
|
29
46
|
|
30
47
|
def convert(self, *, escape: bool = False) -> str:
|
31
|
-
|
32
|
-
tabs = self.image.attributes.pop("tabs", "")
|
33
|
-
identifier = self.image.attributes.pop("identifier", "")
|
34
|
-
result = self.image.attributes.pop("result", "")
|
35
|
-
|
36
|
-
if "/" not in self.mime or not self.content or kind == "only":
|
37
|
-
if self.image.source:
|
38
|
-
source = get_source(
|
39
|
-
self,
|
40
|
-
include_attrs=True,
|
41
|
-
include_identifier=bool(identifier),
|
42
|
-
)
|
43
|
-
kind = "only"
|
44
|
-
else:
|
45
|
-
source = ""
|
46
|
-
result = self.image.url = ""
|
48
|
+
attrs = Attributes.pop(self.image.attributes)
|
47
49
|
|
48
|
-
|
49
|
-
source = get_source(
|
50
|
-
self,
|
51
|
-
include_attrs=True,
|
52
|
-
include_identifier=bool(identifier),
|
53
|
-
)
|
50
|
+
if include_attrs := self._include_attributes():
|
54
51
|
self.image.url = ""
|
55
|
-
|
52
|
+
|
53
|
+
source = get_source(
|
54
|
+
self,
|
55
|
+
console=attrs.source == "console",
|
56
|
+
include_attrs=include_attrs,
|
57
|
+
include_identifier=bool(attrs.identifier),
|
58
|
+
)
|
59
|
+
|
60
|
+
if not self.content or attrs.source in ["console", "only"]:
|
61
|
+
attrs.source = "only" if self.image.source else ""
|
62
|
+
result = ""
|
63
|
+
|
64
|
+
elif self.mime.startswith("text/") and isinstance(self.content, str):
|
65
|
+
result = get_text_markdown(self, attrs.result, escape=escape)
|
56
66
|
|
57
67
|
else:
|
58
|
-
source = get_source(
|
59
|
-
self,
|
60
|
-
include_attrs=False,
|
61
|
-
include_identifier=bool(identifier),
|
62
|
-
)
|
63
68
|
result = get_image_markdown(self)
|
64
69
|
|
65
|
-
if markdown := get_markdown(
|
70
|
+
if markdown := get_markdown(attrs.source, source, result, attrs.tabs):
|
66
71
|
return textwrap.indent(markdown, self.image.indent)
|
67
72
|
|
68
73
|
return "" # no cov
|
69
74
|
|
75
|
+
def _include_attributes(self) -> bool:
|
76
|
+
if "/" not in self.mime or not self.content:
|
77
|
+
return True
|
78
|
+
|
79
|
+
return self.mime.startswith("text/") and isinstance(self.content, str)
|
80
|
+
|
70
81
|
|
71
82
|
def get_source(
|
72
83
|
cell: Cell,
|
73
84
|
*,
|
85
|
+
console: bool = False,
|
74
86
|
include_attrs: bool = False,
|
75
87
|
include_identifier: bool = False,
|
76
88
|
) -> str:
|
89
|
+
if not (source := cell.image.source):
|
90
|
+
return ""
|
91
|
+
|
92
|
+
if console:
|
93
|
+
output = str(cell.content.rstrip())
|
94
|
+
source = f"{_add_prompt(source)}\n{output}"
|
95
|
+
|
77
96
|
attrs = [cell.language]
|
78
97
|
if include_attrs:
|
79
98
|
attrs.extend(cell.image.iter_parts())
|
80
99
|
attr = " ".join(attrs)
|
81
100
|
|
82
|
-
source = cell.image.source
|
83
101
|
if include_identifier:
|
84
102
|
source = f"# #{cell.image.identifier}\n{source}"
|
85
103
|
|
86
104
|
return f"```{attr}\n{source}\n```"
|
87
105
|
|
88
106
|
|
107
|
+
def _add_prompt(source: str) -> str:
|
108
|
+
lines = []
|
109
|
+
for line in source.splitlines():
|
110
|
+
if not line:
|
111
|
+
lines.append("")
|
112
|
+
elif line.startswith(" "):
|
113
|
+
lines.append(f"... {line}")
|
114
|
+
else:
|
115
|
+
lines.append(f">>> {line}")
|
116
|
+
return "\n".join(lines)
|
117
|
+
|
118
|
+
|
89
119
|
def get_text_markdown(cell: Cell, result: str, *, escape: bool = False) -> str:
|
90
120
|
text = str(cell.content.rstrip())
|
91
121
|
|
@@ -103,6 +133,10 @@ def get_image_markdown(cell: Cell) -> str:
|
|
103
133
|
msg = f"{cell.image.url}#{cell.image.identifier} [{cell.mime}]"
|
104
134
|
logger.debug(f"Converting image: {msg}")
|
105
135
|
|
136
|
+
if "/" not in cell.mime:
|
137
|
+
cell.image.url = ""
|
138
|
+
return ""
|
139
|
+
|
106
140
|
ext = cell.mime.split("/")[1].split("+")[0]
|
107
141
|
cell.image.url = f"{uuid.uuid4()}.{ext}"
|
108
142
|
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|