Pi66

git.pi66.xyz
A web interface for the pi66.xyz Git server
git clone https://git.pi66.xyz/git.pi66.xyz

src/plugins/file_builder.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from models import Repo
from progress import progress, progress_done


def _get_file_extension(filename: str) -> str:
    return os.path.splitext(filename)[1].lstrip(".")


def _create_file_preview_page(api, repo_name, repo_desc, git_path, file):
    file_path = os.path.join(git_path, repo_name, file.path)
    file_ext = _get_file_extension(file.path)

    try:
        with open(file_path, "rb") as f:
            content = f.read()
    except FileNotFoundError:
        return

    if b"\x00" in content:
        content = "[BIN FILE]"
    else:
        try:
            decoded = content.decode()
        except UnicodeDecodeError:
            decoded = content.decode(errors="replace")

        content = api.md_to_html(
            config=api.config,
            md_content=f"```{file_ext}\n{decoded}```"
        )

    html = api.jinja_handler(api.config, '{% extends "File-preview.html" %}', plugins={
        "repo_name": repo_name,
        "repo_desc": repo_desc,
        "page_title": f"Pi66 - {repo_name} - {file.path}",
        "metadata_content": repo_desc or "A web interface for the pi66.xyz Git server",
        "file_name": file.path,
        "file_content": content,
    })

    dest = os.path.join(api.config.tree.draft_dest if api.mode == "draft" else api.config.tree.release_dest, f"{repo_name.lower()}/files/{file.path}.html")
    api.save_html(html, dest)


def create_file_pages(api, repos: list[Repo], git_dir):
    total = len(repos)
    for i, repo in enumerate(repos, 1):
        progress("Files", i, total)
        try:
            for file in repo.files:
                _create_file_preview_page(api, repo.name, repo.desc, git_dir, file)

            html = api.jinja_handler(api.config, '{% extends "Files.html" %}', plugins={
                "repo_name": repo.name,
                "repo_desc": repo.desc,
                "page_title": f"Pi66 - {repo.name} - Files",
                "metadata_content": repo.desc or "A web interface for the pi66.xyz Git server",
                "files": repo.files,
            })

            dest = os.path.join(api.config.tree.draft_dest if api.mode == "draft" else api.config.tree.release_dest, f"{repo.name.lower()}/files.html")
            api.save_html(html, dest)
        except Exception as e:
            api.log.warn(f"Error building files for {repo.name}: {e}")
    progress_done("Files", total)