1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00

Interwiki: Start implementing

This commit is contained in:
Timur Ismagilov 2022-05-22 12:25:22 +03:00
parent feb53ec52b
commit ac1391e64a
5 changed files with 111 additions and 0 deletions

View File

@ -16,6 +16,7 @@ var paths struct {
tokensJSON string
userCredentialsJSON string
categoriesJSON string
interwikiJSON string
}
// HyphaeDir returns the path to hyphae storage.
@ -45,6 +46,8 @@ func CategoriesJSON() string { return paths.categoriesJSON }
// FileInRoot returns full path for the given filename if it was placed in the root of the wiki structure.
func FileInRoot(filename string) string { return filepath.Join(cfg.WikiDir, filename) }
func InterwikiJSON() string { return paths.interwikiJSON }
// PrepareWikiRoot ensures all needed directories and files exist and have
// correct permissions.
func PrepareWikiRoot() error {
@ -72,6 +75,7 @@ func PrepareWikiRoot() error {
paths.tokensJSON = filepath.Join(paths.cacheDir, "tokens.json")
paths.categoriesJSON = filepath.Join(cfg.WikiDir, "categories.json")
paths.interwikiJSON = FileInRoot("interwiki.json")
return nil
}

49
interwiki/interwiki.go Normal file
View File

@ -0,0 +1,49 @@
// 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 {
wiki.canonize()
theMap.list = append(theMap.list, &wiki)
for _, prefix := range wiki.Names {
if _, found := theMap.byName[prefix]; found {
log.Fatalf("There are multiple interwiki map entries having the same prefix %s\n", prefix)
} else {
theMap.byName[prefix] = &wiki
}
}
}
log.Printf("Loaded %d interwiki entries\n", len(theMap.list))
}
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
}

14
interwiki/map.go Normal file
View File

@ -0,0 +1,14 @@
package interwiki
// Map is an interwiki map
type Map struct {
list []*Wiki
byName map[string]*Wiki
}
var theMap Map
func init() {
theMap.list = []*Wiki{}
theMap.byName = map[string]*Wiki{}
}

43
interwiki/wiki.go Normal file
View File

@ -0,0 +1,43 @@
package interwiki
// WikiEngine is an enumeration of supported interwiki targets.
type WikiEngine int
const (
// Mycorrhiza is a Mycorrhiza wiki. This is the default value.
Mycorrhiza WikiEngine = iota
// Generic is any website.
Generic
)
// 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.
//
// 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.
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:"-"`
// EngineString is a string name of the engine. It is then converted to Engine. Supported values are:
// * mycorrhiza -> Mycorrhiza
// * generic -> Generic
// All other values will result in an error.
EngineString string `json:"engine"`
}
func (w *Wiki) canonize() {
}

View File

@ -51,6 +51,7 @@ func main() {
migration.MigrateHeadingsMaybe()
shroom.SetHeaderLinks()
categories.Init()
// interwiki.Init()
// Static files:
static.InitFS(files.StaticFiles())