master_parse/__init__.py
def process(
        self,
        contents: List[str],
        cell_code: CommandCode,
        cell_num: int,
        notebook_name: str,
        language: CommandCode,
        params: Params,
    ):
        """
        Runs a cell's content through the template processor.

        :param contents:      the contents, a list of lines with no trailing
                              newline
        :param cell_code:     the cell code (CommandCode.MARKDOWN,
                              CommandCode.MARKDOWN_SANDBOX)
        :param cell_num:      the cell number, for errors/warnings
        :param notebook_name: name of notebook, for errors/warnings
        :param language:      output language (as a CommandCode) of the
                              notebook being generated
        :param params:        the parsed command line parameters

        :return: a tuple with two elements: the possibly-changed command code
                 (because certain expansions only work in %md-sandbox) and the new
                 content as a list of lines with no trailing newline
        """
        self._found_hints = False
        self._in_hints_block = False
        self._total_hints = 0

        s = "\n".join(contents)

        scala = False
        python = False
        r = False
        sql = False
        if language == CommandCode.SQL:
            lang_string = "SQL"
            sql = True
        elif language == CommandCode.SCALA:
            lang_string = "Scala"
            scala = True
        elif language == CommandCode.PYTHON:
            lang_string = "Python"
            python = True
        elif language == CommandCode.R:
            lang_string = "R"
            r = True
        else:
            lang_string = ""

        def check_for_bad_tags(s: str) -> bool:
            bad = False
            lines = s.split("\n")
            for i, line in enumerate(lines):
                line_num = i + 1
                for r in self.BAD_MUSTACHE_REGEXPS:
                    m = r.search(line)
                    if m:
                        warn(
                            f'"{notebook_name}", cell #{cell_num}, '
                            + f"line {line_num}: Possibly bad Mustache tag "
                            + f'"{m.group(1)}"'
                        )
                        bad = True
            return bad

        def strip_leading_and_trailing_blank_lines(text: str) -> str:
            import itertools

            def drop_leading(lines):
                return list(itertools.dropwhile(lambda s: len(s.strip()) == 0, lines))

            lines = drop_leading(text.split("\n"))
            lines.reverse()
            lines = drop_leading(lines)
            lines.reverse()
            return "\n".join(lines)

        def handle_hints(text: str) -> str:
            # Emit the prelude, which contains the JavaScript and the button.
            # Then, pass the text along, for further expansion.
            self._id_prefix = str(_rng.randint(0, 10000))
            self._found_hints = True
            self._in_hints_block = True
            prelude_vars = {"id_prefix": self._id_prefix}
            prelude = self._renderer.render(self.HINTS_PRELUDE_TEMPLATE, prelude_vars)
            return prelude + strip_leading_and_trailing_blank_lines(text)

        def handle_hint(text: str) -> str:
            # The text is the body of the hint. Expand the hints template.
            if not self._in_hints_block:
                raise Exception("Found {{#HINT}} outside required {{#HINTS}} block.")

            self._total_hints += 1
            hint_vars = {
                "id_prefix": self._id_prefix,
                "hint": strip_leading_and_trailing_blank_lines(text),
            }
            return self._renderer.render(self.HINT_TEMPLATE, hint_vars)

        def handle_answer(text: str) -> str:
            # The text is the body of the answer.

            vars = {"id_prefix": self._id_prefix}

            # Render the answer template.
            vars["answer"] = strip_leading_and_trailing_blank_lines(text)
            return self._renderer.render(self.ANSWER_TEMPLATE, vars)

        if params.course_type == CourseType.SELF_PACED:
            self_paced = True
            ilt = False
        elif params.course_type == CourseType.ILT:
            self_paced = False
            ilt = True
        else:
            self_paced = False
            ilt = False

        vars = {
            "copyright_year": params.copyright_year,
            "notebook_language": lang_string,
            "HINT": handle_hint,
            "HINTS": handle_hints,
            "ANSWER": handle_answer,
            "r": r,
            "scala": scala,
            "python": python,
            "sql": sql,
            "ilt": ilt,
            "self_paced": self_paced,
        }

        if params.active_profile:
            for prof in params.all_profiles:
                if prof == params.active_profile:
                    vars[prof.name] = prof.value or prof.name
                else:
                    vars[prof.name] = ""

        vars.update(params.extra_template_vars)
        check_for_bad_tags(s)
        new_content = self._renderer.render(s, vars)

        if self._found_hints:
            new_cell_code = CommandCode.MARKDOWN_SANDBOX
        else:
            new_cell_code = cell_code

        return (new_cell_code, new_content.split("\n"))

        Runs a cell's content through the template processor.

        :param contents:      the contents, a list of lines with no trailing
                              newline
        :param cell_code:     the cell code (CommandCode.MARKDOWN,
                              CommandCode.MARKDOWN_SANDBOX)
        :param cell_num:      the cell number, for errors/warnings
        :param notebook_name: name of notebook, for errors/warnings
        :param language:      output language (as a CommandCode) of the
                              notebook being generated
        :param params:        the parsed command line parameters

        :return: a tuple with two elements: the possibly-changed command code
                 (because certain expansions only work in %md-sandbox) and the new
                 content as a list of lines with no trailing newline
        
Runs a cell's content through the template processor.
Runs a cell ' s content through the template processor .
def process(
        self,
        contents: List[str],
        cell_code: CommandCode,
        cell_num: int,
        notebook_name: str,
        language: CommandCode,
        params: Params,
    ):
        self._found_hints = False
        self._in_hints_block = False
        self._total_hints = 0
        s = "\n".join(contents)
        scala = False
        python = False
        r = False
        sql = False
        if language == CommandCode.SQL:
            lang_string = "SQL"
            sql = True
        elif language == CommandCode.SCALA:
            lang_string = "Scala"
            scala = True
        elif language == CommandCode.PYTHON:
            lang_string = "Python"
            python = True
        elif language == CommandCode.R:
            lang_string = "R"
            r = True
        else:
            lang_string = ""
        def check_for_bad_tags(s: str) -> bool:
            bad = False
            lines = s.split("\n")
            for i, line in enumerate(lines):
                line_num = i + 1
                for r in self.BAD_MUSTACHE_REGEXPS:
                    m = r.search(line)
                    if m:
                        warn(
                            f'"{notebook_name}", cell #{cell_num}, '
                            + f"line {line_num}: Possibly bad Mustache tag "
                            + f'"{m.group(1)}"'
                        )
                        bad = True
            return bad
        def strip_leading_and_trailing_blank_lines(text: str) -> str:
            import itertools
            def drop_leading(lines):
                return list(itertools.dropwhile(lambda s: len(s.strip()) == 0, lines))
            lines = drop_leading(text.split("\n"))
            lines.reverse()
            lines = drop_leading(lines)
            lines.reverse()
            return "\n".join(lines)
        def handle_hints(text: str) -> str:
            self._id_prefix = str(_rng.randint(0, 10000))
            self._found_hints = True
            self._in_hints_block = True
            prelude_vars = {"id_prefix": self._id_prefix}
            prelude = self._renderer.render(self.HINTS_PRELUDE_TEMPLATE, prelude_vars)
            return prelude + strip_leading_and_trailing_blank_lines(text)
        def handle_hint(text: str) -> str:
            if not self._in_hints_block:
                raise Exception("Found {{#HINT}} outside required {{#HINTS}} block.")
            self._total_hints += 1
            hint_vars = {
                "id_prefix": self._id_prefix,
                "hint": strip_leading_and_trailing_blank_lines(text),
            }
            return self._renderer.render(self.HINT_TEMPLATE, hint_vars)
        def handle_answer(text: str) -> str:
            vars = {"id_prefix": self._id_prefix}
            vars["answer"] = strip_leading_and_trailing_blank_lines(text)
            return self._renderer.render(self.ANSWER_TEMPLATE, vars)
        if params.course_type == CourseType.SELF_PACED:
            self_paced = True
            ilt = False
        elif params.course_type == CourseType.ILT:
            self_paced = False
            ilt = True
        else:
            self_paced = False
            ilt = False
        vars = {
            "copyright_year": params.copyright_year,
            "notebook_language": lang_string,
            "HINT": handle_hint,
            "HINTS": handle_hints,
            "ANSWER": handle_answer,
            "r": r,
            "scala": scala,
            "python": python,
            "sql": sql,
            "ilt": ilt,
            "self_paced": self_paced,
        }
        if params.active_profile:
            for prof in params.all_profiles:
                if prof == params.active_profile:
                    vars[prof.name] = prof.value or prof.name
                else:
                    vars[prof.name] = ""
        vars.update(params.extra_template_vars)
        check_for_bad_tags(s)
        new_content = self._renderer.render(s, vars)
        if self._found_hints:
            new_cell_code = CommandCode.MARKDOWN_SANDBOX
        else:
            new_cell_code = cell_code
        return (new_cell_code, new_content.split("\n"))
def process ( self , contents : List [ str ] , cell_code : CommandCode , cell_num : int , notebook_name : str , language : CommandCode , params : Params , ) : self . _found_hints = False self . _in_hints_block = False self . _total_hints = 0 s = "\n" . join ( contents ) scala = False python = False r = False sql = False if language == CommandCode . SQL : lang_string = "SQL" sql = True elif language == CommandCode . SCALA : lang_string = "Scala" scala = True elif language == CommandCode . PYTHON : lang_string = "Python" python = True elif language == CommandCode . R : lang_string = "R" r = True else : lang_string = "" def check_for_bad_tags ( s : str ) -> bool : bad = False lines = s . split ( "\n" ) for i , line in enumerate ( lines ) : line_num = i + 1 for r in self . BAD_MUSTACHE_REGEXPS : m = r . search ( line ) if m : warn ( f'"{notebook_name}", cell #{cell_num}, ' + f"line {line_num}: Possibly bad Mustache tag " + f'"{m.group(1)}"' ) bad = True return bad def strip_leading_and_trailing_blank_lines ( text : str ) -> str : import itertools def drop_leading ( lines ) : return list ( itertools . dropwhile ( lambda s : len ( s . strip ( ) ) == 0 , lines ) ) lines = drop_leading ( text . split ( "\n" ) ) lines . reverse ( ) lines = drop_leading ( lines ) lines . reverse ( ) return "\n" . join ( lines ) def handle_hints ( text : str ) -> str : self . _id_prefix = str ( _rng . randint ( 0 , 10000 ) ) self . _found_hints = True self . _in_hints_block = True prelude_vars = { "id_prefix" : self . _id_prefix } prelude = self . _renderer . render ( self . HINTS_PRELUDE_TEMPLATE , prelude_vars ) return prelude + strip_leading_and_trailing_blank_lines ( text ) def handle_hint ( text : str ) -> str : if not self . _in_hints_block : raise Exception ( "Found {{#HINT}} outside required {{#HINTS}} block." ) self . _total_hints += 1 hint_vars = { "id_prefix" : self . _id_prefix , "hint" : strip_leading_and_trailing_blank_lines ( text ) , } return self . _renderer . render ( self . HINT_TEMPLATE , hint_vars ) def handle_answer ( text : str ) -> str : vars = { "id_prefix" : self . _id_prefix } vars [ "answer" ] = strip_leading_and_trailing_blank_lines ( text ) return self . _renderer . render ( self . ANSWER_TEMPLATE , vars ) if params . course_type == CourseType . SELF_PACED : self_paced = True ilt = False elif params . course_type == CourseType . ILT : self_paced = False ilt = True else : self_paced = False ilt = False vars = { "copyright_year" : params . copyright_year , "notebook_language" : lang_string , "HINT" : handle_hint , "HINTS" : handle_hints , "ANSWER" : handle_answer , "r" : r , "scala" : scala , "python" : python , "sql" : sql , "ilt" : ilt , "self_paced" : self_paced , } if params . active_profile : for prof in params . all_profiles : if prof == params . active_profile : vars [ prof . name ] = prof . value or prof . name else : vars [ prof . name ] = "" vars . update ( params . extra_template_vars ) check_for_bad_tags ( s ) new_content = self . _renderer . render ( s , vars ) if self . _found_hints : new_cell_code = CommandCode . MARKDOWN_SANDBOX else : new_cell_code = cell_code return ( new_cell_code , new_content . split ( "\n" ) )
Runs a cell's content through the template processor.
Runs a cell ' s content through the template processor .
"""
        Runs a cell's content through the template processor.

        :param contents:      the contents, a list of lines with no trailing
                              newline
        :param cell_code:     the cell code (CommandCode.MARKDOWN,
                              CommandCode.MARKDOWN_SANDBOX)
        :param cell_num:      the cell number, for errors/warnings
        :param notebook_name: name of notebook, for errors/warnings
        :param language:      output language (as a CommandCode) of the
                              notebook being generated
        :param params:        the parsed command line parameters

        :return: a tuple with two elements: the possibly-changed command code
                 (because certain expansions only work in %md-sandbox) and the new
                 content as a list of lines with no trailing newline
        """ # Emit the prelude, which contains the JavaScript and the button. # Then, pass the text along, for further expansion. # The text is the body of the hint. Expand the hints template. # The text is the body of the answer. # Render the answer template.
a tuple with two elements: the possibly-changed command code
(because certain expansions only work in %md-sandbox) and the new
content as a list of lines with no trailing newline
a tuple with two elements : the possibly - changed command code ( because certain expansions only work in %md - sandbox ) and the new content as a list of lines with no trailing newline
self
contents
List[str]
the contents, a list of lines with no trailing
newline
the contents a list of lines with no trailing newline
cell_code
CommandCode
the cell code (CommandCode.MARKDOWN,
CommandCode.MARKDOWN_SANDBOX)
the cell code ( CommandCode . MARKDOWN CommandCode . MARKDOWN_SANDBOX )
cell_num
int
the cell number, for errors/warnings
the cell number for errors / warnings
notebook_name
str
name of notebook, for errors/warnings
name of notebook for errors / warnings
language
CommandCode
output language (as a CommandCode) of the
notebook being generated
output language ( as a CommandCode ) of the notebook being generated
params
Params
the parsed command line parameters
the parsed command line parameters