// SPDX-FileCopyrightText: 2025 Romain Maneschi // // SPDX-License-Identifier: EUPL-1.2 package main import ( _ "embed" "errors" "fmt" "io/fs" "path/filepath" "strings" gitroot "gitroot.dev/libs/golang/plugin" "gitroot.dev/libs/golang/plugin/model" ) //go:embed resources/styles/add.css var addStyle string //go:embed resources/styles/pico.min.css var picoStyle string //go:embed resources/styles/simple.min.css var simpleStyle string //go:embed resources/index.md var index string type Plugin struct { server model.Server config *conf renderer *renderer gitWorktree *worktree currentCommit model.Commit branchCommits []*branchCommits } func (p *Plugin) Init(repoName string, confHasChanged bool, serializedConf string) error { p.config = p.newConf(serializedConf) forgeConf, err := p.server.ForgeConf() if err != nil { p.server.LogError("can't get forge conf", err) } p.renderer = p.newRender(repoName, forgeConf) p.branchCommits = make([]*branchCommits, 0) if p.config.generateGitWorktree { p.LoadWorktree() } // css style if _, err := fs.Stat(p.server.Webcontent(), "styles/style.css"); errors.Is(err, fs.ErrNotExist) || confHasChanged { style := "" switch p.config.style { case "pico.min.css": style = picoStyle case "simple.min.css": style = simpleStyle default: // TODO download if distant? Copy if local? } if style == "" { style = simpleStyle } p.server.ModifyWebContent(p.config.style, strings.Join([]string{style, addStyle}, "\n")) } else if err != nil { p.server.LogError("can't stats styles", err) } // index.html if _, err := fs.Stat(p.server.Webcontent(), "index.html"); errors.Is(err, fs.ErrNotExist) || confHasChanged { if _, err := fs.Stat(p.server.Worktree(), "index.html"); errors.Is(err, fs.ErrNotExist) { if _, err := fs.Stat(p.server.Worktree(), "index.md"); errors.Is(err, fs.ErrNotExist) { p.server.ModifyContent("index.md", index) p.server.CommitAllIfNeeded("init web page") p.AddFile("index.md") } else if err != nil { p.server.LogError("can't stats index.md in wortree", err) } } else if err != nil { p.server.LogError("can't stats index.html in wortree", err) } } else if err != nil { p.server.LogError("can't stats index in webContent", err) } // 404.html if _, err := fs.Stat(p.server.Webcontent(), "404.html"); errors.Is(err, fs.ErrNotExist) || confHasChanged { newContent := p.renderer.render("404.html", "

Not found

", map[string]string{"title": "not found"}) p.server.ModifyWebContent("404.html", newContent) } return nil } func (p *Plugin) StartCommit(commit model.Commit) error { p.currentCommit = commit if p.config.branchesDir != "" { p.AddIfNotExist(commit) } return nil } func (p *Plugin) AddFile(fp string) error { if strings.HasSuffix(fp, ".md") { mdContent, err := fs.ReadFile(p.server.Worktree(), fp) if err != nil { p.server.LogError("AddFile ReadFile "+fp, err) return nil } md, meta := mdToHTML(fp, mdContent, p.readInclude) newContent := p.renderer.render(fp, md, meta) p.server.ModifyWebContent(fmt.Sprintf("%s.html", strings.TrimSuffix(fp, ".md")), newContent) } else { content, err := fs.ReadFile(p.server.Worktree(), fp) if err != nil { p.server.LogError("AddFile ReadFile "+fp, err) return nil } p.server.ModifyWebContent(fp, string(content)) } if p.config.generateGitWorktree { p.gitWorktree.addOrModFile(fp, p.currentCommit) } return nil } func relativePath(fromPath string, toPath string) string { absFromPath, _ := filepath.Abs(fromPath) absToPath, _ := filepath.Abs(toPath) dirFromPath, _ := filepath.Split(absFromPath) dirToPath, fileToPath := filepath.Split(absToPath) path, err := filepath.Rel(dirFromPath, dirToPath) if err != nil { return toPath } return fmt.Sprintf("%s/%s", path, fileToPath) } func (p *Plugin) readInclude(from, path string, address []byte) []byte { content, _ := fs.ReadFile(p.server.Worktree(), path) return content } func (p *Plugin) DelFile(fp string) error { if p.config.generateGitWorktree { p.gitWorktree.delFile(fp) } return nil } func (p *Plugin) ModFile(fromPath string, toPath string) error { if fromPath != toPath { p.DelFile(fromPath) } return p.AddFile(toPath) } func (p *Plugin) EndCommit(commit model.Commit) error { return nil } func (p *Plugin) Finish() error { if p.config.generateGitWorktree { p.StoreWorktree() p.gitWorktree.renderHtml("", "worktree", func(fp string, htmlContent string) { p.server.ModifyWebContent(fp, p.renderer.render(fp, htmlContent, map[string]string{"title": "worktree"})) }) } if p.config.branchesDir != "" { p.RenderBranches() } p.config = nil p.gitWorktree = nil return nil } func Build(server model.Server) model.Plugin { return &Plugin{ server: server, } } //go:wasmexport install func main() { loadMimeType() loadEmojis() gitroot.Register(defaultRun, Build) }