twin/app/Main.hs

64 lines
2 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
, verbose :: Bool
2023-07-31 16:09:45 +02:00
, names :: Bool
, cgi :: 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" )
<*> 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")
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-07-31 15:09:44 +02:00
parseArgs (Args t dir h v r c) = 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"
2023-07-31 15:14:59 +02:00
mapM_ (mkIndex r 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
mkIndex False d l = putStrLn $ "=> " ++ path
where path = d++"/"++l
2023-07-30 22:54:04 +02:00
2023-08-05 01:23:06 +02:00
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 (=='#'))