From 397b8764fd05f4108a58126fcf72dcab969b7c4c Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Sun, 6 Aug 2023 16:09:00 +0200 Subject: [PATCH] Added 'return' link option and check if last char is dot --- app/Main.hs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 2e5217e..036e088 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -15,6 +15,7 @@ data Args = Args , verbose :: Bool , names :: Bool , cgi :: Bool + , back :: Bool } args :: Parser Args @@ -25,6 +26,7 @@ args = Args <*> switch ( long "verbose" <> short 'v' <> help "Verbose mode") <*> switch ( long "names" <> short 'n' <> help "Generate link names based on first of each document" ) <*> switch ( long "cgi" <> help "Output gemini file header") + <*> switch ( long "back" <> help "Print 'return to dir' url") main :: IO () main = parseArgs =<< execParser opts @@ -35,7 +37,7 @@ main = parseArgs =<< execParser opts <> header "Generate a gemini page index") parseArgs :: Args -> IO() -parseArgs (Args t dir h v r c) = do +parseArgs (Args t dir h v r c b) = do if c then do print CgiSuccess putStrLn $ "# " ++ t @@ -45,19 +47,29 @@ parseArgs (Args t dir h v r c) = do then putStr $ "\n" ++ h ++ "\n\n" else putStr "\n" - mapM_ (mkIndex r dir) . filter (isSuffixOf ".gmi") =<< getDirectoryContents dir + if b then do + mapM_ (mkIndex r dir) . filter (isSuffixOf ".gmi") =<< getDirectoryContents dir + putStr "\n" + putStrLn "=> ../ " + else mapM_ (mkIndex r dir) . filter (isSuffixOf ".gmi") =<< getDirectoryContents dir mkIndex :: Bool -> FilePath -> FilePath -> IO () mkIndex False d l = putStrLn $ "=> " ++ path where path = d++"/"++l -mkIndex True d l = do - line <- trim . words . head . lines <$> readFile path - putStrLn $ "=> " ++ path ++ " " ++ line - where path = d++"/"++l - trim s - | length s >= 8 = (++ "...") . unwords . take 8 . cleanString $ s - | otherwise = unwords . cleanString $ s - -- remove all '#' signs and any potential resulting empty lists - where cleanString = filter(/="") . map (dropWhile (=='#')) +mkIndex True d l = + let path = d ++ "/" ++ l + trim s + | length s >= 8 = appendDots . unwords . take 8 . cleanString $ s + | otherwise = unwords . cleanString $ s + where + -- remove all '#' signs and any potential resulting empty lists + cleanString = filter (/= "") . map (dropWhile (== '#')) + -- in case the last character is a dot, remove it + appendDots name + | last name == '.' = (++ "...") . init $ name + | otherwise = name + in do + line <- trim . words . head . lines <$> readFile path + putStrLn $ "=> " ++ path ++ " " ++ line