twin/app/Main.hs

58 lines
1.8 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-07-31 14:32:56 +02:00
if c then do
2023-07-31 16:09:45 +02:00
print CgiSuccess
2023-07-31 14:32:56 +02:00
putStrLn $ "# " ++ t
else putStrLn $ "# " ++ t
if not (null h)
2023-07-31 16:09:45 +02:00
then putStr $ "\n" ++ h ++ "\n\n"
2023-07-31 14:32:56 +02:00
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 ()
mkIndex True d l = do
line <- readFile $ d++"/"++l
2023-07-31 16:09:45 +02:00
putStrLn $ "=> " ++ path ++ (head . lines $ line)
where path = d++"/"++l++" "
mkIndex False d l = putStrLn $ "=> " ++ path
where path = d++"/"++l
2023-07-30 22:54:04 +02:00
2023-07-31 14:32:56 +02:00
--compose = (++) <$> ("=> " ++) <*> (show . length) <*> (head . lines =<< readFile)