Skip to content

Instantly share code, notes, and snippets.

@tailcalled
Created October 19, 2012 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tailcalled/3920388 to your computer and use it in GitHub Desktop.
Save tailcalled/3920388 to your computer and use it in GitHub Desktop.
Stuff
-- Example with your sample
vhosts
nullable.se
aliases: nullable.se.dev
redirect: www.nullable.se
serve: /var/www
-- Also trivially equivalent to
vhosts
nullable.se
aliases
nullable.se.dev
redirect
www.nullable.se
serve
/var/www
-- Expanded to
vhosts
nullable.se
serve
FSDirectory
directory: /var/www
nullable.se.dev
serve
FSDirectory
directory: /var/www
www.nullable.se
serve
Redirect: http://nullable.se/
module Parser
import ImaginaryTTXTParser
import Data.Map
data Serve = Redirect String | FSDirectory String deriving (Show) -- Strings for simplification
data ConfigFile = ConfigFile (Map String Serve) deriving (Show)
vhosts (ConfigFile vhosts') = vhosts'
parseConfigFile :: TTXT -> Maybe ConfigFile
parseConfigFile ttxt = (ConfigFile . fromList) <$> (getOnly "vhosts" ttxt >>= parseVHosts)
parseVHosts :: [TTXT] -> Maybe [(String, Serve)]
parseVHosts ttxt = join $ join $ map optionToList $ map parseVHost ttxt
parseVHost :: TTXT -> Maybe [(String, Serve)]
parseVHost ttxt@(TTXT name _) =
do serve <- getVHostServe ttxt
let main = (name, serve)
(TTXT _ aliases) <- getOnly "aliases" ttxt
let aliases' = map ((, serve) . name) aliases
(TTXT _ redirects) <- getOnly "redirects" ttxt
let redirects' = map ((, Redirect name) . name) redirects
return $ main ++ aliases' ++ redirects'
getVHostServes :: TTXT -> Maybe Serve
getVHostServes ttxt =
do serve <- getOnly "serve" ttxt
if not $ hasChildren serve
then FSDirectory $ name serve
else primitiveMakeServe ttxt
primitiveMakeServe :: TTXT -> Maybe Serve
primitiveMakeServe (TTXT "FSDirectory" [TTXT dir []]) = Just $ FSDirectory dir
primitiveMakeServe (TTXT "Redirect" [TTXT url []]) = Just $ Redirect url
primitiveMakeServe _ = Nothing
main =
do ttxt <- ttxtFromFile "config.ttxt"
print $ parseConfigFile ttxt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment