@@ -24,10 +24,9 @@ import Control.Monad.Reader
2424import Control.Monad.State.Strict
2525import Data.Char (isAlphaNum)
2626import Data.Default
27−import Data.List (find, intersperse, sortBy, transpose)
27+import Data.List (find, intersperse, sortOn, transpose)
2828import qualified Data.Map as M
2929import Data.Maybe (fromMaybe, mapMaybe)
30−import Data.Ord (comparing)
3130import qualified Data.Set as Set
3231import Data.Text (Text)
3332import qualified Data.Text as T
@@ -127,7 +126,7 @@ pandocTitleBlock tit auths dat =
127126
128127mmdTitleBlock :: Context Text -> Doc Text
129128mmdTitleBlock (Context hashmap) =
130− vcat $ map go $ sortBy (comparing fst) $ M.toList hashmap
129+ vcat $ map go $ sortOn fst $ M.toList hashmap
131130 where go (k,v) =
132131 case (text (T.unpack k), v) of
133132 (k', ListVal xs)
@@ -148,15 +147,15 @@ mmdTitleBlock (Context hashmap) =
148147plainTitleBlock :: Doc Text -> [Doc Text] -> Doc Text -> Doc Text
149148plainTitleBlock tit auths dat =
150149 tit <> cr <>
151− (hcat (intersperse (text "; ") auths)) <> cr <>
150+ hcat (intersperse (text "; ") auths) <> cr <>
152151 dat <> cr
153152
154153yamlMetadataBlock :: Context Text -> Doc Text
155−yamlMetadataBlock v = "---" $$ (contextToYaml v) $$ "---"
154+yamlMetadataBlock v = "---" $$ contextToYaml v $$ "---"
156155
157156contextToYaml :: Context Text -> Doc Text
158157contextToYaml (Context o) =
159− vcat $ map keyvalToYaml $ sortBy (comparing fst) $ M.toList o
158+ vcat $ map keyvalToYaml $ sortOn fst $ M.toList o
160159 where
161160 keyvalToYaml (k,v) =
162161 case (text (T.unpack k), v) of
@@ -250,7 +249,7 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
250249
251250-- | Return markdown representation of reference key table.
252251refsToMarkdown :: PandocMonad m => WriterOptions -> Refs -> MD m (Doc Text)
253−refsToMarkdown opts refs = mapM (keyToMarkdown opts) refs >>= return . vcat
252+refsToMarkdown opts refs = vcat <$> mapM (keyToMarkdown opts) refs
254253
255254-- | Return markdown representation of a reference key.
256255keyToMarkdown :: PandocMonad m
@@ -446,7 +445,7 @@ blockToMarkdown' opts (Plain inlines) = do
446445 then inlines
447446 else case inlines of
448447 (Str t:ys)
449− | (null ys || startsWithSpace ys)
448+ | null ys || startsWithSpace ys
450449 , beginsWithOrderedListMarker t
451450 -> RawInline (Format "markdown") (escapeMarker t):ys
452451 (Str t:_)
@@ -462,7 +461,7 @@ blockToMarkdown' opts (Para [Image attr alt (src,tgt@(T.stripPrefix "fig:" -> Ju
462461 | isEnabled Ext_raw_html opts &&
463462 not (isEnabled Ext_link_attributes opts) &&
464463 attr /= nullAttr = -- use raw HTML
465− ((<> blankline) . literal . T.strip) <$>
464+ (<> blankline) . literal . T.strip <$>
466465 writeHtml5String opts{ writerTemplate = Nothing }
467466 (Pandoc nullMeta [Para [Image attr alt (src,tgt)]])
468467 | otherwise = blockToMarkdown opts (Para [Image attr alt (src,tit)])
@@ -472,7 +471,7 @@ blockToMarkdown' opts (LineBlock lns) =
472471 if isEnabled Ext_line_blocks opts
473472 then do
474473 mdLines <- mapM (inlineListToMarkdown opts) lns
475− return $ (vcat $ map (hang 2 (literal "| ")) mdLines) <> blankline
474+ return $ vcat (map (hang 2 (literal "| ")) mdLines) <> blankline
476475 else blockToMarkdown opts $ linesToPara lns
477476blockToMarkdown' opts b@(RawBlock f str) = do
478477 variant <- asks envVariant
@@ -582,28 +581,28 @@ blockToMarkdown' opts (CodeBlock attribs str) = do
582581 attrs = if isEnabled Ext_fenced_code_attributes opts
583582 then nowrap $ " " <> attrsToMarkdown attribs
584583 else case attribs of
585− (_,(cls:_),_) -> " " <> literal cls
584+ (_,cls:_,_) -> " " <> literal cls
586585 _ -> empty
587586blockToMarkdown' opts (BlockQuote blocks) = do
588587 variant <- asks envVariant
589588 -- if we're writing literate haskell, put a space before the bird tracks
590589 -- so they won't be interpreted as lhs...
591− let leader = if isEnabled Ext_literate_haskell opts
592− then " > "
593− else if variant == PlainText then " " else "> "
590+ let leader
591+ | isEnabled Ext_literate_haskell opts = " > "
592+ | variant == PlainText = " "
593+ | otherwise = "> "
594594 contents <- blockListToMarkdown opts blocks
595− return $ (prefixed leader contents) <> blankline
595+ return $ prefixed leader contents <> blankline
596596blockToMarkdown' opts t@(Table _ blkCapt specs thead tbody tfoot) = do
597597 let (caption, aligns, widths, headers, rows) = toLegacyTable blkCapt specs thead tbody tfoot
598598 let numcols = maximum (length aligns : length widths :
599599 map length (headers:rows))
600600 caption' <- inlineListToMarkdown opts caption
601− let caption'' = if null caption
602− then blankline
603− else
604− if isEnabled Ext_table_captions opts
605− then blankline $$ (": " <> caption') $$ blankline
606− else blankline $$ caption' $$ blankline
601+ let caption''
602+ | null caption = blankline
603+ | isEnabled Ext_table_captions opts
604+ = blankline $$ (": " <> caption') $$ blankline
605+ | otherwise = blankline $$ caption' $$ blankline
607606 let hasSimpleCells = onlySimpleTableCells $ headers : rows
608607 let isSimple = hasSimpleCells && all (==0) widths
609608 let isPlainBlock (Plain _) = True
@@ -652,7 +651,7 @@ blockToMarkdown' opts t@(Table _ blkCapt specs thead tbody tfoot) = do
652651 (id,) <$> pipeTable (all null headers) aligns' rawHeaders rawRows
653652 | isEnabled Ext_raw_html opts -> fmap (id,) $
654653 literal <$>
655− (writeHtml5String opts{ writerTemplate = Nothing } $ Pandoc nullMeta [t])
654+ writeHtml5String opts{ writerTemplate = Nothing } (Pandoc nullMeta [t])
656655 | otherwise -> return (id, literal "[TABLE]")
657656 return $ nst (tbl $$ caption'') $$ blankline
658657blockToMarkdown' opts (BulletList items) = do
@@ -680,7 +679,7 @@ inList p = local (\env -> env {envInList = True}) p
680679addMarkdownAttribute :: Text -> Text
681680addMarkdownAttribute s =
682681 case span isTagText $ reverse $ parseTags s of
683− (xs,(TagOpen t attrs:rest)) ->
682+ (xs, TagOpen t attrs:rest) ->
684683 renderTags' $ reverse rest ++ (TagOpen t attrs' : reverse xs)
685684 where attrs' = ("markdown","1"):[(x,y) | (x,y) <- attrs,
686685 x /= "markdown"]
@@ -745,17 +744,16 @@ pandocTable opts multiline headless aligns widths rawHeaders rawRows = do
745744 | isSimple = map numChars columns
746745 | otherwise = zipWith relWidth widths columns
747746 let makeRow = hcat . intersperse (lblock 1 (literal " ")) .
748− (zipWith3 alignHeader aligns widthsInChars)
747+ zipWith3 alignHeader aligns widthsInChars
749748 let rows' = map makeRow rawRows
750749 let head' = makeRow rawHeaders
751750 let underline = mconcat $ intersperse (literal " ") $
752751 map (\width -> literal (T.replicate width "-")) widthsInChars
753− let border = if multiline
754− then literal (T.replicate (sum widthsInChars +
755− length widthsInChars - 1) "-")
756− else if headless
757− then underline
758− else empty
752+ let border
753+ | multiline = literal (T.replicate (sum widthsInChars +
754+ length widthsInChars - 1) "-")
755+ | headless = underline
756+ | otherwise = empty
759757 let head'' = if headless
760758 then empty
761759 else border <> cr <> head'
@@ -890,18 +888,17 @@ blockListToMarkdown opts blocks = do
890888 isListBlock (OrderedList _ _) = True
891889 isListBlock (DefinitionList _) = True
892890 isListBlock _ = False
893− commentSep = if variant == PlainText
894− then Null
895− else if isEnabled Ext_raw_html opts
896− then RawBlock "html" "<!-- -->\n"
897− else RawBlock "markdown" " \n"
898− mapM (blockToMarkdown opts) (fixBlocks blocks) >>= return . mconcat
891+ commentSep
892+ | variant == PlainText = Null
893+ | isEnabled Ext_raw_html opts = RawBlock "html" "<!-- -->\n"
894+ | otherwise = RawBlock "markdown" " \n"
895+ mconcat <$> mapM (blockToMarkdown opts) (fixBlocks blocks)
899896
900897getKey :: Doc Text -> Key
901898getKey = toKey . render Nothing
902899
903900findUsableIndex :: [Text] -> Int -> Int
904−findUsableIndex lbls i = if (tshow i) `elem` lbls
901+findUsableIndex lbls i = if tshow i `elem` lbls
905902 then findUsableIndex lbls (i + 1)
906903 else i
907904
@@ -973,19 +970,19 @@ inlineListToMarkdown opts lst = do
973970 go (if inlist then avoidBadWrapsInList lst else lst)
974971 where go [] = return empty
975972 go (i:is) = case i of
976− (Link _ _ _) -> case is of
973+ Link {} -> case is of
977974 -- If a link is followed by another link, or '[', '(' or ':'
978975 -- then we don't shortcut
979− (Link _ _ _):_ -> unshortcutable
980− Space:(Link _ _ _):_ -> unshortcutable
976+ Link {}:_ -> unshortcutable
977+ Space:Link {}:_ -> unshortcutable
981978 Space:(Str(thead -> Just '[')):_ -> unshortcutable
982979 Space:(RawInline _ (thead -> Just '[')):_ -> unshortcutable
983980 Space:(Cite _ _):_ -> unshortcutable
984− SoftBreak:(Link _ _ _):_ -> unshortcutable
981+ SoftBreak:Link {}:_ -> unshortcutable
985982 SoftBreak:(Str(thead -> Just '[')):_ -> unshortcutable
986983 SoftBreak:(RawInline _ (thead -> Just '[')):_ -> unshortcutable
987984 SoftBreak:(Cite _ _):_ -> unshortcutable
988− LineBreak:(Link _ _ _):_ -> unshortcutable
985+ LineBreak:Link {}:_ -> unshortcutable
989986 LineBreak:(Str(thead -> Just '[')):_ -> unshortcutable
990987 LineBreak:(RawInline _ (thead -> Just '[')):_ -> unshortcutable
991988 LineBreak:(Cite _ _):_ -> unshortcutable
@@ -1016,16 +1013,16 @@ avoidBadWrapsInList :: [Inline] -> [Inline]
10161013avoidBadWrapsInList [] = []
10171014avoidBadWrapsInList (s:Str (T.uncons -> Just ('>',cs)):xs) | isSp s =
10181015 Str (" >" <> cs) : avoidBadWrapsInList xs
1019−avoidBadWrapsInList (s:Str (T.uncons -> Just (c, cs)):[])
1020− | T.null cs && isSp s && c `elem` ['-','*','+'] = Str (T.pack [' ', c]) : []
1016+avoidBadWrapsInList [s, Str (T.uncons -> Just (c, cs))]
1017+ | T.null cs && isSp s && c `elem` ['-','*','+'] = [Str $ T.pack [' ', c]]
10211018avoidBadWrapsInList (s:Str (T.uncons -> Just (c, cs)):Space:xs)
10221019 | T.null cs && isSp s && c `elem` ['-','*','+'] =
10231020 Str (T.pack [' ', c]) : Space : avoidBadWrapsInList xs
10241021avoidBadWrapsInList (s:Str cs:Space:xs)
10251022 | isSp s && isOrderedListMarker cs =
10261023 Str (" " <> cs) : Space : avoidBadWrapsInList xs
1027−avoidBadWrapsInList (s:Str cs:[])
1028− | isSp s && isOrderedListMarker cs = Str (" " <> cs) : []
1024+avoidBadWrapsInList [s, Str cs]
1025+ | isSp s && isOrderedListMarker cs = [Str $ " " <> cs]
10291026avoidBadWrapsInList (x:xs) = x : avoidBadWrapsInList xs
10301027
10311028isOrderedListMarker :: Text -> Bool
@@ -1105,7 +1102,7 @@ inlineToMarkdown opts (Strikeout lst) = do
11051102 else contents
11061103inlineToMarkdown _ (Superscript []) = return empty
11071104inlineToMarkdown opts (Superscript lst) =
1108− local (\env -> env {envEscapeSpaces = (envVariant env == Markdown)}) $ do
1105+ local (\env -> env {envEscapeSpaces = envVariant env == Markdown}) $ do
11091106 contents <- inlineListToMarkdown opts lst
11101107 if isEnabled Ext_superscript opts
11111108 then return $ "^" <> contents <> "^"
@@ -1123,7 +1120,7 @@ inlineToMarkdown opts (Superscript lst) =
11231120 Nothing -> literal $ "^(" <> rendered <> ")"
11241121inlineToMarkdown _ (Subscript []) = return empty
11251122inlineToMarkdown opts (Subscript lst) =
1126− local (\env -> env {envEscapeSpaces = (envVariant env == Markdown)}) $ do
1123+ local (\env -> env {envEscapeSpaces = envVariant env == Markdown}) $ do
11271124 contents <- inlineListToMarkdown opts lst
11281125 if isEnabled Ext_subscript opts
11291126 then return $ "~" <> contents <> "~"
@@ -1167,7 +1164,7 @@ inlineToMarkdown opts (Code attr str) = do
11671164 then 0
11681165 else maximum $ map T.length tickGroups
11691166 let marker = T.replicate (longest + 1) "`"
1170− let spacer = if (longest == 0) then "" else " "
1167+ let spacer = if longest == 0 then "" else " "
11711168 let attrs = if isEnabled Ext_inline_code_attributes opts && attr /= nullAttr
11721169 then attrsToMarkdown attr
11731170 else empty
@@ -1296,7 +1293,7 @@ inlineToMarkdown opts lnk@(Link attr txt (src, tit))
12961293 | isEnabled Ext_raw_html opts &&
12971294 not (isEnabled Ext_link_attributes opts) &&
12981295 attr /= nullAttr = -- use raw HTML
1299− (literal . T.strip) <$>
1296+ literal . T.strip <$>
13001297 writeHtml5String opts{ writerTemplate = Nothing } (Pandoc nullMeta [Plain [lnk]])
13011298 | otherwise = do
13021299 variant <- asks envVariant
@@ -1337,7 +1334,7 @@ inlineToMarkdown opts img@(Image attr alternate (source, tit))
13371334 | isEnabled Ext_raw_html opts &&
13381335 not (isEnabled Ext_link_attributes opts) &&
13391336 attr /= nullAttr = -- use raw HTML
1340− (literal . T.strip) <$>
1337+ literal . T.strip <$>
13411338 writeHtml5String opts{ writerTemplate = Nothing } (Pandoc nullMeta [Plain [img]])
13421339 | otherwise = do
13431340 variant <- asks envVariant
@@ -1352,7 +1349,7 @@ inlineToMarkdown opts img@(Image attr alternate (source, tit))
13521349inlineToMarkdown opts (Note contents) = do
13531350 modify (\st -> st{ stNotes = contents : stNotes st })
13541351 st <- get
1355− let ref = literal $ writerIdentifierPrefix opts <> tshow (stNoteNum st + (length $ stNotes st) - 1)
1352+ let ref = literal $ writerIdentifierPrefix opts <> tshow (stNoteNum st + length (stNotes st) - 1)
13561353 if isEnabled Ext_footnotes opts
13571354 then return $ "[^" <> ref <> "]"
13581355 else return $ "[" <> ref <> "]"