nbsync 0.3.3__tar.gz → 0.3.5__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.
Files changed (28) hide show
  1. {nbsync-0.3.3 → nbsync-0.3.5}/PKG-INFO +1 -1
  2. {nbsync-0.3.3 → nbsync-0.3.5}/pyproject.toml +1 -1
  3. {nbsync-0.3.3 → nbsync-0.3.5}/src/nbsync/cell.py +18 -5
  4. {nbsync-0.3.3 → nbsync-0.3.5}/tests/test_cell.py +36 -1
  5. {nbsync-0.3.3 → nbsync-0.3.5}/.devcontainer/devcontainer.json +0 -0
  6. {nbsync-0.3.3 → nbsync-0.3.5}/.devcontainer/postCreate.sh +0 -0
  7. {nbsync-0.3.3 → nbsync-0.3.5}/.devcontainer/starship.toml +0 -0
  8. {nbsync-0.3.3 → nbsync-0.3.5}/.gitattributes +0 -0
  9. {nbsync-0.3.3 → nbsync-0.3.5}/.github/workflows/ci.yaml +0 -0
  10. {nbsync-0.3.3 → nbsync-0.3.5}/.github/workflows/docs.yaml +0 -0
  11. {nbsync-0.3.3 → nbsync-0.3.5}/.github/workflows/publish.yaml +0 -0
  12. {nbsync-0.3.3 → nbsync-0.3.5}/.gitignore +0 -0
  13. {nbsync-0.3.3 → nbsync-0.3.5}/LICENSE +0 -0
  14. {nbsync-0.3.3 → nbsync-0.3.5}/README.md +0 -0
  15. {nbsync-0.3.3 → nbsync-0.3.5}/docs/index.md +0 -0
  16. {nbsync-0.3.3 → nbsync-0.3.5}/mkdocs.yaml +0 -0
  17. {nbsync-0.3.3 → nbsync-0.3.5}/src/nbsync/__init__.py +0 -0
  18. {nbsync-0.3.3 → nbsync-0.3.5}/src/nbsync/logger.py +0 -0
  19. {nbsync-0.3.3 → nbsync-0.3.5}/src/nbsync/markdown.py +0 -0
  20. {nbsync-0.3.3 → nbsync-0.3.5}/src/nbsync/notebook.py +0 -0
  21. {nbsync-0.3.3 → nbsync-0.3.5}/src/nbsync/py.typed +0 -0
  22. {nbsync-0.3.3 → nbsync-0.3.5}/src/nbsync/sync.py +0 -0
  23. {nbsync-0.3.3 → nbsync-0.3.5}/tests/__init__.py +0 -0
  24. {nbsync-0.3.3 → nbsync-0.3.5}/tests/conftest.py +0 -0
  25. {nbsync-0.3.3 → nbsync-0.3.5}/tests/test_logger.py +0 -0
  26. {nbsync-0.3.3 → nbsync-0.3.5}/tests/test_markdown.py +0 -0
  27. {nbsync-0.3.3 → nbsync-0.3.5}/tests/test_notebook.py +0 -0
  28. {nbsync-0.3.3 → nbsync-0.3.5}/tests/test_sync.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nbsync
3
- Version: 0.3.3
3
+ Version: 0.3.5
4
4
  Summary: A core library to synchronize Jupyter notebooks and Markdown documents, enabling seamless integration and dynamic content execution
5
5
  Project-URL: Documentation, https://daizutabi.github.io/nbsync/
6
6
  Project-URL: Source, https://github.com/daizutabi/nbsync
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "nbsync"
7
- version = "0.3.3"
7
+ version = "0.3.5"
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" }
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import html
3
4
  import textwrap
4
5
  import uuid
5
6
  from dataclasses import dataclass
@@ -26,7 +27,7 @@ class Cell:
26
27
  content: bytes | str
27
28
  """The content of the image."""
28
29
 
29
- def convert(self) -> str:
30
+ def convert(self, *, escape: bool = False) -> str:
30
31
  kind = self.image.attributes.pop("source", "")
31
32
  tabs = self.image.attributes.pop("tabs", "")
32
33
  identifier = self.image.attributes.pop("identifier", "")
@@ -49,8 +50,8 @@ class Cell:
49
50
  include_attrs=True,
50
51
  include_identifier=bool(identifier),
51
52
  )
52
- result, self.image.url = self.content, ""
53
- result = result.rstrip()
53
+ self.image.url = ""
54
+ result = get_text_markdown(self, escape=escape)
54
55
 
55
56
  else:
56
57
  source = get_source(
@@ -58,7 +59,7 @@ class Cell:
58
59
  include_attrs=False,
59
60
  include_identifier=bool(identifier),
60
61
  )
61
- result = get_result(self)
62
+ result = get_image_markdown(self)
62
63
 
63
64
  if markdown := get_markdown(kind, source, result, tabs):
64
65
  return textwrap.indent(markdown, self.image.indent)
@@ -84,7 +85,19 @@ def get_source(
84
85
  return f"```{attr}\n{source}\n```"
85
86
 
86
87
 
87
- def get_result(cell: Cell) -> str:
88
+ def get_text_markdown(cell: Cell, *, escape: bool = False) -> str:
89
+ text = str(cell.content.rstrip())
90
+
91
+ if lang := cell.image.attributes.get("result", ""):
92
+ return f"```{lang}\n{text}\n```"
93
+
94
+ if escape and cell.mime == "text/plain":
95
+ return html.escape(text)
96
+
97
+ return text
98
+
99
+
100
+ def get_image_markdown(cell: Cell) -> str:
88
101
  msg = f"{cell.image.url}#{cell.image.identifier} [{cell.mime}]"
89
102
  logger.debug(f"Converting image: {msg}")
90
103
 
@@ -1,3 +1,5 @@
1
+ import textwrap
2
+
1
3
  import nbformat
2
4
  import nbstore.notebook
3
5
  import pytest
@@ -38,7 +40,7 @@ def convert(sync: Synchronizer):
38
40
  def convert(text: str) -> str:
39
41
  cell = next(sync.convert(text))
40
42
  assert isinstance(cell, Cell)
41
- return cell.convert()
43
+ return cell.convert(escape=True)
42
44
 
43
45
  return convert
44
46
 
@@ -106,3 +108,36 @@ def test_unknown(convert):
106
108
  def test_code_block_exec(convert):
107
109
  x = convert('```python exec="1" source="1"\nprint(1+1)\n```')
108
110
  assert x == "```python\nprint(1+1)\n```\n\n2"
111
+
112
+
113
+ def test_code_block_exec_escape_print(convert):
114
+ x = convert('```python exec="1" source="1"\nprint("<1>")\n```')
115
+ assert x == '```python\nprint("<1>")\n```\n\n&lt;1&gt;'
116
+
117
+
118
+ def test_code_block_exec_escape_stream(convert):
119
+ x = convert('```python exec="1" source="1"\n"<1>"\n```')
120
+ assert x == '```python\n"<1>"\n```\n\n&#x27;&lt;1&gt;&#x27;'
121
+
122
+
123
+ def test_code_block_exec_result(convert):
124
+ x = convert('```python exec="1" result="text"\nprint(1+1)\nprint("<1>")\n```')
125
+ assert x == "```text\n2\n<1>\n```"
126
+
127
+
128
+ def test_code_block_html(convert):
129
+ src = textwrap.dedent("""\
130
+ from IPython.display import HTML
131
+ HTML("<div>a</div>")
132
+ """)
133
+ x = convert(f'```python exec="1"\n{src}```')
134
+ assert x == "<div>a</div>"
135
+
136
+
137
+ def test_code_block_html_result(convert):
138
+ src = textwrap.dedent("""\
139
+ from IPython.display import HTML
140
+ HTML("<div>a</div>")
141
+ """)
142
+ x = convert(f'```python exec="1" result="html"\n{src}```')
143
+ assert x == "```html\n<div>a</div>\n```"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes