diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-12-04 08:36:02 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2023-12-05 06:07:41 +0100 |
commit | 1b1c3faabae530229eb675a2e70e744c2f45cbbe (patch) | |
tree | acc3e8eede9053fb5e639deeb553aa600c994598 /app/Issue/Tag.hs | |
parent | 3a76b6f0fc0c9c23000dd82870922c885c34ffa6 (diff) |
feat: add experimental render api
Diffstat (limited to 'app/Issue/Tag.hs')
-rw-r--r-- | app/Issue/Tag.hs | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/app/Issue/Tag.hs b/app/Issue/Tag.hs index ca550aa..b0d4d3c 100644 --- a/app/Issue/Tag.hs +++ b/app/Issue/Tag.hs @@ -4,15 +4,22 @@ module Issue.Tag tagKey, tagValue, tagValuesOf, + TagKey (..), + TagValue (..), ) where import CMark qualified as D import Data.Binary (Binary) -import Data.Maybe (catMaybes, mapMaybe) +import Data.List (intersperse) +import Data.Map qualified as M +import Data.Maybe (catMaybes, isJust, mapMaybe) +import Data.Set qualified as S import Data.Text (Text) import Data.Text qualified as T import GHC.Generics (Generic) +import Render ((<<<)) +import Render qualified as P data Tag = Tag Text (Maybe Text) deriving (Show, Generic, Binary, Eq) @@ -53,3 +60,48 @@ extractTags = collect . D.commonmarkToNode [] . T.words ) . T.lines + +instance P.Render Tag where + render tag + | isJust (tagValue tag) = TagKey tag <<< (" " :: T.Text) <<< TagValue tag + | otherwise = P.render (TagKey tag) + +newtype TagKey = TagKey {unTagKey :: Tag} + +instance P.Render TagKey where + render (TagKey tag) = + P.styled [P.bold, P.color P.Yellow] $ + P.render ("@" <> tagKey tag) + +newtype TagValue = TagValue {unTagValue :: Tag} + +instance P.Render TagValue where + render (TagValue tag) = + maybe P.emptyDoc (P.styled [P.color P.Yellow] . P.render) $ + tagValue tag + +instance P.Render [Tag] where + render tags = + P.vsep $ + map + ( \(tagKey, tagValues) -> + TagKey (Tag tagKey Nothing) + <<< (P.hcat . intersperse P.comma) + (map (P.render . TagValue) (map (Tag tagKey . Just) tagValues)) + ) + tagsAndValues + where + tagsAndValues :: [(T.Text, [T.Text])] + tagsAndValues = + (M.toList . M.map S.toList) + . foldl + ( flip + ( \tag -> + let vs = maybe S.empty S.singleton (tagValue tag) + in M.alter + (Just . maybe vs (S.union vs)) + (tagKey tag) + ) + ) + M.empty + $ tags |