twin/app/Main.hs

79 lines
2.5 KiB
Haskell
Raw Normal View History

2023-07-30 22:54:04 +02:00
module Main where
import System.Directory
import Options.Applicative
import Data.List
2023-07-31 16:09:45 +02:00
data CgiState = CgiSuccess
instance Show CgiState where
show CgiSuccess = "20 text/gemini; lang=en; charset=utf-8\r"
2023-07-31 00:29:26 +02:00
2023-07-30 22:54:04 +02:00
data Args = Args
{ title :: String
, directory :: FilePath
, fileHead :: FilePath
2023-08-06 16:55:27 +02:00
--, verbose :: Bool
2023-07-31 16:09:45 +02:00
, names :: Bool
, cgi :: Bool
, back :: Bool
2023-07-30 22:54:04 +02:00
}
args :: Parser Args
args = Args
2023-07-31 16:09:45 +02:00
<$> strOption ( long "title" <> short 't' <> value "Title Left Blank" <> help "Document title" )
<*> strOption ( long "directory" <> short 'd' <> value "./" <> help "Directory to parse" )
<*> strOption ( long "header" <> short 'H' <> value "" <> help "Header text" )
2023-08-06 16:55:27 +02:00
-- <*> switch ( long "verbose" <> short 'v' <> help "Verbose mode")
2023-07-31 16:09:45 +02:00
<*> 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")
2023-07-30 22:54:04 +02:00
main :: IO ()
main = parseArgs =<< execParser opts
where
opts = info ( args <**> helper)
( fullDesc
2023-07-31 00:29:26 +02:00
<> progDesc "Generate gemini page index"
<> header "Generate a gemini page index")
2023-07-30 22:54:04 +02:00
parseArgs :: Args -> IO()
2023-08-06 16:55:27 +02:00
--parseArgs (Args t dir h v n c b) = do
parseArgs (Args t dir h n c b) = do
2023-08-05 01:23:06 +02:00
if c
then do print CgiSuccess
2023-07-31 14:32:56 +02:00
putStrLn $ "# " ++ t
2023-08-05 01:23:06 +02:00
else putStrLn $ "# " ++ t
2023-07-31 14:32:56 +02:00
if not (null h)
2023-08-05 01:23:06 +02:00
then putStr $ "\n" ++ h ++ "\n\n"
else putStr "\n"
if b then do
2023-08-06 16:55:27 +02:00
mapM_ (mkIndex n dir) . filter (isSuffixOf ".gmi") =<< getDirectoryContents dir
putStr "\n"
2023-08-06 16:42:12 +02:00
putStrLn "=> .. "
2023-08-06 16:55:27 +02:00
else mapM_ (mkIndex n dir) . filter (isSuffixOf ".gmi") =<< getDirectoryContents dir
2023-07-31 14:32:56 +02:00
2023-07-31 15:14:59 +02:00
mkIndex :: Bool -> FilePath -> FilePath -> IO ()
2023-07-31 16:09:45 +02:00
2023-08-06 16:55:27 +02:00
mkIndex False d f = putStrLn $ "=> " ++ path d
where path s
| s == "./" = f
| otherwise = s++"/"++f
2023-07-30 22:54:04 +02:00
2023-08-06 16:55:27 +02:00
mkIndex True d f =
let path = d ++ "/" ++ f
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
2023-08-06 16:42:12 +02:00
| last name == '.' = (++ "...") . reverse . dropWhile (=='.') . reverse $ name
| otherwise = name
in do
line <- trim . words . head . lines <$> readFile path
putStrLn $ "=> " ++ path ++ " " ++ line