@@ -39,13 +39,14 @@ module Text.Pandoc.Writers.Shared (
39 , tagWithAttrs
39 , tagWithAttrs
40 , fixDisplayMath
40 , fixDisplayMath
41 , unsmartify
41 , unsmartify
44import Control.Monad (liftM)
45import Control.Monad (liftM)
45import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
46import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
46 encode, fromJSON)
47 encode, fromJSON)
47import qualified Data.HashMap.Strict as H
48import qualified Data.HashMap.Strict as H
48−import Data.List (groupBy)
49+import Data.List (groupBy, intersperse)
49import qualified Data.Map as M
50import qualified Data.Map as M
50import Data.Maybe (isJust)
51import Data.Maybe (isJust)
51import qualified Data.Text as T
52import qualified Data.Text as T
@@ -216,3 +217,43 @@ unsmartify opts ('\8212':xs)
216unsmartify opts (x:xs) = x : unsmartify opts xs
217unsmartify opts (x:xs) = x : unsmartify opts xs
217unsmartify _ [] = []
218unsmartify _ [] = []
220+gridTable :: Monad m => Bool -> [Alignment] -> [Int]
221+ -> [Doc] -> [[Doc]] -> m Doc
222+gridTable headless aligns widthsInChars headers' rawRows = do
223+ let hpipeBlocks blocks = hcat [beg, middle, end]
224+ where h = maximum (1 : map height blocks)
225+ sep' = lblock 3 $ vcat (map text $ replicate h " | ")
226+ beg = lblock 2 $ vcat (map text $ replicate h "| ")
227+ end = lblock 2 $ vcat (map text $ replicate h " |")
228+ middle = chomp $ hcat $ intersperse sep' blocks
229+ let makeRow = hpipeBlocks . zipWith lblock widthsInChars
230+ let head' = makeRow headers'
231+ let rows' = map (makeRow . map chomp) rawRows
232+ let borderpart ch align widthInChars =
233+ let widthInChars' = if widthInChars < 1 then 1 else widthInChars
234+ in (if (align == AlignLeft || align == AlignCenter)
237+ text (replicate widthInChars' ch) <>
238+ (if (align == AlignRight || align == AlignCenter)
241+ let border ch aligns' widthsInChars' =
243+ hcat (intersperse (char '+') (zipWith (borderpart ch)
244+ aligns' widthsInChars')) <> char '+'
245+ let body = vcat $ intersperse (border '-' (repeat AlignDefault) widthsInChars)
247+ let head'' = if headless
249+ else head' $$ border '=' aligns widthsInChars
252+ border '-' aligns widthsInChars $$
254+ border '-' (repeat AlignDefault) widthsInChars
256+ border '-' (repeat AlignDefault) widthsInChars $$
259+ border '-' (repeat AlignDefault) widthsInChars