def composer_preprocessor(intermediate): """ Performs the following modifications to the intermediate: - Promote questions token to top level of the ast - Converts images to a base64 encoded string to appear directly in the HTML source - Add text tags around the solution if they are not already present :param intermediate: An intermediate parse object :return: A modified intermediate """ def fix_solutions(token): if not filters.has_name(token) and token.definition[0].startswith('<text>'): token.definition = ['<text>'] + token.definition + ['</text>'] return token def escape_html(token): for i in range(len(token.definition)): if not filters.has_name(token.definition[i]): token.definition[i] = token.definition[i].replace('(', '(').replace(')', ')') return token intermediate.ast = filters.promote(intermediate.ast, 'questions') intermediate.ast = filters.img64(intermediate.ast) intermediate.ast = filters.apply_function(intermediate.ast, fix_solutions, 'solution') intermediate.ast = filters.apply_function(intermediate.ast, escape_html, 'prompt') return intermediate
def parser_postprocessor(intermediate): """ Because LaTeX has no defined Prompt the first string, equations, and images found in a question are put under a prompt Token. :param intermediate: An intermediate parse object :return: A modified intermediate """ def def_prompt(token): try: definition = token.definition[0].definition title, prompt = definition.pop(0), [] while len(definition) > 0 and ( not hasattr(definition[0], "name") or filters.has_name(definition[0], ["$", "img", "verbatim", "emphasis"]) ): prompt.append(definition.pop(0)) definition.insert(0, util.Map({"name": "prompt", "definition": prompt})) definition.insert(0, title) return token except AttributeError: raise ( parser_composer.FormatError("Malformed question token definition:" + parser_composer.str_token(token)) ) # Run inner function recursively on the ast filters.apply_function(intermediate.ast, def_prompt, "question", partial=False) return intermediate
def parser_postprocessor(intermediate): """ Because LaTeX has no defined Prompt the first string, equations, and images found in a question are put under a prompt Token. :param intermediate: An intermediate parse object :return: A modified intermediate """ def def_prompt(token): try: definition = token.definition[0].definition title, prompt = definition.pop(0), [] while len(definition) > 0 and (not hasattr(definition[0], 'name') or definition[0].name in ['$', 'img', 'verbatim']): prompt.append(definition.pop(0)) definition.insert( 0, util.Map({ 'name': 'prompt', 'definition': prompt })) definition.insert(0, title) return token except AttributeError: raise (parser_composer.FormatError( 'Malformed question token definition:' + str(token))) # Run inner function recursively on the ast filters.apply_function(intermediate.ast, def_prompt, 'question', partial=False) return intermediate
def composer_preprocessor(intermediate): """ Performs the following modifications to the intermediate: - Promote questions token to top level of the ast - Converts images to a base64 encoded string to appear directly in the HTML source - Add text tags around the solution if they are not already present :param intermediate: An intermediate parse object :return: A modified intermediate """ def fix_solutions(token): if not filters.has_name(token) and token.definition[0].startswith( '<text>'): token.definition = ['<text>'] + token.definition + ['</text>'] return token def escape_html(token): for i in range(len(token.definition)): if not filters.has_name(token.definition[i]): token.definition[i] = token.definition[i].replace( '(', '(').replace(')', ')') return token intermediate.ast = filters.promote(intermediate.ast, 'questions') intermediate.ast = filters.img64(intermediate.ast) intermediate.ast = filters.apply_function(intermediate.ast, fix_solutions, 'solution') intermediate.ast = filters.apply_function(intermediate.ast, escape_html, 'prompt') return intermediate
def parser_postprocessor(intermediate): """ Because LaTeX has no defined Prompt the first string, equations, and images found in a question are put under a prompt Token. :param intermediate: An intermediate parse object :return: A modified intermediate """ def def_prompt(token): try: definition = token.definition[0].definition title, prompt = definition.pop(0), [] while len(definition) > 0 and (not hasattr(definition[0], 'name') or definition[0].name in ['$', 'img', 'verbatim']): prompt.append(definition.pop(0)) definition.insert(0, util.Map({'name': 'prompt', 'definition': prompt})) definition.insert(0, title) return token except AttributeError: raise(parser_composer.FormatError('Malformed question token definition:' + str(token))) # Run inner function recursively on the ast filters.apply_function(intermediate.ast, def_prompt, 'question', partial=False) return intermediate