aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue/Tag.hs
blob: 96051cd3b09e87131fd54550181bdb84baf93760 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
module Issue.Tag
  ( Tag (..),
    extractTags,
    tagKey,
    tagValue,
    tagValuesOf,
    TagKey (..),
    TagValue (..),
  )
where

import CMark qualified as D
import Data.Binary (Binary)
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)

tagKey :: Tag -> Text
tagKey (Tag k _) = k

tagValue :: Tag -> Maybe Text
tagValue (Tag _ v) = v

tagValuesOf :: Text -> [Tag] -> [Text]
tagValuesOf key =
  mapMaybe
    ( \tag ->
        if tagKey tag == key
          then tagValue tag
          else Nothing
    )

extractTags :: [D.Node] -> [Tag]
extractTags = concatMap collect
  where
    collect (D.Node _ (D.CODE _) _) = []
    collect (D.Node _ (D.CODE_BLOCK _ _) _) = []
    collect (D.Node _ (D.TEXT s) ns) = extract s ++ concatMap collect ns
    collect (D.Node _ _ []) = []
    collect (D.Node _ _ ns) = concatMap collect ns

    extract =
      catMaybes
        . map
          ( ( \case
                ((T.uncons -> Just ('@', k)) : v) ->
                  case T.strip (T.unwords v) of
                    "" -> Just (Tag k Nothing)
                    v' -> Just (Tag k (Just v'))
                _ -> Nothing
            )
              . 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