module FloraWeb.Server.OpenSearch where

import Data.Default (def)
import Data.Map qualified as Map
import Network.HTTP.Media qualified as M
import Servant.API
import Text.XML qualified as XML
import Text.XML.Writer as XML

data OpenSearchXML

instance Accept OpenSearchXML where
  contentType _ = "application" M.// "opensearchdescription+xml" M./: ("charset", "utf-8")

instance MimeRender OpenSearchXML XML.Document where
  mimeRender _ = XML.renderLBS def

openSearchHandler :: (Monad m) => m XML.Document
openSearchHandler =
  pure $
    openSearchDocument "OpenSearchDescription" $! do
      element "ShortName" $! content "Flora"
      element "Description" $! content "Search on Flora"
      element "InputEncoding" $! content "UTF-8"
      elementA "Url" [("type", "text/html"), ("method", "get"), ("template", "={searchTerms}&ref=opensearch")] empty
      element "moz:searchForm" $! content ""

openSearchDocument :: XML.Name -> XML.XML -> XML.Document
openSearchDocument name children =
  XML.Document
    { documentPrologue = XML.Prologue def def def
    , documentRoot = XML.Element name (Map.fromList [("xmlns", "/-/spec/opensearch/1.1/"), ("xmlns:moz", "/")]) (render children)
    , documentEpilogue = def
    }

module FloraWeb.Server.OpenSearch where

import Data.Default (def)
import Data.Map qualified as Map
import Network.HTTP.Media qualified as M
import Servant.API
import Text.XML qualified as XML
import Text.XML.Writer as XML

data OpenSearchXML

instance Accept OpenSearchXML where
  contentType _ = "application" M.// "opensearchdescription+xml" M./: ("charset", "utf-8")

instance MimeRender OpenSearchXML XML.Document where
  mimeRender _ = XML.renderLBS def

openSearchHandler :: (Monad m) => m XML.Document
openSearchHandler =
  pure $
    openSearchDocument "OpenSearchDescription" $! do
      element "ShortName" $! content "Flora"
      element "Description" $! content "Search on Flora"
      element "InputEncoding" $! content "UTF-8"
      elementA "Url" [("type", "text/html"), ("method", "get"), ("template", "={searchTerms}&ref=opensearch")] empty
      element "moz:searchForm" $! content ""

openSearchDocument :: XML.Name -> XML.XML -> XML.Document
openSearchDocument name children =
  XML.Document
    { documentPrologue = XML.Prologue def def def
    , documentRoot = XML.Element name (Map.fromList [("xmlns", "/-/spec/opensearch/1.1/"), ("xmlns:moz", "/")]) (render children)
    , documentEpilogue = def
    }
