summaryrefslogtreecommitdiff
path: root/src/Data/Abbr.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Abbr.hs')
-rw-r--r--src/Data/Abbr.hs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Data/Abbr.hs b/src/Data/Abbr.hs
new file mode 100644
index 0000000..f42134f
--- /dev/null
+++ b/src/Data/Abbr.hs
@@ -0,0 +1,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) 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