Пример #1
0
def fill_include_fields(template: Template) -> None:
    """ Populate all include fields in the template.

        An 'include' field provides a way of putting re-usable template content into a
        separate file, and including it in place of the field.

        An include field should look like this:

            '{{ include 'path/to/file.html' }}'
    """

    original_template_content = template.content

    def next_include_field():
        """ Return the next probable include/inline field. """

        return first(fields(template.content, with_name_like='include|inline'))

    field = next_include_field()

    while field is not None:
        is_include_command = field.name == TemplateFields.INCLUDE
        is_inline_command = field.name == TemplateFields.INLINE

        # default to blank
        include_content = ''
        include_path = None

        if field.context is not None:
            # the field should contain a path
            include_path = dequote(field.context).strip()

        if include_path is not None and len(include_path) > 0:
            if not os.path.isabs(include_path):
                # it's not an absolute path, so we should make it a relative path
                if template.path is not None:
                    # make the path relative to the path of the containing template
                    include_path = os.path.join(
                        os.path.dirname(template.path), include_path)

            if os.path.isfile(include_path):
                # we've ended up with a path that can be opened
                with open(include_path) as include_file:
                    if is_include_command:
                        # open it and read in the entire contents as is
                        include_content = include_file.read().strip()
                    elif is_inline_command:
                        # read each line
                        for line in include_file.readlines():
                            # stripping excess whitespace and newline in the process
                            include_content += line.strip()
            else:
                WarningDisplay.included_file_not_found_error(
                    WarningContext(os.path.basename(template.path)), include_path)

                include_content = '<strong>&lt;included file not found&gt;</strong>'
        else:
            WarningDisplay.include_should_specify_file(
                WarningContext('{0}:{1}'.format(
                    os.path.basename(template.path),
                    # todo: the line number could potentially be incorrect, as we might not be going
                    # through the original template anymore- the lineno can only serve as a hint
                    get_line_number(field.indices.start, original_template_content))),
                is_inline=is_inline_command)

        # populate the include field with the content; or blank if unresolved
        fill(field, include_content, template, indenting=is_include_command)

        field = next_include_field()
Пример #2
0
def fill_include_fields(template: Template) -> dict:
    """ Populate all include fields in the template.

        An 'include' field provides a way of putting re-usable template content into a
        separate file, and including it in place of the field.

        An include field should look like this:

            '{{ include 'path/to/file.html' }}'
    """

    original_template_content = template.content

    def next_include_field():
        """ Return the next probable include/inline field. """

        return first(fields(template.content, with_name_like='include|inline'))

    field = next_include_field()

    stripped_styles = {}

    while field is not None:
        is_include_command = field.name == TemplateFields.INCLUDE
        is_inline_command = field.name == TemplateFields.INLINE

        # default to blank
        include_content = ''
        include_path = None

        if field.context is not None:
            # the field should contain a path
            include_path = dequote(field.context).strip()

        if include_path is not None and len(include_path) > 0:
            if not os.path.isabs(include_path):
                # it's not an absolute path, so we should make it a relative path
                if template.path is not None:
                    # make the path relative to the path of the containing template
                    include_path = os.path.join(os.path.dirname(template.path),
                                                include_path)

            if os.path.isfile(include_path):
                # we've ended up with a path that can be opened
                with open(include_path) as include_file:
                    if is_include_command:
                        # open it and read in the entire contents as is
                        include_content = include_file.read().strip()
                    elif is_inline_command:
                        # read each line
                        for line in include_file.readlines():
                            # stripping excess whitespace and newline in the process
                            include_content += line.strip()
            else:
                WarningDisplay.included_file_not_found_error(
                    WarningContext(os.path.basename(template.path)),
                    include_path)

                include_content = '<strong>&lt;included file not found&gt;</strong>'

            stripped_template = Template(include_content)
            stripped_styles[include_path] = strip_styles(stripped_template)

            include_content = stripped_template.content
        else:
            WarningDisplay.include_should_specify_file(
                WarningContext('{0}:{1}'.format(
                    os.path.basename(template.path),
                    # todo: the line number could potentially be incorrect, as we might not be going
                    # through the original template anymore- the lineno can only serve as a hint
                    get_line_number(field.indices.start,
                                    original_template_content))),
                is_inline=is_inline_command)

        # populate the include field with the content; or blank if unresolved
        fill(field, include_content, template, indenting=is_include_command)

        field = next_include_field()

    return stripped_styles