From: Cameron Ball Date: Fri, 15 Feb 2019 18:22:53 +0000 (+0800) Subject: Generate the site index X-Git-Url: http://cameron1729.xyz/?p=xyz.git;a=commitdiff_plain;h=2edcb742dd2cb06095b2f659a434b44998da76c6 Generate the site index --- diff --git a/src/Lib.hs b/src/Lib.hs index d502725..80cf44f 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -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 -> " year <> "/" <> month <> "/" <> ((slugify . title . snd) x) <> ".html\">" <> (title . snd) x <> "") 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 = "" - open = "<" `append` tag `append` ">" +wrapInTags :: Text -> [Text] -> Text +wrapInTags tag = foldMap $ (<> close) . (open <>) + where close = "" + 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 -> "" + 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 index 0000000..6a9d20c --- /dev/null +++ b/templates/index.html @@ -0,0 +1,31 @@ + + + + + blog@cameron1729 + + + + $index + + +