2021-06-09 20:44:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-02-12 21:29:45 +00:00
|
|
|
"encoding/xml"
|
2023-01-24 13:30:53 +00:00
|
|
|
"io"
|
2021-06-09 20:44:22 +00:00
|
|
|
"net/http"
|
2021-06-18 12:32:03 +00:00
|
|
|
|
2021-06-28 20:17:18 +00:00
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
2021-06-09 20:44:22 +00:00
|
|
|
)
|
|
|
|
|
2022-02-12 21:29:45 +00:00
|
|
|
type openSearchDescription struct {
|
|
|
|
XMLName xml.Name `xml:"http://a9.com/-/spec/opensearch/1.1/ OpenSearchDescription"`
|
|
|
|
Text string `xml:",chardata"`
|
|
|
|
ShortName string `xml:"ShortName"`
|
|
|
|
Description string `xml:"Description"`
|
|
|
|
URL *openSearchDescriptionUrl `xml:"Url"`
|
|
|
|
SearchForm string `xml:"http://www.mozilla.org/2006/browser/search/ SearchForm"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type openSearchDescriptionUrl struct {
|
|
|
|
Text string `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Method string `xml:"method,attr"`
|
|
|
|
Template string `xml:"template,attr"`
|
|
|
|
Param *openSearchDescriptionUrlParam `xml:"Param"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type openSearchDescriptionUrlParam struct {
|
|
|
|
Text string `xml:",chardata"`
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Value string `xml:"value,attr"`
|
|
|
|
}
|
|
|
|
|
2021-06-09 20:44:22 +00:00
|
|
|
func (a *goBlog) serveOpenSearch(w http.ResponseWriter, r *http.Request) {
|
2021-11-04 07:52:16 +00:00
|
|
|
_, b := a.getBlog(r)
|
2021-08-05 12:53:22 +00:00
|
|
|
title := a.renderMdTitle(b.Title)
|
2021-07-12 14:19:28 +00:00
|
|
|
sURL := a.getFullAddress(b.getRelativePath(defaultIfEmpty(b.Search.Path, defaultSearchPath)))
|
2022-02-12 21:29:45 +00:00
|
|
|
openSearch := &openSearchDescription{
|
|
|
|
ShortName: title,
|
|
|
|
Description: title,
|
|
|
|
URL: &openSearchDescriptionUrl{
|
|
|
|
Type: "text/html",
|
|
|
|
Method: "post",
|
|
|
|
Template: sURL,
|
|
|
|
Param: &openSearchDescriptionUrlParam{
|
|
|
|
Name: "q",
|
|
|
|
Value: "{searchTerms}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SearchForm: sURL,
|
|
|
|
}
|
2023-01-24 13:30:53 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
_, _ = io.WriteString(pw, xml.Header)
|
|
|
|
_ = pw.CloseWithError(xml.NewEncoder(pw).Encode(openSearch))
|
|
|
|
}()
|
2022-04-10 09:46:35 +00:00
|
|
|
w.Header().Set(contentType, "application/opensearchdescription+xml"+contenttype.CharsetUtf8Suffix)
|
2023-01-24 13:30:53 +00:00
|
|
|
_ = pr.CloseWithError(a.min.Get().Minify(contenttype.XML, w, pr))
|
2021-06-09 20:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func openSearchUrl(b *configBlog) string {
|
|
|
|
if b.Search != nil && b.Search.Enabled {
|
2021-07-12 14:19:28 +00:00
|
|
|
return b.getRelativePath(defaultIfEmpty(b.Search.Path, defaultSearchPath) + "/opensearch.xml")
|
2021-06-09 20:44:22 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|