1
0
Fork 0

Code cleanup; name changes and hiding functions

main
Bill Ewanick 2023-05-08 17:38:50 -04:00
parent b9a1657171
commit e6e53be35b
1 changed files with 32 additions and 30 deletions

View File

@ -1,9 +1,7 @@
#! /usr/bin/env nix-shell #! /usr/bin/env nix-shell
#! nix-shell -p "haskellPackages.ghcWithPackages (ps: with ps; [dhall neat-interpolation random])" #! nix-shell -p "haskellPackages.ghcWithPackages (ps: with ps; [dhall neat-interpolation random])"
#! nix-shell -i runghc #! nix-shell -i runghc
{-
Must be run in folder containing the source code
-}
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}
@ -53,7 +51,7 @@ main = do
putStrLn "Validated config successfully" putStrLn "Validated config successfully"
createDirectoryIfMissing True postsOutputPath' createDirectoryIfMissing True postsOutputPath'
unsafeGenerateAllBlogs config generateAllBlogPosts config
putStrLn $ "Generated blog posts successfully to " <> postsOutputPath' putStrLn $ "Generated blog posts successfully to " <> postsOutputPath'
@ -61,14 +59,16 @@ main = do
For all the blog posts For all the blog posts
Write them to file Write them to file
-} -}
unsafeGenerateAllBlogs :: Config -> IO () generateAllBlogPosts :: Config -> IO ()
unsafeGenerateAllBlogs config = generateAllBlogPosts config = forM_ allBlogPosts' writeToFile'
forM_ (allBlogPosts config) (writeToFile config) where
allBlogPosts' = allBlogPosts config
writeToFile' = writeToFile config
allBlogPosts :: Config -> [(FilePath, Text)] allBlogPosts :: Config -> [(FilePath, Text)]
allBlogPosts config = map f zippedDates allBlogPosts config = map createSealText zippedDates
where where
f = uncurry $ sealText config createSealText = uncurry $ sealText config
zippedDates = zip [1..] (allDatesSince (startDate config)) zippedDates = zip [1..] (allDatesSince (startDate config))
allDatesSince :: Day -> [Day] allDatesSince :: Day -> [Day]
@ -129,6 +129,7 @@ sealText config n date = ( fileName', bp)
date' = T.pack . show $ date date' = T.pack . show $ date
title = T.pack $ "Seal Post Number " <> show n title = T.pack $ "Seal Post Number " <> show n
title' = T.replace " " "-" title title' = T.replace " " "-" title
sealImagesPath' = sealImagesPath config
bp = blogPost' (toGregorian date) bp = blogPost' (toGregorian date)
@ -141,7 +142,7 @@ sealText config n date = ( fileName', bp)
"older" "older"
"birthdaySeal.jpg" "birthdaySeal.jpg"
date date
(sealImagesPath config) sealImagesPath'
blogPost' (_, 04, 01) = blogPost' (_, 04, 01) =
blogPost blogPost
@ -151,7 +152,7 @@ sealText config n date = ( fileName', bp)
"supermodel, singing" "supermodel, singing"
"singerSeal.jpg" "singerSeal.jpg"
date date
(sealImagesPath config) sealImagesPath'
blogPost' (_, _, _) = blogPost' (_, _, _) =
blogPost blogPost
@ -159,14 +160,23 @@ sealText config n date = ( fileName', bp)
(randomPull $ looks config) (randomPull $ looks config)
(randomPull $ adjectives1 config) (randomPull $ adjectives1 config)
(randomPull $ adjectives2 config) (randomPull $ adjectives2 config)
(randomPull $ unsafeListDirContents $ sealImagesPath config) (randomPull $ unsafeListDirContents sealImagesPath')
date date
(sealImagesPath config) sealImagesPath'
{- {-
Utils Utils
-} -}
{-
Given a list, returns a random element
-}
randomPull :: [a] -> a
randomPull lst = lst !! r'
where
r' = randomNum 0 l
l = length lst - 1
{- {-
Gives a random number between from and to Gives a random number between from and to
Uses unsafeIO to get the number out of IO Uses unsafeIO to get the number out of IO
@ -175,14 +185,6 @@ sealText config n date = ( fileName', bp)
randomNum :: Random a => a -> a -> a randomNum :: Random a => a -> a -> a
randomNum from to = unsafePerformIO $ randomRIO (from, to) randomNum from to = unsafePerformIO $ randomRIO (from, to)
{-
Given a list, returns a random element
-}
randomPull :: [a] -> a
randomPull lst = lst !! r'
where r' = randomNum 0 l
l = length lst - 1
unsafeListDirContents :: FilePath -> [Text] unsafeListDirContents :: FilePath -> [Text]
unsafeListDirContents = map T.pack . sort . unsafePerformIO . listDirectory unsafeListDirContents = map T.pack . sort . unsafePerformIO . listDirectory