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']]))