quizcomp.parser.renderer.text
1import re 2 3import markdown_it 4 5import quizcomp.model.constants 6import quizcomp.parser.ast 7import quizcomp.parser.common 8import quizcomp.parser.renderer.base 9 10DISALLOWED_CHARACTERS: re.Pattern = re.compile(r'[^\w \-]') 11 12class QuizComposerRendererText(quizcomp.parser.renderer.base.QuizComposerRendererBase): 13 """ 14 The text renderer tries to output plan text that will then be used for special purposes like keys and identifiers. 15 The output here is not meant to represent full documents or be sent to users. 16 """ 17 18 __output__ = quizcomp.model.constants.Format.TEXT.value 19 20 def clean_final(self, text: str, context: quizcomp.parser.common.RenderContext) -> str: 21 # Clean up whitespace. 22 text = re.sub(r'\s+', ' ', text).strip() 23 24 # Remove bad disallowed characters. 25 if (not context.text_allow_all_characters): 26 text = re.sub(DISALLOWED_CHARACTERS, '', text) 27 28 # Clean up whitespace once more. 29 text = re.sub(r'\s+', ' ', text).strip() 30 31 return text 32 33 def _text(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 34 return _clean_text(node.text) 35 36 def _softbreak(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 37 return "\n" 38 39 def _hardbreak(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 40 return "\n\n" 41 42 def _em(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 43 return ''.join([self._render_node(child, context) for child in node.children]) 44 45 def _strong(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 46 return ''.join([self._render_node(child, context) for child in node.children]) 47 48 def _fence(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 49 return self._handle_special_text(node, context) 50 51 def _code_block(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 52 return self._handle_special_text(node, context) 53 54 def _code_inline(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 55 return self._handle_special_text(node, context) 56 57 def _math_block(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 58 return self._handle_special_text(node, context) 59 60 def _math_inline(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 61 return self._handle_special_text(node, context) 62 63 def _image(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 64 return '' 65 66 def _link(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 67 return '' 68 69 def _placeholder(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 70 return _clean_text(node.text) 71 72 def _table(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 73 return '' 74 75 def _thead(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 76 return '' 77 78 def _tbody(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 79 return '' 80 81 def _tr(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 82 return '' 83 84 def _th(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 85 return '' 86 87 def _td(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 88 return '' 89 90 def _bullet_list(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 91 return '' 92 93 def _ordered_list(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 94 return '' 95 96 def _list_item(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 97 return '' 98 99 def _hr(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 100 return '' 101 102 def _heading(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 103 return ''.join([self._render_node(child, context) for child in node.children]) 104 105 def _blockquote(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 106 return ''.join([self._render_node(child, context) for child in node.children]) 107 108 def _handle_special_text(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 109 """ 110 Special text is usually not allowed, 111 but can be enabled with quizcomp.parser.common.RenderContext.text_allow_special_text. 112 """ 113 114 if (context.text_allow_special_text): 115 return node.text.strip() 116 117 return '' 118 119def _clean_text(text: str) -> str: 120 """ Internal text clean. """ 121 122 return text 123 124def get_renderer(options: markdown_it.utils.OptionsDict) -> QuizComposerRendererText: 125 """ Get this renderer and options. """ 126 127 return QuizComposerRendererText()
DISALLOWED_CHARACTERS: re.Pattern =
re.compile('[^\\w \\-]')
13class QuizComposerRendererText(quizcomp.parser.renderer.base.QuizComposerRendererBase): 14 """ 15 The text renderer tries to output plan text that will then be used for special purposes like keys and identifiers. 16 The output here is not meant to represent full documents or be sent to users. 17 """ 18 19 __output__ = quizcomp.model.constants.Format.TEXT.value 20 21 def clean_final(self, text: str, context: quizcomp.parser.common.RenderContext) -> str: 22 # Clean up whitespace. 23 text = re.sub(r'\s+', ' ', text).strip() 24 25 # Remove bad disallowed characters. 26 if (not context.text_allow_all_characters): 27 text = re.sub(DISALLOWED_CHARACTERS, '', text) 28 29 # Clean up whitespace once more. 30 text = re.sub(r'\s+', ' ', text).strip() 31 32 return text 33 34 def _text(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 35 return _clean_text(node.text) 36 37 def _softbreak(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 38 return "\n" 39 40 def _hardbreak(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 41 return "\n\n" 42 43 def _em(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 44 return ''.join([self._render_node(child, context) for child in node.children]) 45 46 def _strong(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 47 return ''.join([self._render_node(child, context) for child in node.children]) 48 49 def _fence(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 50 return self._handle_special_text(node, context) 51 52 def _code_block(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 53 return self._handle_special_text(node, context) 54 55 def _code_inline(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 56 return self._handle_special_text(node, context) 57 58 def _math_block(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 59 return self._handle_special_text(node, context) 60 61 def _math_inline(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 62 return self._handle_special_text(node, context) 63 64 def _image(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 65 return '' 66 67 def _link(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 68 return '' 69 70 def _placeholder(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 71 return _clean_text(node.text) 72 73 def _table(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 74 return '' 75 76 def _thead(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 77 return '' 78 79 def _tbody(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 80 return '' 81 82 def _tr(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 83 return '' 84 85 def _th(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 86 return '' 87 88 def _td(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 89 return '' 90 91 def _bullet_list(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 92 return '' 93 94 def _ordered_list(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 95 return '' 96 97 def _list_item(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 98 return '' 99 100 def _hr(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 101 return '' 102 103 def _heading(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 104 return ''.join([self._render_node(child, context) for child in node.children]) 105 106 def _blockquote(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 107 return ''.join([self._render_node(child, context) for child in node.children]) 108 109 def _handle_special_text(self, node: quizcomp.parser.ast.ASTNode, context: quizcomp.parser.common.RenderContext) -> str: 110 """ 111 Special text is usually not allowed, 112 but can be enabled with quizcomp.parser.common.RenderContext.text_allow_special_text. 113 """ 114 115 if (context.text_allow_special_text): 116 return node.text.strip() 117 118 return ''
The text renderer tries to output plan text that will then be used for special purposes like keys and identifiers. The output here is not meant to represent full documents or be sent to users.
21 def clean_final(self, text: str, context: quizcomp.parser.common.RenderContext) -> str: 22 # Clean up whitespace. 23 text = re.sub(r'\s+', ' ', text).strip() 24 25 # Remove bad disallowed characters. 26 if (not context.text_allow_all_characters): 27 text = re.sub(DISALLOWED_CHARACTERS, '', text) 28 29 # Clean up whitespace once more. 30 text = re.sub(r'\s+', ' ', text).strip() 31 32 return text
Last chance for cleaning before leaving the renderer.
Inherited Members
125def get_renderer(options: markdown_it.utils.OptionsDict) -> QuizComposerRendererText: 126 """ Get this renderer and options. """ 127 128 return QuizComposerRendererText()
Get this renderer and options.