blob: 8ee4bc09b6240a2d2a050192197afd21180c4d12 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
module Data.Abbr
( Abbr (..)
, Abbr_
, abbrHtml
) where
import Prolog
import Network.URI
import Data.Aeson
import Data.Alias
import Data.Set qualified as S
import Text.Blaze
import Text.Blaze.Html
import Data.Linked
data Abbr a = Abbr
{ name :: !a
, shortName :: !(Maybe a)
, link :: !(Maybe URI)
} deriving (Eq, Foldable, Functor, Generic, Ord, Show, Traversable)
instance Binary a => Binary (Abbr a)
type Abbr_ = Abbr StrictText
abbrHtml :: ToMarkup a => Abbr a -> Html
abbrHtml (Abbr n sn l) = linkedHtml $ Linked (fromMaybe n sn) l (n <$ sn)
instance Ord a => ToAliases (Abbr a) a where
toAliases (Abbr t st _) = S.fromList $ t : toList st
instance FromAlias a (Abbr a) where
fromAlias t = pure $ Abbr t Nothing Nothing
instance FromJSON a => FromJSON (Abbr a) where
parseJSON = withObject "Abbr" $ \v -> Abbr
<$> v .: "name"
<*> v .:? "short" .!= Nothing
<*> v .:? "link" .!= Nothing
|