3.9 KiB
Rendering Process
The RouteManager class (/foldsite/src/routes/routes.py) and render_page function (/foldsite/src/rendering/renderer.py) are central to the rendering process.
How Foldsite Determines File Types
The determine_type function (in renderer.py) is crucial for figuring out how to handle a given file or directory. It examines file extensions and directory contents to classify files into broad categories (defined in GENERIC_FILE_MAPPING in /foldsite/src/rendering/__init__.py):
document: Files with extensions like.md,.txt, and.html.image: Files with extensions like.png,.jpg,.jpeg,.gif, and.svg.directory: Directories. If a directory contains files, the most common file extension within that directory is used to infer the directory's "type".other: Files that don't match any of the above categories.multimedia: This is a combination that containsimage.
Template Search
When a request comes in, Foldsite searches for an appropriate template in the templates directory. The search logic is implemented in render_page and follows a specific order, prioritizing more specific templates:
-
Exact Path Match: If a template exists with the exact same path relative to the
templatesdirectory as the requested content file (but with a.htmlextension), it's used. For example, if the request is for/about/team.md, and a template exists attemplates/about/team.md.html, that template will be used. -
Folder-Specific Template: If the requested path is a directory, Foldsite looks for a
__folder.htmltemplate within that directory. For example, if the request is for/blog/, andtemplates/blog/__folder.htmlexists, it will be used. -
Type and Extension-Specific Templates: Foldsite searches for templates named
__{type}.{extension}.htmlwithin the requested directory and its parent directories, moving upwards. For instance, if requesting/blog/post1.md, it would look for:templates/blog/__file.md.htmltemplates/__file.md.html
-
Type and Category-Specific Templates: Similar to the above, but searches for
__{type}.{category}.html. If requesting an image at/images/logo.png, it looks for:templates/images/__file.image.htmltemplates/__file.image.html
-
Base Template: Finally, if no other template is found,
templates/base.htmlis used as a fallback. This template must exist; otherwise, an exception is raised.
Style Search
CSS styles are searched similarly to templates, prioritizing specificity:
-
Exact Path Match: A CSS file with the exact same path as the requested content file (relative to the
stylesdirectory) is used. For example,/about/team.mdwould look forstyles/about/team.md.css. -
Type and Extension-Specific Styles: Searches for
__{type}.{extension}.cssin the requested directory and its parent directories. For example,/blog/post1.mdwould look for:styles/blog/__file.md.cssstyles/__file.md.css
-
Type and Category-Specific Styles: Similar to the above, but searches for
__{type}.{category}.css.styles/images/__file.image.cssstyles/__file.image.css
-
Base Style:
styles/base.cssis always included.
The discovered styles are added to the styles variable, which is passed to the Jinja2 template. The order ensures that more specific styles override general ones.
Error Handling
The render_error_page function (in renderer.py) handles errors. If a requested resource is not found (404 error) or if an exception occurs during processing, this function is called. It looks for a template named __error.html in the templates directory. If found, it's used to render the error page; otherwise, a default error page is generated. The error code, message, and description are passed to the template.