module Issue.Tag (Tag (..), extractTags, internalTags) 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 Text deriving (Show, Generic, Binary) extractTags :: Text -> [Tag] extractTags = catMaybes . map ( ( \case ((T.uncons -> Just ('@', k)) : v) -> Just (Tag k (T.unwords v)) _ -> Nothing ) . T.words ) . T.lines internalTags :: Text -> Maybe Provenance -> [Tag] internalTags title provenance' = concat [ [ Tag "id" (toSpinalCase title) ], maybe [] ( \provenance -> [ Tag "createdAt" (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']]))