git.iko.soy/typeable/pandoc
on master help sign in
Moved gridTable from Markdown writer to Writers.Shared.
commite7336b1feb4c5282b15b0e369539a34984362b40 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 21 Mar 2017, 10:02 (10 years ago) parent48c88d5
3 files changed+43−43
src/Text/Pandoc/Writers/Haddock.hs+1−1 src/Text/Pandoc/Writers/Markdown.hs−41 src/Text/Pandoc/Writers/Shared.hs+42−1
src/Text/Pandoc/Writers/Haddock.hs+1−1
@@ -45,7 +45,7 @@ import Text.Pandoc.Pretty
4545import Text.Pandoc.Shared
4646import Text.Pandoc.Templates (renderTemplate')
4747import Text.Pandoc.Writers.Math (texMathToInlines)
48−import Text.Pandoc.Writers.Shared
48+import Text.Pandoc.Writers.Shared hiding (gridTable)
4949
5050type Notes = [[Block]]
5151data WriterState = WriterState { stNotes :: Notes }
src/Text/Pandoc/Writers/Markdown.hs−41
@@ -705,47 +705,6 @@ pandocTable opts headless aligns widths rawHeaders rawRows = do
705705 else border
706706 return $ head'' $$ underline $$ body $$ bottom
707707
708−gridTable :: PandocMonad m => Bool -> [Alignment] -> [Int]
709− -> [Doc] -> [[Doc]] -> MD m Doc
710−gridTable headless aligns widthsInChars headers' rawRows = do
711− let hpipeBlocks blocks = hcat [beg, middle, end]
712− where h = maximum (1 : map height blocks)
713− sep' = lblock 3 $ vcat (map text $ replicate h " | ")
714− beg = lblock 2 $ vcat (map text $ replicate h "| ")
715− end = lblock 2 $ vcat (map text $ replicate h " |")
716− middle = chomp $ hcat $ intersperse sep' blocks
717− let makeRow = hpipeBlocks . zipWith lblock widthsInChars
718− let head' = makeRow headers'
719− let rows' = map (makeRow . map chomp) rawRows
720− let borderpart ch align widthInChars =
721− let widthInChars' = if widthInChars < 1 then 1 else widthInChars
722− in (if (align == AlignLeft || align == AlignCenter)
723− then char ':'
724− else char ch) <>
725− text (replicate widthInChars' ch) <>
726− (if (align == AlignRight || align == AlignCenter)
727− then char ':'
728− else char ch)
729− let border ch aligns' widthsInChars' =
730− char '+' <>
731− hcat (intersperse (char '+') (zipWith (borderpart ch)
732− aligns' widthsInChars')) <> char '+'
733− let body = vcat $ intersperse (border '-' (repeat AlignDefault) widthsInChars)
734− rows'
735− let head'' = if headless
736− then empty
737− else head' $$ border '=' aligns widthsInChars
738− if headless
739− then return $
740− border '-' aligns widthsInChars $$
741− body $$
742− border '-' (repeat AlignDefault) widthsInChars
743− else return $
744− border '-' (repeat AlignDefault) widthsInChars $$
745− head'' $$
746− body $$
747− border '-' (repeat AlignDefault) widthsInChars
748−
749708itemEndsWithTightList :: [Block] -> Bool
750709itemEndsWithTightList bs =
751710 case bs of
src/Text/Pandoc/Writers/Shared.hs+42−1
@@ -39,13 +39,14 @@ module Text.Pandoc.Writers.Shared (
3939 , tagWithAttrs
4040 , fixDisplayMath
4141 , unsmartify
42+ , gridTable
4243 )
4344where
4445import Control.Monad (liftM)
4546import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
4647 encode, fromJSON)
4748import qualified Data.HashMap.Strict as H
48−import Data.List (groupBy)
49+import Data.List (groupBy, intersperse)
4950import qualified Data.Map as M
5051import Data.Maybe (isJust)
5152import qualified Data.Text as T
@@ -216,3 +217,43 @@ unsmartify opts ('\8212':xs)
216217unsmartify opts (x:xs) = x : unsmartify opts xs
217218unsmartify _ [] = []
218219
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)
235+ then char ':'
236+ else char ch) <>
237+ text (replicate widthInChars' ch) <>
238+ (if (align == AlignRight || align == AlignCenter)
239+ then char ':'
240+ else char ch)
241+ let border ch aligns' widthsInChars' =
242+ char '+' <>
243+ hcat (intersperse (char '+') (zipWith (borderpart ch)
244+ aligns' widthsInChars')) <> char '+'
245+ let body = vcat $ intersperse (border '-' (repeat AlignDefault) widthsInChars)
246+ rows'
247+ let head'' = if headless
248+ then empty
249+ else head' $$ border '=' aligns widthsInChars
250+ if headless
251+ then return $
252+ border '-' aligns widthsInChars $$
253+ body $$
254+ border '-' (repeat AlignDefault) widthsInChars
255+ else return $
256+ border '-' (repeat AlignDefault) widthsInChars $$
257+ head'' $$
258+ body $$
259+ border '-' (repeat AlignDefault) widthsInChars