makeTable :: Dialect -> Maybe [String] -> String -> String
makeTable dialect header csvData = unlines $ case dialect of
        Md -> makePipeTable
        Rst -> makeGridTable

    where

        (columnFormats, table) = parseCSV header csvData
        (headerRow : rows) = table

        makeRow corner cells = concat [ corner, intercalate corner cells, corner ]
        makeSepRow corner cell = makeRow corner (map cell columnFormats)

        makeSep (LeftAligned w) = replicate (w+2) '-'
        makeSep (RightAligned w) = replicate (w+2) '-'

        makeBigSep c (LeftAligned w) = alignmentChar ++ replicate (w+1) c
        makeBigSep c (RightAligned w) = replicate (w+1) c ++ alignmentChar

        alignmentChar = case dialect of
            Md -> ":"
            Rst -> "="

        makeGridTable =
            [ sepLine
            , makeRow "|" headerRow
            , makeSepRow "+" (makeBigSep '=')
            ] ++ concat [ [makeRow "|" row, sepLine] | row <- rows ]
            where
                sepLine = makeSepRow "+" makeSep

        makePipeTable =
            [ makeRow "|" headerRow
            , makeSepRow "|" (makeBigSep '-')
            ] ++ map (makeRow "|") rows
makeTable :: Dialect -> Maybe [String] -> String -> String
makeTable dialect header csvData = unlines $ case dialect of
        Md -> makePipeTable
        Rst -> makeGridTable

    where

        (columnFormats, table) = parseCSV header csvData
        (headerRow : rows) = table

        makeRow corner cells = concat [ corner, intercalate corner cells, corner ]
        makeSepRow corner cell = makeRow corner (map cell columnFormats)

        makeSep (LeftAligned w) = replicate (w+2) '-'
        makeSep (RightAligned w) = replicate (w+2) '-'

        makeBigSep c (LeftAligned w) = alignmentChar ++ replicate (w+1) c
        makeBigSep c (RightAligned w) = replicate (w+1) c ++ alignmentChar

        alignmentChar = case dialect of
            Md -> ":"
            Rst -> "="

        makeGridTable =
            [ sepLine
            , makeRow "|" headerRow
            , makeSepRow "+" (makeBigSep '=')
            ] ++ concat [ [makeRow "|" row, sepLine] | row <- rows ]
            where
                sepLine = makeSepRow "+" makeSep

        makePipeTable =
            [ makeRow "|" headerRow
            , makeSepRow "|" (makeBigSep '-')
            ] ++ map (makeRow "|") rows
makeTable dialect header csvData = unlines $ case dialect of
        Md -> makePipeTable
        Rst -> makeGridTable

    where

        (columnFormats, table) = parseCSV header csvData
        (headerRow : rows) = table

        makeRow corner cells = concat [ corner, intercalate corner cells, corner ]
        makeSepRow corner cell = makeRow corner (map cell columnFormats)

        makeSep (LeftAligned w) = replicate (w+2) '-'
        makeSep (RightAligned w) = replicate (w+2) '-'

        makeBigSep c (LeftAligned w) = alignmentChar ++ replicate (w+1) c
        makeBigSep c (RightAligned w) = replicate (w+1) c ++ alignmentChar

        alignmentChar = case dialect of
            Md -> ":"
            Rst -> "="

        makeGridTable =
            [ sepLine
            , makeRow "|" headerRow
            , makeSepRow "+" (makeBigSep '=')
            ] ++ concat [ [makeRow "|" row, sepLine] | row <- rows ]
            where
                sepLine = makeSepRow "+" makeSep

        makePipeTable =
            [ makeRow "|" headerRow
            , makeSepRow "|" (makeBigSep '-')
            ] ++ map (makeRow "|") rows