    module Text.Tabular.Latex where

    import Data.List (intersperse)
    import Text.Tabular

    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 = [String]
    -> (rh -> String)
    -> (ch -> String)
    -> (a -> String)
    -> Table rh ch a
    -> String
    forall rh ch a.
    [String]
    -> (rh -> String)
    -> (ch -> String)
    -> (a -> String)
    -> Table rh ch a
    -> String
    renderUsing (String -> [String]
    forall a. a -> [a]
    repeat String
    "r")

    renderUsing :: [String] -- ^ column header specifications including label (l,h,p{3cm},etc)
                -> (rh -> String)
                -> (ch -> String)
                -> (a -> String) -> Table rh ch a -> String
    renderUsing :: forall rh ch a.
    [String]
    -> (rh -> String)
    -> (ch -> String)
    -> (a -> String)
    -> Table rh ch a
    -> String
    renderUsing [String]
    cols rh -> String
    fr ch -> String
    fc a -> String
    f (Table Header rh
    rh Header ch
    ch [[a]]
    cells) =
     [String] -> String
    unlines ([String] -> String) -> [String] -> String
    forall a b. (a -> b) -> a -> b
    $ ( String
    "\\begin{tabular}{" String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    hspec String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    "}")
             String -> [String] -> [String]
    forall a. a -> [a] -> [a]
    : [ String -> String
    addTableNl String
    header
               , String
    hline
               , ((Either Properties String -> String)
    -> [Either Properties String] -> String
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap ((Properties -> String)
    -> (String -> String) -> Either Properties String -> String
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either Properties -> String
    vAttr String -> String
    addTableNl) ([Either Properties String] -> String)
    -> [Either Properties String] -> String
    forall a b. (a -> b) -> a -> b
    $
                  Header String -> [Either Properties String]
    forall h. Header h -> [Either Properties h]
    flattenHeader (Header String -> [Either Properties String])
    -> Header String -> [Either Properties String]
    forall a b. (a -> b) -> a -> b
    $ (([a], String) -> String) -> Header ([a], String) -> Header String
    forall a b. (a -> b) -> Header a -> Header b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ([a], String) -> String
    row (Header ([a], String) -> Header String)
    -> Header ([a], String) -> Header String
    forall a b. (a -> b) -> a -> b
    $ [a] -> [[a]] -> Header String -> Header ([a], String)
    forall h a. h -> [h] -> Header a -> Header (h, a)
    zipHeader [] [[a]]
    cells (Header String -> Header ([a], String))
    -> Header String -> Header ([a], String)
    forall a b. (a -> b) -> a -> b
    $ (rh -> String) -> Header rh -> Header String
    forall a b. (a -> b) -> Header a -> Header b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap rh -> String
    fr Header rh
    rh)
               , String
    "\\end{tabular}" ]
     where
      ch2 :: Header String
    ch2 = Properties -> [Header String] -> Header String
    forall h. Properties -> [Header h] -> Header h
    Group Properties
    DoubleLine [(String -> Header String
    forall h. h -> Header h
    Header String
    ""),(ch -> String) -> Header ch -> Header String
    forall a b. (a -> b) -> Header a -> Header b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ch -> String
    fc Header ch
    ch]
      hspec :: String
    hspec  = (Either Properties (String, String) -> String)
    -> [Either Properties (String, String)] -> String
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap ((Properties -> String)
    -> ((String, String) -> String)
    -> Either Properties (String, String)
    -> String
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either Properties -> String
    hAttr (String, String) -> String
    forall a b. (a, b) -> a
    fst) ([Either Properties (String, String)] -> String)
    -> [Either Properties (String, String)] -> String
    forall a b. (a -> b) -> a -> b
    $ Header (String, String) -> [Either Properties (String, String)]
    forall h. Header h -> [Either Properties h]
    flattenHeader
             (Header (String, String) -> [Either Properties (String, String)])
    -> Header (String, String) -> [Either Properties (String, String)]
    forall a b. (a -> b) -> a -> b
    $ String -> [String] -> Header String -> Header (String, String)
    forall h a. h -> [h] -> Header a -> Header (h, a)
    zipHeader String
    "" [String]
    cols Header String
    ch2
      header :: String
    header = [String] -> String
    texCols ([String] -> String)
    -> (Header String -> [String]) -> Header String -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (String -> String) -> [String] -> [String]
    forall a b. (a -> b) -> [a] -> [b]
    map String -> String
    label ([String] -> [String])
    -> (Header String -> [String]) -> Header String -> [String]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Header String -> [String]
    forall h. Header h -> [h]
    headerContents (Header String -> String) -> Header String -> String
    forall a b. (a -> b) -> a -> b
    $ Header String
    ch2
      --
      row :: ([a], String) -> String
    row ([a]
    cs,String
    h) = [String] -> String
    texCols ([String] -> String) -> [String] -> String
    forall a b. (a -> b) -> a -> b
    $ String -> String
    label 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
      texCols :: [String] -> String
    texCols  = [String] -> String
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat ([String] -> String)
    -> ([String] -> [String]) -> [String] -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> [String] -> [String]
    forall a. a -> [a] -> [a]
    intersperse String
    " & "
      texRows :: [String] -> [String]
    texRows  = (String -> String) -> [String] -> [String]
    forall a b. (a -> b) -> [a] -> [b]
    map String -> String
    addTableNl
      rhStrings :: [rh]
    rhStrings = Header rh -> [rh]
    forall h. Header h -> [h]
    headerContents Header rh
    rh

    hline :: String
    hline :: String
    hline = String
    "\\hline"

    addTableNl :: String -> String
    addTableNl :: String -> String
    addTableNl = (String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    "\\\\\n")

    label :: String -> String
    label :: String -> String
    label String
    s = String
    "\\textbf{" String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    s String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    "}"

    hAttr :: Properties -> String
    hAttr :: Properties -> String
    hAttr Properties
    NoLine     = String
    ""
    hAttr Properties
    SingleLine = String
    "|"
    hAttr Properties
    DoubleLine = String
    "||"

    vAttr :: Properties -> String
    vAttr :: Properties -> String
    vAttr Properties
    NoLine     = String
    ""
    vAttr Properties
    SingleLine = String
    hline
    vAttr Properties
    DoubleLine = String
    hline String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    hline