git.iko.soy/typeable/pandoc
on master help sign in
Moved more gridTable calculations to Writers.Shared.
commite6cdf21fa5d68409f362bd89cc56090d34983cb3 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 21 Mar 2017, 10:16 (10 years ago) parente7336b1
2 files changed+33−29
src/Text/Pandoc/Writers/Markdown.hs+3−24 src/Text/Pandoc/Writers/Shared.hs+30−5
src/Text/Pandoc/Writers/Markdown.hs+3−24
@@ -565,30 +565,9 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
565565 pandocTable opts (all null headers) aligns' widths'
566566 rawHeaders rawRows
567567 | isEnabled Ext_grid_tables opts &&
568− writerColumns opts >= 8 * numcols -> do
569− let widths'' = if all (==0) widths'
570− then replicate numcols
571− (1.0 / fromIntegral numcols)
572− else widths'
573− let widthsInChars = map ((\x -> x - 3) . floor .
574− (fromIntegral (writerColumns opts) *)) widths''
575− rawHeaders' <- zipWithM
576− blockListToMarkdown
577− (map (\w -> opts{writerColumns =
578− min (w - 2) (writerColumns opts)})
579− widthsInChars)
580− headers
581− rawRows' <- mapM
582− (\cs -> zipWithM
583− blockListToMarkdown
584− (map (\w -> opts{writerColumns =
585− min (w - 2) (writerColumns opts)})
586− widthsInChars)
587− cs)
588− rows
589− fmap (id,) $
590− gridTable (all null headers) aligns' widthsInChars
591− rawHeaders' rawRows'
568+ writerColumns opts >= 8 * numcols -> (id,) <$>
569+ gridTable opts blockListToMarkdown
570+ (all null headers) aligns' widths' headers rows
592571 | isEnabled Ext_raw_html opts -> fmap (id,) $
593572 text <$>
594573 (writeHtml5String def $ Pandoc nullMeta [t])
src/Text/Pandoc/Writers/Shared.hs+30−5
@@ -42,7 +42,7 @@ module Text.Pandoc.Writers.Shared (
4242 , gridTable
4343 )
4444where
45−import Control.Monad (liftM)
45+import Control.Monad (liftM, zipWithM)
4646import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
4747 encode, fromJSON)
4848import qualified Data.HashMap.Strict as H
@@ -217,9 +217,34 @@ unsmartify opts ('\8212':xs)
217217unsmartify opts (x:xs) = x : unsmartify opts xs
218218unsmartify _ [] = []
219219
220−gridTable :: Monad m => Bool -> [Alignment] -> [Int]
221− -> [Doc] -> [[Doc]] -> m Doc
222−gridTable headless aligns widthsInChars headers' rawRows = do
220+gridTable :: Monad m
221+ => WriterOptions
222+ -> (WriterOptions -> [Block] -> m Doc)
223+ -> Bool -- ^ headless
224+ -> [Alignment]
225+ -> [Double]
226+ -> [[Block]]
227+ -> [[[Block]]]
228+ -> m Doc
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)
235+ else widths
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)
241+ headers
242+ rawRows <- mapM
243+ (\cs -> zipWithM blocksToDoc
244+ (map (\w -> opts{writerColumns =
245+ min (w - 2) (writerColumns opts)}) widthsInChars)
246+ cs)
247+ rows
223248 let hpipeBlocks blocks = hcat [beg, middle, end]
224249 where h = maximum (1 : map height blocks)
225250 sep' = lblock 3 $ vcat (map text $ replicate h " | ")
@@ -227,7 +252,7 @@ gridTable headless aligns widthsInChars headers' rawRows = do
227252 end = lblock 2 $ vcat (map text $ replicate h " |")
228253 middle = chomp $ hcat $ intersperse sep' blocks
229254 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
230− let head' = makeRow headers'
255+ let head' = makeRow rawHeaders
231256 let rows' = map (makeRow . map chomp) rawRows
232257 let borderpart ch align widthInChars =
233258 let widthInChars' = if widthInChars < 1 then 1 else widthInChars