blog/blog-meta/nice-date.hs

22 lines
775 B
Haskell
Raw Normal View History

2021-07-23 21:35:02 -04:00
import Text.Pandoc.Definition
import qualified Data.Map.Strict as Map
import Data.Time
import Text.Pandoc.JSON
import Data.Text (unpack, pack)
2021-07-23 21:35:02 -04:00
import Data.Char (toLower)
import Misc
2021-07-23 21:35:02 -04:00
main :: IO ()
main = toJSONFilter \(Pandoc (Meta m) body) -> do
m' <- Map.alterF reformat "date" m
2021-07-23 21:35:02 -04:00
pure $ Pandoc (Meta m') body
reformat :: Maybe MetaValue -> IO (Maybe MetaValue)
reformat Nothing = pure Nothing
reformat (Just (toText -> Just txt)) = do
-- extra '-'s in %-m and %-d to allow leading zeroes to be skipped
date <- parseTimeM True defaultTimeLocale "%Y-%-m-%-d" $ unpack txt
let str = formatTime defaultTimeLocale "%A %-e %B %Y" (date :: Day)
pure $ Just $ MetaString $ pack $ map toLower str
reformat (Just d) = fail $ "date is\n" <> show d <> "\nwanted a string"