twin/app/Main.hs
2023-07-31 14:32:56 +02:00

77 lines
1.9 KiB
Haskell

module Main where
import System.Directory
import Options.Applicative
import Data.List
success :: String
success = "20 text/gemini; lang=en; charset=utf-8\r\n"
data Args = Args
{ title :: String
, directory :: FilePath
, fileHead :: FilePath
, verbose :: Bool
, refs :: Bool
, cgi :: Bool
}
args :: Parser Args
args = Args
<$> 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 "references"
<> help "Generate link reference based on first line of document" )
<*> switch
( long "cgi"
<> help "Output gemini file header")
main :: IO ()
main = parseArgs =<< execParser opts
where
opts = info ( args <**> helper)
( fullDesc
<> progDesc "Generate gemini page index"
<> header "Generate a gemini page index")
parseArgs :: Args -> IO()
parseArgs (Args t dir h v c r) = do
if c then do
putStr success
putStrLn $ "# " ++ t
else putStrLn $ "# " ++ t
if not (null h)
then putStr $ "\n\n" ++ h ++ "\n\n"
else putStr "\n"
mkIndex r . filter (isSuffixOf ".gmi") =<< getDirectoryContents dir
mkIndex :: Bool -> [String] -> IO ()
mkIndex True l = mapM_ (putStrLn . ("=> " ++)) l
-- where firstLine f = do
-- print $ head . lines =<< readFile f
mkIndex False l = mapM_ (putStrLn . ("=> " ++)) l
firstLine :: FilePath -> IO ()
firstLine f = do
putStr . head . lines =<< readFile f
--compose = (++) <$> ("=> " ++) <*> (show . length) <*> (head . lines =<< readFile)