mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-01-07 02:10:26 +00:00
Interwiki: Implement (*Wiki).canonize
This commit is contained in:
parent
ac1391e64a
commit
38ab54b8c3
@ -20,7 +20,7 @@ func Init() {
|
|||||||
theMap.list = append(theMap.list, &wiki)
|
theMap.list = append(theMap.list, &wiki)
|
||||||
for _, prefix := range wiki.Names {
|
for _, prefix := range wiki.Names {
|
||||||
if _, found := theMap.byName[prefix]; found {
|
if _, found := theMap.byName[prefix]; found {
|
||||||
log.Fatalf("There are multiple interwiki map entries having the same prefix ‘%s’\n", prefix)
|
log.Fatalf("There are multiple uses of the same prefix ‘%s’\n", prefix)
|
||||||
} else {
|
} else {
|
||||||
theMap.byName[prefix] = &wiki
|
theMap.byName[prefix] = &wiki
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,21 @@
|
|||||||
package interwiki
|
package interwiki
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/util"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
// WikiEngine is an enumeration of supported interwiki targets.
|
// WikiEngine is an enumeration of supported interwiki targets.
|
||||||
type WikiEngine int
|
type WikiEngine int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Mycorrhiza is a Mycorrhiza wiki. This is the default value.
|
|
||||||
Mycorrhiza WikiEngine = iota
|
Mycorrhiza WikiEngine = iota
|
||||||
|
OddMuse
|
||||||
|
MediaWiki
|
||||||
|
MoinMoin1
|
||||||
|
MoinMoin2
|
||||||
|
DokuWiki
|
||||||
// Generic is any website.
|
// Generic is any website.
|
||||||
Generic
|
Generic
|
||||||
)
|
)
|
||||||
@ -22,7 +32,7 @@ type Wiki struct {
|
|||||||
// http://wiki.example.org/view/%s
|
// 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.
|
// 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.
|
||||||
//
|
//
|
||||||
// This field is optional. For Generic wikis, it is automatically set to <URL>/%s; for Mycorrhiza wikis, it is automatically set to <URL>/hypha/%s.
|
// 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.
|
||||||
LinkFormat string `json:"link_format"`
|
LinkFormat string `json:"link_format"`
|
||||||
|
|
||||||
// Description is a plain-text description of the wiki.
|
// Description is a plain-text description of the wiki.
|
||||||
@ -31,13 +41,46 @@ type Wiki struct {
|
|||||||
// Engine is the engine of the wiki. This field is not set in JSON.
|
// Engine is the engine of the wiki. This field is not set in JSON.
|
||||||
Engine WikiEngine `json:"-"`
|
Engine WikiEngine `json:"-"`
|
||||||
|
|
||||||
// EngineString is a string name of the engine. It is then converted to Engine. Supported values are:
|
// 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.
|
||||||
// * mycorrhiza -> Mycorrhiza
|
|
||||||
// * generic -> Generic
|
|
||||||
// All other values will result in an error.
|
|
||||||
EngineString string `json:"engine"`
|
EngineString string `json:"engine"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Wiki) canonize() {
|
var wikiEnginesLookup = map[string]WikiEngine{
|
||||||
|
"mycorrhiza": Mycorrhiza,
|
||||||
|
"oddmuse": OddMuse,
|
||||||
|
"mediawiki": MediaWiki,
|
||||||
|
"moin1": MoinMoin1,
|
||||||
|
"moin2": MoinMoin2,
|
||||||
|
"dokuwiki": DokuWiki,
|
||||||
|
"generic": Generic,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Wiki) canonize() {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user