packages feed

CHXHtml-0.1.1: htmlHelp.hs

import Network.FastCGI
import Text.CHXHtml.XHtml1_strict
import List
-- CHXHtml htmlHelp.hs
-- 
-- Paul Talaga -- June 11, 2010
-- 
-- Expose the htmlHelp function as a web interface.
--
-- If the 'q' parameter is set, split on , and run through htmlHelp function.


main = runFastCGI $ handleErrors test

test :: CGI CGIResult
test  = do q <- (Network.FastCGI.getInput "q");
           case q of
               Just m -> outputFPS $ render_bs (htmlHelpPage m) 
               Nothing -> outputFPS $ render_bs (htmlHelpPage "html,body,div")


-- htmlHelp page accepting the user's query
htmlHelpPage query = 
    _html [
        _head [
            _title [pcdata "CHXHtml htmlHelp "],
            link_ [href_att "/fuzz.css", rel_att "stylesheet", type_att "text/css"]
        ],
        _body [_h1 [pcdata "htmlHelp Web Interface"],
            _p [pcdata "Web interface for the htmlHelp function in the Haskell Text.CHXHtml library."],
            div_ [class_att "block"] [_code [pcdata "htmlHelp :: [String] -> [[String]]]"]
                    ],
            _p [pcdata "Specify the nesting context below, such as ", _code [pcdata "'html,body,div'"],pcdata "Which returns a list of W3C allowable children and attributes for Xhtml1_strict."],
            _p [pcdata "We've 'webified' the function, allowing the omisison of \" in the list of Strings as well as pretty-printing the output.  Content created by CHXHtml and Haskell!"],
            form_ [method_att Get, action_att "htmlHelp.fcg"] [p_ [] [    
                    _label [pcdata "Input:"],
                    input_ [type_att "text", name_att "q", size_att "50", value_att query],
                    input_ [type_att "submit",value_att "Submit"]
                ]
            ],
            (result query)
        ]
    ]
             
-- Produce the html for the result, if any
result query
    | query == "" = _div []
    | otherwise = _div [
                        _hr,
                        _h2 [pcdata ("Results for: " ++ concat (intersperse " -> " (splitInput query ',' [])))],  -- make the query nicer with -> between tags
                        div_ [style_att "float:left;width:220px;"] [
                            _h3 [pcdata "Allowed Children:"],
                            div_ [class_att "block"] (intersperse _br (map pcdata children))
                            ],
                        div_ [style_att "float:left;width:50px;"] [pcdata_bs (s2b"&nbsp;")],    -- TODO: add character codes to library! _bs does not escape content
                        div_ [style_att "float:left;width:300px;"] [
                            _h3 [pcdata "Allowed Attributes:"],
                            div_ [class_att "block"] (intersperse _br (map pcdata attributes))
                            ]
                       ]
    where r = htmlHelp (splitInput query ',' [])
          children = r !! 0 -- TODO: eek, this is dangerous, change
          attributes = r !! 1
          
splitInput :: String -> Char -> [String] -> [String] 
-- Given a string, delimter, and [], create a list of strings.
splitInput (x:xs) d (s:sx)
    | x == ' ' = splitInput xs d (s:sx)     -- ignore spaces
    | x == d = splitInput xs d ("":(s:sx))
    | otherwise = splitInput xs d ((s ++ [x]):sx)
splitInput [] _ result = reverse result  
splitInput s c [] = splitInput s c [""]