Search/open-search.go

83 lines
2.1 KiB
Go
Raw Normal View History

2024-06-12 14:26:50 +02:00
package main
import (
"encoding/xml"
"fmt"
"os"
)
type OpenSearchDescription struct {
2025-06-18 13:55:13 +02:00
XMLName xml.Name `xml:"OpenSearchDescription"`
Xmlns string `xml:"xmlns,attr"`
ShortName string `xml:"ShortName"`
LongName string `xml:"LongName"`
Description string `xml:"Description"`
Tags string `xml:"Tags,omitempty"`
InputEncoding string `xml:"InputEncoding"`
OutputEncoding string `xml:"OutputEncoding"`
Image *Image `xml:"Image,omitempty"`
URLs []URL `xml:"Url"`
2024-06-12 14:26:50 +02:00
}
type URL struct {
Type string `xml:"type,attr"`
2025-06-18 13:55:13 +02:00
Method string `xml:"method,attr,omitempty"`
2024-06-12 14:26:50 +02:00
Template string `xml:"template,attr"`
}
2025-06-18 13:55:13 +02:00
type Image struct {
Height int `xml:"height,attr"`
Width int `xml:"width,attr"`
Type string `xml:"type,attr"`
URL string `xml:",chardata"`
}
2024-08-08 15:05:05 +02:00
2025-06-18 13:55:13 +02:00
func generateOpenSearchXML(config Config) {
2024-10-25 13:54:29 +02:00
baseURL := addProtocol(config.Domain)
2025-06-18 13:55:13 +02:00
2024-06-12 14:26:50 +02:00
opensearch := OpenSearchDescription{
2025-06-18 13:55:13 +02:00
Xmlns: "http://a9.com/-/spec/opensearch/1.1/",
ShortName: Translate("site_name"),
LongName: Translate("site_name") + " Search",
Description: Translate("site_description"),
Tags: Translate("site_tags"),
InputEncoding: "UTF-8",
OutputEncoding: "UTF-8",
Image: &Image{
Height: 16,
Width: 16,
Type: "image/x-icon",
URL: fmt.Sprintf("%s/favicon.ico", baseURL),
},
2024-08-21 22:56:21 +02:00
URLs: []URL{
{
Type: "text/html",
2025-06-18 13:55:13 +02:00
Method: "get",
2024-08-21 22:56:21 +02:00
Template: fmt.Sprintf("%s/search?q={searchTerms}", baseURL),
},
{
Type: "application/x-suggestions+json",
Template: fmt.Sprintf("%s/suggestions?q={searchTerms}", baseURL),
},
2024-06-12 14:26:50 +02:00
},
}
file, err := os.Create("static/opensearch.xml")
if err != nil {
2024-08-10 13:27:23 +02:00
printErr("Error creating OpenSearch file: %v", err)
2024-06-12 14:26:50 +02:00
return
}
defer file.Close()
2025-06-18 13:55:13 +02:00
file.WriteString(`<?xml version="1.0" encoding="UTF-8"?>` + "\n")
2024-06-12 14:26:50 +02:00
enc := xml.NewEncoder(file)
enc.Indent(" ", " ")
if err := enc.Encode(opensearch); err != nil {
2024-08-10 13:27:23 +02:00
printErr("Error encoding OpenSearch XML: %v", err)
2024-06-12 14:26:50 +02:00
return
}
2024-08-10 13:27:23 +02:00
printInfo("OpenSearch description file generated successfully.")
2024-06-12 14:26:50 +02:00
}