59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
type OpenSearchDescription struct {
|
|
XMLName xml.Name `xml:"OpenSearchDescription"`
|
|
Xmlns string `xml:"xmlns,attr"`
|
|
ShortName string `xml:"ShortName"`
|
|
Description string `xml:"Description"`
|
|
Tags string `xml:"Tags"`
|
|
URLs []URL `xml:"Url"`
|
|
}
|
|
|
|
type URL struct {
|
|
Type string `xml:"type,attr"`
|
|
Template string `xml:"template,attr"`
|
|
}
|
|
|
|
func generateOpenSearchXML(config Config) {
|
|
// Ensure that language is initialized in `main` before calling this function
|
|
|
|
baseURL := addProtocol(config.Domain)
|
|
opensearch := OpenSearchDescription{
|
|
Xmlns: "http://a9.com/-/spec/opensearch/1.1/",
|
|
ShortName: Translate("site_name"),
|
|
Description: Translate("site_description"),
|
|
Tags: Translate("site_tags"),
|
|
URLs: []URL{
|
|
{
|
|
Type: "text/html",
|
|
Template: fmt.Sprintf("%s/search?q={searchTerms}", baseURL),
|
|
},
|
|
{
|
|
Type: "application/x-suggestions+json",
|
|
Template: fmt.Sprintf("%s/suggestions?q={searchTerms}", baseURL),
|
|
},
|
|
},
|
|
}
|
|
|
|
file, err := os.Create("static/opensearch.xml")
|
|
if err != nil {
|
|
printErr("Error creating OpenSearch file: %v", err)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
enc := xml.NewEncoder(file)
|
|
enc.Indent(" ", " ")
|
|
if err := enc.Encode(opensearch); err != nil {
|
|
printErr("Error encoding OpenSearch XML: %v", err)
|
|
return
|
|
}
|
|
|
|
printInfo("OpenSearch description file generated successfully.")
|
|
}
|