python-substack 0.1.22__tar.gz → 0.1.24__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.
- {python_substack-0.1.22 → python_substack-0.1.24}/PKG-INFO +19 -1
- {python_substack-0.1.22 → python_substack-0.1.24}/README.md +16 -0
- {python_substack-0.1.22 → python_substack-0.1.24}/pyproject.toml +3 -1
- {python_substack-0.1.22 → python_substack-0.1.24}/substack/__init__.py +1 -1
- python_substack-0.1.24/substack/mdrender.py +210 -0
- python_substack-0.1.24/substack/nodes.py +148 -0
- {python_substack-0.1.22 → python_substack-0.1.24}/substack/post.py +60 -271
- {python_substack-0.1.22 → python_substack-0.1.24}/LICENSE +0 -0
- {python_substack-0.1.22 → python_substack-0.1.24}/substack/api.py +0 -0
- {python_substack-0.1.22 → python_substack-0.1.24}/substack/exceptions.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: python-substack
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.24
|
|
4
4
|
Summary: A Python wrapper around the Substack API.
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -16,6 +16,8 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.13
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.14
|
|
18
18
|
Requires-Dist: PyYAML (>=6.0,<7.0)
|
|
19
|
+
Requires-Dist: markdown-it-py (>=3.0,<4.0)
|
|
20
|
+
Requires-Dist: mdit-py-plugins (>=0.4,<0.5)
|
|
19
21
|
Requires-Dist: python-dotenv (>=1.2.1,<2.0.0)
|
|
20
22
|
Requires-Dist: requests (>=2.32.0,<3.0.0)
|
|
21
23
|
Project-URL: Homepage, https://github.com/ma2za/python-substack
|
|
@@ -180,6 +182,22 @@ This is a paragraph with **bold** and *italic* text.
|
|
|
180
182
|
"""
|
|
181
183
|
post.from_markdown(markdown_content, api=api)
|
|
182
184
|
|
|
185
|
+
# Markdown footnotes are supported too. References become inline anchors and
|
|
186
|
+
# definitions become footnote blocks, numbered by order of first appearance.
|
|
187
|
+
# Labels can be numbers or names (e.g. [^1] or [^source]).
|
|
188
|
+
footnote_markdown = """
|
|
189
|
+
A claim that needs support.[^1] Another, with a named label.[^source]
|
|
190
|
+
|
|
191
|
+
[^1]: The supporting detail, with a [link](https://example.com).
|
|
192
|
+
[^source]: Author, *Title* (2025).
|
|
193
|
+
"""
|
|
194
|
+
post.from_markdown(footnote_markdown, api=api)
|
|
195
|
+
|
|
196
|
+
# Or build footnotes manually:
|
|
197
|
+
post.paragraph(content=[{"content": "Some claim."}]).footnote_anchor(1)
|
|
198
|
+
post.footnote(1, "The note text, with **formatting** allowed.")
|
|
199
|
+
|
|
200
|
+
|
|
183
201
|
draft = api.post_draft(post.get_draft())
|
|
184
202
|
|
|
185
203
|
# set section (can only be done after first posting the draft)
|
|
@@ -156,6 +156,22 @@ This is a paragraph with **bold** and *italic* text.
|
|
|
156
156
|
"""
|
|
157
157
|
post.from_markdown(markdown_content, api=api)
|
|
158
158
|
|
|
159
|
+
# Markdown footnotes are supported too. References become inline anchors and
|
|
160
|
+
# definitions become footnote blocks, numbered by order of first appearance.
|
|
161
|
+
# Labels can be numbers or names (e.g. [^1] or [^source]).
|
|
162
|
+
footnote_markdown = """
|
|
163
|
+
A claim that needs support.[^1] Another, with a named label.[^source]
|
|
164
|
+
|
|
165
|
+
[^1]: The supporting detail, with a [link](https://example.com).
|
|
166
|
+
[^source]: Author, *Title* (2025).
|
|
167
|
+
"""
|
|
168
|
+
post.from_markdown(footnote_markdown, api=api)
|
|
169
|
+
|
|
170
|
+
# Or build footnotes manually:
|
|
171
|
+
post.paragraph(content=[{"content": "Some claim."}]).footnote_anchor(1)
|
|
172
|
+
post.footnote(1, "The note text, with **formatting** allowed.")
|
|
173
|
+
|
|
174
|
+
|
|
159
175
|
draft = api.post_draft(post.get_draft())
|
|
160
176
|
|
|
161
177
|
# set section (can only be done after first posting the draft)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "python-substack"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.24"
|
|
4
4
|
description = "A Python wrapper around the Substack API."
|
|
5
5
|
authors = ["Paolo Mazza <mazzapaolo2019@gmail.com>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -21,6 +21,8 @@ python = "<4.0,>=3.10"
|
|
|
21
21
|
requests = "^2.32.0"
|
|
22
22
|
python-dotenv = "^1.2.1"
|
|
23
23
|
PyYAML = "^6.0"
|
|
24
|
+
markdown-it-py = "^3.0"
|
|
25
|
+
mdit-py-plugins = "^0.4"
|
|
24
26
|
|
|
25
27
|
[tool.poetry.group.dev.dependencies]
|
|
26
28
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
__author__ = "Paolo Mazza"
|
|
4
4
|
__email__ = "mazzapaolo2019@gmail.com"
|
|
5
5
|
__license__ = "MIT License"
|
|
6
|
-
__version__ = "0.1.
|
|
6
|
+
__version__ = "0.1.24"
|
|
7
7
|
__url__ = "https://github.com/ma2za/python-substack"
|
|
8
8
|
__download_url__ = "https://pypi.python.org/pypi/python-substack"
|
|
9
9
|
__description__ = "A Python wrapper around the Substack API"
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"""Markdown -> Substack ProseMirror via markdown-it-py.
|
|
2
|
+
|
|
3
|
+
Implements Post.from_markdown() using a real CommonMark parser (markdown-it-py)
|
|
4
|
+
plus the standard footnote plugin, with a small renderer that walks the syntax
|
|
5
|
+
tree into Substack's node schema.
|
|
6
|
+
|
|
7
|
+
Node construction goes through ``substack.nodes`` so the (undocumented) schema
|
|
8
|
+
lives in exactly one place.
|
|
9
|
+
|
|
10
|
+
Footnotes: Substack numbers footnote anchors by their position in the document
|
|
11
|
+
and pairs them one-to-one, in order, with the footnote blocks at the end (it
|
|
12
|
+
ignores any explicit number and does not support one block serving several
|
|
13
|
+
anchors). So each reference is emitted as its own sequentially-numbered anchor,
|
|
14
|
+
and a matching footnote block is appended for each -- a definition referenced
|
|
15
|
+
more than once is duplicated, which mirrors how Substack's own editor behaves.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import copy
|
|
21
|
+
from typing import Dict, List, Optional
|
|
22
|
+
|
|
23
|
+
from markdown_it import MarkdownIt
|
|
24
|
+
from markdown_it.tree import SyntaxTreeNode
|
|
25
|
+
from mdit_py_plugins.footnote import footnote_plugin
|
|
26
|
+
|
|
27
|
+
from substack import nodes
|
|
28
|
+
from substack.nodes import MarkType, NodeType
|
|
29
|
+
|
|
30
|
+
_MARK_FOR = {
|
|
31
|
+
"strong": {"type": MarkType.STRONG},
|
|
32
|
+
"em": {"type": MarkType.EM},
|
|
33
|
+
"s": {"type": MarkType.STRIKETHROUGH},
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _make_parser() -> MarkdownIt:
|
|
38
|
+
return MarkdownIt("commonmark").use(footnote_plugin).enable("strikethrough")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _coalesce(out_nodes: List[Dict]) -> List[Dict]:
|
|
42
|
+
"""Merge adjacent text nodes that carry identical marks (e.g. softbreaks)."""
|
|
43
|
+
merged: List[Dict] = []
|
|
44
|
+
for node in out_nodes:
|
|
45
|
+
if (
|
|
46
|
+
merged
|
|
47
|
+
and node.get("type") == NodeType.TEXT
|
|
48
|
+
and merged[-1].get("type") == NodeType.TEXT
|
|
49
|
+
and node.get("marks") == merged[-1].get("marks")
|
|
50
|
+
):
|
|
51
|
+
merged[-1]["text"] += node["text"]
|
|
52
|
+
else:
|
|
53
|
+
merged.append(node)
|
|
54
|
+
return merged
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _render_inline(node: SyntaxTreeNode, marks: List[Dict], ctx: Dict) -> List[Dict]:
|
|
58
|
+
"""Render an inline subtree into a flat list of text / anchor nodes."""
|
|
59
|
+
out: List[Dict] = []
|
|
60
|
+
for child in node.children:
|
|
61
|
+
t = child.type
|
|
62
|
+
if t == "text":
|
|
63
|
+
if child.content:
|
|
64
|
+
out.append(nodes.text(child.content, marks))
|
|
65
|
+
elif t == "code_inline":
|
|
66
|
+
out.append(nodes.text(child.content, marks + [nodes.code_mark()]))
|
|
67
|
+
elif t in _MARK_FOR:
|
|
68
|
+
out.extend(_render_inline(child, marks + [_MARK_FOR[t]], ctx))
|
|
69
|
+
elif t == "link":
|
|
70
|
+
href = child.attrs.get("href", "")
|
|
71
|
+
out.extend(_render_inline(child, marks + [nodes.link_mark(href)], ctx))
|
|
72
|
+
elif t in ("softbreak", "hardbreak"):
|
|
73
|
+
out.append(nodes.text(" ", marks))
|
|
74
|
+
elif t == "footnote_ref":
|
|
75
|
+
# Number anchors by document position and record which definition each
|
|
76
|
+
# one points to, so matching blocks can be emitted 1:1 afterwards.
|
|
77
|
+
ctx["order"].append(child.meta["id"])
|
|
78
|
+
out.append(nodes.footnote_anchor(len(ctx["order"])))
|
|
79
|
+
elif t == "image":
|
|
80
|
+
# Inline images are rare in this schema; fall back to alt text.
|
|
81
|
+
alt = child.attrs.get("alt") or "".join(
|
|
82
|
+
c.content for c in child.children if c.type == "text"
|
|
83
|
+
)
|
|
84
|
+
if alt:
|
|
85
|
+
out.append(nodes.text(alt, marks))
|
|
86
|
+
return _coalesce(out)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _only_image(inline: SyntaxTreeNode) -> Optional[SyntaxTreeNode]:
|
|
90
|
+
"""If an inline node is just an image (optionally wrapped in a link), return it."""
|
|
91
|
+
kids = [c for c in inline.children if c.type != "softbreak"]
|
|
92
|
+
if len(kids) == 1 and kids[0].type == "image":
|
|
93
|
+
return kids[0]
|
|
94
|
+
if len(kids) == 1 and kids[0].type == "link":
|
|
95
|
+
inner = [c for c in kids[0].children if c.type != "softbreak"]
|
|
96
|
+
if len(inner) == 1 and inner[0].type == "image":
|
|
97
|
+
img = inner[0]
|
|
98
|
+
img._link_href = kids[0].attrs.get("href") # type: ignore[attr-defined]
|
|
99
|
+
return img
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _captioned_image(img: SyntaxTreeNode, api) -> Dict:
|
|
104
|
+
src = img.attrs.get("src", "")
|
|
105
|
+
if src.startswith("/"):
|
|
106
|
+
src = src[1:]
|
|
107
|
+
if api is not None and not src.startswith("http"):
|
|
108
|
+
try:
|
|
109
|
+
src = api.get_image(src).get("url")
|
|
110
|
+
except Exception:
|
|
111
|
+
pass
|
|
112
|
+
# markdown-it stores the image alt text as the node's content, not in attrs.
|
|
113
|
+
alt = img.content or img.attrs.get("alt") or None
|
|
114
|
+
# Standard markdown image title `` maps to Substack's caption node.
|
|
115
|
+
title = img.attrs.get("title") or None
|
|
116
|
+
caption = [nodes.text(title)] if title else None
|
|
117
|
+
return nodes.captioned_image(
|
|
118
|
+
src,
|
|
119
|
+
alt=alt,
|
|
120
|
+
href=getattr(img, "_link_href", None),
|
|
121
|
+
caption=caption,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _render_block(node: SyntaxTreeNode, api, ctx: Dict) -> List[Dict]:
|
|
126
|
+
"""Render a block-level node into zero or more Substack nodes."""
|
|
127
|
+
t = node.type
|
|
128
|
+
|
|
129
|
+
if t == "paragraph":
|
|
130
|
+
inline = node.children[0]
|
|
131
|
+
img = _only_image(inline)
|
|
132
|
+
if img is not None:
|
|
133
|
+
return [_captioned_image(img, api)]
|
|
134
|
+
return [nodes.paragraph(_render_inline(inline, [], ctx))]
|
|
135
|
+
|
|
136
|
+
if t == "heading":
|
|
137
|
+
level = int(node.tag[1])
|
|
138
|
+
return [nodes.heading(_render_inline(node.children[0], [], ctx), level=level)]
|
|
139
|
+
|
|
140
|
+
if t == "hr":
|
|
141
|
+
return [nodes.horizontal_rule()]
|
|
142
|
+
|
|
143
|
+
if t in ("fence", "code_block"):
|
|
144
|
+
return [
|
|
145
|
+
nodes.code_block(
|
|
146
|
+
node.content.rstrip("\n"), language=node.info.strip() or None
|
|
147
|
+
)
|
|
148
|
+
]
|
|
149
|
+
|
|
150
|
+
if t == "blockquote":
|
|
151
|
+
paras: List[Dict] = []
|
|
152
|
+
for child in node.children:
|
|
153
|
+
paras.extend(_render_block(child, api, ctx))
|
|
154
|
+
return [nodes.blockquote(paras)]
|
|
155
|
+
|
|
156
|
+
if t == "bullet_list":
|
|
157
|
+
return [nodes.bullet_list(_render_list_items(node, api, ctx))]
|
|
158
|
+
|
|
159
|
+
if t == "ordered_list":
|
|
160
|
+
return [nodes.ordered_list(_render_list_items(node, api, ctx))]
|
|
161
|
+
|
|
162
|
+
# footnote_block is handled separately in markdown_to_doc; ignore it here.
|
|
163
|
+
return []
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _render_list_items(list_node: SyntaxTreeNode, api, ctx: Dict) -> List[Dict]:
|
|
167
|
+
items = []
|
|
168
|
+
for li in list_node.children:
|
|
169
|
+
content: List[Dict] = []
|
|
170
|
+
for child in li.children:
|
|
171
|
+
content.extend(_render_block(child, api, ctx))
|
|
172
|
+
items.append({"type": NodeType.LIST_ITEM, "content": content})
|
|
173
|
+
return items
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _footnote_definitions(tree: SyntaxTreeNode, api) -> Dict[int, List[Dict]]:
|
|
177
|
+
"""Map each footnote id to its rendered block content."""
|
|
178
|
+
definitions: Dict[int, List[Dict]] = {}
|
|
179
|
+
for node in tree.children:
|
|
180
|
+
if node.type != "footnote_block":
|
|
181
|
+
continue
|
|
182
|
+
for fn in node.children:
|
|
183
|
+
# A footnote's own content should not register anchors of its own.
|
|
184
|
+
local_ctx = {"order": []}
|
|
185
|
+
content: List[Dict] = []
|
|
186
|
+
for child in fn.children:
|
|
187
|
+
content.extend(_render_block(child, api, local_ctx))
|
|
188
|
+
definitions[fn.meta["id"]] = content
|
|
189
|
+
return definitions
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def markdown_to_doc(markdown_content: str, api=None) -> List[Dict]:
|
|
193
|
+
"""Convert Markdown into a list of Substack ProseMirror block nodes."""
|
|
194
|
+
tree = SyntaxTreeNode(_make_parser().parse(markdown_content))
|
|
195
|
+
|
|
196
|
+
definitions = _footnote_definitions(tree, api)
|
|
197
|
+
|
|
198
|
+
ctx: Dict = {"order": []}
|
|
199
|
+
out: List[Dict] = []
|
|
200
|
+
for node in tree.children:
|
|
201
|
+
if node.type == "footnote_block":
|
|
202
|
+
continue
|
|
203
|
+
out.extend(_render_block(node, api, ctx))
|
|
204
|
+
|
|
205
|
+
# Emit one footnote block per reference, in anchor order, numbered to match.
|
|
206
|
+
for number, footnote_id in enumerate(ctx["order"], start=1):
|
|
207
|
+
content = copy.deepcopy(definitions.get(footnote_id, []))
|
|
208
|
+
out.append(nodes.footnote(number, content))
|
|
209
|
+
|
|
210
|
+
return out
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""ProseMirror node builders for Substack documents.
|
|
2
|
+
|
|
3
|
+
Centralises the (undocumented) Substack ProseMirror schema in one place.
|
|
4
|
+
The node-type strings ("paragraph", "footnoteAnchor", "image2", ...) and
|
|
5
|
+
their shapes live here rather than as inline dict literals scattered across
|
|
6
|
+
post.py, giving:
|
|
7
|
+
|
|
8
|
+
* one source of truth for node shapes (so a schema change is a one-line fix),
|
|
9
|
+
* discoverable, typed constructors instead of bare dict literals,
|
|
10
|
+
* a natural seam for validation.
|
|
11
|
+
|
|
12
|
+
The builders intentionally return plain dicts so they stay 100% compatible with
|
|
13
|
+
the existing draft_body structure.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from typing import Dict, List, Optional
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class NodeType:
|
|
22
|
+
DOC = "doc"
|
|
23
|
+
PARAGRAPH = "paragraph"
|
|
24
|
+
HEADING = "heading"
|
|
25
|
+
TEXT = "text"
|
|
26
|
+
BLOCKQUOTE = "blockquote"
|
|
27
|
+
CODE_BLOCK = "codeBlock"
|
|
28
|
+
HORIZONTAL_RULE = "horizontal_rule"
|
|
29
|
+
BULLET_LIST = "bullet_list"
|
|
30
|
+
ORDERED_LIST = "ordered_list"
|
|
31
|
+
LIST_ITEM = "list_item"
|
|
32
|
+
FOOTNOTE = "footnote"
|
|
33
|
+
FOOTNOTE_ANCHOR = "footnoteAnchor"
|
|
34
|
+
CAPTIONED_IMAGE = "captionedImage"
|
|
35
|
+
CAPTION = "caption"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class MarkType:
|
|
39
|
+
STRONG = "strong"
|
|
40
|
+
EM = "em"
|
|
41
|
+
CODE = "code"
|
|
42
|
+
STRIKETHROUGH = "strikethrough"
|
|
43
|
+
LINK = "link"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def code_mark() -> Dict:
|
|
47
|
+
return {"type": MarkType.CODE}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def text(value: str, marks: Optional[List[Dict]] = None) -> Dict:
|
|
51
|
+
node: Dict = {"type": NodeType.TEXT, "text": value}
|
|
52
|
+
if marks:
|
|
53
|
+
node["marks"] = marks
|
|
54
|
+
return node
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def link_mark(href: str) -> Dict:
|
|
58
|
+
return {"type": MarkType.LINK, "attrs": {"href": href}}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def paragraph(content: Optional[List[Dict]] = None) -> Dict:
|
|
62
|
+
return {"type": NodeType.PARAGRAPH, "content": content or []}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def heading(content: List[Dict], level: int = 1) -> Dict:
|
|
66
|
+
return {"type": NodeType.HEADING, "content": content, "attrs": {"level": level}}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def horizontal_rule() -> Dict:
|
|
70
|
+
return {"type": NodeType.HORIZONTAL_RULE}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def blockquote(paragraphs: List[Dict]) -> Dict:
|
|
74
|
+
node: Dict = {"type": NodeType.BLOCKQUOTE}
|
|
75
|
+
if paragraphs:
|
|
76
|
+
node["content"] = paragraphs
|
|
77
|
+
return node
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def list_item(content_nodes: List[Dict]) -> Dict:
|
|
81
|
+
return {
|
|
82
|
+
"type": NodeType.LIST_ITEM,
|
|
83
|
+
"content": [paragraph(content_nodes)],
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def bullet_list(items: List[Dict]) -> Dict:
|
|
88
|
+
return {"type": NodeType.BULLET_LIST, "content": items}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def ordered_list(items: List[Dict]) -> Dict:
|
|
92
|
+
return {"type": NodeType.ORDERED_LIST, "content": items}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def code_block(code: str, language: Optional[str] = None) -> Dict:
|
|
96
|
+
node: Dict = {"type": NodeType.CODE_BLOCK, "content": [text(code)]}
|
|
97
|
+
if language:
|
|
98
|
+
node["attrs"] = {"language": language}
|
|
99
|
+
return node
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def captioned_image(
|
|
103
|
+
src: str,
|
|
104
|
+
alt: Optional[str] = None,
|
|
105
|
+
href: Optional[str] = None,
|
|
106
|
+
caption: Optional[List[Dict]] = None,
|
|
107
|
+
image_size: str = "normal",
|
|
108
|
+
) -> Dict:
|
|
109
|
+
content: List[Dict] = [
|
|
110
|
+
{
|
|
111
|
+
"type": "image2",
|
|
112
|
+
"attrs": {
|
|
113
|
+
"src": src,
|
|
114
|
+
"srcNoWatermark": None,
|
|
115
|
+
"fullscreen": False,
|
|
116
|
+
"imageSize": image_size,
|
|
117
|
+
"height": 819,
|
|
118
|
+
"width": 1456,
|
|
119
|
+
"resizeWidth": 728 if image_size == "normal" else None,
|
|
120
|
+
"bytes": None,
|
|
121
|
+
"alt": alt,
|
|
122
|
+
"title": None,
|
|
123
|
+
"type": None,
|
|
124
|
+
"href": href,
|
|
125
|
+
"belowTheFold": False,
|
|
126
|
+
"topImage": False,
|
|
127
|
+
"internalRedirect": None,
|
|
128
|
+
"isProcessing": False,
|
|
129
|
+
"align": None,
|
|
130
|
+
"offset": False,
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
]
|
|
134
|
+
if caption:
|
|
135
|
+
content.append({"type": NodeType.CAPTION, "content": caption})
|
|
136
|
+
return {"type": NodeType.CAPTIONED_IMAGE, "content": content}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def footnote_anchor(number: int) -> Dict:
|
|
140
|
+
return {"type": NodeType.FOOTNOTE_ANCHOR, "attrs": {"number": number}}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def footnote(number: int, paragraphs: List[Dict]) -> Dict:
|
|
144
|
+
return {
|
|
145
|
+
"type": NodeType.FOOTNOTE,
|
|
146
|
+
"attrs": {"number": number},
|
|
147
|
+
"content": paragraphs or [paragraph()],
|
|
148
|
+
}
|
|
@@ -11,6 +11,7 @@ from typing import Dict, List
|
|
|
11
11
|
__all__ = ["Post", "parse_inline", "tokens_to_text_nodes"]
|
|
12
12
|
|
|
13
13
|
from substack.exceptions import SectionNotExistsException
|
|
14
|
+
from substack import nodes
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
def tokens_to_text_nodes(tokens: List[Dict]) -> List[Dict]:
|
|
@@ -543,6 +544,58 @@ class Post:
|
|
|
543
544
|
|
|
544
545
|
return self
|
|
545
546
|
|
|
547
|
+
def footnote_anchor(self, number: int):
|
|
548
|
+
"""
|
|
549
|
+
|
|
550
|
+
Add an inline footnote reference (the superscript marker) to the last block.
|
|
551
|
+
|
|
552
|
+
Args:
|
|
553
|
+
number: The footnote number this anchor points to.
|
|
554
|
+
|
|
555
|
+
Returns:
|
|
556
|
+
Self for method chaining.
|
|
557
|
+
|
|
558
|
+
"""
|
|
559
|
+
content = self.draft_body["content"][-1].get("content", [])
|
|
560
|
+
content += [nodes.footnote_anchor(number)]
|
|
561
|
+
self.draft_body["content"][-1]["content"] = content
|
|
562
|
+
return self
|
|
563
|
+
|
|
564
|
+
def footnote(self, number: int, content=None):
|
|
565
|
+
"""
|
|
566
|
+
|
|
567
|
+
Append a footnote block (the note shown at the foot of the post).
|
|
568
|
+
|
|
569
|
+
Args:
|
|
570
|
+
number: The footnote number, matching a footnote_anchor.
|
|
571
|
+
content: Text string or list of inline token dicts. A plain string is
|
|
572
|
+
parsed for inline Markdown and may contain blank-line-separated
|
|
573
|
+
paragraphs; a parse_inline() token list or a list of ready text
|
|
574
|
+
nodes is also accepted (single paragraph).
|
|
575
|
+
|
|
576
|
+
Returns:
|
|
577
|
+
Self for method chaining.
|
|
578
|
+
|
|
579
|
+
"""
|
|
580
|
+
paragraphs: List[Dict] = []
|
|
581
|
+
if isinstance(content, str):
|
|
582
|
+
# Blank lines separate paragraphs within the footnote.
|
|
583
|
+
for chunk in re.split(r"\n\s*\n", content):
|
|
584
|
+
chunk = chunk.strip()
|
|
585
|
+
if chunk:
|
|
586
|
+
paragraphs.append(nodes.paragraph(tokens_to_text_nodes(parse_inline(chunk))))
|
|
587
|
+
elif isinstance(content, list):
|
|
588
|
+
# Accept either parse_inline tokens ({"content": ...}) or text nodes.
|
|
589
|
+
if content and content[0].get("type") == "text":
|
|
590
|
+
text_nodes = content
|
|
591
|
+
else:
|
|
592
|
+
text_nodes = tokens_to_text_nodes(content)
|
|
593
|
+
paragraphs.append(nodes.paragraph(text_nodes))
|
|
594
|
+
|
|
595
|
+
node: Dict = nodes.footnote(number, paragraphs)
|
|
596
|
+
self.draft_body["content"] = self.draft_body.get("content", []) + [node]
|
|
597
|
+
return self
|
|
598
|
+
|
|
546
599
|
def from_markdown(self, markdown_content: str, api=None):
|
|
547
600
|
"""
|
|
548
601
|
Parse Markdown content and add it to the post.
|
|
@@ -559,6 +612,10 @@ class Post:
|
|
|
559
612
|
- Ordered lists: Lines starting with '1.', '2.', etc.
|
|
560
613
|
- Horizontal rules: Lines with ---, ***, or ___
|
|
561
614
|
- Inline formatting: **bold**, *italic*, ***bold+italic***, `code`, ~~strikethrough~~
|
|
615
|
+
- Footnotes: ``text.[^label]`` references plus ``[^label]: definition``
|
|
616
|
+
lines. References become inline anchors and definitions become
|
|
617
|
+
footnote blocks, numbered by order of first appearance. Labels may be
|
|
618
|
+
numbers or names (e.g. ``[^1]`` or ``[^agi-book]``).
|
|
562
619
|
|
|
563
620
|
Args:
|
|
564
621
|
markdown_content: Markdown string to parse and add to the post.
|
|
@@ -572,276 +629,8 @@ class Post:
|
|
|
572
629
|
>>> post = Post("Title", "Subtitle", user_id)
|
|
573
630
|
>>> post.from_markdown("# Heading\\n\\nThis is **bold** text with [a link](https://example.com).")
|
|
574
631
|
"""
|
|
575
|
-
|
|
576
|
-
blocks = []
|
|
577
|
-
current_block: List[str] = []
|
|
578
|
-
in_code_block = False
|
|
579
|
-
code_block_language = None
|
|
580
|
-
|
|
581
|
-
for line in lines:
|
|
582
|
-
# Check for fenced code block start/end
|
|
583
|
-
if line.strip().startswith("```"):
|
|
584
|
-
if in_code_block:
|
|
585
|
-
# End of code block
|
|
586
|
-
if current_block:
|
|
587
|
-
blocks.append({
|
|
588
|
-
"type": "code",
|
|
589
|
-
"language": code_block_language,
|
|
590
|
-
"content": "\n".join(current_block)
|
|
591
|
-
})
|
|
592
|
-
current_block = []
|
|
593
|
-
in_code_block = False
|
|
594
|
-
code_block_language = None
|
|
595
|
-
else:
|
|
596
|
-
# Start of code block
|
|
597
|
-
if current_block:
|
|
598
|
-
blocks.append({"type": "text", "content": "\n".join(current_block)})
|
|
599
|
-
current_block = []
|
|
600
|
-
# Extract language if specified
|
|
601
|
-
language = line.strip()[3:].strip()
|
|
602
|
-
code_block_language = language if language else None
|
|
603
|
-
in_code_block = True
|
|
604
|
-
continue
|
|
605
|
-
|
|
606
|
-
if in_code_block:
|
|
607
|
-
# Inside code block - collect lines as-is
|
|
608
|
-
current_block.append(line)
|
|
609
|
-
else:
|
|
610
|
-
# Regular content
|
|
611
|
-
if line.strip() == "":
|
|
612
|
-
# Empty line - end current block if it has content
|
|
613
|
-
if current_block:
|
|
614
|
-
blocks.append({"type": "text", "content": "\n".join(current_block)})
|
|
615
|
-
current_block = []
|
|
616
|
-
else:
|
|
617
|
-
current_block.append(line)
|
|
618
|
-
|
|
619
|
-
# Add any remaining content
|
|
620
|
-
if current_block:
|
|
621
|
-
if in_code_block:
|
|
622
|
-
blocks.append({
|
|
623
|
-
"type": "code",
|
|
624
|
-
"language": code_block_language,
|
|
625
|
-
"content": "\n".join(current_block)
|
|
626
|
-
})
|
|
627
|
-
else:
|
|
628
|
-
blocks.append({"type": "text", "content": "\n".join(current_block)})
|
|
629
|
-
|
|
630
|
-
# Process blocks
|
|
631
|
-
for block in blocks:
|
|
632
|
-
if block["type"] == "code":
|
|
633
|
-
# Add code block
|
|
634
|
-
code_content = block.get("content", "").strip()
|
|
635
|
-
if code_content:
|
|
636
|
-
# Substack uses "codeBlock" type
|
|
637
|
-
code_attrs = {}
|
|
638
|
-
if block.get("language"):
|
|
639
|
-
code_attrs["language"] = block["language"]
|
|
640
|
-
self.add({
|
|
641
|
-
"type": "codeBlock",
|
|
642
|
-
"content": code_content, # Pass as string, code_block method will handle it
|
|
643
|
-
"attrs": code_attrs
|
|
644
|
-
})
|
|
645
|
-
else:
|
|
646
|
-
# Process text block
|
|
647
|
-
text_content = block.get("content", "").strip()
|
|
648
|
-
if not text_content:
|
|
649
|
-
continue
|
|
650
|
-
|
|
651
|
-
# Check for horizontal rule: ---, ***, ___
|
|
652
|
-
if re.match(r'^(\*{3,}|-{3,}|_{3,})\s*$', text_content):
|
|
653
|
-
self.horizontal_rule()
|
|
654
|
-
continue
|
|
655
|
-
|
|
656
|
-
# Process headings (lines starting with '#' characters)
|
|
657
|
-
if text_content.startswith("#"):
|
|
658
|
-
level = len(text_content) - len(text_content.lstrip("#"))
|
|
659
|
-
heading_text = text_content.lstrip("#").strip()
|
|
660
|
-
if heading_text: # Only add if there's actual text
|
|
661
|
-
self.heading(content=heading_text, level=min(level, 6))
|
|
662
|
-
|
|
663
|
-
# Process images using Markdown image syntax: 
|
|
664
|
-
# Also handle linked images: [](link_url)
|
|
665
|
-
elif text_content.startswith("!") or (text_content.startswith("[") and "](link)
|
|
667
|
-
linked_image_match = re.match(r'\[!\[([^\]]*)\]\(([^)]+)\)\]\(([^)]+)\)', text_content)
|
|
668
|
-
if linked_image_match:
|
|
669
|
-
# Linked image - create image with href
|
|
670
|
-
alt_text = linked_image_match.group(1)
|
|
671
|
-
image_url = linked_image_match.group(2)
|
|
672
|
-
link_url = linked_image_match.group(3)
|
|
673
|
-
|
|
674
|
-
# Adjust image URL if it starts with a slash
|
|
675
|
-
image_url = image_url[1:] if image_url.startswith("/") else image_url
|
|
676
|
-
|
|
677
|
-
# If api is provided and image_url is a local file, upload it
|
|
678
|
-
if api is not None:
|
|
679
|
-
try:
|
|
680
|
-
image = api.get_image(image_url)
|
|
681
|
-
image_url = image.get("url")
|
|
682
|
-
except Exception:
|
|
683
|
-
# If upload fails, use original URL
|
|
684
|
-
pass
|
|
685
|
-
|
|
686
|
-
self.add({
|
|
687
|
-
"type": "captionedImage",
|
|
688
|
-
"src": image_url,
|
|
689
|
-
"alt": alt_text,
|
|
690
|
-
"href": link_url
|
|
691
|
-
})
|
|
692
|
-
else:
|
|
693
|
-
# Regular image: 
|
|
694
|
-
match = re.match(r"!\[.*?\]\((.*?)\)", text_content)
|
|
695
|
-
if match:
|
|
696
|
-
image_url = match.group(1)
|
|
697
|
-
# Adjust image URL if it starts with a slash
|
|
698
|
-
image_url = image_url[1:] if image_url.startswith("/") else image_url
|
|
699
|
-
|
|
700
|
-
# If api is provided and image_url is a local file, upload it
|
|
701
|
-
if api is not None:
|
|
702
|
-
try:
|
|
703
|
-
image = api.get_image(image_url)
|
|
704
|
-
image_url = image.get("url")
|
|
705
|
-
except Exception:
|
|
706
|
-
# If upload fails, use original URL
|
|
707
|
-
pass
|
|
708
|
-
|
|
709
|
-
self.add({"type": "captionedImage", "src": image_url})
|
|
710
|
-
|
|
711
|
-
# Process paragraphs, bullet lists, ordered lists, or blockquotes
|
|
712
|
-
else:
|
|
713
|
-
if "\n" in text_content:
|
|
714
|
-
# Process each line, grouping consecutive bullets/ordered items
|
|
715
|
-
# into list nodes and consecutive blockquote lines into a
|
|
716
|
-
# single blockquote node.
|
|
717
|
-
pending_bullets: List[List[Dict]] = []
|
|
718
|
-
pending_quotes: List[str] = []
|
|
719
|
-
pending_ordered: List[List[Dict]] = []
|
|
720
|
-
|
|
721
|
-
def flush_bullets():
|
|
722
|
-
if not pending_bullets:
|
|
723
|
-
return
|
|
724
|
-
list_items = []
|
|
725
|
-
for bullet_nodes in pending_bullets:
|
|
726
|
-
list_items.append({
|
|
727
|
-
"type": "list_item",
|
|
728
|
-
"content": [{"type": "paragraph", "content": bullet_nodes}],
|
|
729
|
-
})
|
|
730
|
-
self.draft_body["content"].append(
|
|
731
|
-
{"type": "bullet_list", "content": list_items}
|
|
732
|
-
)
|
|
733
|
-
pending_bullets.clear()
|
|
734
|
-
|
|
735
|
-
def flush_quotes():
|
|
736
|
-
if not pending_quotes:
|
|
737
|
-
return
|
|
738
|
-
paragraphs: List[Dict] = []
|
|
739
|
-
for quote_line in pending_quotes:
|
|
740
|
-
tokens = parse_inline(quote_line)
|
|
741
|
-
text_nodes = tokens_to_text_nodes(tokens)
|
|
742
|
-
if text_nodes:
|
|
743
|
-
paragraphs.append({"type": "paragraph", "content": text_nodes})
|
|
744
|
-
node: Dict = {"type": "blockquote"}
|
|
745
|
-
if paragraphs:
|
|
746
|
-
node["content"] = paragraphs
|
|
747
|
-
self.draft_body["content"].append(node)
|
|
748
|
-
pending_quotes.clear()
|
|
749
|
-
|
|
750
|
-
def flush_ordered():
|
|
751
|
-
if not pending_ordered:
|
|
752
|
-
return
|
|
753
|
-
list_items = []
|
|
754
|
-
for item_nodes in pending_ordered:
|
|
755
|
-
list_items.append({
|
|
756
|
-
"type": "list_item",
|
|
757
|
-
"content": [{"type": "paragraph", "content": item_nodes}],
|
|
758
|
-
})
|
|
759
|
-
self.draft_body["content"].append(
|
|
760
|
-
{"type": "ordered_list", "content": list_items}
|
|
761
|
-
)
|
|
762
|
-
pending_ordered.clear()
|
|
763
|
-
|
|
764
|
-
for line in text_content.split("\n"):
|
|
765
|
-
line = line.strip()
|
|
766
|
-
if not line:
|
|
767
|
-
flush_bullets()
|
|
768
|
-
flush_ordered()
|
|
769
|
-
flush_quotes()
|
|
770
|
-
continue
|
|
771
|
-
|
|
772
|
-
# Check for blockquote marker
|
|
773
|
-
if line.startswith("> ") or line == ">":
|
|
774
|
-
flush_bullets()
|
|
775
|
-
flush_ordered()
|
|
776
|
-
quote_text = line[2:] if line.startswith("> ") else ""
|
|
777
|
-
pending_quotes.append(quote_text)
|
|
778
|
-
continue
|
|
779
|
-
|
|
780
|
-
# Check for ordered list marker
|
|
781
|
-
ordered_match = re.match(r'^(\d+)\.\s+(.*)', line)
|
|
782
|
-
if ordered_match:
|
|
783
|
-
flush_bullets()
|
|
784
|
-
flush_quotes()
|
|
785
|
-
item_text = ordered_match.group(2).strip()
|
|
786
|
-
tokens = parse_inline(item_text)
|
|
787
|
-
text_nodes = tokens_to_text_nodes(tokens)
|
|
788
|
-
if text_nodes:
|
|
789
|
-
pending_ordered.append(text_nodes)
|
|
790
|
-
continue
|
|
791
|
-
|
|
792
|
-
# Check for bullet marker
|
|
793
|
-
bullet_text = None
|
|
794
|
-
if line.startswith("* "):
|
|
795
|
-
bullet_text = line[2:].strip()
|
|
796
|
-
elif line.startswith("- "):
|
|
797
|
-
bullet_text = line[2:].strip()
|
|
798
|
-
elif line.startswith("*") and not line.startswith("**"):
|
|
799
|
-
bullet_text = line[1:].strip()
|
|
800
|
-
|
|
801
|
-
if bullet_text is not None:
|
|
802
|
-
flush_ordered()
|
|
803
|
-
flush_quotes()
|
|
804
|
-
tokens = parse_inline(bullet_text)
|
|
805
|
-
text_nodes = tokens_to_text_nodes(tokens)
|
|
806
|
-
if text_nodes:
|
|
807
|
-
pending_bullets.append(text_nodes)
|
|
808
|
-
else:
|
|
809
|
-
flush_bullets()
|
|
810
|
-
flush_ordered()
|
|
811
|
-
flush_quotes()
|
|
812
|
-
tokens = parse_inline(line)
|
|
813
|
-
self.add({"type": "paragraph", "content": tokens})
|
|
814
|
-
|
|
815
|
-
flush_bullets()
|
|
816
|
-
flush_ordered()
|
|
817
|
-
flush_quotes()
|
|
818
|
-
else:
|
|
819
|
-
# Single line — blockquote, ordered list, or paragraph
|
|
820
|
-
if text_content.startswith("> ") or text_content == ">":
|
|
821
|
-
quote_text = text_content[2:] if text_content.startswith("> ") else ""
|
|
822
|
-
tokens = parse_inline(quote_text)
|
|
823
|
-
text_nodes = tokens_to_text_nodes(tokens)
|
|
824
|
-
para = {"type": "paragraph", "content": text_nodes} if text_nodes else {"type": "paragraph"}
|
|
825
|
-
self.draft_body["content"] = self.draft_body.get("content", []) + [
|
|
826
|
-
{"type": "blockquote", "content": [para]}
|
|
827
|
-
]
|
|
828
|
-
|
|
829
|
-
elif re.match(r'^(\d+)\.\s+(.*)', text_content):
|
|
830
|
-
ordered_match = re.match(r'^(\d+)\.\s+(.*)', text_content)
|
|
831
|
-
item_text = ordered_match.group(2).strip()
|
|
832
|
-
tokens = parse_inline(item_text)
|
|
833
|
-
text_nodes = tokens_to_text_nodes(tokens)
|
|
834
|
-
if text_nodes:
|
|
835
|
-
list_item = {
|
|
836
|
-
"type": "list_item",
|
|
837
|
-
"content": [{"type": "paragraph", "content": text_nodes}],
|
|
838
|
-
}
|
|
839
|
-
self.draft_body["content"].append(
|
|
840
|
-
{"type": "ordered_list", "content": [list_item]}
|
|
841
|
-
)
|
|
842
|
-
|
|
843
|
-
else:
|
|
844
|
-
tokens = parse_inline(text_content)
|
|
845
|
-
self.add({"type": "paragraph", "content": tokens})
|
|
632
|
+
from substack import mdrender
|
|
846
633
|
|
|
634
|
+
rendered = mdrender.markdown_to_doc(markdown_content, api=api)
|
|
635
|
+
self.draft_body["content"] = self.draft_body.get("content", []) + rendered
|
|
847
636
|
return self
|
|
File without changes
|
|
File without changes
|
|
File without changes
|