edwh-editorjs 2.0.1__tar.gz → 2.1.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.
Files changed (30) hide show
  1. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/CHANGELOG.md +16 -0
  2. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/PKG-INFO +1 -1
  3. edwh_editorjs-2.1.0/editorjs/__about__.py +1 -0
  4. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/editorjs/blocks.py +23 -7
  5. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/editorjs/core.py +4 -3
  6. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/tests/test_core.py +59 -3
  7. edwh_editorjs-2.0.1/editorjs/__about__.py +0 -1
  8. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/.github/workflows/build_documentation.yml +0 -0
  9. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/.github/workflows/publish_to_pypi.yml +0 -0
  10. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/.github/workflows/pytest.yml +0 -0
  11. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/.gitignore +0 -0
  12. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/README.md +0 -0
  13. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/editorjs/__init__.py +0 -0
  14. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/editorjs/exceptions.py +0 -0
  15. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/editorjs/helpers.py +0 -0
  16. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/editorjs/types.py +0 -0
  17. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/.gitignore +0 -0
  18. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/class_index.html +0 -0
  19. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/favicon_32_cb_58284776.png +0 -0
  20. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/function_index.html +0 -0
  21. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/index.html +0 -0
  22. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/keybd_closed_cb_ce680311.png +0 -0
  23. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/status.json +0 -0
  24. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/style_cb_8e611ae1.css +0 -0
  25. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/z_a93c8aeb4b8fa1f9___init___py.html +0 -0
  26. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/z_a93c8aeb4b8fa1f9_blocks_py.html +0 -0
  27. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/z_a93c8aeb4b8fa1f9_exceptions_py.html +0 -0
  28. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/htmlcov/z_a93c8aeb4b8fa1f9_parser_py.html +0 -0
  29. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/pyproject.toml +0 -0
  30. {edwh_editorjs-2.0.1 → edwh_editorjs-2.1.0}/tests/__init__.py +0 -0
@@ -2,6 +2,22 @@
2
2
 
3
3
  <!--next-version-placeholder-->
4
4
 
5
+ ## v2.1.0 (2024-11-26)
6
+
7
+ ### Feature
8
+
9
+ * `EditorJS.from_json` now also accepts a (json) list of blocks instead of the full object (dictionary with time, version, blocks) ([`9f24c15`](https://github.com/educationwarehouse/edwh-editorjs/commit/9f24c15983948db96153901e804b9a2b11029f49))
10
+
11
+ ### Fix
12
+
13
+ * Better separation between raw and paragraph blocks ([`f7620cf`](https://github.com/educationwarehouse/edwh-editorjs/commit/f7620cf6f34a0a9b9a218fb1c78a67a04f15b78e))
14
+
15
+ ## v2.0.2 (2024-11-26)
16
+
17
+ ### Fix
18
+
19
+ * Raw block pt2 ([`37ee6bc`](https://github.com/educationwarehouse/edwh-editorjs/commit/37ee6bc09dd8f54ca22f2652be1c5920d74779d1))
20
+
5
21
  ## v2.0.1 (2024-11-26)
6
22
 
7
23
  ### Fix
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: edwh-editorjs
3
- Version: 2.0.1
3
+ Version: 2.1.0
4
4
  Summary: EditorJS.py
5
5
  Project-URL: Homepage, https://github.com/educationwarehouse/edwh-EditorJS
6
6
  Author-email: SKevo <skevo.cw@gmail.com>, Robin van der Noord <robin.vdn@educationwarehouse.nl>
@@ -0,0 +1 @@
1
+ __version__ = "2.1.0"
@@ -130,12 +130,20 @@ class HeadingBlock(EditorJSBlock):
130
130
  return child.get("value", "")
131
131
 
132
132
 
133
+ def paragraph_block(text: str):
134
+ return {"type": "paragraph", "data": {"text": text}}
135
+
136
+
137
+ def raw_block(html: str):
138
+ return {"type": "raw", "data": {"html": html}}
139
+
140
+
133
141
  @block("paragraph")
134
142
  class ParagraphBlock(EditorJSBlock):
135
143
  @classmethod
136
144
  def to_markdown(cls, data: EditorChildData) -> str:
137
145
  text = data.get("text", "")
138
- return f"{text}\n"
146
+ return f"{text}\n\n"
139
147
 
140
148
  @classmethod
141
149
  def to_json(cls, node: MDChildNode) -> list[dict]:
@@ -165,7 +173,7 @@ class ParagraphBlock(EditorJSBlock):
165
173
  else:
166
174
  # <editorjs>something</editorjs> = 3 children
167
175
  result.extend(
168
- EditorJSCustom.to_json({"children": nodes[idx: idx + 2]})
176
+ EditorJSCustom.to_json({"children": nodes[idx : idx + 2]})
169
177
  )
170
178
 
171
179
  skip = 2
@@ -173,9 +181,14 @@ class ParagraphBlock(EditorJSBlock):
173
181
 
174
182
  elif _type == "image":
175
183
  if current_text:
176
- result.append({"data": {"text": current_text}, "type": "raw" if any_html else "paragraph"})
184
+ # {"id":"zksvpxQTDD","type":"raw","data":{"html":"<marquee> raw </marquee>"}}
185
+ result.append(
186
+ raw_block(current_text)
187
+ if any_html
188
+ else paragraph_block(current_text)
189
+ )
177
190
  current_text = ""
178
- any_html = False # reset
191
+ any_html = False # reset
179
192
 
180
193
  result.extend(ImageBlock.to_json(child))
181
194
  else:
@@ -194,7 +207,9 @@ class ParagraphBlock(EditorJSBlock):
194
207
 
195
208
  # final text after image:
196
209
  if current_text:
197
- result.append({"data": {"text": current_text}, "type": "raw" if any_html else "paragraph"})
210
+ result.append(
211
+ raw_block(current_text) if any_html else paragraph_block(current_text)
212
+ )
198
213
 
199
214
  return result
200
215
 
@@ -430,7 +445,8 @@ class RawBlock(EditorJSBlock):
430
445
 
431
446
  @classmethod
432
447
  def to_markdown(cls, data: EditorChildData) -> str:
433
- return data.get("html", "")
448
+ text = data.get("html", "")
449
+ return f"{text}\n\n"
434
450
 
435
451
  @classmethod
436
452
  def to_json(cls, node: MDChildNode) -> list[dict]:
@@ -499,7 +515,7 @@ class TableBlock(EditorJSBlock):
499
515
  "data": {
500
516
  "content": table,
501
517
  "withHeadings": with_headings,
502
- }
518
+ },
503
519
  }
504
520
  ]
505
521
 
@@ -31,13 +31,14 @@ class EditorJS:
31
31
  self._md = markdown2.Markdown(extras=extras) # todo: striketrough, ?
32
32
 
33
33
  @classmethod
34
- def from_json(cls, data: str | dict) -> Self:
34
+ def from_json(cls, data: str | dict | list) -> Self:
35
35
  """
36
36
  Load from EditorJS JSON Blocks
37
37
  """
38
- data = data if isinstance(data, dict) else json.loads(data)
38
+ data = data if isinstance(data, (dict, list)) else json.loads(data)
39
+ blocks = data["blocks"] if isinstance(data, dict) else data
39
40
  markdown_items = []
40
- for child in data["blocks"]:
41
+ for child in blocks:
41
42
  _type = child["type"]
42
43
  if not (block := BLOCKS.get(_type)):
43
44
  raise TypeError(f"Unsupported block type `{_type}`")
@@ -114,12 +114,68 @@ def test_attachment():
114
114
 
115
115
 
116
116
  def test_raw_html():
117
+ # e = EditorJS.from_markdown(textwrap.dedent("""
118
+ # # Raw HTML
119
+ #
120
+ # <marquee>This ain't no paragraph</marquee>
121
+ #
122
+ # This is a paragraph
123
+ # """))
124
+ #
125
+ # blocks = json.loads(e.to_json())
126
+ #
127
+ # print(blocks)
128
+ #
129
+ # assert blocks["blocks"][1]["type"] == "raw", blocks["blocks"][1]["type"]
130
+ # assert blocks["blocks"][2]["type"] == "paragraph", blocks["blocks"][2]["type"]
131
+ #
132
+
133
+ raw_html_json = r"""{"time":1730989152705,"blocks":[{"id":"DGQwRibbof","type":"paragraph","data":{"text":"The Start"}},{"id":"NrvWoJ2bVI","type":"raw","data":{"html":" <marquee> <kaas>mannetje </kaas> </marquee>Einde!"}},{"id":"DGQwRibbof","type":"paragraph","data":{"text":"The End!"}}],"version":"2.30.6"}"""
134
+
135
+ e = EditorJS.from_json(raw_html_json)
136
+ blocks = json.loads(e.to_json())
137
+
138
+ print(blocks)
139
+
140
+ assert blocks["blocks"][0]["type"] == "paragraph", blocks["blocks"][0]["type"]
141
+ assert blocks["blocks"][1]["type"] == "raw", blocks["blocks"][1]["type"]
142
+ assert blocks["blocks"][2]["type"] == "paragraph", blocks["blocks"][2]["type"]
143
+
144
+
145
+ def test_code():
117
146
  e = EditorJS.from_markdown(textwrap.dedent("""
118
- # Raw HTML
147
+ Read code:
119
148
 
120
- <marquee>This ain't no paragraph</marquee>
149
+ ```
150
+ <marquee> code </marquee>
151
+ ```
152
+
153
+ End of code
121
154
  """))
122
155
 
123
156
  blocks = json.loads(e.to_json())
124
157
 
125
- assert blocks["blocks"][1]["type"] == "raw", blocks["blocks"][1]["type"]
158
+ assert blocks["blocks"][0]["type"] == "paragraph", blocks["blocks"][0]["type"]
159
+ assert blocks["blocks"][1]["type"] == "code", blocks["blocks"][1]["type"]
160
+ assert blocks["blocks"][2]["type"] == "paragraph", blocks["blocks"][2]["type"]
161
+
162
+ # check if it's still the same after export and import:
163
+
164
+ e = EditorJS.from_json(blocks)
165
+
166
+ blocks = json.loads(e.to_json())
167
+
168
+ assert blocks["blocks"][0]["type"] == "paragraph", blocks["blocks"][0]["type"]
169
+ assert blocks["blocks"][1]["type"] == "code", blocks["blocks"][1]["type"]
170
+ assert blocks["blocks"][2]["type"] == "paragraph", blocks["blocks"][2]["type"]
171
+
172
+ # note: without `time` and `version` boilerplate:
173
+ blocks_json = r"""[{"id":"DD966BQf_t","type":"paragraph","data":{"text":"Pre"}},{"id":"sjr2JyuC1y","type":"code","data":{"code":"html"}},{"id":"aJN_wgBv7b","type":"paragraph","data":{"text":"Post"}}]"""
174
+
175
+ e = EditorJS.from_json(blocks_json)
176
+
177
+ blocks = json.loads(e.to_json())
178
+
179
+ assert blocks["blocks"][0]["type"] == "paragraph", blocks["blocks"][0]["type"]
180
+ assert blocks["blocks"][1]["type"] == "code", blocks["blocks"][1]["type"]
181
+ assert blocks["blocks"][2]["type"] == "paragraph", blocks["blocks"][2]["type"]
@@ -1 +0,0 @@
1
- __version__ = "2.0.1"
File without changes
File without changes