{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE BangPatterns #-}
module Text.Pandoc.Writers.Markdown (
writeMarkdown,
writeCommonMark,
writeMarkua,
writePlain) where
import Control.Monad (foldM, zipWithM, MonadPlus(..), when)
import Control.Monad.Reader ( asks, MonadReader(local) )
import Control.Monad.State.Strict ( gets, modify )
import Data.Default
import Data.List (intersperse, sortOn, union)
import Data.List.NonEmpty (nonEmpty, NonEmpty(..))
import qualified Data.Map as M
import Data.Maybe (fromMaybe, mapMaybe, isNothing)
import qualified Data.Set as Set
import Data.Text (Text)
import Data.Char (isSpace)
import qualified Data.Text as T
import Text.HTML.TagSoup (Tag (..), isTagText, parseTags)
import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
import Text.Pandoc.Definition
import Text.Pandoc.Logging
import Text.Pandoc.Options
import Text.Pandoc.Parsing hiding (blankline, blanklines, char, space)
import Text.DocLayout
import Text.Pandoc.Shared
import Text.Pandoc.Writers.Shared
import Text.Pandoc.Templates (renderTemplate)
import Text.DocTemplates (Val(..), Context(..), FromContext(..))
import Text.Pandoc.Walk
import Text.Pandoc.Writers.HTML (writeHtml5String)
import Text.Pandoc.Writers.Markdown.Inline (inlineListToMarkdown,
linkAttributes,
attrsToMarkdown,
attrsToMarkua)
import Text.Pandoc.Writers.Markdown.Table (pipeTable, pandocTable)
import Text.Pandoc.Writers.Markdown.Types (MarkdownVariant(..),
WriterState(..),
WriterEnv(..),
Ref, Refs, MD, evalMD)
writeMarkdown :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeMarkdown WriterOptions
opts Pandoc
document =
forall (m :: * -> *) a.
PandocMonad m =>
MD m a -> WriterEnv -> WriterState -> m a
evalMD (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> MD m Text
pandocToMarkdown WriterOptions
opts{
writerWrapText :: WrapOption
writerWrapText = if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_hard_line_breaks WriterOptions
opts
then WrapOption
WrapNone
else WriterOptions -> WrapOption
writerWrapText WriterOptions
opts }
Pandoc
document) forall a. Default a => a
def forall a. Default a => a
def
writePlain :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writePlain :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writePlain WriterOptions
opts Pandoc
document =
forall (m :: * -> *) a.
PandocMonad m =>
MD m a -> WriterEnv -> WriterState -> m a
evalMD (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> MD m Text
pandocToMarkdown WriterOptions
opts Pandoc
document) forall a. Default a => a
def{ envVariant :: MarkdownVariant
envVariant = MarkdownVariant
PlainText } forall a. Default a => a
def
writeCommonMark :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeCommonMark :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeCommonMark WriterOptions
opts Pandoc
document =
forall (m :: * -> *) a.
PandocMonad m =>
MD m a -> WriterEnv -> WriterState -> m a
evalMD (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> MD m Text
pandocToMarkdown WriterOptions
opts' Pandoc
document) forall a. Default a => a
def{ envVariant :: MarkdownVariant
envVariant = MarkdownVariant
Commonmark } forall a. Default a => a
def
where
opts' :: WriterOptions
opts' = WriterOptions
opts{ writerExtensions :: Extensions
writerExtensions =
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_all_symbols_escapable forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_intraword_underscores forall a b. (a -> b) -> a -> b
$
WriterOptions -> Extensions
writerExtensions WriterOptions
opts ,
writerWrapText :: WrapOption
writerWrapText =
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_hard_line_breaks WriterOptions
opts
then WrapOption
WrapNone
else WriterOptions -> WrapOption
writerWrapText WriterOptions
opts }
writeMarkua :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeMarkua :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeMarkua WriterOptions
opts Pandoc
document =
forall (m :: * -> *) a.
PandocMonad m =>
MD m a -> WriterEnv -> WriterState -> m a
evalMD (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> MD m Text
pandocToMarkdown WriterOptions
opts' Pandoc
document) forall a. Default a => a
def{ envVariant :: MarkdownVariant
envVariant = MarkdownVariant
Markua } forall a. Default a => a
def
where
opts' :: WriterOptions
opts' = WriterOptions
opts{ writerExtensions :: Extensions
writerExtensions =
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_hard_line_breaks forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_pipe_tables forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_fancy_lists forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_startnum forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_strikeout forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_subscript forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_superscript forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_definition_lists forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_smart forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
enableExtension Extension
Ext_footnotes
forall a. Monoid a => a
mempty ,
writerWrapText :: WrapOption
writerWrapText =
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_hard_line_breaks WriterOptions
opts
then WrapOption
WrapNone
else WriterOptions -> WrapOption
writerWrapText WriterOptions
opts }
pandocTitleBlock :: Doc Text -> [Doc Text] -> Doc Text -> Doc Text
pandocTitleBlock :: Doc Text -> [Doc Text] -> Doc Text -> Doc Text
pandocTitleBlock Doc Text
tit [Doc Text]
auths Doc Text
dat =
forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
2 (forall a. HasChars a => String -> Doc a
text String
"% ") Doc Text
tit forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>
forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
2 (forall a. HasChars a => String -> Doc a
text String
"% ") (forall a. [Doc a] -> Doc a
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. IsString a => Doc a -> Doc a
nowrap [Doc Text]
auths) forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>
forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
2 (forall a. HasChars a => String -> Doc a
text String
"% ") Doc Text
dat forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
mmdTitleBlock :: Context Text -> Doc Text
mmdTitleBlock :: Context Text -> Doc Text
mmdTitleBlock (Context Map Text (Val Text)
hashmap) =
forall a. [Doc a] -> Doc a
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {a}.
(HasChars a, ToText a, FromText a) =>
(Text, Val a) -> Doc a
go forall a b. (a -> b) -> a -> b
$ forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (Text -> Text
T.toCaseFold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
M.toList Map Text (Val Text)
hashmap
where go :: (Text, Val a) -> Doc a
go (Text
k,Val a
v) =
case (forall a. HasChars a => String -> Doc a
text (Text -> String
T.unpack Text
k), Val a
v) of
(Doc a
k', ListVal [Val a]
xs)
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Val a]
xs -> forall a. Doc a
empty
| Bool
otherwise -> Doc a
k' forall a. Semigroup a => a -> a -> a
<> Doc a
":" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
space forall a. Semigroup a => a -> a -> a
<>
forall a. [Doc a] -> Doc a
hcat (forall a. a -> [a] -> [a]
intersperse Doc a
"; " forall a b. (a -> b) -> a -> b
$
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe forall a b. FromContext a b => Val a -> Maybe b
fromVal [Val a]
xs)
(Doc a
k', SimpleVal Doc a
x)
| forall a. Doc a -> Bool
isEmpty Doc a
x -> forall a. Doc a
empty
| Bool
otherwise -> Doc a
k' forall a. Semigroup a => a -> a -> a
<> Doc a
":" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
space forall a. Semigroup a => a -> a -> a
<>
forall a. IsString a => Int -> Doc a -> Doc a
nest Int
2 (forall {a}. HasChars a => Doc a -> Doc a
removeBlankLines (forall a. Doc a -> Doc a
chomp Doc a
x))
(Doc a, Val a)
_ -> forall a. Doc a
empty
removeBlankLines :: Doc a -> Doc a
removeBlankLines BlankLines{} = forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => String -> Doc a
text String
"." forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
removeBlankLines (Concat Doc a
x Doc a
y) = Doc a -> Doc a
removeBlankLines Doc a
x forall a. Semigroup a => a -> a -> a
<>
Doc a -> Doc a
removeBlankLines Doc a
y
removeBlankLines Doc a
x = Doc a
x
plainTitleBlock :: Doc Text -> [Doc Text] -> Doc Text -> Doc Text
plainTitleBlock :: Doc Text -> [Doc Text] -> Doc Text -> Doc Text
plainTitleBlock Doc Text
tit [Doc Text]
auths Doc Text
dat =
Doc Text
tit forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>
forall a. [Doc a] -> Doc a
hcat (forall a. a -> [a] -> [a]
intersperse (forall a. HasChars a => String -> Doc a
text String
"; ") [Doc Text]
auths) forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>
Doc Text
dat forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
yamlMetadataBlock :: Context Text -> Doc Text
yamlMetadataBlock :: Context Text -> Doc Text
yamlMetadataBlock Context Text
v = Doc Text
"---" forall a. Doc a -> Doc a -> Doc a
$$ Context Text -> Doc Text
contextToYaml Context Text
v forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"---"
contextToYaml :: Context Text -> Doc Text
contextToYaml :: Context Text -> Doc Text
contextToYaml (Context Map Text (Val Text)
o) =
forall a. [Doc a] -> Doc a
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Text, Val Text) -> Doc Text
keyvalToYaml forall a b. (a -> b) -> a -> b
$ forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (Text -> Text
T.toCaseFold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
M.toList Map Text (Val Text)
o
where
keyvalToYaml :: (Text, Val Text) -> Doc Text
keyvalToYaml (Text
k,Val Text
v) =
case (forall a. HasChars a => String -> Doc a
text (Text -> String
T.unpack Text
k), Val Text
v) of
(Doc Text
k', ListVal [Val Text]
vs)
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Val Text]
vs -> forall a. Doc a
empty
| Bool
otherwise -> (Doc Text
k' forall a. Semigroup a => a -> a -> a
<> Doc Text
":") forall a. Doc a -> Doc a -> Doc a
$$ Val Text -> Doc Text
valToYaml Val Text
v
(Doc Text
k', MapVal (Context Map Text (Val Text)
m))
| forall k a. Map k a -> Bool
M.null Map Text (Val Text)
m -> Doc Text
k' forall a. Semigroup a => a -> a -> a
<> Doc Text
": {}"
| Bool
otherwise -> (Doc Text
k' forall a. Semigroup a => a -> a -> a
<> Doc Text
":") forall a. Doc a -> Doc a -> Doc a
$$ forall a. IsString a => Int -> Doc a -> Doc a
nest Int
2 (Val Text -> Doc Text
valToYaml Val Text
v)
(Doc Text
_, SimpleVal Doc Text
x)
| forall a. Doc a -> Bool
isEmpty Doc Text
x -> forall a. Doc a
empty
(Doc Text
_, Val Text
NullVal) -> forall a. Doc a
empty
(Doc Text
k', Val Text
_) -> Doc Text
k' forall a. Semigroup a => a -> a -> a
<> Doc Text
":" forall a. Doc a -> Doc a -> Doc a
<+> forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
2 Doc Text
"" (Val Text -> Doc Text
valToYaml Val Text
v)
valToYaml :: Val Text -> Doc Text
valToYaml :: Val Text -> Doc Text
valToYaml (ListVal [Val Text]
xs) =
forall a. [Doc a] -> Doc a
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\Val Text
v -> forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
2 Doc Text
"- " (Val Text -> Doc Text
valToYaml Val Text
v)) [Val Text]
xs
valToYaml (MapVal Context Text
c) = Context Text -> Doc Text
contextToYaml Context Text
c
valToYaml (BoolVal Bool
True) = Doc Text
"true"
valToYaml (BoolVal Bool
False) = Doc Text
"false"
valToYaml (SimpleVal Doc Text
x)
| forall a. Doc a -> Bool
isEmpty Doc Text
x = forall a. Doc a
empty
| Bool
otherwise =
if forall a. Doc a -> Bool
hasNewlines Doc Text
x
then forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
0 (Doc Text
"|" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr) Doc Text
x
else case Doc Text
x of
Text Int
_ Text
t | Text -> Bool
isSpecialString Text
t ->
Doc Text
"\"" forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
escapeInDoubleQuotes Doc Text
x forall a. Semigroup a => a -> a -> a
<> Doc Text
"\""
Doc Text
_ | forall a. Maybe a -> Bool
isNothing (forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Bool -> Text -> Maybe Bool
needsDoubleQuotes Bool
True Doc Text
x) ->
Doc Text
"\"" forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
escapeInDoubleQuotes Doc Text
x forall a. Semigroup a => a -> a -> a
<> Doc Text
"\""
| Bool
otherwise -> Doc Text
x
where
isSpecialString :: Text -> Bool
isSpecialString Text
t = forall a. Ord a => a -> Set a -> Bool
Set.member Text
t Set Text
specialStrings
specialStrings :: Set Text
specialStrings = forall a. Ord a => [a] -> Set a
Set.fromList
[Text
"y", Text
"Y", Text
"yes", Text
"Yes", Text
"YES", Text
"n", Text
"N",
Text
"no", Text
"No", Text
"NO", Text
"true", Text
"True", Text
"TRUE",
Text
"false", Text
"False", Text
"FALSE", Text
"on", Text
"On", Text
"ON",
Text
"off", Text
"Off", Text
"OFF", Text
"null", Text
"Null",
Text
"NULL", Text
"~", Text
"*"]
needsDoubleQuotes :: Bool -> Text -> Maybe Bool
needsDoubleQuotes Bool
isFirst Text
t
= if (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
isBadAnywhere Text
t Bool -> Bool -> Bool
||
(Bool
isFirst Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
isYamlPunct (Int -> Text -> Text
T.take Int
1 Text
t))
then forall a. Maybe a
Nothing
else forall a. a -> Maybe a
Just Bool
False
isBadAnywhere :: Char -> Bool
isBadAnywhere Char
'#' = Bool
True
isBadAnywhere Char
':' = Bool
True
isBadAnywhere Char
_ = Bool
False
hasNewlines :: Doc a -> Bool
hasNewlines Doc a
NewLine = Bool
True
hasNewlines BlankLines{} = Bool
True
hasNewlines Doc a
CarriageReturn = Bool
True
hasNewlines (Concat Doc a
w Doc a
z) = Doc a -> Bool
hasNewlines Doc a
w Bool -> Bool -> Bool
|| Doc a -> Bool
hasNewlines Doc a
z
hasNewlines Doc a
_ = Bool
False
isYamlPunct :: Char -> Bool
isYamlPunct = (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'-',Char
'?',Char
':',Char
',',Char
'[',Char
']',Char
'{',Char
'}',
Char
'#',Char
'&',Char
'*',Char
'!',Char
'|',Char
'>',Char
'\'',Char
'"', Char
'%',Char
'@',Char
'`'])
escapeInDoubleQuotes :: Text -> Text
escapeInDoubleQuotes = Text -> Text -> Text -> Text
T.replace Text
"\"" Text
"\\\"" forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text -> Text
T.replace Text
"\\" Text
"\\\\"
valToYaml Val Text
_ = forall a. Doc a
empty
pandocToMarkdown :: PandocMonad m => WriterOptions -> Pandoc -> MD m Text
pandocToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> MD m Text
pandocToMarkdown WriterOptions
opts (Pandoc Meta
meta [Block]
blocks) = do
let colwidth :: Maybe Int
colwidth = if WriterOptions -> WrapOption
writerWrapText WriterOptions
opts forall a. Eq a => a -> a -> Bool
== WrapOption
WrapAuto
then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WriterOptions -> Int
writerColumns WriterOptions
opts
else forall a. Maybe a
Nothing
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
Context Text
metadata <- forall (m :: * -> *) a.
(Monad m, TemplateTarget a) =>
([Block] -> m (Doc a))
-> ([Inline] -> m (Doc a)) -> Meta -> m (Context a)
metaToContext'
(forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts)
(forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts)
Meta
meta
let title' :: Doc Text
title' = forall a. a -> Maybe a -> a
fromMaybe forall a. Doc a
empty forall a b. (a -> b) -> a -> b
$ forall a b. FromContext a b => Text -> Context a -> Maybe b
getField Text
"title" Context Text
metadata
let authors' :: [Doc Text]
authors' = forall a. a -> Maybe a -> a
fromMaybe [] forall a b. (a -> b) -> a -> b
$ forall a b. FromContext a b => Text -> Context a -> Maybe b
getField Text
"author" Context Text
metadata
let date' :: Doc Text
date' = forall a. a -> Maybe a -> a
fromMaybe forall a. Doc a
empty forall a b. (a -> b) -> a -> b
$ forall a b. FromContext a b => Text -> Context a -> Maybe b
getField Text
"date" Context Text
metadata
let titleblock :: Doc Text
titleblock = case WriterOptions -> Maybe (Template Text)
writerTemplate WriterOptions
opts of
Just Template Text
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText ->
Doc Text -> [Doc Text] -> Doc Text -> Doc Text
plainTitleBlock Doc Text
title' [Doc Text]
authors' Doc Text
date'
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_yaml_metadata_block WriterOptions
opts ->
Context Text -> Doc Text
yamlMetadataBlock Context Text
metadata
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_pandoc_title_block WriterOptions
opts ->
Doc Text -> [Doc Text] -> Doc Text -> Doc Text
pandocTitleBlock Doc Text
title' [Doc Text]
authors' Doc Text
date'
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_mmd_title_block WriterOptions
opts ->
Context Text -> Doc Text
mmdTitleBlock Context Text
metadata
| Bool
otherwise -> forall a. Doc a
empty
Maybe (Template Text)
Nothing -> forall a. Doc a
empty
let modifyTOC :: Block -> Block
modifyTOC =
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_link_attributes WriterOptions
opts Bool -> Bool -> Bool
|| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_attributes WriterOptions
opts
then forall a. a -> a
id
else forall a b. Walkable a b => (a -> a) -> b -> b
walk forall a b. (a -> b) -> a -> b
$ \Inline
inln -> case Inline
inln of
Link Attr
_attr [Inline]
contents Target
tgt -> Attr -> [Inline] -> Target -> Inline
Link Attr
nullAttr [Inline]
contents Target
tgt
Inline
_ -> Inline
inln
Doc Text
toc <- if WriterOptions -> Bool
writerTableOfContents WriterOptions
opts
then forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown WriterOptions
opts forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block -> Block
modifyTOC forall a b. (a -> b) -> a -> b
$ WriterOptions -> [Block] -> Block
toTableOfContents WriterOptions
opts [Block]
blocks
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty
let blocks' :: [Block]
blocks' = if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_citations WriterOptions
opts
then case forall a. [a] -> [a]
reverse [Block]
blocks of
(Div (Text
"refs",[Text]
_,[Target]
_) [Block]
_):[Block]
xs -> forall a. [a] -> [a]
reverse [Block]
xs
[Block]
_ -> [Block]
blocks
else [Block]
blocks
Doc Text
body <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts [Block]
blocks'
Doc Text
notesAndRefs' <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> MD m (Doc Text)
notesAndRefs WriterOptions
opts
let main :: Doc Text
main = Doc Text
body forall a. Semigroup a => a -> a -> a
<> Doc Text
notesAndRefs'
let context :: Context Text
context =
forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"toc" Doc Text
toc
forall a b. (a -> b) -> a -> b
$ forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"table-of-contents" Doc Text
toc
forall a b. (a -> b) -> a -> b
$ forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"body" Doc Text
main
forall a b. (a -> b) -> a -> b
$ (if Meta -> Bool
isNullMeta Meta
meta
then forall a. a -> a
id
else forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"titleblock" Doc Text
titleblock)
forall a b. (a -> b) -> a -> b
$ forall a.
TemplateTarget a =>
WriterOptions -> Context a -> Context a
addVariablesToContext WriterOptions
opts Context Text
metadata
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => Maybe Int -> Doc a -> a
render Maybe Int
colwidth forall a b. (a -> b) -> a -> b
$
case WriterOptions -> Maybe (Template Text)
writerTemplate WriterOptions
opts of
Maybe (Template Text)
Nothing -> Doc Text
main
Just Template Text
tpl -> forall a b.
(TemplateTarget a, ToContext a b) =>
Template a -> b -> Doc a
renderTemplate Template Text
tpl Context Text
context
refsToMarkdown :: PandocMonad m => WriterOptions -> Refs -> MD m (Doc Text)
refsToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Refs -> MD m (Doc Text)
refsToMarkdown WriterOptions
opts Refs
refs = forall a. [Doc a] -> Doc a
vcat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Ref -> MD m (Doc Text)
keyToMarkdown WriterOptions
opts) Refs
refs
keyToMarkdown :: PandocMonad m
=> WriterOptions
-> Ref
-> MD m (Doc Text)
keyToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Ref -> MD m (Doc Text)
keyToMarkdown WriterOptions
opts (Text
label', (Text
src, Text
tit), Attr
attr) = do
let tit' :: Doc Text
tit' = if Text -> Bool
T.null Text
tit
then forall a. Doc a
empty
else forall a. Doc a
space forall a. Semigroup a => a -> a -> a
<> Doc Text
"\"" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
tit forall a. Semigroup a => a -> a -> a
<> Doc Text
"\""
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IsString a => Int -> Doc a -> Doc a
nest Int
2 forall a b. (a -> b) -> a -> b
$ forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
2
(Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
label' forall a. Semigroup a => a -> a -> a
<> Doc Text
"]:" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
space) (forall a. HasChars a => a -> Doc a
literal Text
src forall a. Semigroup a => a -> a -> a
<> Doc Text
tit')
forall a. Doc a -> Doc a -> Doc a
<+> WriterOptions -> Attr -> Doc Text
linkAttributes WriterOptions
opts Attr
attr
notesToMarkdown :: PandocMonad m => WriterOptions -> [[Block]] -> MD m (Doc Text)
notesToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [[Block]] -> MD m (Doc Text)
notesToMarkdown WriterOptions
opts [[Block]]
notes = do
Int
n <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Int
stNoteNum
[Doc Text]
notes' <- forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Int -> [Block] -> MD m (Doc Text)
noteToMarkdown WriterOptions
opts) [Int
n..] [[Block]]
notes
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st { stNoteNum :: Int
stNoteNum = WriterState -> Int
stNoteNum WriterState
st forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Block]]
notes }
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
vsep [Doc Text]
notes'
noteToMarkdown :: PandocMonad m => WriterOptions -> Int -> [Block] -> MD m (Doc Text)
noteToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Int -> [Block] -> MD m (Doc Text)
noteToMarkdown WriterOptions
opts Int
num [Block]
blocks = do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts [Block]
blocks
let num' :: Doc Text
num' = forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ WriterOptions -> Text
writerIdentifierPrefix WriterOptions
opts forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> Text
tshow Int
num
let marker :: Doc Text
marker = if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_footnotes WriterOptions
opts
then forall a. HasChars a => a -> Doc a
literal Text
"[^" forall a. Semigroup a => a -> a -> a
<> Doc Text
num' forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"]:"
else forall a. HasChars a => a -> Doc a
literal Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
num' forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"]"
let markerSize :: Int
markerSize = Int
4 forall a. Num a => a -> a -> a
+ forall a. (IsString a, HasChars a) => Doc a -> Int
offset Doc Text
num'
let hspacer :: Doc Text
hspacer = case WriterOptions -> Int
writerTabStop WriterOptions
opts forall a. Num a => a -> a -> a
- Int
markerSize of
Int
n | Int
n forall a. Ord a => a -> a -> Bool
> Int
0 -> forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.replicate Int
n Text
" "
Int
_ -> forall a. HasChars a => a -> Doc a
literal Text
" "
let spacer :: Doc Text
spacer = case [Block]
blocks of
Para{}:[Block]
_ -> Doc Text
hspacer
Plain{}:[Block]
_ -> Doc Text
hspacer
[Block]
_ -> forall a. Doc a
cr
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_footnotes WriterOptions
opts
then forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang (WriterOptions -> Int
writerTabStop WriterOptions
opts) (Doc Text
marker forall a. Semigroup a => a -> a -> a
<> Doc Text
spacer) Doc Text
contents
else Doc Text
marker forall a. Semigroup a => a -> a -> a
<> Doc Text
spacer forall a. Semigroup a => a -> a -> a
<> Doc Text
contents
classOrAttrsToMarkdown :: WriterOptions -> Attr -> Doc Text
classOrAttrsToMarkdown :: WriterOptions -> Attr -> Doc Text
classOrAttrsToMarkdown WriterOptions
_ (Text
"",[Text
cls],[]) = forall a. HasChars a => a -> Doc a
literal Text
cls
classOrAttrsToMarkdown WriterOptions
opts Attr
attrs = WriterOptions -> Attr -> Doc Text
attrsToMarkdown WriterOptions
opts Attr
attrs
olMarker :: Parsec Text ParserState ()
olMarker :: Parsec Text ParserState ()
olMarker = do (Int
start, ListNumberStyle
style', ListNumberDelim
delim) <- forall s (m :: * -> *).
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s ParserState m (Int, ListNumberStyle, ListNumberDelim)
anyOrderedListMarker
if ListNumberDelim
delim forall a. Eq a => a -> a -> Bool
== ListNumberDelim
Period Bool -> Bool -> Bool
&&
(ListNumberStyle
style' forall a. Eq a => a -> a -> Bool
== ListNumberStyle
UpperAlpha Bool -> Bool -> Bool
|| (ListNumberStyle
style' forall a. Eq a => a -> a -> Bool
== ListNumberStyle
UpperRoman Bool -> Bool -> Bool
&&
Int
start forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int
1, Int
5, Int
10, Int
50, Int
100, Int
500, Int
1000]))
then forall (m :: * -> *) a. MonadPlus m => m a
mzero
else forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof
beginsWithOrderedListMarker :: Text -> Bool
beginsWithOrderedListMarker :: Text -> Bool
beginsWithOrderedListMarker Text
str =
case forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> String -> s -> Either ParseError a
runParser Parsec Text ParserState ()
olMarker ParserState
defaultParserState String
"para start" (Int -> Text -> Text
T.take Int
10 Text
str) of
Left ParseError
_ -> Bool
False
Right ()
_ -> Bool
True
notesAndRefs :: PandocMonad m => WriterOptions -> MD m (Doc Text)
notesAndRefs :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> MD m (Doc Text)
notesAndRefs WriterOptions
opts = do
Doc Text
notes' <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> [[Block]]
stNotes forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [[Block]] -> MD m (Doc Text)
notesToMarkdown WriterOptions
opts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s { stNotes :: [[Block]]
stNotes = [] }
Doc Text
refs' <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Refs
stRefs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Refs -> MD m (Doc Text)
refsToMarkdown WriterOptions
opts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s { stPrevRefs :: Refs
stPrevRefs = WriterState -> Refs
stPrevRefs WriterState
s forall a. [a] -> [a] -> [a]
++ WriterState -> Refs
stRefs WriterState
s
, stRefs :: Refs
stRefs = []}
let endSpacing :: Doc a
endSpacing =
if | WriterOptions -> ReferenceLocation
writerReferenceLocation WriterOptions
opts forall a. Eq a => a -> a -> Bool
== ReferenceLocation
EndOfDocument -> forall a. Doc a
empty
| forall a. Doc a -> Bool
isEmpty Doc Text
notes' Bool -> Bool -> Bool
&& forall a. Doc a -> Bool
isEmpty Doc Text
refs' -> forall a. Doc a
empty
| Bool
otherwise -> forall a. Doc a
blankline
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
(if forall a. Doc a -> Bool
isEmpty Doc Text
notes' then forall a. Doc a
empty else forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> Doc Text
notes') forall a. Semigroup a => a -> a -> a
<>
(if forall a. Doc a -> Bool
isEmpty Doc Text
refs' then forall a. Doc a
empty else forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> Doc Text
refs') forall a. Semigroup a => a -> a -> a
<>
forall a. Doc a
endSpacing
blockToMarkdown :: PandocMonad m
=> WriterOptions
-> Block
-> MD m (Doc Text)
blockToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown WriterOptions
opts Block
blk =
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\WriterEnv
env -> WriterEnv
env {envBlockLevel :: Int
envBlockLevel = WriterEnv -> Int
envBlockLevel WriterEnv
env forall a. Num a => a -> a -> a
+ Int
1}) forall a b. (a -> b) -> a -> b
$
do Doc Text
doc <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown' WriterOptions
opts Block
blk
Int
blkLevel <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> Int
envBlockLevel
if WriterOptions -> ReferenceLocation
writerReferenceLocation WriterOptions
opts forall a. Eq a => a -> a -> Bool
== ReferenceLocation
EndOfBlock Bool -> Bool -> Bool
&& Int
blkLevel forall a. Eq a => a -> a -> Bool
== Int
1
then forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> MD m (Doc Text)
notesAndRefs WriterOptions
opts forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Doc Text
d -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
doc forall a. Semigroup a => a -> a -> a
<> Doc Text
d)
else forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
doc
blockToMarkdown' :: PandocMonad m
=> WriterOptions
-> Block
-> MD m (Doc Text)
blockToMarkdown' :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown' WriterOptions
opts (Div Attr
attrs [Block]
ils) = do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts [Block]
ils
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
case () of
()
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua ->
case () of
() | Text
"blurb" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes' -> forall a. IsString a => String -> Doc a -> Doc a
prefixed String
"B> " Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| Text
"aside" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes' -> forall a. IsString a => String -> Doc a -> Doc a
prefixed String
"A> " Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| (forall a. Int -> [a] -> [a]
take Int
3 (Text -> String
T.unpack Text
id')) forall a. Eq a => a -> a -> Bool
== String
"ref" -> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| Bool
otherwise -> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_fenced_divs WriterOptions
opts Bool -> Bool -> Bool
&&
Attr
attrs forall a. Eq a => a -> a -> Bool
/= Attr
nullAttr ->
let attrsToMd :: Attr -> Doc Text
attrsToMd = if MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Commonmark
then WriterOptions -> Attr -> Doc Text
attrsToMarkdown WriterOptions
opts
else WriterOptions -> Attr -> Doc Text
classOrAttrsToMarkdown WriterOptions
opts
in forall a. IsString a => Doc a -> Doc a
nowrap (forall a. HasChars a => a -> Doc a
literal Text
":::" forall a. Doc a -> Doc a -> Doc a
<+> Attr -> Doc Text
attrsToMd Attr
attrs) forall a. Doc a -> Doc a -> Doc a
$$
forall a. Doc a -> Doc a
chomp Doc Text
contents forall a. Doc a -> Doc a -> Doc a
$$
forall a. HasChars a => a -> Doc a
literal Text
":::" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_native_divs WriterOptions
opts Bool -> Bool -> Bool
||
(forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts Bool -> Bool -> Bool
&&
(MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Commonmark Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_markdown_in_html_blocks WriterOptions
opts)) ->
forall a. HasChars a => a -> Attr -> Doc a
tagWithAttrs Text
"div" Attr
attrs forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<>
Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> Doc Text
"</div>" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_markdown_attribute WriterOptions
opts ->
forall a. HasChars a => a -> Attr -> Doc a
tagWithAttrs Text
"div" Attr
attrs' forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<>
Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> Doc Text
"</div>" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| Bool
otherwise -> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
where (Text
id',[Text]
classes',[Target]
kvs') = Attr
attrs
attrs' :: Attr
attrs' = (Text
id',[Text]
classes',(Text
"markdown",Text
"1")forall a. a -> [a] -> [a]
:[Target]
kvs')
blockToMarkdown' WriterOptions
opts (Plain [Inline]
inlines) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let escapeMarker :: Text -> Text
escapeMarker = (Char -> Text) -> Text -> Text
T.concatMap forall a b. (a -> b) -> a -> b
$ \Char
x -> if (Char -> Bool) -> Text -> Bool
T.any (forall a. Eq a => a -> a -> Bool
== Char
x) Text
".()"
then String -> Text
T.pack [Char
'\\', Char
x]
else Char -> Text
T.singleton Char
x
let startsWithSpace :: [Inline] -> Bool
startsWithSpace (Inline
Space:[Inline]
_) = Bool
True
startsWithSpace (Inline
SoftBreak:[Inline]
_) = Bool
True
startsWithSpace [Inline]
_ = Bool
False
let inlines' :: [Inline]
inlines' =
if MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText
then [Inline]
inlines
else case [Inline]
inlines of
(Str Text
t:[Inline]
ys)
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
ys Bool -> Bool -> Bool
|| [Inline] -> Bool
startsWithSpace [Inline]
ys
, Text -> Bool
beginsWithOrderedListMarker Text
t
-> Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"markdown") (Text -> Text
escapeMarker Text
t)forall a. a -> [a] -> [a]
:[Inline]
ys
(Str Text
t:[Inline]
_)
| Text
t forall a. Eq a => a -> a -> Bool
== Text
"+" Bool -> Bool -> Bool
|| Text
t forall a. Eq a => a -> a -> Bool
== Text
"-" Bool -> Bool -> Bool
||
(Text
t forall a. Eq a => a -> a -> Bool
== Text
"%" Bool -> Bool -> Bool
&& forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_pandoc_title_block WriterOptions
opts Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_all_symbols_escapable WriterOptions
opts)
-> Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"markdown") Text
"\\" forall a. a -> [a] -> [a]
: [Inline]
inlines
[Inline]
_ -> [Inline]
inlines
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
inlines'
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
blockToMarkdown' WriterOptions
opts (Para [Inline]
inlines) =
(forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown WriterOptions
opts ([Inline] -> Block
Plain [Inline]
inlines)
blockToMarkdown' WriterOptions
opts (LineBlock [[Inline]]
lns) =
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_line_blocks WriterOptions
opts
then do
[Doc Text]
mdLines <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts) [[Inline]]
lns
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
vcat (forall a b. (a -> b) -> [a] -> [b]
map (forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
2 (forall a. HasChars a => a -> Doc a
literal Text
"| ")) [Doc Text]
mdLines) forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
else forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown WriterOptions
opts forall a b. (a -> b) -> a -> b
$ [[Inline]] -> Block
linesToPara [[Inline]]
lns
blockToMarkdown' WriterOptions
opts b :: Block
b@(RawBlock Format
f Text
str) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let Format Text
fmt = Format
f
let rawAttribBlock :: MD m (Doc Text)
rawAttribBlock = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
(forall a. HasChars a => a -> Doc a
literal Text
"```{=" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
fmt forall a. Semigroup a => a -> a -> a
<> Doc Text
"}") forall a. Doc a -> Doc a -> Doc a
$$
forall a. HasChars a => a -> Doc a
literal Text
str forall a. Doc a -> Doc a -> Doc a
$$
(forall a. HasChars a => a -> Doc a
literal Text
"```" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n")
let renderEmpty :: MD m (Doc Text)
renderEmpty = forall a. Monoid a => a
mempty forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (Block -> LogMessage
BlockNotRendered Block
b)
case MarkdownVariant
variant of
MarkdownVariant
PlainText
| Format
f forall a. Eq a => a -> a -> Bool
== Format
"plain" -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n"
MarkdownVariant
Commonmark
| Format
f forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Format
"gfm", Format
"commonmark", Format
"commonmark_x", Format
"markdown"]
-> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n"
| Format
f forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Format
"html", Format
"html5", Format
"html4"]
-> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Doc a
literal (Text -> Text
removeBlankLinesInHTML Text
str) forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n"
MarkdownVariant
Markdown
| Format
f forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Format
"markdown", Format
"markdown_github", Format
"markdown_phpextra",
Format
"markdown_mmd", Format
"markdown_strict"]
-> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n"
MarkdownVariant
Markua -> MD m (Doc Text)
renderEmpty
MarkdownVariant
_ | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_attribute WriterOptions
opts -> MD m (Doc Text)
rawAttribBlock
| Format
f forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Format
"html", Format
"html5", Format
"html4"]
, forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_markdown_attribute WriterOptions
opts
-> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Doc a
literal (Text -> Text
addMarkdownAttribute Text
str) forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n"
| Format
f forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Format
"html", Format
"html5", Format
"html4"]
, forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts
-> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n"
| Format
f forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Format
"latex", Format
"tex"]
, forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_tex WriterOptions
opts
-> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"\n"
MarkdownVariant
_ -> MD m (Doc Text)
renderEmpty
blockToMarkdown' WriterOptions
opts Block
HorizontalRule = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let indicator :: Text
indicator = case MarkdownVariant
variant of
MarkdownVariant
Markua -> Text
"* * *"
MarkdownVariant
_ -> Int -> Text -> Text
T.replicate (WriterOptions -> Int
writerColumns WriterOptions
opts) Text
"-"
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
indicator forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
blockToMarkdown' WriterOptions
opts (Header Int
level Attr
attr [Inline]
inlines) = do
Int
blkLevel <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> Int
envBlockLevel
Doc Text
refs <- if WriterOptions -> ReferenceLocation
writerReferenceLocation WriterOptions
opts forall a. Eq a => a -> a -> Bool
== ReferenceLocation
EndOfSection Bool -> Bool -> Bool
&& Int
blkLevel forall a. Eq a => a -> a -> Bool
== Int
1
then forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> MD m (Doc Text)
notesAndRefs WriterOptions
opts
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
Set Text
ids <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Set Text
stIds
let autoId :: Text
autoId = Extensions -> [Inline] -> Set Text -> Text
uniqueIdent (WriterOptions -> Extensions
writerExtensions WriterOptions
opts) [Inline]
inlines Set Text
ids
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ stIds :: Set Text
stIds = forall a. Ord a => a -> Set a -> Set a
Set.insert Text
autoId Set Text
ids }
let attr' :: Doc Text
attr' = case Attr
attr of
(Text
"",[],[]) -> forall a. Doc a
empty
(Text
id',[],[]) | (forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_auto_identifiers WriterOptions
opts
Bool -> Bool -> Bool
|| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_gfm_auto_identifiers WriterOptions
opts)
Bool -> Bool -> Bool
&& Text
id' forall a. Eq a => a -> a -> Bool
== Text
autoId -> forall a. Doc a
empty
(Text
id',[Text]
_,[Target]
_) | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_mmd_header_identifiers WriterOptions
opts ->
forall a. Doc a
space forall a. Semigroup a => a -> a -> a
<> forall {a}. HasChars a => Doc a -> Doc a
brackets (forall a. HasChars a => a -> Doc a
literal Text
id')
Attr
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua -> WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts Attr
attr
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_header_attributes WriterOptions
opts Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_attributes WriterOptions
opts ->
forall a. Doc a
space forall a. Semigroup a => a -> a -> a
<> WriterOptions -> Attr -> Doc Text
attrsToMarkdown WriterOptions
opts Attr
attr
| Bool
otherwise -> forall a. Doc a
empty
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts forall a b. (a -> b) -> a -> b
$
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
lineBreakToSpace forall a b. (a -> b) -> a -> b
$
if Int
level forall a. Eq a => a -> a -> Bool
== Int
1 Bool -> Bool -> Bool
&& MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_gutenberg WriterOptions
opts
then forall a. Walkable Inline a => a -> a
capitalize [Inline]
inlines
else [Inline]
inlines
let setext :: Bool
setext = WriterOptions -> Bool
writerSetextHeaders WriterOptions
opts
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
setext Bool -> Bool -> Bool
&& forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_literate_haskell WriterOptions
opts) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report forall a b. (a -> b) -> a -> b
$ Int -> Text -> LogMessage
ATXHeadingInLHS Int
level (forall a. HasChars a => Maybe Int -> Doc a -> a
render forall a. Maybe a
Nothing Doc Text
contents)
let hdr :: Doc Text
hdr = forall a. IsString a => Doc a -> Doc a
nowrap forall a b. (a -> b) -> a -> b
$ case Int
level of
Int
1 | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText ->
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_gutenberg WriterOptions
opts
then forall a. Int -> Doc a
blanklines Int
3 forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Int -> Doc a
blanklines Int
2
else Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| Bool
setext ->
Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
attr' forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal (Int -> Text -> Text
T.replicate (forall a. (IsString a, HasChars a) => Doc a -> Int
offset Doc Text
contents) Text
"=") forall a. Semigroup a => a -> a -> a
<>
forall a. Doc a
blankline
Int
2 | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText ->
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_gutenberg WriterOptions
opts
then forall a. Int -> Doc a
blanklines Int
2 forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
else Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| Bool
setext ->
Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
attr' forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal (Int -> Text -> Text
T.replicate (forall a. (IsString a, HasChars a) => Doc a -> Int
offset Doc Text
contents) Text
"-") forall a. Semigroup a => a -> a -> a
<>
forall a. Doc a
blankline
Int
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText Bool -> Bool -> Bool
|| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_literate_haskell WriterOptions
opts ->
Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
Int
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua -> Doc Text
attr' forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal (Int -> Text -> Text
T.replicate Int
level Text
"#")
forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
space forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
Int
_ -> forall a. HasChars a => a -> Doc a
literal (Int -> Text -> Text
T.replicate Int
level Text
"#") forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
space forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
attr' forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
refs forall a. Semigroup a => a -> a -> a
<> Doc Text
hdr
blockToMarkdown' WriterOptions
opts (CodeBlock (Text
_,[Text]
classes,[Target]
_) Text
str)
| Text
"haskell" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes Bool -> Bool -> Bool
&& Text
"literate" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_literate_haskell WriterOptions
opts =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IsString a => String -> Doc a -> Doc a
prefixed String
"> " (forall a. HasChars a => a -> Doc a
literal Text
str) forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
blockToMarkdown' WriterOptions
opts (CodeBlock Attr
attribs Text
str) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
case Attr
attribs forall a. Eq a => a -> a -> Bool
== Attr
nullAttr of
Bool
False | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Commonmark Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_backtick_code_blocks WriterOptions
opts ->
Doc Text
backticks forall a. Semigroup a => a -> a -> a
<> Doc Text
attrs forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> Doc Text
backticks forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_fenced_code_blocks WriterOptions
opts ->
Doc Text
tildes forall a. Semigroup a => a -> a -> a
<> Doc Text
attrs forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> Doc Text
tildes forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
Bool
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua -> forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts Attr
attribs forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> Doc Text
backticks forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>
forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> Doc Text
backticks forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| Bool
otherwise -> forall a. IsString a => Int -> Doc a -> Doc a
nest (WriterOptions -> Int
writerTabStop WriterOptions
opts) (forall a. HasChars a => a -> Doc a
literal Text
str) forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
where
endlineLen :: Char -> Int
endlineLen Char
c = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
3 ((forall a. Num a => a -> a -> a
+Int
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum) forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Maybe (NonEmpty a)
nonEmpty
[Text -> Int
T.length Text
ln
| Text
ln <- forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
trim (Text -> [Text]
T.lines Text
str)
, String -> Text
T.pack [Char
c,Char
c,Char
c] Text -> Text -> Bool
`T.isPrefixOf` Text
ln
, (Char -> Bool) -> Text -> Bool
T.all (forall a. Eq a => a -> a -> Bool
== Char
c) Text
ln]
endline :: Char -> Doc Text
endline Char
c = forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.replicate (Char -> Int
endlineLen Char
c) forall a b. (a -> b) -> a -> b
$ Char -> Text
T.singleton Char
c
backticks :: Doc Text
backticks = Char -> Doc Text
endline Char
'`'
tildes :: Doc Text
tildes = Char -> Doc Text
endline Char
'~'
attrs :: Doc Text
attrs = if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_fenced_code_attributes WriterOptions
opts Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_attributes WriterOptions
opts
then forall a. IsString a => Doc a -> Doc a
nowrap forall a b. (a -> b) -> a -> b
$ Doc Text
" " forall a. Semigroup a => a -> a -> a
<> WriterOptions -> Attr -> Doc Text
classOrAttrsToMarkdown WriterOptions
opts Attr
attribs
else case Attr
attribs of
(Text
_,Text
cls:[Text]
_,[Target]
_) -> Doc Text
" " forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
cls
Attr
_ -> forall a. Doc a
empty
blockToMarkdown' WriterOptions
opts (BlockQuote [Block]
blocks) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let leader :: String
leader
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_literate_haskell WriterOptions
opts = String
" > "
| MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText = String
" "
| Bool
otherwise = String
"> "
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts [Block]
blocks
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IsString a => String -> Doc a -> Doc a
prefixed String
leader Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
blockToMarkdown' WriterOptions
opts t :: Block
t@(Table Attr
_ Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot) = do
let ([Inline]
caption, [Alignment]
aligns, [Double]
widths, [[Block]]
headers, [[[Block]]]
rows) = Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> ([Inline], [Alignment], [Double], [[Block]], [[[Block]]])
toLegacyTable Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot
let numcols :: Int
numcols = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Alignment]
aligns forall a. a -> [a] -> NonEmpty a
:| forall (t :: * -> *) a. Foldable t => t a -> Int
length [Double]
widths forall a. a -> [a] -> [a]
:
forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length ([[Block]]
headersforall a. a -> [a] -> [a]
:[[[Block]]]
rows))
Doc Text
caption' <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
caption
let caption'' :: Doc Text
caption''
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
caption = forall a. Doc a
blankline
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_table_captions WriterOptions
opts
= forall a. Doc a
blankline forall a. Doc a -> Doc a -> Doc a
$$ (Doc Text
": " forall a. Semigroup a => a -> a -> a
<> Doc Text
caption') forall a. Doc a -> Doc a -> Doc a
$$ forall a. Doc a
blankline
| Bool
otherwise = forall a. Doc a
blankline forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
caption' forall a. Doc a -> Doc a -> Doc a
$$ forall a. Doc a
blankline
let hasSimpleCells :: Bool
hasSimpleCells = [[[Block]]] -> Bool
onlySimpleTableCells forall a b. (a -> b) -> a -> b
$ [[Block]]
headers forall a. a -> [a] -> [a]
: [[[Block]]]
rows
let isSimple :: Bool
isSimple = Bool
hasSimpleCells Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. Eq a => a -> a -> Bool
==Double
0) [Double]
widths
let isPlainBlock :: Block -> Bool
isPlainBlock (Plain [Inline]
_) = Bool
True
isPlainBlock Block
_ = Bool
False
let hasBlocks :: Bool
hasBlocks = Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Block -> Bool
isPlainBlock forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ [[Block]]
headersforall a. a -> [a] -> [a]
:[[[Block]]]
rows)
let padRow :: [Doc a] -> [Doc a]
padRow [Doc a]
r = case Int
numcols forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length [Doc a]
r of
Int
x | Int
x forall a. Ord a => a -> a -> Bool
> Int
0 -> [Doc a]
r forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate Int
x forall a. Doc a
empty
| Bool
otherwise -> [Doc a]
r
let aligns' :: [Alignment]
aligns' = case Int
numcols forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length [Alignment]
aligns of
Int
x | Int
x forall a. Ord a => a -> a -> Bool
> Int
0 -> [Alignment]
aligns forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate Int
x Alignment
AlignDefault
| Bool
otherwise -> [Alignment]
aligns
let widths' :: [Double]
widths' = case Int
numcols forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length [Double]
widths of
Int
x | Int
x forall a. Ord a => a -> a -> Bool
> Int
0 -> [Double]
widths forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate Int
x Double
0.0
| Bool
otherwise -> [Double]
widths
(Doc Text -> Doc Text
nst,Doc Text
tbl) <-
case Bool
True of
Bool
_ | Bool
isSimple Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_simple_tables WriterOptions
opts -> do
[Doc Text]
rawHeaders <- forall {a}. [Doc a] -> [Doc a]
padRow forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts) [[Block]]
headers
[[Doc Text]]
rawRows <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. [Doc a] -> [Doc a]
padRow forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts))
[[[Block]]]
rows
(forall a. IsString a => Int -> Doc a -> Doc a
nest Int
2,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
PandocMonad m =>
WriterOptions
-> Bool
-> Bool
-> [Alignment]
-> [Double]
-> [Doc Text]
-> [[Doc Text]]
-> MD m (Doc Text)
pandocTable WriterOptions
opts Bool
False (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
headers)
[Alignment]
aligns' [Double]
widths' [Doc Text]
rawHeaders [[Doc Text]]
rawRows
| Bool
isSimple Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_pipe_tables WriterOptions
opts -> do
[Doc Text]
rawHeaders <- forall {a}. [Doc a] -> [Doc a]
padRow forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts) [[Block]]
headers
[[Doc Text]]
rawRows <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. [Doc a] -> [Doc a]
padRow forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts))
[[[Block]]]
rows
(forall a. a -> a
id,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
PandocMonad m =>
WriterOptions
-> Bool
-> [Alignment]
-> [Double]
-> [Doc Text]
-> [[Doc Text]]
-> MD m (Doc Text)
pipeTable WriterOptions
opts (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
headers) [Alignment]
aligns' [Double]
widths'
[Doc Text]
rawHeaders [[Doc Text]]
rawRows
| Bool -> Bool
not Bool
hasBlocks Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_multiline_tables WriterOptions
opts -> do
[Doc Text]
rawHeaders <- forall {a}. [Doc a] -> [Doc a]
padRow forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts) [[Block]]
headers
[[Doc Text]]
rawRows <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. [Doc a] -> [Doc a]
padRow forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts))
[[[Block]]]
rows
(forall a. IsString a => Int -> Doc a -> Doc a
nest Int
2,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
PandocMonad m =>
WriterOptions
-> Bool
-> Bool
-> [Alignment]
-> [Double]
-> [Doc Text]
-> [[Doc Text]]
-> MD m (Doc Text)
pandocTable WriterOptions
opts Bool
True (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
headers)
[Alignment]
aligns' [Double]
widths' [Doc Text]
rawHeaders [[Doc Text]]
rawRows
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_grid_tables WriterOptions
opts Bool -> Bool -> Bool
&&
WriterOptions -> Int
writerColumns WriterOptions
opts forall a. Ord a => a -> a -> Bool
>= Int
8 forall a. Num a => a -> a -> a
* Int
numcols -> (forall a. a -> a
id,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
forall (m :: * -> *) a.
(Monad m, HasChars a) =>
WriterOptions
-> (WriterOptions -> [Block] -> m (Doc a))
-> Bool
-> [Alignment]
-> [Double]
-> [[Block]]
-> [[[Block]]]
-> m (Doc a)
gridTable WriterOptions
opts forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown
(forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
headers) [Alignment]
aligns' [Double]
widths' [[Block]]
headers [[[Block]]]
rows
| Bool
hasSimpleCells Bool -> Bool -> Bool
&&
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_pipe_tables WriterOptions
opts -> do
[Doc Text]
rawHeaders <- forall {a}. [Doc a] -> [Doc a]
padRow forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts) [[Block]]
headers
[[Doc Text]]
rawRows <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. [Doc a] -> [Doc a]
padRow forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts))
[[[Block]]]
rows
(forall a. a -> a
id,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
PandocMonad m =>
WriterOptions
-> Bool
-> [Alignment]
-> [Double]
-> [Doc Text]
-> [[Doc Text]]
-> MD m (Doc Text)
pipeTable WriterOptions
opts (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
headers) [Alignment]
aligns' [Double]
widths'
[Doc Text]
rawHeaders [[Doc Text]]
rawRows
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> a
id,) forall a b. (a -> b) -> a -> b
$
forall a. HasChars a => a -> Doc a
literal forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
removeBlankLinesInHTML forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeHtml5String WriterOptions
opts{ writerTemplate :: Maybe (Template Text)
writerTemplate = forall a. Maybe a
Nothing } (Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta [Block
t])
| Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> a
id, forall a. HasChars a => a -> Doc a
literal Text
"[TABLE]")
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text -> Doc Text
nst (Doc Text
tbl forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
caption'') forall a. Doc a -> Doc a -> Doc a
$$ forall a. Doc a
blankline
blockToMarkdown' WriterOptions
opts (BulletList [[Block]]
items) = do
[Doc Text]
contents <- forall (m :: * -> *) a. Monad m => MD m a -> MD m a
inList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
bulletListItemToMarkdown WriterOptions
opts) [[Block]]
items
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (if [[Block]] -> Bool
isTightList [[Block]]
items then forall a. [Doc a] -> Doc a
vcat else forall a. [Doc a] -> Doc a
vsep)
[Doc Text]
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
blockToMarkdown' WriterOptions
opts (OrderedList (Int
start,ListNumberStyle
sty,ListNumberDelim
delim) [[Block]]
items) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let start' :: Int
start' = if MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Commonmark Bool -> Bool -> Bool
|| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_startnum WriterOptions
opts
then Int
start
else Int
1
let sty' :: ListNumberStyle
sty' = if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_fancy_lists WriterOptions
opts then ListNumberStyle
sty else ListNumberStyle
DefaultStyle
let delim' :: ListNumberDelim
delim' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_fancy_lists WriterOptions
opts =
case MarkdownVariant
variant of
MarkdownVariant
Markua -> if ListNumberDelim
delim forall a. Eq a => a -> a -> Bool
== ListNumberDelim
TwoParens then ListNumberDelim
OneParen else ListNumberDelim
delim
MarkdownVariant
_ -> ListNumberDelim
delim
| MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Commonmark Bool -> Bool -> Bool
&&
(ListNumberDelim
delim forall a. Eq a => a -> a -> Bool
== ListNumberDelim
OneParen Bool -> Bool -> Bool
|| ListNumberDelim
delim forall a. Eq a => a -> a -> Bool
== ListNumberDelim
TwoParens) = ListNumberDelim
OneParen
| Bool
otherwise = ListNumberDelim
DefaultDelim
let attribs :: (Int, ListNumberStyle, ListNumberDelim)
attribs = (Int
start', ListNumberStyle
sty', ListNumberDelim
delim')
let markers :: [Text]
markers = (Int, ListNumberStyle, ListNumberDelim) -> [Text]
orderedListMarkers (Int, ListNumberStyle, ListNumberDelim)
attribs
let markers' :: [Text]
markers' = case MarkdownVariant
variant of
MarkdownVariant
Markua -> [Text]
markers
MarkdownVariant
_ -> forall a b. (a -> b) -> [a] -> [b]
map (\Text
m -> if Text -> Int
T.length Text
m forall a. Ord a => a -> a -> Bool
< Int
3
then Text
m forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate (Int
3 forall a. Num a => a -> a -> a
- Text -> Int
T.length Text
m) Text
" "
else Text
m) [Text]
markers
[Doc Text]
contents <- forall (m :: * -> *) a. Monad m => MD m a -> MD m a
inList forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Text -> [Block] -> MD m (Doc Text)
orderedListItemToMarkdown WriterOptions
opts) [Text]
markers' [[Block]]
items
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (if [[Block]] -> Bool
isTightList [[Block]]
items then forall a. [Doc a] -> Doc a
vcat else forall a. [Doc a] -> Doc a
vsep) [Doc Text]
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
blockToMarkdown' WriterOptions
opts (DefinitionList [([Inline], [[Block]])]
items) = do
[Doc Text]
contents <- forall (m :: * -> *) a. Monad m => MD m a -> MD m a
inList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> ([Inline], [[Block]]) -> MD m (Doc Text)
definitionListItemToMarkdown WriterOptions
opts) [([Inline], [[Block]])]
items
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [Doc Text]
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
blockToMarkdown' WriterOptions
opts (Figure Attr
figattr Caption
capt [Block]
body) = do
let combinedAttr :: (a, b, c) -> Maybe (Text, b, c)
combinedAttr (a, b, c)
imgattr = case (a, b, c)
imgattr of
(a
"", b
cls, c
kv) | (Text
figid, [], []) <- Attr
figattr -> forall a. a -> Maybe a
Just (Text
figid, b
cls, c
kv)
(a, b, c)
_ -> forall a. Maybe a
Nothing
let combinedAlt :: [Inline] -> Maybe [Inline]
combinedAlt [Inline]
alt = case Caption
capt of
Caption Maybe [Inline]
Nothing [] -> if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
alt
then forall a. a -> Maybe a
Just [Text -> Inline
Str Text
"image"]
else forall a. a -> Maybe a
Just [Inline]
alt
Caption Maybe [Inline]
Nothing [Plain [Inline]
captInlines]
| [Inline]
captInlines forall a. Eq a => a -> a -> Bool
== [Inline]
alt Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
alt -> forall a. a -> Maybe a
Just [Inline]
captInlines
Caption
_ -> forall a. Maybe a
Nothing
case [Block]
body of
[Plain [Image Attr
imgAttr [Inline]
alt (Text
src, Text
ttl)]]
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_implicit_figures WriterOptions
opts
, Just [Inline]
descr <- [Inline] -> Maybe [Inline]
combinedAlt [Inline]
alt
, Just Attr
imgAttr' <- forall {a} {b} {c}.
(Eq a, IsString a) =>
(a, b, c) -> Maybe (Text, b, c)
combinedAttr Attr
imgAttr
, forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_link_attributes WriterOptions
opts Bool -> Bool -> Bool
|| Attr
imgAttr' forall a. Eq a => a -> a -> Bool
== Attr
nullAttr
-> do
let tgt' :: Target
tgt' = (Text
src, forall a. a -> Maybe a -> a
fromMaybe Text
ttl forall a b. (a -> b) -> a -> b
$ Text -> Text -> Maybe Text
T.stripPrefix Text
"fig:" Text
ttl)
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Attr -> [Inline] -> Target -> Inline
Image Attr
imgAttr' [Inline]
descr Target
tgt']
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
[Block]
_ ->
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts
then forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Attr -> Caption -> [Block] -> MD m (Doc Text)
figureToMarkdown WriterOptions
opts Attr
figattr Caption
capt [Block]
body
else forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown' WriterOptions
opts forall a b. (a -> b) -> a -> b
$ Attr -> Caption -> [Block] -> Block
figureDiv Attr
figattr Caption
capt [Block]
body
inList :: Monad m => MD m a -> MD m a
inList :: forall (m :: * -> *) a. Monad m => MD m a -> MD m a
inList MD m a
p = forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\WriterEnv
env -> WriterEnv
env {envInList :: Bool
envInList = Bool
True}) MD m a
p
addMarkdownAttribute :: Text -> Text
addMarkdownAttribute :: Text -> Text
addMarkdownAttribute Text
s =
case forall a. (a -> Bool) -> [a] -> ([a], [a])
span forall str. Tag str -> Bool
isTagText forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall str. StringLike str => str -> [Tag str]
parseTags Text
s of
([Tag Text]
xs, TagOpen Text
t [Target]
attrs:[Tag Text]
rest) ->
[Tag Text] -> Text
renderTags' forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse [Tag Text]
rest forall a. [a] -> [a] -> [a]
++ (forall str. str -> [Attribute str] -> Tag str
TagOpen Text
t [Target]
attrs' forall a. a -> [a] -> [a]
: forall a. [a] -> [a]
reverse [Tag Text]
xs)
where attrs' :: [Target]
attrs' = (Text
"markdown",Text
"1")forall a. a -> [a] -> [a]
:[(Text
x,Text
y) | (Text
x,Text
y) <- [Target]
attrs,
Text
x forall a. Eq a => a -> a -> Bool
/= Text
"markdown"]
([Tag Text], [Tag Text])
_ -> Text
s
figureToMarkdown :: PandocMonad m
=> WriterOptions
-> Attr
-> Caption
-> [Block]
-> MD m (Doc Text)
figureToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Attr -> Caption -> [Block] -> MD m (Doc Text)
figureToMarkdown WriterOptions
opts attr :: Attr
attr@(Text
ident, [Text]
classes, [Target]
kvs) Caption
capt [Block]
body
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts =
(forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HasChars a => a -> Doc a
literal forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.strip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeHtml5String
WriterOptions
opts{ writerTemplate :: Maybe (Template Text)
writerTemplate = forall a. Maybe a
Nothing }
(Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta [Attr -> Caption -> [Block] -> Block
Figure Attr
attr Caption
capt [Block]
body])
| Bool
otherwise = let attr' :: Attr
attr' = (Text
ident, [Text
"figure"] forall a. Eq a => [a] -> [a] -> [a]
`union` [Text]
classes, [Target]
kvs)
in forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown' WriterOptions
opts (Attr -> [Block] -> Block
Div Attr
attr' [Block]
body)
itemEndsWithTightList :: [Block] -> Bool
itemEndsWithTightList :: [Block] -> Bool
itemEndsWithTightList [Block]
bs =
case [Block]
bs of
[Plain [Inline]
_, BulletList [[Block]]
xs] -> [[Block]] -> Bool
isTightList [[Block]]
xs
[Plain [Inline]
_, OrderedList (Int, ListNumberStyle, ListNumberDelim)
_ [[Block]]
xs] -> [[Block]] -> Bool
isTightList [[Block]]
xs
[Block]
_ -> Bool
False
bulletListItemToMarkdown :: PandocMonad m => WriterOptions -> [Block] -> MD m (Doc Text)
bulletListItemToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
bulletListItemToMarkdown WriterOptions
opts [Block]
bs = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let exts :: Extensions
exts = WriterOptions -> Extensions
writerExtensions WriterOptions
opts
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts forall a b. (a -> b) -> a -> b
$ Extensions -> [Block] -> [Block]
taskListItemToAscii Extensions
exts [Block]
bs
let start :: Text
start = case MarkdownVariant
variant of
MarkdownVariant
Markua -> Text
"* "
MarkdownVariant
Commonmark -> Text
"- "
MarkdownVariant
_ -> Text
"- " forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate (WriterOptions -> Int
writerTabStop WriterOptions
opts forall a. Num a => a -> a -> a
- Int
2) Text
" "
let contents' :: Doc Text
contents' = if [Block] -> Bool
itemEndsWithTightList [Block]
bs
then forall a. Doc a -> Doc a
chomp Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
else Doc Text
contents
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang (Text -> Int
T.length Text
start) (forall a. HasChars a => a -> Doc a
literal Text
start) Doc Text
contents'
orderedListItemToMarkdown :: PandocMonad m
=> WriterOptions
-> Text
-> [Block]
-> MD m (Doc Text)
orderedListItemToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Text -> [Block] -> MD m (Doc Text)
orderedListItemToMarkdown WriterOptions
opts Text
marker [Block]
bs = do
let exts :: Extensions
exts = WriterOptions -> Extensions
writerExtensions WriterOptions
opts
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts forall a b. (a -> b) -> a -> b
$ Extensions -> [Block] -> [Block]
taskListItemToAscii Extensions
exts [Block]
bs
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let sps :: Doc Text
sps = case WriterOptions -> Int
writerTabStop WriterOptions
opts forall a. Num a => a -> a -> a
- Text -> Int
T.length Text
marker of
Int
n | Int
n forall a. Ord a => a -> a -> Bool
> Int
0 -> forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.replicate Int
n Text
" "
Int
_ -> forall a. HasChars a => a -> Doc a
literal Text
" "
let ind :: Int
ind = if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_four_space_rule WriterOptions
opts
then WriterOptions -> Int
writerTabStop WriterOptions
opts
else forall a. Ord a => a -> a -> a
max (WriterOptions -> Int
writerTabStop WriterOptions
opts) (Text -> Int
T.length Text
marker forall a. Num a => a -> a -> a
+ Int
1)
let start :: Doc Text
start = case MarkdownVariant
variant of
MarkdownVariant
Markua -> forall a. HasChars a => a -> Doc a
literal Text
marker forall a. Semigroup a => a -> a -> a
<> Doc Text
" "
MarkdownVariant
_ -> forall a. HasChars a => a -> Doc a
literal Text
marker forall a. Semigroup a => a -> a -> a
<> Doc Text
sps
let contents' :: Doc Text
contents' = if [Block] -> Bool
itemEndsWithTightList [Block]
bs
then forall a. Doc a -> Doc a
chomp Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
else Doc Text
contents
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
ind Doc Text
start Doc Text
contents'
definitionListItemToMarkdown :: PandocMonad m
=> WriterOptions
-> ([Inline],[[Block]])
-> MD m (Doc Text)
definitionListItemToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> ([Inline], [[Block]]) -> MD m (Doc Text)
definitionListItemToMarkdown WriterOptions
opts ([Inline]
label, [[Block]]
defs) = do
Doc Text
labelText <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown WriterOptions
opts ([Inline] -> Block
Plain [Inline]
label)
[[Doc Text]]
defs' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown WriterOptions
opts)) [[Block]]
defs
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_definition_lists WriterOptions
opts
then do
let tabStop :: Int
tabStop = WriterOptions -> Int
writerTabStop WriterOptions
opts
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let leader :: Doc Text
leader = case MarkdownVariant
variant of
MarkdownVariant
PlainText -> Doc Text
" "
MarkdownVariant
Markua -> Doc Text
":"
MarkdownVariant
_ -> Doc Text
": "
let sps :: Doc Text
sps = case WriterOptions -> Int
writerTabStop WriterOptions
opts forall a. Num a => a -> a -> a
- Int
3 of
Int
n | Int
n forall a. Ord a => a -> a -> Bool
> Int
0 -> forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.replicate Int
n Text
" "
Int
_ -> forall a. HasChars a => a -> Doc a
literal Text
" "
let isTight :: Bool
isTight = case [[Block]]
defs of
((Plain [Inline]
_ : [Block]
_): [[Block]]
_) -> Bool
True
[[Block]]
_ -> Bool
False
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_compact_definition_lists WriterOptions
opts
then do
let contents :: Doc Text
contents = forall a. [Doc a] -> Doc a
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\[Doc Text]
d -> forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
tabStop (Doc Text
leader forall a. Semigroup a => a -> a -> a
<> Doc Text
sps)
forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
vcat [Doc Text]
d forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr) [[Doc Text]]
defs'
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IsString a => Doc a -> Doc a
nowrap Doc Text
labelText forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
else do
let contents :: Doc Text
contents = (if Bool
isTight then forall a. [Doc a] -> Doc a
vcat else forall a. [Doc a] -> Doc a
vsep) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map
(\[Doc Text]
d -> forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
hang Int
tabStop (Doc Text
leader forall a. Semigroup a => a -> a -> a
<> Doc Text
sps) forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
vcat [Doc Text]
d)
[[Doc Text]]
defs'
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => Doc a -> Doc a
nowrap Doc Text
labelText forall a. Doc a -> Doc a -> Doc a
$$
(if Bool
isTight then forall a. Doc a
empty else forall a. Doc a
blankline) forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
else
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IsString a => Doc a -> Doc a
nowrap (forall a. Doc a -> Doc a
chomp Doc Text
labelText forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
" " forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr) forall a. Semigroup a => a -> a -> a
<>
forall a. [Doc a] -> Doc a
vsep (forall a b. (a -> b) -> [a] -> [b]
map forall a. [Doc a] -> Doc a
vsep [[Doc Text]]
defs') forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
blockListToMarkdown :: PandocMonad m
=> WriterOptions
-> [Block]
-> MD m (Doc Text)
blockListToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> MD m (Doc Text)
blockListToMarkdown WriterOptions
opts [Block]
blocks = do
Bool
inlist <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> Bool
envInList
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let fixBlocks :: [Block] -> [Block]
fixBlocks (Block
b : CodeBlock Attr
attr Text
x : [Block]
rest)
| (Bool -> Bool
not (MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Commonmark Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_backtick_code_blocks WriterOptions
opts Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_fenced_code_blocks WriterOptions
opts) Bool -> Bool -> Bool
||
Attr
attr forall a. Eq a => a -> a -> Bool
== Attr
nullAttr)
Bool -> Bool -> Bool
&& Block -> Bool
isListBlock Block
b
= Block
b forall a. a -> [a] -> [a]
: Block
commentSep forall a. a -> [a] -> [a]
: Attr -> Text -> Block
CodeBlock Attr
attr Text
x forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks [Block]
rest
fixBlocks (b1 :: Block
b1@(BulletList [[Block]]
_) : b2 :: Block
b2@(BulletList [[Block]]
_) : [Block]
bs) =
Block
b1 forall a. a -> [a] -> [a]
: Block
commentSep forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks (Block
b2forall a. a -> [a] -> [a]
:[Block]
bs)
fixBlocks (b1 :: Block
b1@(OrderedList (Int, ListNumberStyle, ListNumberDelim)
_ [[Block]]
_) : b2 :: Block
b2@(OrderedList (Int, ListNumberStyle, ListNumberDelim)
_ [[Block]]
_) : [Block]
bs) =
Block
b1 forall a. a -> [a] -> [a]
: Block
commentSep forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks (Block
b2forall a. a -> [a] -> [a]
:[Block]
bs)
fixBlocks (b1 :: Block
b1@(DefinitionList [([Inline], [[Block]])]
_) : b2 :: Block
b2@(DefinitionList [([Inline], [[Block]])]
_) : [Block]
bs) =
Block
b1 forall a. a -> [a] -> [a]
: Block
commentSep forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks (Block
b2forall a. a -> [a] -> [a]
:[Block]
bs)
fixBlocks (Plain [Inline]
ils : bs :: [Block]
bs@(RawBlock{}:[Block]
_)) =
[Inline] -> Block
Plain [Inline]
ils forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks [Block]
bs
fixBlocks (Plain [Inline]
ils : bs :: [Block]
bs@(Div{}:[Block]
_))
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_fenced_divs WriterOptions
opts =
[Inline] -> Block
Para [Inline]
ils forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks [Block]
bs
fixBlocks (Plain [Inline]
ils : [Block]
bs) | Bool
inlist =
[Inline] -> Block
Plain [Inline]
ils forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks [Block]
bs
fixBlocks (Plain [Inline]
ils : [Block]
bs) =
[Inline] -> Block
Para [Inline]
ils forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks [Block]
bs
fixBlocks (r :: Block
r@(RawBlock Format
f Text
raw) : Block
b : [Block]
bs)
| Bool -> Bool
not (Text -> Bool
T.null Text
raw)
, Text -> Char
T.last Text
raw forall a. Eq a => a -> a -> Bool
/= Char
'\n' =
case Block
b of
Plain{} -> Block
r forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks (Block
bforall a. a -> [a] -> [a]
:[Block]
bs)
RawBlock{} -> Block
r forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks (Block
bforall a. a -> [a] -> [a]
:[Block]
bs)
Block
_ -> Format -> Text -> Block
RawBlock Format
f (Text
raw forall a. Semigroup a => a -> a -> a
<> Text
"\n") forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks (Block
bforall a. a -> [a] -> [a]
:[Block]
bs)
fixBlocks (Block
x : [Block]
xs) = Block
x forall a. a -> [a] -> [a]
: [Block] -> [Block]
fixBlocks [Block]
xs
fixBlocks [] = []
isListBlock :: Block -> Bool
isListBlock (BulletList [[Block]]
_) = Bool
True
isListBlock (OrderedList (Int, ListNumberStyle, ListNumberDelim)
_ [[Block]]
_) = Bool
True
isListBlock (DefinitionList [([Inline], [[Block]])]
_) = Bool
True
isListBlock Block
_ = Bool
False
commentSep :: Block
commentSep
| MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText = [Inline] -> Block
Plain []
| MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua = [Inline] -> Block
Plain []
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts = Format -> Text -> Block
RawBlock Format
"html" Text
"<!-- -->\n"
| Bool
otherwise = Format -> Text -> Block
RawBlock Format
"markdown" Text
" \n"
forall a. Monoid a => [a] -> a
mconcat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> MD m (Doc Text)
blockToMarkdown WriterOptions
opts) ([Block] -> [Block]
fixBlocks [Block]
blocks)
lineBreakToSpace :: Inline -> Inline
lineBreakToSpace :: Inline -> Inline
lineBreakToSpace Inline
LineBreak = Inline
Space
lineBreakToSpace Inline
SoftBreak = Inline
Space
lineBreakToSpace Inline
x = Inline
x
removeBlankLinesInHTML :: Text -> Text
removeBlankLinesInHTML :: Text -> Text
removeBlankLinesInHTML = String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> String -> String
go Bool
False forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
where go :: Bool -> String -> String
go Bool
_ [] = []
go Bool
True (Char
'\n':String
cs) = String
" " forall a. Semigroup a => a -> a -> a
<> Bool -> String -> String
go Bool
False String
cs
go Bool
False (Char
'\n':String
cs) = Char
'\n' forall a. a -> [a] -> [a]
: Bool -> String -> String
go Bool
True String
cs
go !Bool
afternewline (!Char
c:String
cs)
| Char -> Bool
isSpace Char
c = Char
c forall a. a -> [a] -> [a]
: Bool -> String -> String
go Bool
afternewline String
cs
| Bool
otherwise = Char
c forall a. a -> [a] -> [a]
: Bool -> String -> String
go Bool
False String
cs