1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-09 03:00:25 +00:00
mycorrhiza/interwiki/wiki.go

107 lines
2.8 KiB
Go
Raw Normal View History

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.
type WikiEngine int
const (
Mycorrhiza WikiEngine = iota
2022-05-24 15:59:18 +00:00
OddMuse
MediaWiki
MoinMoin1
MoinMoin2
DokuWiki
2022-05-22 09:25:22 +00:00
// Generic is any website.
Generic
)
// EmojiWithName returns a Unicode emoji that kinda represents the engine and the engine name. One day we might move to actual images. OK for now.
func (we WikiEngine) EmojiWithName() string {
switch we {
case Mycorrhiza:
return "🍄 Mycorrhiza"
case OddMuse:
return "🐫 OddMuse"
case MediaWiki:
return "🌻 MediaWiki"
case MoinMoin1:
return "Ⓜ️ MoinMoin 1.9"
case MoinMoin2:
return "Ⓜ️ MoinMoin 2.*"
case DokuWiki:
return "📝 DokuWiki"
default:
return "🌐 Generic"
}
}
2022-05-22 09:25:22 +00:00
// Wiki is an entry in the interwiki map.
type Wiki struct {
// Names is a slice of link prefices that correspond to this wiki.
Names []string `json:"names"`
// URL is the address of the wiki.
URL string `json:"url"`
// LinkFormat is a format string for incoming interwiki links. The format strings should look like this:
// http://wiki.example.org/view/%s
// where %s is where text will be inserted. No other % instructions are supported yet. They will be added once we learn of their use cases.
//
2022-05-24 15:59:18 +00:00
// This field is optional. For other wikis, it is automatically set to <URL>/%s; for Mycorrhiza wikis, it is automatically set to <URL>/hypha/%s.
2022-05-22 09:25:22 +00:00
LinkFormat string `json:"link_format"`
// Description is a plain-text description of the wiki.
Description string `json:"description"`
// Engine is the engine of the wiki. This field is not set in JSON.
Engine WikiEngine `json:"-"`
2022-05-24 15:59:18 +00:00
// EngineString is a string name of the engine. It is then converted to Engine. See the code to learn the supported values. All other values will result in an error.
2022-05-22 09:25:22 +00:00
EngineString string `json:"engine"`
}
2022-05-24 15:59:18 +00:00
var wikiEnginesLookup = map[string]WikiEngine{
"mycorrhiza": Mycorrhiza,
"oddmuse": OddMuse,
"mediawiki": MediaWiki,
"moin1": MoinMoin1,
"moin2": MoinMoin2,
"dokuwiki": DokuWiki,
"generic": Generic,
}
2022-05-22 09:25:22 +00:00
func (w *Wiki) canonize() {
2022-05-24 15:59:18 +00:00
if engine, ok := wikiEnginesLookup[w.EngineString]; ok {
w.Engine = engine
w.EngineString = "" // Ain't gonna need it anymore
} else {
log.Fatalf("Unknown engine %s\n", w.EngineString)
}
if len(w.Names) == 0 {
log.Fatalln("Cannot have a wiki in the interwiki map with no name")
}
if w.URL == "" {
log.Fatalf("Wiki %s has no URL\n", w.Names[0])
}
for i, prefix := range w.Names {
w.Names[i] = util.CanonicalName(prefix)
}
2022-05-22 09:25:22 +00:00
2022-05-24 15:59:18 +00:00
if w.LinkFormat == "" {
switch w.Engine {
case Mycorrhiza:
w.LinkFormat = fmt.Sprintf("%s/hypha/%%s", w.URL)
default:
w.LinkFormat = fmt.Sprintf("%s/%%s", w.URL)
}
}
2022-05-22 09:25:22 +00:00
}