2022-05-22 09:25:22 +00:00
|
|
|
|
// Package interwiki provides interwiki capabilities. Most of them, at least.
|
|
|
|
|
package interwiki
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/files"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
|
var (
|
|
|
|
|
record, err = readInterwiki()
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
for _, wiki := range record {
|
2022-05-27 13:43:40 +00:00
|
|
|
|
wiki := wiki // This line is required
|
2022-05-22 09:25:22 +00:00
|
|
|
|
wiki.canonize()
|
|
|
|
|
theMap.list = append(theMap.list, &wiki)
|
|
|
|
|
for _, prefix := range wiki.Names {
|
|
|
|
|
if _, found := theMap.byName[prefix]; found {
|
2022-05-24 15:59:18 +00:00
|
|
|
|
log.Fatalf("There are multiple uses of the same prefix ‘%s’\n", prefix)
|
2022-05-22 09:25:22 +00:00
|
|
|
|
} else {
|
|
|
|
|
theMap.byName[prefix] = &wiki
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.Printf("Loaded %d interwiki entries\n", len(theMap.list))
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 17:05:22 +00:00
|
|
|
|
func HrefLinkFormatFor(prefix string) string {
|
|
|
|
|
if wiki, ok := theMap.byName[prefix]; ok {
|
2022-05-28 17:19:40 +00:00
|
|
|
|
return wiki.LinkHrefFormat
|
2022-05-28 17:05:22 +00:00
|
|
|
|
}
|
2022-05-28 17:19:40 +00:00
|
|
|
|
return "{NAME}" // TODO: error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ImgSrcFormatFor(prefix string) string {
|
|
|
|
|
if wiki, ok := theMap.byName[prefix]; ok {
|
|
|
|
|
return wiki.ImgSrcFormat
|
|
|
|
|
}
|
|
|
|
|
return "{NAME}" // TODO: error
|
2022-05-28 17:05:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 09:25:22 +00:00
|
|
|
|
func readInterwiki() ([]Wiki, error) {
|
|
|
|
|
var (
|
|
|
|
|
record []Wiki
|
|
|
|
|
fileContents, err = os.ReadFile(files.InterwikiJSON())
|
|
|
|
|
)
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return record, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(fileContents, &record)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return record, nil
|
|
|
|
|
}
|