    module Text.Tabular.Csv where

    import Data.List (intersperse, transpose)
    import Text.CSV (printCSV)
    import Text.Tabular

    -- | for simplicity, we assume that each cell is rendered
    --   on a single line
    render :: (rh -> String)
           -> (ch -> String)
           -> (a -> String)
           -> Table rh ch a
           -> String
    render :: forall rh ch a.
    (rh -> String)
    -> (ch -> String) -> (a -> String) -> Table rh ch a -> String
    render rh -> String
    fr ch -> String
    fc a -> String
    f (Table Header rh
    rh Header ch
    ch [[a]]
    cells) =
      CSV -> String
    printCSV (CSV -> String) -> CSV -> String
    forall a b. (a -> b) -> a -> b
    $ [String]
    chStrings [String] -> CSV -> CSV
    forall a. a -> [a] -> [a]
    : CSV
    cells2
     where
      -- cell2 includes the row and column labels
      cells2 :: CSV
    cells2 = (String -> [a] -> [String]) -> [String] -> [[a]] -> CSV
    forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
    zipWith (\String
    h [a]
    cs -> String
    h String -> [String] -> [String]
    forall a. a -> [a] -> [a]
    : (a -> String) -> [a] -> [String]
    forall a b. (a -> b) -> [a] -> [b]
    map a -> String
    f [a]
    cs) [String]
    rhStrings [[a]]
    cells
      chStrings :: [String]
    chStrings = String
    "" String -> [String] -> [String]
    forall a. a -> [a] -> [a]
    : ((ch -> String) -> [ch] -> [String]
    forall a b. (a -> b) -> [a] -> [b]
    map ch -> String
    fc ([ch] -> [String]) -> [ch] -> [String]
    forall a b. (a -> b) -> a -> b
    $ Header ch -> [ch]
    forall h. Header h -> [h]
    headerContents Header ch
    ch)
      rhStrings :: [String]
    rhStrings = (rh -> String) -> [rh] -> [String]
    forall a b. (a -> b) -> [a] -> [b]
    map rh -> String
    fr ([rh] -> [String]) -> [rh] -> [String]
    forall a b. (a -> b) -> a -> b
    $ Header rh -> [rh]
    forall h. Header h -> [h]
    headerContents Header rh
    rh