nbsync 0.3.8__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: nbsync
3
- Version: 0.3.8
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.8"
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,74 +45,83 @@ class Cell:
28
45
  """The content of the image."""
29
46
 
30
47
  def convert(self, *, escape: bool = False) -> str:
31
- kind = self.image.attributes.pop("source", "")
32
- tabs = self.image.attributes.pop("tabs", "")
33
- identifier = self.image.attributes.pop("identifier", "")
34
-
35
- if "/" not in self.mime or not self.content or kind == "source-only":
36
- if self.image.source:
37
- source = get_source(
38
- self,
39
- include_attrs=True,
40
- include_identifier=bool(identifier),
41
- )
42
- kind = "only"
43
- else:
44
- source = ""
45
- result, self.image.url = "", ""
48
+ attrs = Attributes.pop(self.image.attributes)
46
49
 
47
- elif self.mime.startswith("text/") and isinstance(self.content, str):
48
- source = get_source(
49
- self,
50
- include_attrs=True,
51
- include_identifier=bool(identifier),
52
- )
50
+ if include_attrs := self._include_attributes():
53
51
  self.image.url = ""
54
- result = get_text_markdown(self, escape=escape)
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)
55
66
 
56
67
  else:
57
- source = get_source(
58
- self,
59
- include_attrs=False,
60
- include_identifier=bool(identifier),
61
- )
62
68
  result = get_image_markdown(self)
63
69
 
64
- if markdown := get_markdown(kind, source, result, tabs):
70
+ if markdown := get_markdown(attrs.source, source, result, attrs.tabs):
65
71
  return textwrap.indent(markdown, self.image.indent)
66
72
 
67
73
  return "" # no cov
68
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
+
69
81
 
70
82
  def get_source(
71
83
  cell: Cell,
72
84
  *,
85
+ console: bool = False,
73
86
  include_attrs: bool = False,
74
87
  include_identifier: bool = False,
75
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
+
76
96
  attrs = [cell.language]
77
97
  if include_attrs:
78
- parts = cell.image.iter_parts(exclude_attributes=["result"])
79
- attrs.extend(parts)
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
 
89
- def get_text_markdown(cell: Cell, *, escape: bool = False) -> str:
90
- text = str(cell.content.rstrip())
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)
91
117
 
92
- if result := cell.image.attributes.get("result", ""):
93
- if not is_truelike(result):
94
- lang = result
95
- return f"```{lang}\n{text}\n```"
96
118
 
97
- if cell.mime == "text/plain":
98
- return f"```text\n{text}\n```"
119
+ def get_text_markdown(cell: Cell, result: str, *, escape: bool = False) -> str:
120
+ text = str(cell.content.rstrip())
121
+
122
+ if result:
123
+ result = "text" if is_truelike(result) else result
124
+ return f"```{result}\n{text}\n```"
99
125
 
100
126
  if escape and cell.mime == "text/plain":
101
127
  return html.escape(text)
@@ -107,6 +133,10 @@ def get_image_markdown(cell: Cell) -> str:
107
133
  msg = f"{cell.image.url}#{cell.image.identifier} [{cell.mime}]"
108
134
  logger.debug(f"Converting image: {msg}")
109
135
 
136
+ if "/" not in cell.mime:
137
+ cell.image.url = ""
138
+ return ""
139
+
110
140
  ext = cell.mime.split("/")[1].split("+")[0]
111
141
  cell.image.url = f"{uuid.uuid4()}.{ext}"
112
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