2018-12-23 00:13:57 +01:00
|
|
|
import os
|
2020-06-11 00:54:34 +02:00
|
|
|
import re
|
2022-06-26 02:38:55 +02:00
|
|
|
from typing import List, Match
|
|
|
|
from xml.etree.ElementTree import Element
|
2018-12-23 00:13:57 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
from markdown import Extension, Markdown
|
|
|
|
from markdown.blockparser import BlockParser
|
|
|
|
from markdown.blockprocessors import BlockProcessor
|
2018-12-23 00:13:57 +01:00
|
|
|
|
|
|
|
from zerver.lib.exceptions import InvalidMarkdownIncludeStatement
|
2022-06-26 02:38:55 +02:00
|
|
|
from zerver.lib.markdown.priorities import BLOCK_PROCESSOR_PRIORITIES
|
2018-12-23 00:13:57 +01:00
|
|
|
|
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
class IncludeExtension(Extension):
|
|
|
|
def __init__(self, base_path: str) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.base_path = base_path
|
2018-12-23 00:13:57 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
def extendMarkdown(self, md: Markdown) -> None:
|
|
|
|
md.parser.blockprocessors.register(
|
|
|
|
IncludeBlockProcessor(md.parser, self.base_path),
|
|
|
|
"include",
|
|
|
|
BLOCK_PROCESSOR_PRIORITIES["include"],
|
2018-12-23 00:13:57 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
class IncludeBlockProcessor(BlockProcessor):
|
|
|
|
RE = re.compile(r"^ {,3}\{!([^!]+)!\} *$", re.M)
|
2020-04-22 01:45:30 +02:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
def __init__(self, parser: BlockParser, base_path: str) -> None:
|
|
|
|
super().__init__(parser)
|
|
|
|
self.base_path = base_path
|
2018-12-23 00:13:57 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
def test(self, parent: Element, block: str) -> bool: # type: ignore[override] # https://github.com/python/typeshed/pull/8166
|
|
|
|
return bool(self.RE.search(block))
|
2018-12-23 00:13:57 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
def expand_include(self, m: Match[str]) -> str:
|
|
|
|
try:
|
|
|
|
with open(os.path.normpath(os.path.join(self.base_path, m[1]))) as f:
|
|
|
|
lines = f.read().splitlines()
|
|
|
|
except OSError as e:
|
|
|
|
raise InvalidMarkdownIncludeStatement(m[0].strip()) from e
|
2018-12-23 00:13:57 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
for prep in self.parser.md.preprocessors:
|
|
|
|
lines = prep.run(lines)
|
2018-12-23 00:13:57 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
return "\n".join(lines)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-26 02:38:55 +02:00
|
|
|
def run(self, parent: Element, blocks: List[str]) -> None:
|
2022-06-27 23:03:22 +02:00
|
|
|
self.parser.state.set("include")
|
|
|
|
self.parser.parseChunk(parent, self.RE.sub(self.expand_include, blocks.pop(0)))
|
|
|
|
self.parser.state.reset()
|
2022-06-26 02:38:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
def makeExtension(base_path: str) -> IncludeExtension:
|
|
|
|
return IncludeExtension(base_path=base_path)
|