module MkHTML where -- document structure type HTML = String html :: String -> HTML -> HTML html hd bod = tagged "html" (headTitle (string hd) ++++ body bod) headTitle :: String -> HTML headTitle s = tagged "head" (tagged "title" s) body :: HTML -> HTML body h = tagged "body" h -- elements inside the body para :: HTML -> HTML para h = tagged "p" h header :: Int -> HTML -> HTML header i = tagged ("h" ++ show i) link :: String -> HTML -> HTML link url txt = attrTagged "a" [("href",url)] txt image :: String -> Int -> HTML image url size = attrTag "img" [("src",url),("width", show size)] bold :: HTML -> HTML bold = tagged "b" italic :: HTML -> HTML italic = tagged "i" typewriter :: HTML -> HTML typewriter = tagged "tt" colored :: String -> HTML -> HTML colored s = attrTagged "font" [("color",s)] string :: String -> HTML string s = s list :: [HTML] -> HTML list = unlines table :: [[HTML]] -> HTML table rows = attrTagged "table" [("border","yes")] (list [tagged "tr" (list [tagged "td" cell | cell <- row]) | row <- rows]) queryLink :: String -> HTML -> HTML queryLink q s = link (q ++ s) s googledText :: String -> HTML googledText s = unwords [queryLink "http://www.google.se/search?q=" w | w <- words s] tydaedText :: String -> HTML tydaedText s = unwords [queryLink "http://www.tyda.se/?w=" w | w <- words s] -- HTML auxiliaries tagged :: String -> HTML -> HTML tagged t h = tag t ++ h ++ endTag t attrTagged :: String -> [(String,String)] -> HTML -> HTML attrTagged t attrs h = attrTag t attrs ++ h ++ endTag t tag :: String -> String tag t = "<" ++ t ++ ">" endTag :: String -> String endTag t = tag ("/" ++ t) attrTag :: String -> [(String,String)] -> String attrTag t attrs = tag (t +++ unwords [a ++"=" ++ "\"" ++ v ++ "\"" | (a,v) <- attrs]) -- general auxiliaries (+++) :: String -> String -> String s +++ t = s ++ " " ++ t (++++) :: String -> String -> String s ++++ t = s ++ "\n" ++ t grid :: Int -> [a] -> [[a]] grid i [] = [] grid i xs = take i xs : grid i (drop i xs) -- exampel test0 :: HTML test0 = html "Hello" (string "Hello World") test1 :: HTML test1 = html "Head-titeln: texten overst i ramen" (list [ string "Body: texten i textfaltet.", header 1 "Huvudtiteln", para (list [ string "Paragraf, bestaende av text och annat, t.ex.", link "http://www.haskell.org" "lankar till webbsidor", bold "text i fetstil", italic "text i kursiv", typewriter "text i maskinskrift", colored "red" "text i rod font", string "bilder, som ar lankar till bildfiler och kan skalas", string "till onskad storlek (i nasta paragraf):" ]), para (image "didno.jpg" 512), header 2 "Lite mindre titel" ]) test2 :: HTML test2 = html "Fahrenheit to Celcius" (table [[show f, show (5*(f - 32)/9)] | f <- [0,20 .. 300]]) test3 :: HTML test3 = html "Google bakom lankarna" (googledText "Varje ord som klickas skickas till Google") test4 :: HTML test4 = html "Klicka fram engelska ord" (tydaedText "Varje ord som klickas skickas till en svensk engelsk ordbok") mk_test5 :: IO () mk_test5 = do s <- readFile "pics" let pics = lines s let doc = html "Pictures" (table (grid 5 [link pic (image pic 128) | pic <- pics])) writeFile "test5.html" doc -- skapa alla exempelfiler test :: IO () test = do writeFile "test0.html" test0 writeFile "test1.html" test1 writeFile "test2.html" test2 writeFile "test3.html" test3 writeFile "test4.html" test4 mk_test5