2022-10-26 09:57:55 +00:00
|
|
|
module Main where
|
|
|
|
|
2022-10-27 10:20:36 +00:00
|
|
|
import Control.Applicative hiding (some)
|
|
|
|
import Control.Monad
|
|
|
|
import Data.Functor
|
|
|
|
import Data.Void
|
2022-10-28 10:55:46 +00:00
|
|
|
import GHC.Stack.CCS (whereFrom)
|
|
|
|
import GHC.StaticPtr (StaticPtrInfo)
|
2022-10-27 10:20:36 +00:00
|
|
|
import System.Environment
|
|
|
|
import System.Exit
|
|
|
|
import Text.Megaparsec hiding (satisfy)
|
|
|
|
import Text.Megaparsec.Char
|
2022-10-26 10:37:48 +00:00
|
|
|
|
2022-10-26 22:15:32 +00:00
|
|
|
type Parser = Parsec Void String
|
2022-10-26 11:59:46 +00:00
|
|
|
|
2022-10-28 10:55:46 +00:00
|
|
|
newtype URL = URL String
|
|
|
|
|
|
|
|
data Info =
|
|
|
|
Bread String |
|
|
|
|
List [Info] |
|
|
|
|
Header String |
|
|
|
|
Subheader String |
|
|
|
|
Subsubheader String |
|
|
|
|
Image URL |
|
|
|
|
Video URL |
|
|
|
|
Link URL Info |
|
|
|
|
Spoiler Info
|
|
|
|
|
|
|
|
instance Show Info where
|
|
|
|
show :: Info -> String
|
|
|
|
show info = case info of
|
|
|
|
Bread text -> text
|
|
|
|
List texts -> "[LIST]" ++ concatMap show texts ++ "[/LIST]"
|
|
|
|
Header text -> "[TITLE]" ++ text ++ "[/TITLE]"
|
|
|
|
Subheader text -> "[SIZE=4]" ++ text ++ "[/SIZE]"
|
|
|
|
Subsubheader text -> "[SIZE=2]" ++ text ++ "[/SIZE]"
|
|
|
|
Image (URL url) -> "[IMG]" ++ url ++ "[/IMG]"
|
|
|
|
Video (URL url) -> "[VIDEO]" ++ url ++ "[/VIDEO]"
|
|
|
|
Link (URL url) inf -> "[URL=\"" ++ url ++ "\"]" ++ show inf ++ "[/IMG]"
|
|
|
|
Spoiler inf -> "[SPOILER]" ++ show inf ++ "[/SPOILER]"
|
|
|
|
|
|
|
|
newtype Post = Post [Info]
|
|
|
|
|
2022-10-26 22:26:14 +00:00
|
|
|
parseHelper :: Parser String -> String -> String
|
|
|
|
parseHelper parser x = case parse parser "" x of
|
|
|
|
Left bundle -> error $ errorBundlePretty bundle
|
2022-10-27 10:20:36 +00:00
|
|
|
Right text -> text
|
2022-10-26 22:26:14 +00:00
|
|
|
|
2022-10-26 13:29:48 +00:00
|
|
|
parseFile :: String -> String
|
2022-10-26 22:26:14 +00:00
|
|
|
parseFile = unlines . map (parseHelper fileParser) . lines
|
2022-10-26 22:15:32 +00:00
|
|
|
|
|
|
|
fileParser :: Parser String
|
2022-10-27 10:20:36 +00:00
|
|
|
fileParser = try $
|
|
|
|
string "* " <|>
|
|
|
|
string "** "
|
2022-10-26 10:03:45 +00:00
|
|
|
|
2022-10-26 13:29:48 +00:00
|
|
|
parseCli :: [String] -> IO String
|
2022-10-27 10:20:36 +00:00
|
|
|
parseCli [] = return usage
|
|
|
|
parseCli [x] =
|
2022-10-26 13:29:48 +00:00
|
|
|
if head x /= '-'
|
|
|
|
then parseFile <$> readFile x
|
2022-10-26 22:26:14 +00:00
|
|
|
else return $ useArgs $ parseHelper argParse x
|
|
|
|
parseCli [x, _] = return $ useArgs $ parseHelper argParse x
|
2022-10-27 10:20:36 +00:00
|
|
|
parseCli _ = fail "Too many arugemnts"
|
2022-10-26 10:37:48 +00:00
|
|
|
|
2022-10-26 22:15:32 +00:00
|
|
|
argParse :: Parser String
|
2022-10-27 10:20:36 +00:00
|
|
|
argParse = string "-" >> some alphaNumChar
|
2022-10-26 10:37:48 +00:00
|
|
|
|
2022-10-26 11:59:46 +00:00
|
|
|
useArgs :: String -> String
|
2022-10-27 10:20:36 +00:00
|
|
|
useArgs [] = ""
|
2022-10-26 13:29:48 +00:00
|
|
|
useArgs ('h' : _) = usage
|
|
|
|
useArgs ('v' : _) = version
|
2022-10-27 10:20:36 +00:00
|
|
|
useArgs (_ : xs) = useArgs xs
|
2022-10-26 10:37:48 +00:00
|
|
|
|
2022-10-26 11:59:46 +00:00
|
|
|
usage :: String
|
2022-10-26 13:29:48 +00:00
|
|
|
usage = "Usage: otm [-hv] [file]"
|
|
|
|
|
2022-10-26 11:59:46 +00:00
|
|
|
version :: String
|
|
|
|
version = "otm 0.1"
|
2022-10-26 22:26:14 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = putStr =<< parseCli =<< getArgs
|