GitRoot

craft your forge, build your project, grow your community freely
  1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
  2//
  3// SPDX-License-Identifier: EUPL-1.2
  4
  5package main
  6
  7import (
  8	"github.com/tidwall/gjson"
  9	gitroot "gitroot.dev/libs/golang/plugin/model"
 10)
 11
 12type conf struct {
 13	style               string
 14	favicon             string
 15	header              string
 16	menu                []link
 17	footer              string
 18	generateGitWorktree bool
 19	branchesDir         string
 20	meta                map[string]string
 21}
 22
 23var defaultConf = map[string]any{
 24	"style":   "simple.min.css",
 25	"favicon": "icon.png",
 26	"header":  "<h1>{{repo.name}}</h1>",
 27	"menu": []link{
 28		{Display: "🏠 Home", Link: "/"},
 29		{Display: "📖 Readme", Link: "/README.html"},
 30		{Display: "🐞 Issues", Link: "/boards/issues.html"},
 31		{Display: "⚖ Licence", Link: "/LICENCE.html"},
 32	},
 33	"footer":              "<p>Clone with <code>git clone {{repo.url}}</code></p><small>Hosted with ❤️ by <a href=\"https://gitroot.dev\">GitRoot</a></small>",
 34	"generateGitWorktree": true,
 35	"branchesDir":         "branches",
 36	"meta": map[string]string{
 37		"description":       "a short description",
 38		"og:title":          "title displayed in social",
 39		"og:description":    "description displayed in social",
 40		"og:type":           "article",
 41		"fediverse:creator": "@forge@gitroot.dev",
 42	},
 43}
 44
 45var defaultRun = []gitroot.PluginRun{{
 46	Path:   "**/*",
 47	Branch: []string{"main"},
 48	When:   gitroot.PluginRunWhenAll,
 49	Write: gitroot.PluginWrite{
 50		Git: []gitroot.PluginWriteRight{{
 51			Path: "index.md",
 52			Can:  []gitroot.PluginWriteRightCan{gitroot.PluginWriteRightCanAdd},
 53		}},
 54		Web: []gitroot.PluginWriteRight{{
 55			Path: "**/*",
 56			Can:  gitroot.PluginWriteRightCanAll,
 57		}},
 58	},
 59	Configuration: defaultConf,
 60}, {
 61	Path:   "**/*",
 62	Branch: []string{"*", "!main"},
 63	When:   gitroot.PluginRunWhenAll,
 64	Write: gitroot.PluginWrite{
 65		Git: []gitroot.PluginWriteRight{},
 66		Web: []gitroot.PluginWriteRight{{
 67			Path: "branches/**/*",
 68			Can:  gitroot.PluginWriteRightCanAll,
 69		}},
 70	},
 71	Configuration: defaultConf,
 72}}
 73
 74type link struct {
 75	Link    string `json:"link"`
 76	Display string `json:"display"`
 77}
 78
 79func (p *Plugin) newConf(serializedConf string) *conf {
 80	jsonConf := gjson.Parse(serializedConf)
 81	menus := []link{}
 82	for _, l := range jsonConf.Get("menu").Array() {
 83		menus = append(menus, link{
 84			Link:    l.Get("link").String(),
 85			Display: l.Get("display").String(),
 86		})
 87	}
 88	metaJson := jsonConf.Get("meta").Map()
 89	meta := make(map[string]string, len(metaJson))
 90	for key, val := range metaJson {
 91		meta[key] = val.String()
 92	}
 93	style := jsonConf.Get("style").String()
 94	if style == "" {
 95		style = "simple.min.css"
 96	}
 97	return &conf{
 98		style:               style,
 99		favicon:             jsonConf.Get("favicon").String(),
100		header:              jsonConf.Get("header").String(),
101		menu:                menus,
102		footer:              jsonConf.Get("footer").String(),
103		generateGitWorktree: jsonConf.Get("generateGitWorktree").Bool(),
104		branchesDir:         jsonConf.Get("branchesDir").String(),
105		meta:                meta,
106	}
107}