{- | Export spreadsheet table data as HTML table. This is derived from -} module Hledger.Write.Html ( printHtml, ) where import Hledger.Write.Spreadsheet (Type(..), Style(..), Emphasis(..), Cell(..)) import qualified Data.Text.Lazy as TL import qualified Data.Text as T import Text.Printf (printf) printHtml :: [[Cell]] -> TL.Text printHtml table = TL.unlines $ map (TL.fromStrict . T.pack) $ "" : (table >>= \row -> "" : (row >>= formatCell) ++ "" : []) ++ "
" : [] formatCell :: Cell -> [String] formatCell cell = (let str = escape $ T.unpack $ cellContent cell in case cellStyle cell of Head -> printf "%s" str Body emph -> let align = case cellType cell of TypeString -> "" _ -> " align=right" (emphOpen, emphClose) = case emph of Item -> ("", "") Total -> ("", "") in printf "%s%s%s" align emphOpen str emphClose) : [] escape :: String -> String escape = concatMap $ \c -> case c of '\n' -> "
" '&' -> "&" '<' -> "<" '>' -> ">" '"' -> """ '\'' -> "'" _ -> [c]