{- This file is part of Hoogle, (c) Neil Mitchell 2004-2005 http://www.cs.york.ac.uk/~ndm/hoogle/ This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. -} {- | The Web interface, expects to be run as a CGI script. This does not require Haskell CGI etc, it just dumps HTML to the console -} module Web.Main where import Hoogle.Hoogle import Hoogle.TextUtil import Web.CGI import Web.Lambdabot import Data.Char import System.Environment import Data.List import Data.Maybe import System.Directory -- | Should the output be sent to the console and a file. -- If true then both, the file is 'debugFile'. -- Useful mainly for debugging. debugOut = False fakeArgs :: IO [(String, String)] fakeArgs = return $ [("q","map"), ("format","sherlock")] -- | The main function main :: IO () main = do args <- if debugOut then fakeArgs else cgiArgs putStr "Content-type: text/html\n\n" appendFile "log.txt" (show args ++ "\n") let input = lookupDef "" "q" args if null input then hoogleBlank args else do let p = hoogleParse input case hoogleParseError p of Just x -> showError input x Nothing -> showResults p args lookupDef :: Eq key => val -> key -> [(key, val)] -> val lookupDef def key list = case lookup key list of Nothing -> def Just x -> x lookupDefInt :: Eq key => Int -> key -> [(key, String)] -> Int lookupDefInt def key list = case lookup key list of Nothing -> def Just x -> case reads x of [(x,"")] -> x _ -> def -- | Show the search box hoogleBlank :: [(String,String)] -> IO () hoogleBlank args = do debugInit outputFile (if ("package","gtk") `elem` args then "front_gtk" else "front") -- | Replace all occurances of $ with the parameter outputFileParam :: FilePath -> String -> IO () outputFileParam x param = do src <- readFile ("res/" ++ x ++ ".inc") putLine (f src) where f ('$':xs) = param ++ f xs f (x:xs) = x : f xs f [] = [] outputFile :: FilePath -> IO () outputFile x = do src <- readFile ("res/" ++ x ++ ".inc") putLine src showError :: String -> String -> IO () showError input err = do debugInit outputFileParam "prefix" input outputFileParam "error" err outputFileParam "suffix" input -- | Perform a search, dump the results using 'putLine' showResults :: Search -> [(String, String)] -> IO () showResults input args = do let useGtk = ("package","gtk") `elem` args res <- hoogleResults (if useGtk then "res/gtk.txt" else "res/hoogle.txt") input let lres = length res search = hoogleSearch input tSearch = showText search useres = take num $ drop start res debugInit outputFileParam (if useGtk then "prefix_gtk" else "prefix") tSearch putLine $ "
" ++ "Searched for " ++ showTags search ++ " | " ++ (if lres == 0 then "No results found" else f lres) ++ " |
Hoogle says: " ++ showTags x ++ "
" lam <- Web.Lambdabot.query (lookupDef "" "q" args) case lam of Nothing -> return () Just x -> putLine $ "" ++ "Lambdabot says: " ++ x ++ "
" if null res then outputFileParam "noresults" tSearch else putLine $ "