def process_js(tag: QqTag): """ Output: \_codeblock \_item \_code ...(str)... \_output ...(str)... \_item \_code ...(str)... \_output ...(str)... \ref ... \seealso ... \require ... :param tag: :return: """ codeblock = QqTag("_codeblock") cumulative_code = [] current_chunk = [] for child in itertools.chain(tag, [None]): if isinstance(child, str) and child.strip(): cumulative_code.append(child) current_chunk.append(child) elif child is None or child.name == 'out': code = "".join(cumulative_code) if current_chunk: res = node_exec(code) if res or child is None: code_tag = QqTag("_code", [strip_blank_lines("".join( current_chunk))]) codeblock.append_child(code_tag) if res: codeblock.append_child(QqTag("_output", [res])) current_chunk.clear() if child is not None: logger = "console.log({})".format(child.text_content) res = node_exec(code + ";\n" + logger) code_tag = QqTag("_code", [strip_blank_lines( "".join(current_chunk) + child.text_content.rstrip() + ";")]) current_chunk.clear() codeblock.append_child(code_tag) codeblock.append_child(QqTag("_output", [res])) else: codeblock.append_child(child) return codeblock
def process_js(tag: QqTag): """ Output: \_codeblock \_item \_code ...(str)... \_output ...(str)... \_item \_code ...(str)... \_output ...(str)... \ref ... \seealso ... \require ... :param tag: :return: """ codeblock = QqTag("_codeblock") cumulative_code = [] current_chunk = [] for child in itertools.chain(tag, [None]): if isinstance(child, str) and child.strip(): cumulative_code.append(child) current_chunk.append(child) elif child is None or child.name == 'out': code = "".join(cumulative_code) if current_chunk: res = node_exec(code) if res or child is None: code_tag = QqTag( "_code", [strip_blank_lines("".join(current_chunk))]) codeblock.append_child(code_tag) if res: codeblock.append_child(QqTag("_output", [res])) current_chunk.clear() if child is not None: logger = "console.log({})".format(child.text_content) res = node_exec(code + ";\n" + logger) code_tag = QqTag("_code", [ strip_blank_lines("".join(current_chunk) + child.text_content.rstrip() + ";") ]) current_chunk.clear() codeblock.append_child(code_tag) codeblock.append_child(QqTag("_output", [res])) else: codeblock.append_child(child) return codeblock
def handle_quiz(self, tag: QqTag) -> str: """ Uses tags: choice, correct, comment Example: \question Do you like qqmbr? \quiz \choice No. \comment You didn't even try! \choice \correct Yes, i like it very much! \comment And so do I! :param tag: :return: """ if not tag.exists('md5id'): tag.append_child(QqTag('md5id', [self.tag_hash_id(tag)])) template = Template( filename=os.path.join(self.templates_dir, "quiz.html")) return template.render(formatter=self, tag=tag)