Generate the site index
authorCameron Ball <cameron@cameron1729.xyz>
Fri, 15 Feb 2019 18:22:53 +0000 (02:22 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Fri, 15 Feb 2019 18:27:51 +0000 (02:27 +0800)
src/Lib.hs
templates/index.html [new file with mode: 0644]

index d502725..80cf44f 100644 (file)
@@ -48,6 +48,9 @@ entryListToPaths el = concat $ concat $ map (\(year, months)
                                                        "/" `append`
                                                        month) entries) months) el
 
+entryListToIndex :: EntryList -> Text
+entryListToIndex el = unorderedList Nothing $ map (\(year, monthList) -> unorderedList (Just year) $ map (\(month, entryList) -> unorderedList (Just month) $ map (\x -> "<a href=\"" <> year <> "/" <> month <> "/" <> ((slugify . title . snd) x) <>  ".html\">" <> (title . snd) x <> "</a>") entryList) monthList) el
+
 i2t :: Integer -> Text
 i2t = (fromString . show)
 
@@ -62,19 +65,25 @@ entryToMarkup entry template = toStrict $ render template tmplContext
         Nothing -> []
         Just a -> [("video", a)]
 
-wrapInTags tag text = intercalate "" $ map ((`append` close) . (open `append`)) text where
-  close = "</" `append` tag `append` ">"
-  open = "<" `append` tag `append` ">"
+wrapInTags :: Text -> [Text] -> Text
+wrapInTags tag = foldMap $ (<> close) . (open <>)
+  where  close = "</" `append` tag `append` ">"
+         open = "<" `append` tag `append` ">"
 
 paragraphs = wrapInTags "p"
 
+unorderedList :: Maybe Text -> [Text] -> Text
+unorderedList heading items = case heading of
+  Just x -> x `append` unorderedList Nothing items
+  Nothing -> "<ul>" `append` (wrapInTags "li" items) `append` "</ul>"
+
 rawTextToEntry rawEntry = BlogEntry (posixSecondsToUTCTime (fromInteger (read (unpack $ meta !! 0)))) (meta !! 1) (paragraphs (snd (splitAt 1 fileLines))) (lookup 2 metaMap)
   where
     metaMap = zip [0..] meta
     meta = splitOn "::" (fileLines !! 0)
     fileLines = lines rawEntry
 
-allFilesIn dir = filter (/= "..")<$>(filter(/= "."))<$>(getDirectoryContents dir)
+allFilesIn dir = filter (/= "..")<$>(filter(/= "."))<$>(filter(/= ".gitignore"))<$>(getDirectoryContents dir)
 
 exportEntry entry = do
   rawTemplateNormal <- readFile "templates/entry.html"
@@ -90,14 +99,23 @@ exportEntry entry = do
                 format (postDate entry) "%B",
                 slugify (title entry) `append` ".html"]) $ unpack $ entryToMarkup entry entryTemplate
 
+exportIndex :: EntryList -> IO()
+exportIndex entryList = do
+  rawTemplate <- readFile "templates/index.html"
+
+  let indexTemplate = template . pack $ rawTemplate
+  let indexContext = context [("index", entryListToIndex entryList)]
+
+  writeFile "build/index.html" $ unpack $ toStrict $ render indexTemplate indexContext
+
 someFunc = do
-  files <- allFilesIn "./entries"
+  files <- allFilesIn "entries"
   rawEntries <- mapM (\x -> (readFile ("entries/" ++ x)) >>= (\y -> (return . fromString) y)) files
   entryList <- mapM (\raw -> return $ rawTextToEntry raw) rawEntries
   entryPaths <- return $ entryListToPaths $ stampsToEntryList entryList
-  mapM_ (putStrLn . unpack)  entryPaths
   mapM_ (createDirectoryIfMissing True . ("build/" ++) . unpack) entryPaths
   mapM_ exportEntry entryList
+  exportIndex $ stampsToEntryList entryList
 
 context :: [(Text, Text)] -> Context
 context assocs x = case lookup x $ assocs of
diff --git a/templates/index.html b/templates/index.html
new file mode 100644 (file)
index 0000000..6a9d20c
--- /dev/null
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="utf-8">
+        <title>blog@cameron1729</title>
+        <style>
+         body, html {
+             width: 100%;
+             height: 100%;
+             padding: 0px;
+             margin: 0px;
+         }
+         body {
+             background-color: #BCBCBC;
+             font-family: helvetica;
+             font-weight: bold;
+         }
+         a {
+             color: #666666;
+             text-decoration: none;
+         }
+         a:hover {
+             color: #999999;
+         }
+        </style>
+    </head>
+    <body>
+       $index
+    </body>
+</html>
+