From 4d710afae3ebb80fef00d134e741c69aef5fef8c Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Wed, 2 Aug 2023 18:26:08 -0400 Subject: [PATCH 01/10] Update versions --- flake.lock | 14 +++++++------- flake.nix | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/flake.lock b/flake.lock index 4e6b285..dd13566 100644 --- a/flake.lock +++ b/flake.lock @@ -2,16 +2,16 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1681482634, - "narHash": "sha256-cT/nr3L8khEYZSGp8qqwxFH+/q4/547MfyOdSj6MhBk=", + "lastModified": 1690927903, + "narHash": "sha256-D5gCaCROnjEKDOel//8TO/pOP87pAEtT0uT8X+0Bj/U=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "fda0d99c2cbbb5c89d8855d258cb0821bd9113ad", + "rev": "bd836ac5e5a7358dea73cb74a013ca32864ccb86", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixos-22.11", + "ref": "nixos-23.05", "repo": "nixpkgs", "type": "github" } @@ -42,11 +42,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "lastModified": 1689068808, + "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=", "owner": "numtide", "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 019212b..f65712b 100644 --- a/flake.nix +++ b/flake.nix @@ -2,7 +2,7 @@ description = "A Nix flake for a blog-post generating script, written in Haskell, with a seal bent."; inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05"; utils.url = "github:numtide/flake-utils"; }; From 73235cf0ab75d9823db99210ba8bdaf426e29714 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Wed, 2 Aug 2023 18:32:04 -0400 Subject: [PATCH 02/10] Move build script to Nix derivation --- .gitignore | 3 ++- flake.nix | 15 ++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index f2a83d1..804bcaf 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,9 @@ dist generateSealPosts -# Direnv lorri stuff +# Nix related .direnv +result # Too many posts, hiding for now posts \ No newline at end of file diff --git a/flake.nix b/flake.nix index f65712b..91f732c 100644 --- a/flake.nix +++ b/flake.nix @@ -18,18 +18,23 @@ ]); in { + packages.default = pkgs.runCommand "generateSealPosts" { } '' + echo Generating seal posts + mkdir -p $out/bin + ${ghc'}/bin/ghc \ + -O2 \ + -static \ + -o $out/bin/generateSealPosts \ + ${./generateSealPosts.hs} + ''; + devShells.default = pkgs.mkShell { name = "seal-generator-shell"; - buildInputs = with pkgs.haskellPackages; [ ghc' hlint haskell-language-server - - (pkgs.writeShellScriptBin "build-seal-generator" '' - ${ghc'}/bin/ghc -outputdir dist -O2 -static generateSealPosts.hs - '') ]; }; }); From 46b00b50e96ddfdd4974b0414f7556c8ccdce8e7 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Sun, 6 Aug 2023 21:32:51 -0400 Subject: [PATCH 03/10] Update README --- README.md | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 23deda8..92b6ff3 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,13 @@ It's an in-joke. ## What's all this code? -- generate/ - - generateSealPosts.hs - - A Haskell script that checks the website/posts folder and creates a blog post for every day from 1998 until the current date. There you will find the adjective lists if you think of more words to describe seals. -- dist/ - - Place for the Haskell build artifacts to go. +- [config.dhall](./config.dhall) + - Typed non-deterministic config file for modifying the generator. +- [flake.nix](./flake.nix) + - Entry point for all the nix ways of running and building the code. +- [generateSealPosts.hs](./generateSealPosts.hs) + - A Haskell script that creates a blog post for every day from the configured date until the current day. ## These seals need more adjectives -In [config.dhall](https://git.ewanick.com/bill/sealPostGenerator/src/branch/main/config.dhall) you'll find two lists of adjectives. For the repo and add your own! Editing dhall config files does not require recompilation of the Haskell executable! - -## Recent work - -- Should running the generate function replace existing posts, or not? Probably not. - - But need to split out the archive build vs the incremental post creation. +In [config.dhall](./config.dhall) you'll find two lists of adjectives. For the repo and add your own! Editing dhall config files does not require recompilation of the Haskell executable! From 72cf946eb7ad1fb8151df3af3eb2c5a94a14af54 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Mon, 7 Aug 2023 17:43:26 -0400 Subject: [PATCH 04/10] Fix image link issues --- generateSealPosts.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generateSealPosts.hs b/generateSealPosts.hs index c01fe7e..aa81ba0 100755 --- a/generateSealPosts.hs +++ b/generateSealPosts.hs @@ -109,7 +109,7 @@ blogPost title see adj1 adj2 seal date imagesPath = $see this $adj1, $adj2 seal! A picture of a $adj1, $adj2 seal! <3 From 615f2ff4462f316e5d6f243dfa75e59f43c838f4 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Tue, 8 Aug 2023 17:18:22 -0400 Subject: [PATCH 05/10] Rename Seal for drop 2 to work Don't want to use birthday/april fools photos randomly --- generateSealPosts.hs | 5 ++++- images/{singerSeal.jpg => _singerSeal.jpg} | Bin 2 files changed, 4 insertions(+), 1 deletion(-) rename images/{singerSeal.jpg => _singerSeal.jpg} (100%) diff --git a/generateSealPosts.hs b/generateSealPosts.hs index aa81ba0..f6a974e 100755 --- a/generateSealPosts.hs +++ b/generateSealPosts.hs @@ -186,7 +186,10 @@ randomPull lst = lst !! r' randomNum from to = unsafePerformIO $ randomRIO (from, to) unsafeListDirContents :: FilePath -> [Text] -unsafeListDirContents = map T.pack . sort . unsafePerformIO . listDirectory +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 prettyPrint :: Show a => [a] -> IO () prettyPrint = putStr . unlines . map show diff --git a/images/singerSeal.jpg b/images/_singerSeal.jpg similarity index 100% rename from images/singerSeal.jpg rename to images/_singerSeal.jpg From f25dd9c99382fd4e945e4d0d034254fc552d9aca Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Tue, 8 Aug 2023 17:26:50 -0400 Subject: [PATCH 06/10] Change up how randomness works Introduce a seed fed into `randoms (mkStdGen (seed config)` Some light formatting --- .vscode/settings.json | 1 + config.dhall | 5 +- generateSealPosts.hs | 103 +++++++++++++++++++----------------------- 3 files changed, 51 insertions(+), 58 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 727200a..3580a6d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "Dhall", "NOINLINE", "runghc", + "txts", "uncurry", "unlines", "utct" diff --git a/config.dhall b/config.dhall index 4753bbe..e19d969 100644 --- a/config.dhall +++ b/config.dhall @@ -1,6 +1,7 @@ -{ sealImagesPath = "./images" -, postsOutputPath = "./posts" +{ sealImagesPath = "images" +, postsOutputPath = "posts" , startDate = 2022-01-01 +, seed = +137 , adjectives1 = [ "absorbing" , "adorable" diff --git a/generateSealPosts.hs b/generateSealPosts.hs index f6a974e..fca41c2 100755 --- a/generateSealPosts.hs +++ b/generateSealPosts.hs @@ -13,8 +13,7 @@ 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, fromGregorian, - toGregorian) +import Data.Time.Calendar (Day, addDays, diffDays, toGregorian) import Data.Time.Clock (UTCTime (utctDay), getCurrentTime) import Dhall (FromDhall, Generic, auto, input) import NeatInterpolation (text) @@ -22,7 +21,7 @@ import NeatInterpolation (text) import System.Directory (createDirectoryIfMissing, doesFileExist, listDirectory) import System.IO.Unsafe (unsafePerformIO) -import System.Random (Random, randomRIO) +import System.Random (Random (randoms), mkStdGen) {- @@ -35,6 +34,7 @@ data Config = Config , sealImagesPath :: FilePath , postsOutputPath :: FilePath , startDate :: Day + , seed :: Int } deriving (Generic, Show) instance FromDhall Config @@ -68,8 +68,13 @@ generateAllBlogPosts config = forM_ allBlogPosts' writeToFile' allBlogPosts :: Config -> [(FilePath, Text)] allBlogPosts config = map createSealText zippedDates where - createSealText = uncurry $ sealText config - zippedDates = zip [1..] (allDatesSince (startDate config)) + 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 allDatesSince :: Day -> [Day] allDatesSince startDate = map (`addDays` startDate) [0..daysSinceStart] @@ -87,39 +92,11 @@ 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! - A picture of a $adj1, $adj2 seal! <3 - |] - {- Returns a filePath, and a corresponding random blog post -} -sealText :: Config -> Integer -> Day -> (FilePath, Text) -sealText config n date = ( fileName', bp ) +sealText :: Config -> Integer -> Int -> Day -> (FilePath, Text) +sealText config n rand date = ( fileName', bp ) where fileName' = show date <> "-" @@ -157,34 +134,48 @@ sealText config n date = ( fileName', bp ) blogPost' (_, _, _) = blogPost title - (randomPull $ looks config) - (randomPull $ adjectives1 config) - (randomPull $ adjectives2 config) - (randomPull $ unsafeListDirContents sealImagesPath') + (randomPull rand (looks config)) + (randomPull rand (adjectives1 config)) + (randomPull rand (adjectives2 config)) + (randomPull rand (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! + A picture of a $adj1, $adj2 seal! <3 + |] {- 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 -- ^^^^^^ From f4abf812ada6adf9d6371e424bcacfcda20e8f26 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Tue, 26 Sep 2023 09:46:01 -0400 Subject: [PATCH 07/10] Fix broken link issue --- generateSealPosts.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generateSealPosts.hs b/generateSealPosts.hs index fca41c2..605816e 100755 --- a/generateSealPosts.hs +++ b/generateSealPosts.hs @@ -127,7 +127,7 @@ sealText config n rand date = ( fileName', bp ) "Foolishly look at" "beautiful" "supermodel, singing" - "singerSeal.jpg" + "_singerSeal.jpg" date sealImagesPath' From 032daf33e48748e0c55433a4c4e00d49975c4d6c Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Mon, 15 Jan 2024 18:16:16 -0500 Subject: [PATCH 08/10] Add dhall-lsp-server to IDE --- flake.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flake.nix b/flake.nix index 91f732c..d370e72 100644 --- a/flake.nix +++ b/flake.nix @@ -35,6 +35,8 @@ ghc' hlint haskell-language-server + + dhall-lsp-server ]; }; }); From 92bd2d5deef4f6b2fead2b1e0bddd2dc0f5c2efa Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Mon, 15 Jan 2024 18:16:34 -0500 Subject: [PATCH 09/10] Tribute to Dad --- config.dhall | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.dhall b/config.dhall index e19d969..82143b9 100644 --- a/config.dhall +++ b/config.dhall @@ -1,7 +1,7 @@ { sealImagesPath = "images" , postsOutputPath = "posts" -, startDate = 2022-01-01 -, seed = +137 +, startDate = 1959-01-15 +, seed = +2016 , adjectives1 = [ "absorbing" , "adorable" From 296a5b56e00cfbf0c44133da5bab28404ddf8ea0 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Mon, 15 Jan 2024 18:16:56 -0500 Subject: [PATCH 10/10] Add fileExtension config for md files --- config.dhall | 1 + generateSealPosts.hs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/config.dhall b/config.dhall index 82143b9..cbddeb2 100644 --- a/config.dhall +++ b/config.dhall @@ -2,6 +2,7 @@ , postsOutputPath = "posts" , startDate = 1959-01-15 , seed = +2016 +, fileExtension = "md" , adjectives1 = [ "absorbing" , "adorable" diff --git a/generateSealPosts.hs b/generateSealPosts.hs index 605816e..ccd1560 100755 --- a/generateSealPosts.hs +++ b/generateSealPosts.hs @@ -30,6 +30,7 @@ import System.Random (Random (randoms), mkStdGen) data Config = Config { adjectives1 :: [Text] , adjectives2 :: [Text] + , fileExtension :: String , looks :: [Text] , sealImagesPath :: FilePath , postsOutputPath :: FilePath @@ -102,11 +103,12 @@ sealText config n rand date = ( fileName', bp ) show date <> "-" <> "seal-post-" <> show n - <> ".markdown" + <> "." <> fileExtension' date' = T.pack . show $ date title = T.pack $ "Seal Post Number " <> show n title' = T.replace " " "-" title sealImagesPath' = sealImagesPath config + fileExtension' = fileExtension config bp = blogPost' (toGregorian date)