@@ -42,7 +42,7 @@ module Text.Pandoc.Writers.Shared (
42 , gridTable
42 , gridTable
45−import Control.Monad (liftM)
45+import Control.Monad (liftM, zipWithM)
46import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
46import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
47 encode, fromJSON)
47 encode, fromJSON)
48import qualified Data.HashMap.Strict as H
48import qualified Data.HashMap.Strict as H
@@ -217,9 +217,34 @@ unsmartify opts ('\8212':xs)
217unsmartify opts (x:xs) = x : unsmartify opts xs
217unsmartify opts (x:xs) = x : unsmartify opts xs
218unsmartify _ [] = []
218unsmartify _ [] = []
220−gridTable :: Monad m => Bool -> [Alignment] -> [Int]
220+gridTable :: Monad m
222+ -> (WriterOptions -> [Block] -> m Doc)
223+ -> Bool -- ^ headless
221− -> [Doc] -> [[Doc]] -> m Doc
228+ -> m Doc
222−gridTable headless aligns widthsInChars headers' rawRows = do
229+gridTable opts blocksToDoc headless aligns widths headers rows = do
230+ let numcols = maximum (length aligns : length widths :
231+ map length (headers:rows))
232+ let widths' = if all (==0) widths
233+ then replicate numcols
234+ (1.0 / fromIntegral numcols)
236+ let widthsInChars = map ((\x -> x - 3) . floor .
237+ (fromIntegral (writerColumns opts) *)) widths'
238+ rawHeaders <- zipWithM blocksToDoc
239+ (map (\w -> opts{writerColumns =
240+ min (w - 2) (writerColumns opts)}) widthsInChars)
243+ (\cs -> zipWithM blocksToDoc
244+ (map (\w -> opts{writerColumns =
245+ min (w - 2) (writerColumns opts)}) widthsInChars)
223 let hpipeBlocks blocks = hcat [beg, middle, end]
248 let hpipeBlocks blocks = hcat [beg, middle, end]
224 where h = maximum (1 : map height blocks)
249 where h = maximum (1 : map height blocks)
225 sep' = lblock 3 $ vcat (map text $ replicate h " | ")
250 sep' = lblock 3 $ vcat (map text $ replicate h " | ")
@@ -227,7 +252,7 @@ gridTable headless aligns widthsInChars headers' rawRows = do
227 end = lblock 2 $ vcat (map text $ replicate h " |")
252 end = lblock 2 $ vcat (map text $ replicate h " |")
228 middle = chomp $ hcat $ intersperse sep' blocks
253 middle = chomp $ hcat $ intersperse sep' blocks
229 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
254 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
230− let head' = makeRow headers'
255+ let head' = makeRow rawHeaders
231 let rows' = map (makeRow . map chomp) rawRows
256 let rows' = map (makeRow . map chomp) rawRows
232 let borderpart ch align widthInChars =
257 let borderpart ch align widthInChars =
233 let widthInChars' = if widthInChars < 1 then 1 else widthInChars
258 let widthInChars' = if widthInChars < 1 then 1 else widthInChars