forked from GitHub/gf-core
gf-server: added a way to list the available pgf files
This commit is contained in:
@@ -15,6 +15,8 @@ import Control.Monad
|
|||||||
import Data.Char
|
import Data.Char
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
import System.Directory
|
||||||
|
import System.FilePath
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@@ -25,12 +27,17 @@ main = do initFastCGI
|
|||||||
cgiMain :: Cache PGF -> CGI CGIResult
|
cgiMain :: Cache PGF -> CGI CGIResult
|
||||||
cgiMain cache =
|
cgiMain cache =
|
||||||
do path <- pathInfo
|
do path <- pathInfo
|
||||||
case filter (not . null) $ splitBy (=='/') path of
|
jsonp <- serveResource cache $ filter (not . null) $ splitBy (=='/') path
|
||||||
[file,command] -> do pgf <- liftIO $ readCache cache file
|
outputJSONP jsonp
|
||||||
json <- pgfMain pgf command
|
|
||||||
outputJSONP json
|
serveResource :: Cache PGF -> [String] -> CGI JSValue
|
||||||
_ -> throwCGIError 400 "Unknown resource" ["Unknown resource: " ++ show path,
|
serveResource cache resource =
|
||||||
"Use /grammar.pgf/command"]
|
case resource of
|
||||||
|
[] -> liftIO doListGrammars
|
||||||
|
[file] -> serveResource cache [file,"grammar"]
|
||||||
|
[file,command] -> do pgf <- liftIO $ readCache cache $ cleanFilePath file
|
||||||
|
pgfMain pgf command
|
||||||
|
_ -> throwCGIError 400 "Unknown resource" ["Unknown resource: " ++ show resource]
|
||||||
|
|
||||||
pgfMain :: PGF -> String -> CGI JSValue
|
pgfMain :: PGF -> String -> CGI JSValue
|
||||||
pgfMain pgf command =
|
pgfMain pgf command =
|
||||||
@@ -81,6 +88,13 @@ pgfMain pgf command =
|
|||||||
Just lang | lang `elem` PGF.languages pgf -> return $ Just lang
|
Just lang | lang `elem` PGF.languages pgf -> return $ Just lang
|
||||||
| otherwise -> throwCGIError 400 "Unknown language" ["Unknown language: " ++ l]
|
| otherwise -> throwCGIError 400 "Unknown language" ["Unknown language: " ++ l]
|
||||||
|
|
||||||
|
doListGrammars :: IO JSValue
|
||||||
|
doListGrammars =
|
||||||
|
do cwd <- getCurrentDirectory
|
||||||
|
ps <- getDirectoryContents cwd
|
||||||
|
let fs = filter ((== ".pgf") . map toLower . takeExtension) $ map takeFileName ps
|
||||||
|
return $ showJSON $ map toJSObject [[("name", f)] | f <- fs]
|
||||||
|
|
||||||
doTranslate :: PGF -> String -> Maybe PGF.Type -> Maybe PGF.Language -> Maybe PGF.Language -> JSValue
|
doTranslate :: PGF -> String -> Maybe PGF.Type -> Maybe PGF.Language -> Maybe PGF.Language -> JSValue
|
||||||
doTranslate pgf input mcat mfrom mto = showJSON $ map toJSObject
|
doTranslate pgf input mcat mfrom mto = showJSON $ map toJSObject
|
||||||
[[("from", PGF.showLanguage from),("to", PGF.showLanguage to),("text",output)]
|
[[("from", PGF.showLanguage from),("to", PGF.showLanguage to),("text",output)]
|
||||||
@@ -154,3 +168,8 @@ selectLanguage pgf macc = case acceptable of
|
|||||||
|
|
||||||
langCodeLanguage :: PGF -> String -> Maybe PGF.Language
|
langCodeLanguage :: PGF -> String -> Maybe PGF.Language
|
||||||
langCodeLanguage pgf code = listToMaybe [l | l <- PGF.languages pgf, PGF.languageCode pgf l == Just code]
|
langCodeLanguage pgf code = listToMaybe [l | l <- PGF.languages pgf, PGF.languageCode pgf l == Just code]
|
||||||
|
|
||||||
|
-- * General utilities
|
||||||
|
|
||||||
|
cleanFilePath :: FilePath -> FilePath
|
||||||
|
cleanFilePath = takeFileName
|
||||||
@@ -11,6 +11,7 @@ executable pgf.fcgi
|
|||||||
old-time,
|
old-time,
|
||||||
unix,
|
unix,
|
||||||
directory,
|
directory,
|
||||||
|
filepath,
|
||||||
containers,
|
containers,
|
||||||
gf >= 3.0,
|
gf >= 3.0,
|
||||||
cgi >= 3001.1.7.0,
|
cgi >= 3001.1.7.0,
|
||||||
|
|||||||
Reference in New Issue
Block a user