1
0
Fork 0

Compare commits

..

No commits in common. "f25dd9c99382fd4e945e4d0d034254fc552d9aca" and "72cf946eb7ad1fb8151df3af3eb2c5a94a14af54" have entirely different histories.

4 changed files with 59 additions and 55 deletions

View File

@ -4,7 +4,6 @@
"Dhall",
"NOINLINE",
"runghc",
"txts",
"uncurry",
"unlines",
"utct"

View File

@ -1,7 +1,6 @@
{ sealImagesPath = "images"
, postsOutputPath = "posts"
{ sealImagesPath = "./images"
, postsOutputPath = "./posts"
, startDate = 2022-01-01
, seed = +137
, adjectives1 =
[ "absorbing"
, "adorable"

View File

@ -13,7 +13,8 @@ import Data.List (sort)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Data.Time.Calendar (Day, addDays, diffDays, toGregorian)
import Data.Time.Calendar (Day, addDays, diffDays, fromGregorian,
toGregorian)
import Data.Time.Clock (UTCTime (utctDay), getCurrentTime)
import Dhall (FromDhall, Generic, auto, input)
import NeatInterpolation (text)
@ -21,7 +22,7 @@ import NeatInterpolation (text)
import System.Directory (createDirectoryIfMissing, doesFileExist,
listDirectory)
import System.IO.Unsafe (unsafePerformIO)
import System.Random (Random (randoms), mkStdGen)
import System.Random (Random, randomRIO)
{-
@ -34,7 +35,6 @@ data Config = Config
, sealImagesPath :: FilePath
, postsOutputPath :: FilePath
, startDate :: Day
, seed :: Int
} deriving (Generic, Show)
instance FromDhall Config
@ -68,13 +68,8 @@ generateAllBlogPosts config = forM_ allBlogPosts' writeToFile'
allBlogPosts :: Config -> [(FilePath, Text)]
allBlogPosts config = map createSealText zippedDates
where
createSealText = uncurry3 $ sealText config
zippedDates = zip3 [1..] randomNumbers (allDatesSince (startDate config))
randomNumbers = randoms (mkStdGen (seed config))
-- | Converts a curried function to a function on a triple.
uncurry3 :: (a -> b -> c -> d) -> ((a, b, c) -> d)
uncurry3 f (a,b,c) = f a b c
createSealText = uncurry $ sealText config
zippedDates = zip [1..] (allDatesSince (startDate config))
allDatesSince :: Day -> [Day]
allDatesSince startDate = map (`addDays` startDate) [0..daysSinceStart]
@ -92,11 +87,39 @@ writeToFile config (fp, txt) = do
fp' = postsOutputPath config <> "/" <> fp
{-
Blog post format
-}
blogPost
:: Text
-> Text
-> Text
-> Text
-> Text
-> Day
-> FilePath
-> Text
blogPost title see adj1 adj2 seal date imagesPath =
let imagesPath' = T.pack imagesPath
in
[text|
---
title: $title
---
$see this $adj1, $adj2 seal!
<img
src="/$imagesPath'/$seal"
alt="A picture of a $adj1, $adj2 seal! <3"
width="400"
/>
|]
{-
Returns a filePath, and a corresponding random blog post
-}
sealText :: Config -> Integer -> Int -> Day -> (FilePath, Text)
sealText config n rand date = ( fileName', bp )
sealText :: Config -> Integer -> Day -> (FilePath, Text)
sealText config n date = ( fileName', bp )
where
fileName' =
show date <> "-"
@ -134,53 +157,36 @@ sealText config n rand date = ( fileName', bp )
blogPost' (_, _, _) =
blogPost
title
(randomPull rand (looks config))
(randomPull rand (adjectives1 config))
(randomPull rand (adjectives2 config))
(randomPull rand (unsafeListDirContents sealImagesPath'))
(randomPull $ looks config)
(randomPull $ adjectives1 config)
(randomPull $ adjectives2 config)
(randomPull $ unsafeListDirContents sealImagesPath')
date
sealImagesPath'
where
randomPull r txts = txts !! (rand `mod` l)
where l = length txts - 1
{-
Blog post format
-}
blogPost
:: Text
-> Text
-> Text
-> Text
-> Text
-> Day
-> FilePath
-> Text
blogPost title see adj1 adj2 seal date imagesPath =
let imagesPath' = T.pack imagesPath
in
[text|
---
title: $title
---
$see this $adj1, $adj2 seal!
<img
src="/$imagesPath'/$seal"
alt="A picture of a $adj1, $adj2 seal! <3"
width="400"
/>
|]
{-
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
Uses unsafeIO to get the number out of IO
It's safe because we're only shuffling
-}
randomNum :: Random a => a -> a -> a
randomNum from to = unsafePerformIO $ randomRIO (from, to)
unsafeListDirContents :: FilePath -> [Text]
unsafeListDirContents = map T.pack . drop 2 . sort . unsafePerformIO . listDirectory
-- ^^^^^^
-- drop 2 used to remove the birthday and singer photos
-- TODO: find a better way to hardcode this
unsafeListDirContents = map T.pack . sort . unsafePerformIO . listDirectory
prettyPrint :: Show a => [a] -> IO ()
prettyPrint = putStr . unlines . map show

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB