@@ -24,6 +24,7 @@ import Control.Monad.Reader
2424import Control.Monad.State.Strict
2525import Data.Default
2626import Data.List (intersperse, sortOn, transpose)
27+import Data.List.NonEmpty (nonEmpty, NonEmpty(..))
2728import qualified Data.Map as M
2829import Data.Maybe (fromMaybe, mapMaybe)
2930import qualified Data.Set as Set
@@ -492,19 +493,20 @@ blockToMarkdown' opts (CodeBlock attribs str) = do
492493 | isEnabled Ext_fenced_code_blocks opts ->
493494 tildes <> attrs <> cr <> literal str <> cr <> tildes <> blankline
494495 _ -> nest (writerTabStop opts) (literal str) <> blankline
495− where endline c = literal $ case [T.length ln
496− | ln <- map trim (T.lines str)
497− , T.pack [c,c,c] `T.isPrefixOf` ln
498− , T.all (== c) ln] of
499− [] -> T.replicate 3 $ T.singleton c
500− xs -> T.replicate (maximum xs + 1) $ T.singleton c
501− backticks = endline '`'
502− tildes = endline '~'
503− attrs = if isEnabled Ext_fenced_code_attributes opts
504− then nowrap $ " " <> attrsToMarkdown attribs
505− else case attribs of
506− (_,cls:_,_) -> " " <> literal cls
507− _ -> empty
496+ where
497+ endlineLen c = maybe 3 ((+1) . maximum) $ nonEmpty $
498+ [T.length ln
499+ | ln <- map trim (T.lines str)
500+ , T.pack [c,c,c] `T.isPrefixOf` ln
501+ , T.all (== c) ln]
502+ endline c = literal $ T.replicate (endlineLen c) $ T.singleton c
503+ backticks = endline '`'
504+ tildes = endline '~'
505+ attrs = if isEnabled Ext_fenced_code_attributes opts
506+ then nowrap $ " " <> attrsToMarkdown attribs
507+ else case attribs of
508+ (_,cls:_,_) -> " " <> literal cls
509+ _ -> empty
508510blockToMarkdown' opts (BlockQuote blocks) = do
509511 variant <- asks envVariant
510512 -- if we're writing literate haskell, put a space before the bird tracks
@@ -517,7 +519,7 @@ blockToMarkdown' opts (BlockQuote blocks) = do
517519 return $ prefixed leader contents <> blankline
518520blockToMarkdown' opts t@(Table _ blkCapt specs thead tbody tfoot) = do
519521 let (caption, aligns, widths, headers, rows) = toLegacyTable blkCapt specs thead tbody tfoot
520− let numcols = maximum (length aligns : length widths :
522+ let numcols = maximum (length aligns :| length widths :
521523 map length (headers:rows))
522524 caption' <- inlineListToMarkdown opts caption
523525 let caption''
@@ -619,7 +621,8 @@ pipeTable headless aligns rawHeaders rawRows = do
619621 blockFor AlignCenter x y = cblock (x + 2) (sp <> y <> sp) <> lblock 0 empty
620622 blockFor AlignRight x y = rblock (x + 2) (y <> sp) <> lblock 0 empty
621623 blockFor _ x y = lblock (x + 2) (sp <> y) <> lblock 0 empty
622− let widths = map (max 3 . maximum . map offset) $ transpose (rawHeaders : rawRows)
624+ let widths = map (max 3 . maybe 3 maximum . nonEmpty . map offset) $
625+ transpose (rawHeaders : rawRows)
623626 let torow cs = nowrap $ literal "|" <>
624627 hcat (intersperse (literal "|") $
625628 zipWith3 blockFor aligns widths (map chomp cs))
@@ -653,11 +656,11 @@ pandocTable opts multiline headless aligns widths rawHeaders rawRows = do
653656 -- Number of characters per column necessary to output every cell
654657 -- without requiring a line break.
655658 -- The @+2@ is needed for specifying the alignment.
656− let numChars = (+ 2) . maximum . map offset
659+ let numChars = (+ 2) . maybe 0 maximum . nonEmpty . map offset
657660 -- Number of characters per column necessary to output every cell
658661 -- without requiring a line break *inside a word*.
659662 -- The @+2@ is needed for specifying the alignment.
660− let minNumChars = (+ 2) . maximum . map minOffset
663+ let minNumChars = (+ 2) . maybe 0 maximum . nonEmpty . map minOffset
661664 let columns = transpose (rawHeaders : rawRows)
662665 -- minimal column width without wrapping a single word
663666 let relWidth w col =