forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugdown: Trigger test failure for invalid Markdown include statements.
This commit adds a custom Markdown include extension which is identical to the original except when a macro file can't be found, it raises a custom JsonableError exception, which we can catch and then trigger an appropriate test failure. Fixes: zulip#10947
- Loading branch information
1 parent
a378407
commit 8a02e17
Showing
7 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
1 change: 1 addition & 0 deletions
1
templates/zerver/tests/markdown/test_custom_include_extension.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{!nonexistent-macro.md!} |
1 change: 1 addition & 0 deletions
1
templates/zerver/tests/markdown/test_custom_include_extension_empty.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{!empty.md!} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import print_function | ||
import re | ||
import os | ||
from typing import Any, Dict, Optional, List | ||
|
||
import markdown | ||
from markdown_include.include import MarkdownInclude, IncludePreprocessor | ||
|
||
from zerver.lib.exceptions import InvalidMarkdownIncludeStatement | ||
|
||
INC_SYNTAX = re.compile(r'\{!\s*(.+?)\s*!\}') | ||
|
||
|
||
class MarkdownIncludeCustom(MarkdownInclude): | ||
def extendMarkdown(self, md: markdown.Markdown, md_globals: Dict[str, Any]) -> None: | ||
md.preprocessors.add( | ||
'include_wrapper', | ||
IncludeCustomPreprocessor(md, self.getConfigs()), | ||
'_begin' | ||
) | ||
|
||
class IncludeCustomPreprocessor(IncludePreprocessor): | ||
""" | ||
This is a custom implementation of the markdown_include | ||
extension that checks for include statements and if the included | ||
macro file does not exist or can't be opened, raises a custom | ||
JsonableError exception. The rest of the functionality is identical | ||
to the original markdown_include extension. | ||
""" | ||
def run(self, lines: List[str]) -> List[str]: | ||
done = False | ||
while not done: | ||
for line in lines: | ||
loc = lines.index(line) | ||
m = INC_SYNTAX.search(line) | ||
|
||
if m: | ||
filename = m.group(1) | ||
filename = os.path.expanduser(filename) | ||
if not os.path.isabs(filename): | ||
filename = os.path.normpath( | ||
os.path.join(self.base_path, filename) | ||
) | ||
try: | ||
with open(filename, 'r', encoding=self.encoding) as r: | ||
text = r.readlines() | ||
except Exception as e: | ||
print('Warning: could not find file {}. Error: {}'.format(filename, e)) | ||
lines[loc] = INC_SYNTAX.sub('', line) | ||
raise InvalidMarkdownIncludeStatement(m.group(0).strip()) | ||
|
||
line_split = INC_SYNTAX.split(line) | ||
if len(text) == 0: | ||
text.append('') | ||
for i in range(len(text)): | ||
text[i] = text[i].rstrip('\r\n') | ||
text[0] = line_split[0] + text[0] | ||
text[-1] = text[-1] + line_split[2] | ||
lines = lines[:loc] + text + lines[loc+1:] | ||
break | ||
else: | ||
done = True | ||
|
||
return lines | ||
|
||
def makeExtension(*args: Any, **kwargs: str) -> MarkdownIncludeCustom: | ||
return MarkdownIncludeCustom(kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters