Add code snippets to long-form stories.
This commit is contained in:
@@ -233,16 +233,22 @@ def save_inline_images(post_id):
|
||||
def article_blocks(body, images):
|
||||
image_map = {str(index): image for index, image in enumerate(images, start=1)}
|
||||
blocks = []
|
||||
marker = re.compile(r"\[\[image:(\d+)\]\]")
|
||||
marker = re.compile(
|
||||
r"```(?P<language>[A-Za-z0-9][A-Za-z0-9_-]*)?[ \t]*\r?\n(?P<code>.*?)(?:\r?\n)?```|\[\[image:(?P<image>\d+)\]\]",
|
||||
re.DOTALL,
|
||||
)
|
||||
cursor = 0
|
||||
for match in marker.finditer(body):
|
||||
if match.start() > cursor:
|
||||
blocks.append(("text", body[cursor:match.start()]))
|
||||
image = image_map.get(match.group(1))
|
||||
if image is not None:
|
||||
blocks.append(("image", image))
|
||||
if match.group("code") is not None:
|
||||
blocks.append(("code", {"language": match.group("language") or "text", "content": match.group("code")}))
|
||||
else:
|
||||
blocks.append(("text", match.group(0)))
|
||||
image = image_map.get(match.group("image"))
|
||||
if image is not None:
|
||||
blocks.append(("image", image))
|
||||
else:
|
||||
blocks.append(("text", match.group(0)))
|
||||
cursor = match.end()
|
||||
if cursor < len(body):
|
||||
blocks.append(("text", body[cursor:]))
|
||||
|
||||
Reference in New Issue
Block a user