wordpress-markdown-blog-loader 1.6.3__tar.gz → 1.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.
Files changed (24) hide show
  1. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/PKG-INFO +2 -2
  2. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/README.md +1 -1
  3. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/pyproject.toml +1 -1
  4. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/api.py +2 -1
  5. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/blog.py +5 -2
  6. wordpress_markdown_blog_loader-1.7.0/src/wordpress_markdown_blog_loader/html_to_gutenberg.py +99 -0
  7. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader.egg-info/PKG-INFO +2 -2
  8. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader.egg-info/SOURCES.txt +3 -1
  9. wordpress_markdown_blog_loader-1.7.0/tests/test_html_to_gutenberg.py +138 -0
  10. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/LICENSE +0 -0
  11. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/setup.cfg +0 -0
  12. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/__init__.py +0 -0
  13. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/__main__.py +0 -0
  14. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/check_links.py +0 -0
  15. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/download.py +0 -0
  16. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/name_to_email.py +0 -0
  17. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/new.py +0 -0
  18. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/remove_newlines.py +0 -0
  19. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader/upload.py +0 -0
  20. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader.egg-info/dependency_links.txt +0 -0
  21. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader.egg-info/entry_points.txt +0 -0
  22. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader.egg-info/requires.txt +0 -0
  23. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/src/wordpress_markdown_blog_loader.egg-info/top_level.txt +0 -0
  24. {wordpress_markdown_blog_loader-1.6.3 → wordpress_markdown_blog_loader-1.7.0}/tests/test_blog.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wordpress-markdown-blog-loader
3
- Version: 1.6.3
3
+ Version: 1.7.0
4
4
  Summary: load markdown blogs into wordpress
5
5
  Author-email: Mark van Holsteijn <markvanholsteijn@binx.io>
6
6
  License: APL2
@@ -119,7 +119,7 @@ alias wp-md='docker run \
119
119
  -v $HOME:$HOME \
120
120
  -v $HOME/.wordpress.ini:/root/.wordpress.ini \
121
121
  -v $PWD:/$PWD \
122
- -w $PWD ghcr.io/binxio/wordpress-markdown-blog-loader:1.6.3
122
+ -w $PWD ghcr.io/binxio/wordpress-markdown-blog-loader:1.7.0
123
123
  ```'
124
124
  Assuming that your WordPress app password is stored in the 1password Private vault under the name `wordpress app password`
125
125
 
@@ -68,7 +68,7 @@ alias wp-md='docker run \
68
68
  -v $HOME:$HOME \
69
69
  -v $HOME/.wordpress.ini:/root/.wordpress.ini \
70
70
  -v $PWD:/$PWD \
71
- -w $PWD ghcr.io/binxio/wordpress-markdown-blog-loader:1.6.3
71
+ -w $PWD ghcr.io/binxio/wordpress-markdown-blog-loader:1.7.0
72
72
  ```'
73
73
  Assuming that your WordPress app password is stored in the 1password Private vault under the name `wordpress app password`
74
74
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "wordpress-markdown-blog-loader"
7
- version = "1.6.3"
7
+ version = "1.7.0"
8
8
  authors = [{name = "Mark van Holsteijn", email = "markvanholsteijn@binx.io"}]
9
9
  license = {text = "APL2"}
10
10
  description = "load markdown blogs into wordpress"
@@ -115,7 +115,8 @@ class WordpressEndpoint:
115
115
 
116
116
  class User(dict):
117
117
  def __init__(self, u):
118
- self.update(u)
118
+ if u:
119
+ self.update(u)
119
120
 
120
121
  @property
121
122
  def name(self):
@@ -16,6 +16,7 @@ from binx_og_image_generator.generator import Blog as ImageGeneratorBlog
16
16
  from markdown import markdown
17
17
 
18
18
  from wordpress_markdown_blog_loader.api import Post, Medium
19
+ from wordpress_markdown_blog_loader import html_to_gutenberg
19
20
  from wordpress_markdown_blog_loader.api import Wordpress, WordpressEndpoint
20
21
  from wordpress_markdown_blog_loader.remove_newlines import (
21
22
  remove_newlines_from_paragraphs,
@@ -299,9 +300,11 @@ class Blog(object):
299
300
 
300
301
  content = self.markdown_image_pattern.sub(replace_references, self.content)
301
302
  html = markdown(
302
- content, extensions=["fenced_code", "attr_list", "tables", "footnotes"]
303
+ content, extensions=["fenced_code", "attr_list", "tables", "footnotes"],
303
304
  )
304
- return remove_newlines_from_paragraphs(html)
305
+
306
+ html = remove_newlines_from_paragraphs(html)
307
+ return html_to_gutenberg.convert(self.title, html)
305
308
 
306
309
  @property
307
310
  def local_image_references(self) -> set[str]:
@@ -0,0 +1,99 @@
1
+ from bs4 import BeautifulSoup, NavigableString, Comment
2
+
3
+
4
+ def _wrap_in_gutenberg_comments(element):
5
+ if isinstance(element, (NavigableString, Comment)):
6
+ return str(element)
7
+ if element.name == "p":
8
+ return _wrap_paragraph(element)
9
+ if element.name.startswith("h"):
10
+ return _wrap_heading(element)
11
+ if element.name == "pre":
12
+ return _wrap_pre(element)
13
+ if element.name in ("ul", "ol"):
14
+ return _wrap_list(element)
15
+ if element.name == "img":
16
+ return _wrap_image(element)
17
+ return str(element)
18
+
19
+
20
+ def _wrap_paragraph(element):
21
+ return f"<!-- wp:paragraph -->\n{str(element)}\n<!-- /wp:paragraph -->"
22
+
23
+
24
+ def _wrap_heading(element):
25
+ element["class"] = ["wp-block-heading"]
26
+ return f"<!-- wp:heading -->\n{str(element)}\n<!-- /wp:heading -->"
27
+
28
+
29
+ def _wrap_pre(element):
30
+ code_elem = element.find("code")
31
+ language = None
32
+ # Handle pre class logic
33
+ classes = element.get("class", [])
34
+ if "wp-block-code" in classes:
35
+ element["class"] = ["wp-block-code"]
36
+ elif "class" in element.attrs:
37
+ del element["class"]
38
+ # Extract language from code if available
39
+ if code_elem:
40
+ code_classes = code_elem.get("class", [])
41
+ for cls in code_classes:
42
+ if cls.startswith("language-"):
43
+ language = cls.split("-", 1)[1]
44
+ if "class" in code_elem.attrs:
45
+ del code_elem["class"]
46
+ # it is unclear how to specify the language in Wordpress the language
47
+ return f"<!-- wp:code -->\n{str(element)}\n<!-- /wp:code -->"
48
+
49
+
50
+ def _wrap_list(element):
51
+ ordered = element.name == "ol"
52
+ class_attr = element.get("class", [None])[0]
53
+ params = []
54
+ if ordered:
55
+ params.append('"ordered":true')
56
+ if class_attr:
57
+ params.append(f'"className":"{class_attr}"')
58
+ wp_list_type = f" {{ {', '.join(params)} }}" if params else ""
59
+ # Wrap <li> in wp:list-item comments
60
+ soup = BeautifulSoup(str(element), "html.parser")
61
+ for li in soup.find_all("li"):
62
+ li.insert_before(Comment(" wp:list-item "))
63
+ li.insert_after(Comment(" /wp:list-item "))
64
+ return f"<!-- wp:list{wp_list_type} -->\n{soup}\n<!-- /wp:list -->"
65
+
66
+
67
+ def _wrap_image(element):
68
+ return f"<!-- wp:image -->\n{str(element)}\n<!-- /wp:image -->"
69
+
70
+
71
+ def convert(title, html_body):
72
+ soup = BeautifulSoup(html_body, "html.parser")
73
+ blocks = []
74
+
75
+ for element in soup.body.contents if soup.body else soup.contents:
76
+ blocks.append(_wrap_in_gutenberg_comments(element))
77
+
78
+ title = title.replace('"', '\\"')
79
+ hero_block = f'<!-- wp:xebia/blog-hero {{"blogHeroTitle":"{title}","lock":{{"move":true,"remove":true}}}} /-->\n\n'
80
+ content_section_start = "<!-- wp:xebia/content-section -->\n"
81
+ content_section_end = "\n<!-- /wp:xebia/content-section -->"
82
+ content_section = (
83
+ '<div class="wp-block-xebia-content-section">' + "\n".join(blocks) + "</div>"
84
+ )
85
+
86
+ return hero_block + content_section_start + content_section + content_section_end
87
+
88
+
89
+ def main():
90
+ with open("input.html") as f:
91
+ html_body = f.read()
92
+ title = "A piece of yaml"
93
+ gutenberg_html = convert(title, html_body)
94
+ with open("converted.html", "w") as f:
95
+ f.write(gutenberg_html)
96
+
97
+
98
+ if __name__ == "__main__":
99
+ main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wordpress-markdown-blog-loader
3
- Version: 1.6.3
3
+ Version: 1.7.0
4
4
  Summary: load markdown blogs into wordpress
5
5
  Author-email: Mark van Holsteijn <markvanholsteijn@binx.io>
6
6
  License: APL2
@@ -119,7 +119,7 @@ alias wp-md='docker run \
119
119
  -v $HOME:$HOME \
120
120
  -v $HOME/.wordpress.ini:/root/.wordpress.ini \
121
121
  -v $PWD:/$PWD \
122
- -w $PWD ghcr.io/binxio/wordpress-markdown-blog-loader:1.6.3
122
+ -w $PWD ghcr.io/binxio/wordpress-markdown-blog-loader:1.7.0
123
123
  ```'
124
124
  Assuming that your WordPress app password is stored in the 1password Private vault under the name `wordpress app password`
125
125
 
@@ -7,6 +7,7 @@ src/wordpress_markdown_blog_loader/api.py
7
7
  src/wordpress_markdown_blog_loader/blog.py
8
8
  src/wordpress_markdown_blog_loader/check_links.py
9
9
  src/wordpress_markdown_blog_loader/download.py
10
+ src/wordpress_markdown_blog_loader/html_to_gutenberg.py
10
11
  src/wordpress_markdown_blog_loader/name_to_email.py
11
12
  src/wordpress_markdown_blog_loader/new.py
12
13
  src/wordpress_markdown_blog_loader/remove_newlines.py
@@ -17,4 +18,5 @@ src/wordpress_markdown_blog_loader.egg-info/dependency_links.txt
17
18
  src/wordpress_markdown_blog_loader.egg-info/entry_points.txt
18
19
  src/wordpress_markdown_blog_loader.egg-info/requires.txt
19
20
  src/wordpress_markdown_blog_loader.egg-info/top_level.txt
20
- tests/test_blog.py
21
+ tests/test_blog.py
22
+ tests/test_html_to_gutenberg.py
@@ -0,0 +1,138 @@
1
+ import unittest
2
+ from bs4 import BeautifulSoup, NavigableString, Comment
3
+
4
+ from wordpress_markdown_blog_loader.html_to_gutenberg import (
5
+ _wrap_paragraph,
6
+ _wrap_heading,
7
+ _wrap_pre,
8
+ _wrap_list,
9
+ _wrap_image,
10
+ _wrap_in_gutenberg_comments,
11
+ convert,
12
+ )
13
+
14
+
15
+ class TestGutenbergWrapFunctions(unittest.TestCase):
16
+ def test_wrap_paragraph(self):
17
+ soup = BeautifulSoup("<p>This is a paragraph.</p>", "html.parser")
18
+ p_elem = soup.p
19
+ expected = (
20
+ "<!-- wp:paragraph -->\n<p>This is a paragraph.</p>\n<!-- /wp:paragraph -->"
21
+ )
22
+ self.assertEqual(_wrap_paragraph(p_elem), expected)
23
+
24
+ def test_wrap_heading(self):
25
+ soup = BeautifulSoup("<h2>Heading</h2>", "html.parser")
26
+ h_elem = soup.h2
27
+ result = _wrap_heading(h_elem)
28
+ self.assertIn('class="wp-block-heading"', result)
29
+ self.assertTrue(result.startswith("<!-- wp:heading -->"))
30
+ self.assertTrue(result.endswith("<!-- /wp:heading -->"))
31
+
32
+ def test_wrap_pre(self):
33
+ # pre with code and both have classes
34
+ html = '<pre class="wp-block-code hljs"><code class="language-hcl">terraform code</code></pre>'
35
+ soup = BeautifulSoup(html, "html.parser")
36
+ pre_elem = soup.pre
37
+ result = _wrap_pre(pre_elem)
38
+ self.assertTrue(result.startswith("<!-- wp:code -->"))
39
+ self.assertIn('<pre class="wp-block-code">', result)
40
+ self.assertIn("<code>terraform code</code>", result)
41
+ self.assertTrue(result.endswith("<!-- /wp:code -->"))
42
+
43
+ # pre with no code class
44
+ html = "<pre><code>no class code</code></pre>"
45
+ soup = BeautifulSoup(html, "html.parser")
46
+ pre_elem = soup.pre
47
+ result = _wrap_pre(pre_elem)
48
+ self.assertIn("<pre>", result)
49
+ self.assertIn("<code>no class code</code>", result)
50
+
51
+ def test_wrap_ul_list(self):
52
+ html = '<ul class="wp-block-list"><li>item1</li><li>item2</li></ul>'
53
+ soup = BeautifulSoup(html, "html.parser")
54
+ ul_elem = soup.ul
55
+ result = _wrap_list(ul_elem)
56
+ self.assertTrue(result.startswith("<!-- wp:list"))
57
+ self.assertIn('class="wp-block-list"', result)
58
+ self.assertIn("<!-- wp:list-item -->", result)
59
+ self.assertIn("<!-- /wp:list-item -->", result)
60
+ self.assertTrue(result.endswith("<!-- /wp:list -->"))
61
+
62
+ def test_wrap_ol_list(self):
63
+ html = "<ol><li>first</li><li>second</li></ol>"
64
+ soup = BeautifulSoup(html, "html.parser")
65
+ ol_elem = soup.ol
66
+ result = _wrap_list(ol_elem)
67
+ self.assertIn('"ordered":true', result)
68
+ self.assertIn("<!-- wp:list-item -->", result)
69
+
70
+ def test_wrap_image(self):
71
+ soup = BeautifulSoup('<img src="img.png" alt="desc"/>', "html.parser")
72
+ img_elem = soup.img
73
+ res = _wrap_image(img_elem)
74
+ self.assertTrue(res.startswith("<!-- wp:image -->"))
75
+ self.assertIn('<img alt="desc" src="img.png"/>', res)
76
+ self.assertTrue(res.endswith("<!-- /wp:image -->"))
77
+
78
+ def test_wrap_in_gutenberg_comments_with_comment(self):
79
+ comment = Comment(" a comment ")
80
+ res = _wrap_in_gutenberg_comments(comment)
81
+ self.assertEqual(res, str(comment))
82
+
83
+ def test_wrap_in_gutenberg_comments_with_navigablestring(self):
84
+ ns = NavigableString("just text")
85
+ res = _wrap_in_gutenberg_comments(ns)
86
+ self.assertEqual(res, "just text")
87
+
88
+ def test_wrap_in_gutenberg_comments_fallback(self):
89
+ soup = BeautifulSoup("<span>custom</span>", "html.parser")
90
+ span_elem = soup.span
91
+ res = _wrap_in_gutenberg_comments(span_elem)
92
+ self.assertEqual(res, "<span>custom</span>")
93
+
94
+
95
+ import unittest
96
+
97
+
98
+ class TestConvertFunction(unittest.TestCase):
99
+ def test_convert_minimal(self):
100
+ title = 'My "special" Blog'
101
+ html_body = "<p>Hello world</p>"
102
+ expected_hero = '<!-- wp:xebia/blog-hero {"blogHeroTitle":"My \\"special\\" Blog","lock":{"move":true,"remove":true}} /-->\n\n'
103
+ expected_para = (
104
+ "<!-- wp:paragraph -->\n<p>Hello world</p>\n<!-- /wp:paragraph -->"
105
+ )
106
+
107
+ result = convert(title, html_body)
108
+
109
+ # Check hero block present and title correctly escaped
110
+ self.assertIn(expected_hero, result)
111
+ # Check main content start/end blocks present
112
+ self.assertIn("<!-- wp:xebia/content-section -->", result)
113
+ self.assertIn("<!-- /wp:xebia/content-section -->", result)
114
+ # Check block content wrapped appropriately
115
+ self.assertIn('<div class="wp-block-xebia-content-section">', result)
116
+ self.assertIn(expected_para, result)
117
+
118
+ def test_convert_with_list_and_image(self):
119
+ title = "List and Image"
120
+ html_body = '<ul><li>One</li></ul><img src="a.png" />'
121
+ result = convert(title, html_body)
122
+ # Check both the list and image blocks are present in Gutenberg comments
123
+ self.assertIn("<!-- wp:list", result)
124
+ self.assertIn("<li>One</li>", result)
125
+ self.assertIn("<!-- wp:image -->", result)
126
+ self.assertIn('<img src="a.png"/>', result)
127
+
128
+ def test_convert_handles_multiple_elements(self):
129
+ title = "Test"
130
+ html_body = "<p>First</p><h2>Second</h2><pre><code>Code</code></pre>"
131
+ result = convert(title, html_body)
132
+ self.assertIn("<!-- wp:paragraph -->", result)
133
+ self.assertIn("<!-- wp:heading -->", result)
134
+ self.assertIn("<!-- wp:code -->", result)
135
+
136
+
137
+ if __name__ == "__main__":
138
+ unittest.main()