2022-05-22 09:25:22 +00:00
|
|
|
|
package interwiki
|
|
|
|
|
|
2022-05-24 15:59:18 +00:00
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
|
|
|
"log"
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-22 09:25:22 +00:00
|
|
|
|
// WikiEngine is an enumeration of supported interwiki targets.
|
2022-06-08 13:45:27 +00:00
|
|
|
|
type WikiEngine string
|
2022-05-22 09:25:22 +00:00
|
|
|
|
|
|
|
|
|
const (
|
2022-06-08 13:45:27 +00:00
|
|
|
|
Mycorrhiza WikiEngine = "mycorrhiza"
|
|
|
|
|
Agora WikiEngine = "agora"
|
2022-05-22 09:25:22 +00:00
|
|
|
|
// Generic is any website.
|
2022-06-08 13:45:27 +00:00
|
|
|
|
Generic WikiEngine = "generic"
|
2022-05-22 09:25:22 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-06-08 13:45:27 +00:00
|
|
|
|
func (we WikiEngine) Valid() bool {
|
2022-05-27 13:43:40 +00:00
|
|
|
|
switch we {
|
2022-06-08 13:45:27 +00:00
|
|
|
|
case Mycorrhiza, Agora, Generic:
|
|
|
|
|
return true
|
2022-05-27 13:43:40 +00:00
|
|
|
|
}
|
2022-06-08 13:45:27 +00:00
|
|
|
|
return false
|
2022-05-27 13:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 09:25:22 +00:00
|
|
|
|
// Wiki is an entry in the interwiki map.
|
|
|
|
|
type Wiki struct {
|
2022-06-08 13:45:27 +00:00
|
|
|
|
// Name is the name of the wiki, and is also one of the possible prefices.
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
|
|
|
|
// Aliases are alternative prefices you can use instead of Name. This slice can be empty.
|
|
|
|
|
Aliases []string `json:"aliases,omitempty"`
|
2022-05-22 09:25:22 +00:00
|
|
|
|
|
|
|
|
|
// URL is the address of the wiki.
|
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
|
2022-05-28 17:19:40 +00:00
|
|
|
|
// LinkHrefFormat is a format string for interwiki links. See Mycomarkup internal docs hidden deep inside for more information.
|
2022-05-22 09:25:22 +00:00
|
|
|
|
//
|
2022-06-08 13:45:27 +00:00
|
|
|
|
// This field is optional. If it is not set, it is derived from other data. See the code.
|
2022-05-28 17:19:40 +00:00
|
|
|
|
LinkHrefFormat string `json:"link_href_format"`
|
|
|
|
|
|
|
|
|
|
ImgSrcFormat string `json:"img_src_format"`
|
2022-05-22 09:25:22 +00:00
|
|
|
|
|
2022-06-08 13:45:27 +00:00
|
|
|
|
// Engine is the engine of the wiki. Invalid values will result in a start-up error.
|
|
|
|
|
Engine WikiEngine `json:"engine"`
|
2022-05-24 15:59:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 09:25:22 +00:00
|
|
|
|
func (w *Wiki) canonize() {
|
2022-06-08 13:45:27 +00:00
|
|
|
|
switch {
|
|
|
|
|
case w.Name == "":
|
2022-05-24 15:59:18 +00:00
|
|
|
|
log.Fatalln("Cannot have a wiki in the interwiki map with no name")
|
2022-06-08 13:45:27 +00:00
|
|
|
|
case w.URL == "":
|
|
|
|
|
log.Fatalf("Wiki ‘%s’ has no URL\n", w.Name)
|
|
|
|
|
case !w.Engine.Valid():
|
|
|
|
|
log.Fatalf("Unknown engine ‘%s’ for wiki ‘%s’\n", w.Engine, w.Name)
|
2022-05-24 15:59:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 13:45:27 +00:00
|
|
|
|
w.Name = util.CanonicalName(w.Name)
|
|
|
|
|
for i, alias := range w.Aliases {
|
|
|
|
|
w.Aliases[i] = util.CanonicalName(alias)
|
2022-05-24 15:59:18 +00:00
|
|
|
|
}
|
2022-05-22 09:25:22 +00:00
|
|
|
|
|
2022-05-28 17:19:40 +00:00
|
|
|
|
if w.LinkHrefFormat == "" {
|
|
|
|
|
switch w.Engine {
|
|
|
|
|
case Mycorrhiza:
|
|
|
|
|
w.LinkHrefFormat = fmt.Sprintf("%s/hypha/{NAME}", w.URL)
|
2022-06-08 13:45:27 +00:00
|
|
|
|
case Agora:
|
|
|
|
|
w.LinkHrefFormat = fmt.Sprintf("%s/node/{NAME}", w.URL)
|
2022-05-28 17:19:40 +00:00
|
|
|
|
default:
|
|
|
|
|
w.LinkHrefFormat = fmt.Sprintf("%s/{NAME}", w.URL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if w.ImgSrcFormat == "" {
|
2022-05-24 15:59:18 +00:00
|
|
|
|
switch w.Engine {
|
|
|
|
|
case Mycorrhiza:
|
2022-05-28 17:19:40 +00:00
|
|
|
|
w.ImgSrcFormat = fmt.Sprintf("%s/binary/{NAME}", w.URL)
|
2022-05-24 15:59:18 +00:00
|
|
|
|
default:
|
2022-05-28 17:19:40 +00:00
|
|
|
|
w.ImgSrcFormat = fmt.Sprintf("%s/{NAME}", w.URL)
|
2022-05-24 15:59:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-22 09:25:22 +00:00
|
|
|
|
}
|