twin/app/Main.hs

74 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 00:29:26 +02:00
success :: String
success = "20 text/gemini; lang=en; charset=utf-8\r\n"
2023-07-30 22:54:04 +02:00
data Args = Args
{ title :: String
, directory :: FilePath
, fileHead :: FilePath
, verbose :: Bool
2023-07-31 14:32:56 +02:00
, refs :: Bool
, cgi :: Bool
2023-07-30 22:54:04 +02:00
}
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 ""
2023-07-31 00:29:26 +02:00
<> help "Header text" )
2023-07-30 22:54:04 +02:00
<*> switch
( long "verbose"
<> short 'v'
<> help "Verbose mode")
2023-07-31 14:32:56 +02:00
<*> switch
( long "references"
2023-07-31 15:09:44 +02:00
<> short 'R'
2023-07-31 14:32:56 +02:00
<> help "Generate link reference based on first line of 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
putStr success
putStrLn $ "# " ++ t
else putStrLn $ "# " ++ t
if not (null h)
then putStr $ "\n\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 ()
mkIndex True d l = do
line <- readFile $ d++"/"++l
2023-07-31 15:09:44 +02:00
putStrLn $ "=> " ++ l ++ " " ++ (head . lines $ line)
2023-07-31 15:14:59 +02:00
mkIndex False _ l = putStrLn $ "=> " ++ l
2023-07-30 22:54:04 +02:00
2023-07-31 14:32:56 +02:00
--compose = (++) <$> ("=> " ++) <*> (show . length) <*> (head . lines =<< readFile)