Lindenii Project Forge
Login

server

Lindenii Forge’s main backend daemon
Commit info
ID
cc5ae1a0a1b1abfb147b74e6583f15a585566cf0
Author
Runxi Yu <me@runxiyu.org>
Author date
Sat, 05 Apr 2025 23:25:06 +0800
Committer
Runxi Yu <me@runxiyu.org>
Committer date
Sat, 05 Apr 2025 23:25:06 +0800
Actions
Move trivial template functions into misc
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package forge
package misc

import (
	"net/url"
	"strings"
)

// These are all trivial functions that are intended to be used in HTML
// templates.

// FirstLine returns the first line of a string.
func FirstLine(s string) string {
	before, _, _ := strings.Cut(s, "\n")
	return before
}

// PathEscape escapes the input as an URL path segment.
func PathEscape(s string) string {
	return url.PathEscape(s)
}

// QueryEscape escapes the input as an URL query segment.
func QueryEscape(s string) string {
	return url.QueryEscape(s)
}

// Dereference dereferences a pointer.
func Dereference[T any](p *T) T {
	return *p
}

// DereferenceOrZero dereferences a pointer. If the pointer is nil, the zero
// value of its associated type is returned instead.
func DereferenceOrZero[T any](p *T) T {
	if p != nil {
		return *p
	}
	var z T
	return z
}

// Minus subtracts two numbers.
func Minus(a, b int) int {
	return a - b
}
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package forge

import (
	"embed"
	"html/template"
	"io/fs"

	"github.com/tdewolff/minify/v2"
	"github.com/tdewolff/minify/v2/html"
	"go.lindenii.runxiyu.org/forge/internal/misc"
)

//go:embed LICENSE source.tar.gz
var embeddedSourceFS embed.FS

//go:embed templates/* static/*
//go:embed hookc/hookc git2d/git2d
var embeddedResourcesFS embed.FS

var templates *template.Template //nolint:gochecknoglobals

// loadTemplates minifies and loads HTML templates.
func loadTemplates() (err error) {
	minifier := minify.New()
	minifierOptions := html.Minifier{
		TemplateDelims:      [2]string{"{{", "}}"},
		KeepDefaultAttrVals: true,
	} //exhaustruct:ignore
	minifier.Add("text/html", &minifierOptions)

	templates = template.New("templates").Funcs(template.FuncMap{
		"first_line":        FirstLine,
		"path_escape":       PathEscape,
		"query_escape":      QueryEscape,
		"dereference_error": DereferenceOrZero[error],
		"minus":             Minus,
		"first_line":        misc.FirstLine,
		"path_escape":       misc.PathEscape,
		"query_escape":      misc.QueryEscape,
		"dereference_error": misc.DereferenceOrZero[error],
		"minus":             misc.Minus,
	})

	err = fs.WalkDir(embeddedResourcesFS, "templates", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if !d.IsDir() {
			content, err := fs.ReadFile(embeddedResourcesFS, path)
			if err != nil {
				return err
			}

			minified, err := minifier.Bytes("text/html", content)
			if err != nil {
				return err
			}

			_, err = templates.Parse(misc.BytesToString(minified))
			if err != nil {
				return err
			}
		}
		return nil
	})
	return err
}