6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2025-01-10 00:37:54 +00:00

feat: add IS_FILE_RENDERED filter to enhance file processing flexibility (#1062)

This commit is contained in:
Abdul-Muqadim-Arbisoft 2024-05-28 11:06:51 +05:00 committed by GitHub
parent 3d52a84096
commit d682b74053
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 2 deletions

View File

@ -0,0 +1 @@
- [Feature] Introduces the IS_FILE_RENDERED Filter, which allows developers to specify files that should be copied directly without rendering. This update empowers developers to manage special file types, ensuring that they are transferred intact without undergoing template processing. (by @Abdul-Muqadim-Arbisoft)

View File

@ -194,8 +194,7 @@ class Renderer:
The template_name *always* uses "/" separators, and is not os-dependent. Do not pass the result of The template_name *always* uses "/" separators, and is not os-dependent. Do not pass the result of
os.path.join(...) to this function. os.path.join(...) to this function.
""" """
if is_binary_file(template_name): if not hooks.Filters.IS_FILE_RENDERED.apply(True, template_name):
# Don't try to render binary files
return self.environment.read_bytes(template_name) return self.environment.read_bytes(template_name)
try: try:
@ -551,3 +550,24 @@ def _delete_plugin_templates(plugin: str, root: str, _config: Config) -> None:
raise exceptions.TutorError( raise exceptions.TutorError(
f"Could not delete file {e.filename} from plugin {plugin} in folder {path}" f"Could not delete file {e.filename} from plugin {plugin} in folder {path}"
) )
@hooks.Filters.IS_FILE_RENDERED.add()
def _do_not_render_binary_files(result: bool, path: str) -> bool:
"""
Determine whether a file should be rendered based on its binary nature.
This function checks if a file is binary and updates the rendering decision accordingly.
If the initial decision (`result`) is to render the file, but the file is detected as binary,
the function will override this decision to prevent rendering.
Parameters:
- result (bool): The initial decision on whether the file should be rendered.
- path (str): The file path to check for binary content.
Returns:
- bool: False if the file is binary and was initially set to be rendered, otherwise returns the initial decision.
"""
if result and is_binary_file(path):
result = False
return result

View File

@ -504,6 +504,15 @@ class Filters:
#: filter themselves, but they can apply it to check whether other plugins are enabled. #: filter themselves, but they can apply it to check whether other plugins are enabled.
PLUGINS_LOADED: Filter[list[str], []] = Filter() PLUGINS_LOADED: Filter[list[str], []] = Filter()
#: Use this filter to determine whether a file should be rendered. This can be useful in scenarios where
#: certain types of files need special handling, such as binary files, which should not be rendered as text.
#:
#: This filter expects a boolean return value that indicates whether the file should be rendered.
#:
#: :param bool should_render: Initial decision on rendering the file, typically set to True.
#: :param str file_path: The path to the file being checked.
IS_FILE_RENDERED: Filter[bool, [str]] = Filter()
class Contexts: class Contexts:
""" """