aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue/Tag.hs
blob: 85636b5c7b106c32ddbf8e2350224284aaf76dc3 (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
module Issue.Tag
  ( Tag (..),
    extractTags,
    internalTags,
    tagKey,
    tagValue,
  )
where

import Data.Binary (Binary)
import Data.Maybe (catMaybes)
import Data.Text (Text, pack)
import Data.Text qualified as T
import Data.Time.Clock (UTCTime (utctDay))
import GHC.Generics (Generic)
import Issue.Provenance (Provenance (..))

data Tag = Tag Text (Maybe Text) deriving (Show, Generic, Binary)

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

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

extractTags :: Text -> [Tag]
extractTags =
  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

internalTags :: Text -> Maybe Provenance -> [Tag]
internalTags title provenance' =
  concat
    [ [ Tag "id" $ Just $ toSpinalCase title
      ],
      maybe
        []
        ( \provenance ->
            [ Tag "createdAt" $ Just $ pack $ show $ utctDay provenance.date
            ]
        )
        provenance'
    ]

toSpinalCase :: Text -> Text
toSpinalCase = T.replace " " "-" . T.filter keep . T.toLower
  where
    keep = (`elem` (concat [[' ', '-'], ['a' .. 'z'], ['0' .. '9']]))