1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 19:56:16 +00:00
mycorrhiza/interwiki/interwiki.go
Timur Ismagilov 787882cb80 Mycomarkup: Update to v4.3.2
All things interwiki

Backlinks from img and rockets are temporarily broken until all other blocks gain support of interwiki.
2022-06-05 15:35:40 +03:00

66 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package interwiki provides interwiki capabilities. Most of them, at least.
package interwiki
import (
"encoding/json"
"github.com/bouncepaw/mycomarkup/v4/options"
"github.com/bouncepaw/mycorrhiza/files"
"log"
"os"
)
func Init() {
var (
record, err = readInterwiki()
)
if err != nil {
log.Fatalln(err)
}
for _, wiki := range record {
wiki := wiki // This line is required
wiki.canonize()
theMap.list = append(theMap.list, &wiki)
for _, prefix := range wiki.Names {
if _, found := theMap.byName[prefix]; found {
log.Fatalf("There are multiple uses of the same prefix %s\n", prefix)
} else {
theMap.byName[prefix] = &wiki
}
}
}
log.Printf("Loaded %d interwiki entries\n", len(theMap.list))
}
func HrefLinkFormatFor(prefix string) (string, options.InterwikiError) {
if wiki, ok := theMap.byName[prefix]; ok {
return wiki.LinkHrefFormat, options.Ok
}
return "", options.UnknownPrefix
}
func ImgSrcFormatFor(prefix string) (string, options.InterwikiError) {
if wiki, ok := theMap.byName[prefix]; ok {
return wiki.ImgSrcFormat, options.Ok
}
return "", options.UnknownPrefix
}
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
}