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

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

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 :: 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 -> [T.Text] -> [Tag]
internalTags title provenance' markers =
  concat
    [ [ Tag "id" $ Just $ toSpinalCase title
      ],
      maybe
        []
        ( \provenance ->
            [ Tag "createdAt" $ Just $ pack $ show $ utctDay provenance.first.date,
              Tag "modifiedAt" $ Just $ pack $ show $ utctDay provenance.last.date,
              Tag "author" $ Just $ provenance.first.author.name,
              Tag "editor" $ Just $ provenance.last.author.name
            ]
        )
        provenance',
      map (Tag "type" . Just) markers
    ]

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