Added 'return' link option and check if last char is dot

This commit is contained in:
Nox Sluijtman 2023-08-06 16:09:00 +02:00
parent 6e0fa21ba9
commit 397b8764fd

View file

@ -15,6 +15,7 @@ data Args = Args
, verbose :: Bool , verbose :: Bool
, names :: Bool , names :: Bool
, cgi :: Bool , cgi :: Bool
, back :: Bool
} }
args :: Parser Args args :: Parser Args
@ -25,6 +26,7 @@ args = Args
<*> switch ( long "verbose" <> short 'v' <> help "Verbose mode") <*> 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 "names" <> short 'n' <> help "Generate link names based on first of each document" )
<*> switch ( long "cgi" <> help "Output gemini file header") <*> switch ( long "cgi" <> help "Output gemini file header")
<*> switch ( long "back" <> help "Print 'return to dir' url")
main :: IO () main :: IO ()
main = parseArgs =<< execParser opts main = parseArgs =<< execParser opts
@ -35,7 +37,7 @@ main = parseArgs =<< execParser opts
<> header "Generate a gemini page index") <> header "Generate a gemini page index")
parseArgs :: Args -> IO() parseArgs :: Args -> IO()
parseArgs (Args t dir h v r c) = do parseArgs (Args t dir h v r c b) = do
if c if c
then do print CgiSuccess then do print CgiSuccess
putStrLn $ "# " ++ t putStrLn $ "# " ++ t
@ -45,19 +47,29 @@ parseArgs (Args t dir h v r c) = do
then putStr $ "\n" ++ h ++ "\n\n" then putStr $ "\n" ++ h ++ "\n\n"
else putStr "\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 :: Bool -> FilePath -> FilePath -> IO ()
mkIndex False d l = putStrLn $ "=> " ++ path mkIndex False d l = putStrLn $ "=> " ++ path
where path = d++"/"++l where path = d++"/"++l
mkIndex True d l = do mkIndex True d l =
line <- trim . words . head . lines <$> readFile path let path = d ++ "/" ++ l
putStrLn $ "=> " ++ path ++ " " ++ line trim s
where path = d++"/"++l | length s >= 8 = appendDots . unwords . take 8 . cleanString $ s
trim s | otherwise = unwords . cleanString $ s
| length s >= 8 = (++ "...") . unwords . take 8 . cleanString $ s where
| otherwise = unwords . cleanString $ s -- remove all '#' signs and any potential resulting empty lists
-- remove all '#' signs and any potential resulting empty lists cleanString = filter (/= "") . map (dropWhile (== '#'))
where 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