-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathpost.py
More file actions
633 lines (507 loc) · 19.2 KB
/
Copy pathpost.py
File metadata and controls
633 lines (507 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
"""
Post Utilities
"""
import json
import re
from typing import Dict, List
__all__ = ["Post", "parse_inline", "tokens_to_text_nodes"]
from substack import nodes
from substack.exceptions import SectionNotExistsException
def tokens_to_text_nodes(tokens: List[Dict]) -> List[Dict]:
"""Convert parse_inline() tokens to ProseMirror text nodes.
parse_inline() returns {"content": "text", "marks": [...]}.
ProseMirror expects {"type": "text", "text": "text", "marks": [...]}.
"""
nodes = []
for token in tokens:
if not token or not token.get("content"):
continue
node = {"type": "text", "text": token["content"]}
marks = token.get("marks")
if marks:
node["marks"] = marks
nodes.append(node)
return nodes
def parse_inline(text: str) -> List[Dict]:
"""
Convert inline Markdown in a text string into a list of tokens
for use in the post content.
Supported formatting:
- `code`: Text wrapped in backticks.
- **Bold**: Text wrapped in double asterisks.
- *Italic*: Text wrapped in single asterisks.
- ***Bold+Italic***: Text wrapped in triple asterisks.
- ~~Strikethrough~~: Text wrapped in double tildes.
- [Links]: Text wrapped in square brackets followed by URL in parentheses.
Args:
text: Text string containing inline Markdown formatting.
Returns:
List of token dictionaries with content and marks.
Example:
>>> parse_inline("This is **bold** and this is [a link](https://example.com)")
[{'content': 'This is '}, {'content': 'bold', 'marks': [{'type': 'strong'}]}, {'content': ' and this is '}, {'content': 'a link', 'marks': [{'type': 'link', 'attrs': {'href': 'https://example.com'}}]}]
"""
if not text:
return []
tokens = []
# Pattern order matters: code > links > bold+italic > bold > italic > strikethrough
code_pattern = r"`([^`]+)`"
link_pattern = r"\[([^\]]+)\]\(([^)]+)\)"
bold_italic_pattern = r"\*\*\*([^*]+)\*\*\*"
bold_pattern = r"\*\*([^*]+)\*\*"
italic_pattern = r"(?<!\*)\*([^*]+)\*(?!\*)" # Not preceded or followed by *
strikethrough_pattern = r"~~([^~]+)~~"
# Find all matches with their positions
matches = []
# Inline code FIRST -- content inside backticks must not be parsed for other formatting
for match in re.finditer(code_pattern, text):
matches.append((match.start(), match.end(), "code", match.group(1), None))
# Links
for match in re.finditer(link_pattern, text):
# Skip if it's an image link (starts with ![)
# But do NOT skip normal links at position 0.
if match.start() == 0 or text[match.start() - 1 : match.start() + 1] != "![":
if not any(start <= match.start() < end for start, end, _, _, _ in matches):
matches.append(
(match.start(), match.end(), "link", match.group(1), match.group(2))
)
# Bold+italic combo
for match in re.finditer(bold_italic_pattern, text):
if not any(start <= match.start() < end for start, end, _, _, _ in matches):
matches.append(
(match.start(), match.end(), "bold_italic", match.group(1), None)
)
# Bold
for match in re.finditer(bold_pattern, text):
if not any(start <= match.start() < end for start, end, _, _, _ in matches):
matches.append((match.start(), match.end(), "bold", match.group(1), None))
# Italic
for match in re.finditer(italic_pattern, text):
if not any(start <= match.start() < end for start, end, _, _, _ in matches):
matches.append((match.start(), match.end(), "italic", match.group(1), None))
# Strikethrough
for match in re.finditer(strikethrough_pattern, text):
if not any(start <= match.start() < end for start, end, _, _, _ in matches):
matches.append(
(match.start(), match.end(), "strikethrough", match.group(1), None)
)
# Sort matches by position
matches.sort(key=lambda x: x[0])
# Build tokens
last_pos = 0
for start, end, match_type, content, url in matches:
# Add text before this match
if start > last_pos:
tokens.append({"content": text[last_pos:start]})
# Add the formatted content
if match_type == "code":
tokens.append({"content": content, "marks": [{"type": "code"}]})
elif match_type == "link":
tokens.append(
{
"content": content,
"marks": [{"type": "link", "attrs": {"href": url}}],
}
)
elif match_type == "bold_italic":
tokens.append(
{"content": content, "marks": [{"type": "strong"}, {"type": "em"}]}
)
elif match_type == "bold":
tokens.append({"content": content, "marks": [{"type": "strong"}]})
elif match_type == "italic":
tokens.append({"content": content, "marks": [{"type": "em"}]})
elif match_type == "strikethrough":
tokens.append({"content": content, "marks": [{"type": "strikethrough"}]})
last_pos = end
# Add remaining text
if last_pos < len(text):
tokens.append({"content": text[last_pos:]})
# Filter out empty tokens
tokens = [t for t in tokens if t.get("content")]
return tokens
class Post:
"""
Post utility class
"""
def __init__(
self,
title: str,
subtitle: str,
user_id,
audience: str = None,
write_comment_permissions: str = None,
):
"""
Args:
title:
subtitle:
user_id:
audience: possible values: everyone, only_paid, founding, only_free
write_comment_permissions: none, only_paid, everyone (this field is a mess)
"""
self.draft_title = title
self.draft_subtitle = subtitle
self.draft_body = {"type": "doc", "content": []}
self.draft_bylines = [{"id": int(user_id), "is_guest": False}]
self.audience = audience if audience is not None else "everyone"
self.draft_section_id = None
self.section_chosen = True
# TODO better understand the possible values and combinations with audience
if write_comment_permissions is not None:
self.write_comment_permissions = write_comment_permissions
else:
self.write_comment_permissions = self.audience
def set_section(self, name: str, sections: list):
"""
Args:
name:
sections:
Returns:
"""
section = [s for s in sections if s.get("name") == name]
if len(section) != 1:
raise SectionNotExistsException(name)
section = section[0]
self.draft_section_id = section.get("id")
def add(self, item: Dict):
"""
Add item to draft body.
Args:
item:
Returns:
"""
self.draft_body["content"] = self.draft_body.get("content", []) + [
{"type": item.get("type")}
]
content = item.get("content")
if item.get("type") == "captionedImage":
self.captioned_image(**item)
elif item.get("type") == "embeddedPublication":
self.draft_body["content"][-1]["attrs"] = item.get("url")
elif item.get("type") == "youtube2":
self.youtube(item.get("src"))
elif item.get("type") == "subscribeWidget":
self.subscribe_with_caption(item.get("message"))
elif item.get("type") == "codeBlock":
self.code_block(item.get("content"), item.get("attrs", {}))
else:
if content is not None:
self.add_complex_text(content)
if item.get("type") == "heading":
self.attrs(item.get("level", 1))
marks = item.get("marks")
if marks is not None:
self.marks(marks)
return self
def paragraph(self, content=None):
"""
Args:
content:
Returns:
"""
item = {"type": "paragraph"}
if content is not None:
item["content"] = content
return self.add(item)
def heading(self, content=None, level: int = 1):
"""
Args:
content:
level:
Returns:
"""
item = {"type": "heading"}
if content is not None:
item["content"] = content
item["level"] = level
return self.add(item)
def blockquote(self, content=None):
"""
Add a blockquote to the post.
The blockquote wraps one or more paragraph nodes.
Args:
content: Text string or list of inline token dicts. When a plain
string is provided it is wrapped in a single paragraph node.
Returns:
Self for method chaining.
"""
paragraphs: List[Dict] = []
if content is not None:
if isinstance(content, str):
tokens = parse_inline(content)
text_nodes = [
{"type": "text", "text": t["content"]} for t in tokens if t
]
if text_nodes:
paragraphs.append({"type": "paragraph", "content": text_nodes})
elif isinstance(content, list):
for item in content:
if isinstance(item, dict) and item.get("type") == "paragraph":
paragraphs.append(item)
elif isinstance(item, dict):
text_nodes = [{"type": "text", "text": item.get("content", "")}]
paragraphs.append({"type": "paragraph", "content": text_nodes})
node: Dict = {"type": "blockquote"}
if paragraphs:
node["content"] = paragraphs
self.draft_body["content"] = self.draft_body.get("content", []) + [node]
return self
def horizontal_rule(self):
"""
Returns:
"""
return self.add({"type": "horizontal_rule"})
def attrs(self, level):
"""
Args:
level:
Returns:
"""
content_attrs = self.draft_body["content"][-1].get("attrs", {})
content_attrs.update({"level": level})
self.draft_body["content"][-1]["attrs"] = content_attrs
return self
def captioned_image(
self,
src: str,
fullscreen: bool = False,
imageSize: str = "normal",
height: int = 819,
width: int = 1456,
resizeWidth: int = 728,
bytes: str = None,
alt: str = None,
title: str = None,
type: str = None,
href: str = None,
belowTheFold: bool = False,
internalRedirect: str = None,
):
"""
Add image to body.
Args:
bytes:
alt:
title:
type:
href:
belowTheFold:
internalRedirect:
src:
fullscreen:
imageSize:
height:
width:
resizeWidth:
"""
content = self.draft_body["content"][-1].get("content", [])
content += [
{
"type": "image2",
"attrs": {
"src": src,
"fullscreen": fullscreen,
"imageSize": imageSize,
"height": height,
"width": width,
"resizeWidth": resizeWidth,
"bytes": bytes,
"alt": alt,
"title": title,
"type": type,
"href": href,
"belowTheFold": belowTheFold,
"internalRedirect": internalRedirect,
},
}
]
self.draft_body["content"][-1]["content"] = content
return self
def text(self, value: str):
"""
Add text to the last paragraph.
Args:
value: Text to add to paragraph.
Returns:
"""
content = self.draft_body["content"][-1].get("content", [])
content += [{"type": "text", "text": value}]
self.draft_body["content"][-1]["content"] = content
return self
def add_complex_text(self, text):
"""
Args:
text:
"""
if isinstance(text, str):
self.text(text)
else:
for chunk in text:
if chunk:
self.text(chunk.get("content")).marks(chunk.get("marks", []))
def marks(self, marks):
"""
Args:
marks:
Returns:
"""
content = self.draft_body["content"][-1].get("content", [])[-1]
content_marks = content.get("marks", [])
for mark in marks:
new_mark = {"type": mark.get("type")}
if mark.get("type") == "link":
href = mark.get("href") or mark.get("attrs", {}).get("href")
new_mark.update({"attrs": {"href": href}})
content_marks.append(new_mark)
content["marks"] = content_marks
return self
def remove_last_paragraph(self):
"""Remove last paragraph"""
del self.draft_body.get("content")[-1]
def get_draft(self):
"""
Returns:
"""
out = vars(self)
out["draft_body"] = json.dumps(out["draft_body"])
return out
def subscribe_with_caption(self, message: str = None):
"""
Add subscribe widget with caption
Args:
message:
Returns:
"""
if message is None:
message = """Thanks for reading this newsletter!
Subscribe for free to receive new posts and support my work."""
subscribe = self.draft_body["content"][-1]
subscribe["attrs"] = {
"url": "%%checkout_url%%",
"text": "Subscribe",
"language": "en",
}
subscribe["content"] = [
{
"type": "ctaCaption",
"content": [
{
"type": "text",
"text": message,
}
],
}
]
return self
def youtube(self, value: str):
"""
Add youtube video to post.
Args:
value: youtube url
Returns:
"""
content_attrs = self.draft_body["content"][-1].get("attrs", {})
content_attrs.update({"videoId": value})
self.draft_body["content"][-1]["attrs"] = content_attrs
return self
def code_block(self, content, attrs=None):
"""
Add code block to post.
Args:
content: String containing code or list of text nodes
attrs: Optional attributes like language
Returns:
"""
if attrs is None:
attrs = {}
# Handle content - can be list of text nodes or a string
if isinstance(content, str):
# Convert string to list of text nodes
code_content = [{"type": "text", "text": content}]
elif isinstance(content, list):
code_content = content
else:
code_content = []
# Set up the code block structure
code_block = self.draft_body["content"][-1]
code_block["content"] = code_content
if attrs:
code_block["attrs"] = attrs
return self
def footnote_anchor(self, number: int):
"""
Add an inline footnote reference (the superscript marker) to the last block.
Args:
number: The footnote number this anchor points to.
Returns:
Self for method chaining.
"""
content = self.draft_body["content"][-1].get("content", [])
content += [nodes.footnote_anchor(number)]
self.draft_body["content"][-1]["content"] = content
return self
def footnote(self, number: int, content=None):
"""
Append a footnote block (the note shown at the foot of the post).
Args:
number: The footnote number, matching a footnote_anchor.
content: Text string or list of inline token dicts. A plain string is
parsed for inline Markdown and may contain blank-line-separated
paragraphs; a parse_inline() token list or a list of ready text
nodes is also accepted (single paragraph).
Returns:
Self for method chaining.
"""
paragraphs: List[Dict] = []
if isinstance(content, str):
# Blank lines separate paragraphs within the footnote.
for chunk in re.split(r"\n\s*\n", content):
chunk = chunk.strip()
if chunk:
paragraphs.append(
nodes.paragraph(tokens_to_text_nodes(parse_inline(chunk)))
)
elif isinstance(content, list):
# Accept either parse_inline tokens ({"content": ...}) or text nodes.
if content and content[0].get("type") == "text":
text_nodes = content
else:
text_nodes = tokens_to_text_nodes(content)
paragraphs.append(nodes.paragraph(text_nodes))
node: Dict = nodes.footnote(number, paragraphs)
self.draft_body["content"] = self.draft_body.get("content", []) + [node]
return self
def from_markdown(self, markdown_content: str, api=None):
"""
Parse Markdown content and add it to the post.
Supported Markdown features:
- Headings: Lines starting with '#' characters (1-6 levels)
- Images: Markdown image syntax 
- Linked images: [](link_url) - images that are also links
- Links: [text](url) - inline links in paragraphs
- Code blocks: Fenced code blocks with ```language or ```
- Blockquotes: Lines starting with '>' (consecutive lines grouped)
- Paragraphs: Regular text blocks
- Bullet lists: Lines starting with '*' or '-'
- Ordered lists: Lines starting with '1.', '2.', etc.
- Horizontal rules: Lines with ---, ***, or ___
- Inline formatting: **bold**, *italic*, ***bold+italic***, `code`, ~~strikethrough~~
- Footnotes: ``text.[^label]`` references plus ``[^label]: definition``
lines. References become inline anchors and definitions become
footnote blocks, numbered by order of first appearance. Labels may be
numbers or names (e.g. ``[^1]`` or ``[^agi-book]``).
Args:
markdown_content: Markdown string to parse and add to the post.
api: Optional Api instance for uploading local images. If provided,
local image paths will be uploaded via api.get_image().
Returns:
Self for method chaining.
Example:
>>> post = Post("Title", "Subtitle", user_id)
>>> post.from_markdown("# Heading\\n\\nThis is **bold** text with [a link](https://example.com).")
"""
from substack import mdrender
rendered = mdrender.markdown_to_doc(markdown_content, api=api)
self.draft_body["content"] = self.draft_body.get("content", []) + rendered
return self