2022-05-22 09:25:22 +00:00
|
|
|
|
// Package interwiki provides interwiki capabilities. Most of them, at least.
|
|
|
|
|
package interwiki
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2022-06-04 20:29:40 +00:00
|
|
|
|
"github.com/bouncepaw/mycomarkup/v5/options"
|
2022-05-22 09:25:22 +00:00
|
|
|
|
"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)
|
2022-06-08 13:45:27 +00:00
|
|
|
|
for _, name := range append(wiki.Aliases, wiki.Name) {
|
|
|
|
|
if _, found := theMap.byName[name]; found {
|
|
|
|
|
log.Fatalf("There are multiple uses of the same name ‘%s’\n", name)
|
2022-05-22 09:25:22 +00:00
|
|
|
|
} else {
|
2022-06-08 13:45:27 +00:00
|
|
|
|
theMap.byName[name] = &wiki
|
2022-05-22 09:25:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.Printf("Loaded %d interwiki entries\n", len(theMap.list))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 19:55:00 +00:00
|
|
|
|
func HrefLinkFormatFor(prefix string) (string, options.InterwikiError) {
|
2022-05-28 17:05:22 +00:00
|
|
|
|
if wiki, ok := theMap.byName[prefix]; ok {
|
2022-06-04 19:55:00 +00:00
|
|
|
|
return wiki.LinkHrefFormat, options.Ok
|
2022-05-28 17:05:22 +00:00
|
|
|
|
}
|
2022-06-04 19:55:00 +00:00
|
|
|
|
return "", options.UnknownPrefix
|
2022-05-28 17:19:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 19:55:00 +00:00
|
|
|
|
func ImgSrcFormatFor(prefix string) (string, options.InterwikiError) {
|
2022-05-28 17:19:40 +00:00
|
|
|
|
if wiki, ok := theMap.byName[prefix]; ok {
|
2022-06-04 19:55:00 +00:00
|
|
|
|
return wiki.ImgSrcFormat, options.Ok
|
2022-05-28 17:19:40 +00:00
|
|
|
|
}
|
2022-06-04 19:55:00 +00:00
|
|
|
|
return "", options.UnknownPrefix
|
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
|
|
|
|
|
}
|