RosettaCodeData/Task/CSV-to-HTML-translation/Haskell/csv-to-html-translation-1.hs
Ingy döt Net 764da6cbbb CDE
2013-04-10 16:57:12 -07:00

26 lines
1 KiB
Haskell

--import Data.List.Split (splitOn) -- if the import is available
splitOn :: Char -> String -> [String] -- otherwise
splitOn delim = foldr (\x rest ->
if x == delim then "" : rest
else (x:head rest):tail rest) [""]
htmlEscape :: String -> String
htmlEscape = concatMap escapeChar
where escapeChar '<' = "&lt;"
escapeChar '>' = "&gt;"
escapeChar '&' = "&amp;"
escapeChar '"' = "&quot;" --"
escapeChar c = [c]
toHtmlRow :: [String] -> String
toHtmlRow [] = "<tr></tr>"
toHtmlRow cols = let htmlColumns = concatMap toHtmlCol cols
in "<tr>\n" ++ htmlColumns ++ "</tr>"
where toHtmlCol x = " <td>" ++ htmlEscape x ++ "</td>\n"
csvToTable :: String -> String
csvToTable csv = let rows = map (splitOn ',') $ lines csv
html = unlines $ map toHtmlRow rows
in "<table>\n" ++ html ++ "</table>"
main = interact csvToTable