Created
October 19, 2012 20:08
-
-
Save tailcalled/3920388 to your computer and use it in GitHub Desktop.
Stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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