{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
module Text.Pandoc.Writers.Markdown.Inline (
inlineListToMarkdown,
linkAttributes,
attrsToMarkdown,
attrsToMarkua
) where
import Control.Monad (when, liftM2)
import Control.Monad.Reader
( asks, MonadReader(local) )
import Control.Monad.State.Strict
( MonadState(get), gets, modify )
import Data.Char (isAlphaNum, isDigit)
import Data.List (find, intersperse)
import Data.List.NonEmpty (nonEmpty)
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
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.URI (urlEncode, escapeURI, isURI)
import Text.Pandoc.Writers.Shared
import Text.Pandoc.Walk
import Text.Pandoc.Writers.HTML (writeHtml5String)
import Text.Pandoc.Writers.Math (texMathToInlines)
import Text.Pandoc.XML (toHtml5Entities)
import Data.Coerce (coerce)
import Text.Pandoc.Writers.Markdown.Types (MarkdownVariant(..),
WriterState(..),
WriterEnv(..), MD)
escapeText :: WriterOptions -> Text -> Text
escapeText :: WriterOptions -> Text -> Text
escapeText WriterOptions
opts = String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
go' forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
where
startsWithSpace :: String -> Bool
startsWithSpace (Char
' ':String
_) = Bool
True
startsWithSpace (Char
'\t':String
_) = Bool
True
startsWithSpace [] = Bool
True
startsWithSpace String
_ = Bool
False
go' :: String -> String
go' (Char
'#':String
cs)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_space_in_atx_header WriterOptions
opts
= if String -> Bool
startsWithSpace (forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
==Char
'#') String
cs)
then Char
'\\'forall a. a -> [a] -> [a]
:Char
'#'forall a. a -> [a] -> [a]
:String -> String
go String
cs
else Char
'#'forall a. a -> [a] -> [a]
:String -> String
go String
cs
| Bool
otherwise = Char
'\\'forall a. a -> [a] -> [a]
:Char
'#'forall a. a -> [a] -> [a]
:String -> String
go String
cs
go' (Char
'@':String
cs)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_citations WriterOptions
opts =
case String
cs of
(Char
d:String
_)
| Char -> Bool
isAlphaNum Char
d Bool -> Bool -> Bool
|| Char
d forall a. Eq a => a -> a -> Bool
== Char
'_' Bool -> Bool -> Bool
|| Char
d forall a. Eq a => a -> a -> Bool
== Char
'{'
-> Char
'\\'forall a. a -> [a] -> [a]
:Char
'@'forall a. a -> [a] -> [a]
:String -> String
go String
cs
String
_ -> Char
'@'forall a. a -> [a] -> [a]
:String -> String
go String
cs
go' String
cs = String -> String
go String
cs
go :: String -> String
go [] = []
go [Char
'\\'] = [Char
'\\',Char
'\\']
go (Char
'-':Char
'-':String
cs)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_smart WriterOptions
opts = Char
'\\'forall a. a -> [a] -> [a]
:Char
'-'forall a. a -> [a] -> [a]
:String -> String
go(Char
'-'forall a. a -> [a] -> [a]
:String
cs)
go (Char
'.':Char
'.':Char
'.':String
cs)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_smart WriterOptions
opts = Char
'\\'forall a. a -> [a] -> [a]
:Char
'.'forall a. a -> [a] -> [a]
:Char
'.'forall a. a -> [a] -> [a]
:Char
'.'forall a. a -> [a] -> [a]
:String -> String
go String
cs
go (Char
c:Char
'_':Char
d:String
cs)
| Char -> Bool
isAlphaNum Char
c
, Char -> Bool
isAlphaNum Char
d =
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_intraword_underscores WriterOptions
opts
then Char
cforall a. a -> [a] -> [a]
:Char
'_'forall a. a -> [a] -> [a]
:String -> String
go (Char
dforall a. a -> [a] -> [a]
:String
cs)
else Char
cforall a. a -> [a] -> [a]
:Char
'\\'forall a. a -> [a] -> [a]
:Char
'_'forall a. a -> [a] -> [a]
:String -> String
go (Char
dforall a. a -> [a] -> [a]
:String
cs)
go (Char
'\\':Char
c:String
cs)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_tex WriterOptions
opts = Char
'\\'forall a. a -> [a] -> [a]
:Char
'\\'forall a. a -> [a] -> [a]
:String -> String
go (Char
cforall a. a -> [a] -> [a]
:String
cs)
| Char -> Bool
isAlphaNum Char
c = Char
'\\' forall a. a -> [a] -> [a]
: String -> String
go (Char
cforall a. a -> [a] -> [a]
:String
cs)
| Bool
otherwise = Char
'\\'forall a. a -> [a] -> [a]
:Char
'\\'forall a. a -> [a] -> [a]
: String -> String
go String
cs
go (Char
'!':Char
'[':String
cs) = Char
'\\'forall a. a -> [a] -> [a]
:Char
'!'forall a. a -> [a] -> [a]
:Char
'['forall a. a -> [a] -> [a]
: String -> String
go String
cs
go (Char
'=':Char
'=':String
cs)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_mark WriterOptions
opts = Char
'\\'forall a. a -> [a] -> [a]
:Char
'='forall a. a -> [a] -> [a]
:String -> String
go (Char
'='forall a. a -> [a] -> [a]
:String
cs)
go (Char
'~':Char
'~':String
cs)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_strikeout WriterOptions
opts = Char
'\\'forall a. a -> [a] -> [a]
:Char
'~'forall a. a -> [a] -> [a]
:String -> String
go (Char
'~'forall a. a -> [a] -> [a]
:String
cs)
go (Char
c:String
cs) =
case Char
c of
Char
'[' -> Char
'\\'forall a. a -> [a] -> [a]
:Char
cforall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
']' -> Char
'\\'forall a. a -> [a] -> [a]
:Char
cforall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'`' -> Char
'\\'forall a. a -> [a] -> [a]
:Char
cforall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'*' -> Char
'\\'forall a. a -> [a] -> [a]
:Char
cforall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'_' -> Char
'\\'forall a. a -> [a] -> [a]
:Char
cforall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'>' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_all_symbols_escapable WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'>'forall a. a -> [a] -> [a]
:String -> String
go String
cs
| Bool
otherwise -> String
">" forall a. [a] -> [a] -> [a]
++ String -> String
go String
cs
Char
'<' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_all_symbols_escapable WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'<'forall a. a -> [a] -> [a]
:String -> String
go String
cs
| Bool
otherwise -> String
"<" forall a. [a] -> [a] -> [a]
++ String -> String
go String
cs
Char
'|' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_pipe_tables WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'|'forall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'^' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_superscript WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'^'forall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'~' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_subscript WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'~'forall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'$' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_tex_math_dollars WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'$'forall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'\'' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_smart WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'\''forall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
'"' | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_smart WriterOptions
opts -> Char
'\\'forall a. a -> [a] -> [a]
:Char
'"'forall a. a -> [a] -> [a]
:String -> String
go String
cs
Char
_ -> Char
c forall a. a -> [a] -> [a]
: String -> String
go String
cs
escapeMarkuaString :: Text -> Text
escapeMarkuaString :: Text -> Text
escapeMarkuaString Text
s = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Text -> Text -> Text -> Text
T.replace) Text
s [(Text
"--",Text
"~-~-"),
(Text
"**",Text
"~*~*"),(Text
"//",Text
"~/~/"),(Text
"^^",Text
"~^~^"),(Text
",,",Text
"~,~,")]
attrsToMarkdown :: WriterOptions -> Attr -> Doc Text
attrsToMarkdown :: WriterOptions -> Attr -> Doc Text
attrsToMarkdown WriterOptions
opts Attr
attribs = forall a. HasChars a => Doc a -> Doc a
braces forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
hsep [Doc Text
attribId, Doc Text
attribClasses, Doc Text
attribKeys]
where attribId :: Doc Text
attribId = case Attr
attribs of
(Text
"",[Text]
_,[(Text, Text)]
_) -> forall a. Doc a
empty
(Text
i,[Text]
_,[(Text, Text)]
_) -> Doc Text
"#" forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
escAttr (WriterOptions -> Text
writerIdentifierPrefix WriterOptions
opts forall a. Semigroup a => a -> a -> a
<> Text
i)
attribClasses :: Doc Text
attribClasses = case Attr
attribs of
(Text
_,[],[(Text, Text)]
_) -> forall a. Doc a
empty
(Text
_,[Text]
cs,[(Text, Text)]
_) -> forall a. [Doc a] -> Doc a
hsep forall a b. (a -> b) -> a -> b
$
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Doc Text
escAttr forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"."forall a. Semigroup a => a -> a -> a
<>)) forall a b. (a -> b) -> a -> b
$
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
T.null) [Text]
cs
attribKeys :: Doc Text
attribKeys = case Attr
attribs of
(Text
_,[Text]
_,[]) -> forall a. Doc a
empty
(Text
_,[Text]
_,[(Text, Text)]
ks) -> forall a. [Doc a] -> Doc a
hsep forall a b. (a -> b) -> a -> b
$
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
k,Text
v) -> Text -> Doc Text
escAttr Text
k
forall a. Semigroup a => a -> a -> a
<> Doc Text
"=\"" forall a. Semigroup a => a -> a -> a
<>
Text -> Doc Text
escAttr Text
v forall a. Semigroup a => a -> a -> a
<> Doc Text
"\"") [(Text, Text)]
ks
escAttr :: Text -> Doc Text
escAttr = forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Char -> Doc Text
escAttrChar forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
escAttrChar :: Char -> Doc Text
escAttrChar Char
'"' = forall a. HasChars a => a -> Doc a
literal Text
"\\\""
escAttrChar Char
'\\' = forall a. HasChars a => a -> Doc a
literal Text
"\\\\"
escAttrChar Char
c = forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Char -> Text
T.singleton Char
c
attrsToMarkua:: WriterOptions -> Attr -> Doc Text
attrsToMarkua :: WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts Attr
attributes
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Doc Text]
list = forall a. Doc a
empty
| Bool
otherwise = forall a. HasChars a => Doc a -> Doc a
braces forall a b. (a -> b) -> a -> b
$ [Doc Text] -> Doc Text
intercalateDocText [Doc Text]
list
where attrId :: [Doc Text]
attrId = case Attr
attributes of
(Text
"",[Text]
_,[(Text, Text)]
_) -> []
(Text
i,[Text]
_,[(Text, Text)]
_) -> [forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Text
"id: " forall a. Semigroup a => a -> a -> a
<> WriterOptions -> Text
writerIdentifierPrefix WriterOptions
opts forall a. Semigroup a => a -> a -> a
<> Text
i]
attrClasses :: [Doc Text]
attrClasses = case Attr
attributes of
(Text
_,[],[(Text, Text)]
_) -> []
(Text
_,[Text]
classes,[(Text, Text)]
_) -> forall a b. (a -> b) -> [a] -> [b]
map (Text -> Doc Text
escAttr forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"class: " forall a. Semigroup a => a -> a -> a
<>))
[Text]
classes
attrKeyValues :: [Doc Text]
attrKeyValues = case Attr
attributes of
(Text
_,[Text]
_,[]) -> []
(Text
_,[Text]
_,[(Text, Text)]
keyvalues) -> forall a b. (a -> b) -> [a] -> [b]
map ((\(Text
k,Text
v) -> Text -> Doc Text
escAttr Text
k
forall a. Semigroup a => a -> a -> a
<> Doc Text
": " forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
escAttr Text
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(Text, Text) -> (Text, Text)
preprocessKeyValues) [(Text, Text)]
keyvalues
escAttr :: Text -> Doc Text
escAttr = forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Char -> Doc Text
escAttrChar forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
escAttrChar :: Char -> Doc Text
escAttrChar Char
'"' = forall a. HasChars a => a -> Doc a
literal Text
"\""
escAttrChar Char
c = forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Char -> Text
T.singleton Char
c
list :: [Doc Text]
list = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Doc Text]
attrId, [Doc Text]
attrClasses, [Doc Text]
attrKeyValues]
preprocessKeyValues :: (Text, Text) -> (Text, Text)
preprocessKeyValues :: (Text, Text) -> (Text, Text)
preprocessKeyValues (Text
key,Text
value)
| Text
key forall a. Eq a => a -> a -> Bool
== Text
"alt" Bool -> Bool -> Bool
||
Text
key forall a. Eq a => a -> a -> Bool
== Text
"caption" Bool -> Bool -> Bool
||
Text
key forall a. Eq a => a -> a -> Bool
== Text
"title" = (Text
key, Text -> Text
inquotes Text
value)
| Bool
otherwise = (Text
key,Text
value)
intercalateDocText :: [Doc Text] -> Doc Text
intercalateDocText :: [Doc Text] -> Doc Text
intercalateDocText [] = forall a. Doc a
empty
intercalateDocText [Doc Text
x] = Doc Text
x
intercalateDocText (Doc Text
x:[Doc Text]
xs) = Doc Text
x forall a. Semigroup a => a -> a -> a
<> Doc Text
", " forall a. Semigroup a => a -> a -> a
<> ([Doc Text] -> Doc Text
intercalateDocText [Doc Text]
xs)
addKeyValueToAttr :: Attr -> (Text,Text) -> Attr
addKeyValueToAttr :: Attr -> (Text, Text) -> Attr
addKeyValueToAttr (Text
ident,[Text]
classes,[(Text, Text)]
kvs) (Text
key,Text
value)
| Bool -> Bool
not (Text -> Bool
T.null Text
key) Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Bool
T.null Text
value) = (Text
ident,
[Text]
classes,
(Text
key,Text
value)forall a. a -> [a] -> [a]
: [(Text, Text)]
kvs)
| Bool
otherwise = (Text
ident,[Text]
classes,[(Text, Text)]
kvs)
linkAttributes :: WriterOptions -> Attr -> Doc Text
linkAttributes :: WriterOptions -> Attr -> Doc Text
linkAttributes WriterOptions
opts Attr
attr =
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) Bool -> Bool -> Bool
&& Attr
attr forall a. Eq a => a -> a -> Bool
/= Attr
nullAttr
then WriterOptions -> Attr -> Doc Text
attrsToMarkdown WriterOptions
opts Attr
attr
else forall a. Doc a
empty
getKey :: Doc Text -> Key
getKey :: Doc Text -> Key
getKey = Text -> Key
toKey forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HasChars a => Maybe Int -> Doc a -> a
render forall a. Maybe a
Nothing
findUsableIndex :: [Text] -> Int -> Int
findUsableIndex :: [Text] -> Int -> Int
findUsableIndex [Text]
lbls Int
i = if forall a. Show a => a -> Text
tshow Int
i forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
lbls
then [Text] -> Int -> Int
findUsableIndex [Text]
lbls (Int
i forall a. Num a => a -> a -> a
+ Int
1)
else Int
i
getNextIndex :: PandocMonad m => MD m Int
getNextIndex :: forall (m :: * -> *). PandocMonad m => MD m Int
getNextIndex = do
Refs
prevRefs <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Refs
stPrevRefs
Refs
refs <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Refs
stRefs
Int
i <- (forall a. Num a => a -> a -> a
+ Int
1) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Int
stLastIdx
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s{ stLastIdx :: Int
stLastIdx = Int
i }
let refLbls :: [Text]
refLbls = forall a b. (a -> b) -> [a] -> [b]
map (\(Text
r,(Text, Text)
_,Attr
_) -> Text
r) forall a b. (a -> b) -> a -> b
$ Refs
prevRefs forall a. [a] -> [a] -> [a]
++ Refs
refs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Text] -> Int -> Int
findUsableIndex [Text]
refLbls Int
i
getReference :: PandocMonad m => Attr -> Doc Text -> Target -> MD m Text
getReference :: forall (m :: * -> *).
PandocMonad m =>
Attr -> Doc Text -> (Text, Text) -> MD m Text
getReference Attr
attr Doc Text
label (Text, Text)
target = do
Refs
refs <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Refs
stRefs
case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\(Text
_,(Text, Text)
t,Attr
a) -> (Text, Text)
t forall a. Eq a => a -> a -> Bool
== (Text, Text)
target Bool -> Bool -> Bool
&& Attr
a forall a. Eq a => a -> a -> Bool
== Attr
attr) Refs
refs of
Just (Text
ref, (Text, Text)
_, Attr
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return Text
ref
Maybe (Text, (Text, Text), Attr)
Nothing -> do
Map Key (Map ((Text, Text), Attr) Int)
keys <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Map Key (Map ((Text, Text), Attr) Int)
stKeys
let key :: Key
key = Doc Text -> Key
getKey Doc Text
label
let rawkey :: Text
rawkey = coerce :: forall a b. Coercible a b => a -> b
coerce Key
key
case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Key
key Map Key (Map ((Text, Text), Attr) Int)
keys of
Maybe (Map ((Text, Text), Attr) Int)
Nothing -> do
(Text
lab', Int
idx) <- if Text -> Bool
T.null Text
rawkey Bool -> Bool -> Bool
||
Text -> Int
T.length Text
rawkey forall a. Ord a => a -> a -> Bool
> Int
999 Bool -> Bool -> Bool
||
(Char -> Bool) -> Text -> Bool
T.any (\Char
c -> Char
c forall a. Eq a => a -> a -> Bool
== Char
'[' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
']') Text
rawkey
then do
Int
i <- forall (m :: * -> *). PandocMonad m => MD m Int
getNextIndex
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Show a => a -> Text
tshow Int
i, Int
i)
else
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. HasChars a => Maybe Int -> Doc a -> a
render forall a. Maybe a
Nothing Doc Text
label, Int
0)
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
s -> WriterState
s{
stRefs :: Refs
stRefs = (Text
lab', (Text, Text)
target, Attr
attr) forall a. a -> [a] -> [a]
: Refs
refs,
stKeys :: Map Key (Map ((Text, Text), Attr) Int)
stKeys = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert (Doc Text -> Key
getKey Doc Text
label)
(forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert ((Text, Text)
target, Attr
attr) Int
idx forall a. Monoid a => a
mempty)
(WriterState -> Map Key (Map ((Text, Text), Attr) Int)
stKeys WriterState
s) })
forall (m :: * -> *) a. Monad m => a -> m a
return Text
lab'
Just Map ((Text, Text), Attr) Int
km ->
case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ((Text, Text)
target, Attr
attr) Map ((Text, Text), Attr) Int
km of
Just Int
i -> do
let lab' :: Text
lab' = forall a. HasChars a => Maybe Int -> Doc a -> a
render forall a. Maybe a
Nothing forall a b. (a -> b) -> a -> b
$
Doc Text
label forall a. Semigroup a => a -> a -> a
<> if Int
i forall a. Eq a => a -> a -> Bool
== Int
0
then forall a. Monoid a => a
mempty
else forall a. HasChars a => a -> Doc a
literal (forall a. Show a => a -> Text
tshow Int
i)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Text
lab', (Text, Text)
target, Attr
attr) forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` Refs
refs) forall a b. (a -> b) -> a -> b
$
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
s -> WriterState
s{
stRefs :: Refs
stRefs = (Text
lab', (Text, Text)
target, Attr
attr) forall a. a -> [a] -> [a]
: Refs
refs })
forall (m :: * -> *) a. Monad m => a -> m a
return Text
lab'
Maybe Int
Nothing -> do
Int
i <- forall (m :: * -> *). PandocMonad m => MD m Int
getNextIndex
let lab' :: Text
lab' = forall a. Show a => a -> Text
tshow Int
i
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
s -> WriterState
s{
stRefs :: Refs
stRefs = (Text
lab', (Text, Text)
target, Attr
attr) forall a. a -> [a] -> [a]
: Refs
refs,
stKeys :: Map Key (Map ((Text, Text), Attr) Int)
stKeys = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Key
key
(forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert ((Text, Text)
target, Attr
attr) Int
i Map ((Text, Text), Attr) Int
km)
(WriterState -> Map Key (Map ((Text, Text), Attr) Int)
stKeys WriterState
s) })
forall (m :: * -> *) a. Monad m => a -> m a
return Text
lab'
inlineListToMarkdown :: PandocMonad m => WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
ils = do
Bool
inlist <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> Bool
envInList
Bool -> Doc Text -> Doc Text
avoidBadWraps Bool
inlist forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {m :: * -> *}.
PandocMonad m =>
[Inline] -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
go [Inline]
ils
where go :: [Inline] -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
go [] = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
go (x :: Inline
x@Math{}:y :: Inline
y@(Str Text
t):[Inline]
zs)
| (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit (Int -> Text -> Text
T.take Int
1 Text
t)
= forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. Semigroup a => a -> a -> a
(<>) (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts Inline
x)
([Inline] -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
go (Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"html") Text
"<!-- -->" forall a. a -> [a] -> [a]
: Inline
y forall a. a -> [a] -> [a]
: [Inline]
zs))
go (Str Text
t : Inline
i : [Inline]
is)
| Inline -> Bool
isLinkOrSpan Inline
i
, Int -> Text -> Text
T.takeEnd Int
1 Text
t forall a. Eq a => a -> a -> Bool
== Text
"!"
= do Doc Text
x <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts (Text -> Inline
Str (Int -> Text -> Text
T.dropEnd Int
1 Text
t))
((Doc Text
x forall a. Semigroup a => a -> a -> a
<> Doc Text
"\\!") forall a. Semigroup a => a -> a -> a
<>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Inline] -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
go (Inline
iforall a. a -> [a] -> [a]
:[Inline]
is)
go (Inline
i:[Inline]
is) = case Inline
i of
Link {} -> case [Inline]
is of
Link {}:[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
Space:Link {}:[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
Space:(Str(Text -> Maybe Char
thead -> Just Char
'[')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
Space:(RawInline Format
_ (Text -> Maybe Char
thead -> Just Char
'[')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
Space:(Cite [Citation]
_ [Inline]
_):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
SoftBreak:Link {}:[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
SoftBreak:(Str(Text -> Maybe Char
thead -> Just Char
'[')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
SoftBreak:(RawInline Format
_ (Text -> Maybe Char
thead -> Just Char
'[')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
SoftBreak:(Cite [Citation]
_ [Inline]
_):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
LineBreak:Link {}:[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
LineBreak:(Str(Text -> Maybe Char
thead -> Just Char
'[')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
LineBreak:(RawInline Format
_ (Text -> Maybe Char
thead -> Just Char
'[')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Inline
LineBreak:(Cite [Citation]
_ [Inline]
_):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
(Cite [Citation]
_ [Inline]
_):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Str (Text -> Maybe Char
thead -> Just Char
'['):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Str (Text -> Maybe Char
thead -> Just Char
'('):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
Str (Text -> Maybe Char
thead -> Just Char
':'):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
(RawInline Format
_ (Text -> Maybe Char
thead -> Just Char
'[')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
(RawInline Format
_ (Text -> Maybe Char
thead -> Just Char
'(')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
(RawInline Format
_ (Text -> Maybe Char
thead -> Just Char
':')):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
(RawInline Format
_ (Text -> Text -> Maybe Text
T.stripPrefix Text
" [" -> Just Text
_ )):[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable
[Inline]
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
shortcutable
Inline
_ -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
shortcutable
where
shortcutable :: ReaderT WriterEnv (StateT WriterState m) (Doc Text)
shortcutable = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. Semigroup a => a -> a -> a
(<>) (forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts Inline
i) ([Inline] -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
go [Inline]
is)
unshortcutable :: ReaderT WriterEnv (StateT WriterState m) (Doc Text)
unshortcutable = do
Doc Text
iMark <- forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
(\WriterEnv
env -> WriterEnv
env { envRefShortcutable :: Bool
envRefShortcutable = Bool
False })
(forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts Inline
i)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Doc Text
iMark forall a. Semigroup a => a -> a -> a
<>) ([Inline] -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
go [Inline]
is)
thead :: Text -> Maybe Char
thead = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (Char, Text)
T.uncons
isLinkOrSpan :: Inline -> Bool
isLinkOrSpan Link{} = Bool
True
isLinkOrSpan Span{} = Bool
True
isLinkOrSpan Inline
_ = Bool
False
avoidBadWraps :: Bool -> Doc Text -> Doc Text
avoidBadWraps :: Bool -> Doc Text -> Doc Text
avoidBadWraps Bool
inListItem = [Doc Text] -> Doc Text
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Doc a -> [Doc a]
toList
where
go :: [Doc Text] -> Doc Text
go [] = forall a. Monoid a => a
mempty
go (Doc Text
BreakingSpace : Text Int
len Text
t : Doc Text
BreakingSpace : [Doc Text]
xs)
= case Text -> Maybe (Char, Text)
T.uncons Text
t of
Just (Char
c,Text
t')
| Char
c forall a. Eq a => a -> a -> Bool
== Char
'>'
Bool -> Bool -> Bool
|| ((Char
c forall a. Eq a => a -> a -> Bool
== Char
'-' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'*' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'+') Bool -> Bool -> Bool
&& Text -> Bool
T.null Text
t')
Bool -> Bool -> Bool
|| (Bool
inListItem Bool -> Bool -> Bool
&& Text -> Bool
isOrderedListMarker Text
t)
Bool -> Bool -> Bool
|| (Text
t forall a. Eq a => a -> a -> Bool
== Text
"1." Bool -> Bool -> Bool
|| Text
t forall a. Eq a => a -> a -> Bool
== Text
"1)")
-> forall a. Int -> a -> Doc a
Text (Int
len forall a. Num a => a -> a -> a
+ Int
1) (Text
" " forall a. Semigroup a => a -> a -> a
<> Text
t) forall a. Semigroup a => a -> a -> a
<> [Doc Text] -> Doc Text
go (forall a. Doc a
BreakingSpace forall a. a -> [a] -> [a]
: [Doc Text]
xs)
Maybe (Char, Text)
_ -> forall a. Doc a
BreakingSpace forall a. Semigroup a => a -> a -> a
<> forall a. Int -> a -> Doc a
Text Int
len Text
t forall a. Semigroup a => a -> a -> a
<> [Doc Text] -> Doc Text
go (forall a. Doc a
BreakingSpace forall a. a -> [a] -> [a]
: [Doc Text]
xs)
go (Doc Text
x:[Doc Text]
xs) = Doc Text
x forall a. Semigroup a => a -> a -> a
<> [Doc Text] -> Doc Text
go [Doc Text]
xs
toList :: Doc a -> [Doc a]
toList (Concat (Concat Doc a
a Doc a
b) Doc a
c) = Doc a -> [Doc a]
toList (forall a. Doc a -> Doc a -> Doc a
Concat Doc a
a (forall a. Doc a -> Doc a -> Doc a
Concat Doc a
b Doc a
c))
toList (Concat Doc a
a Doc a
b) = Doc a
a forall a. a -> [a] -> [a]
: Doc a -> [Doc a]
toList Doc a
b
toList Doc a
x = [Doc a
x]
isOrderedListMarker :: Text -> Bool
isOrderedListMarker :: Text -> Bool
isOrderedListMarker Text
xs = Bool -> Bool
not (Text -> Bool
T.null Text
xs) Bool -> Bool -> Bool
&& (Text -> Char
T.last Text
xs forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'.',Char
')']) Bool -> Bool -> Bool
&&
forall {a} {b}. Either a b -> Bool
isRight (forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> String -> s -> Either ParseError a
runParser (forall s (m :: * -> *).
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s ParserState m ListAttributes
anyOrderedListMarker forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof)
ParserState
defaultParserState String
"" Text
xs)
where
isRight :: Either a b -> Bool
isRight (Right b
_) = Bool
True
isRight (Left a
_) = Bool
False
inlineToMarkdown :: PandocMonad m => WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts (Span (Text
"",[Text
"emoji"],[(Text, Text)]
kvs) [Str Text
s]) =
case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"data-emoji" [(Text, Text)]
kvs of
Just Text
emojiname | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_emoji WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
":" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
emojiname forall a. Semigroup a => a -> a -> a
<> Doc Text
":"
Maybe Text
_ -> forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts (Text -> Inline
Str Text
s)
inlineToMarkdown WriterOptions
opts (Span (Text
"",[Text
"mark"],[]) [Inline]
ils)
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_mark WriterOptions
opts
= do Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
ils
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"==" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"=="
inlineToMarkdown WriterOptions
opts (Span Attr
attrs [Inline]
ils) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
ils
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Attr
attrs of
(Text
_,[Text
"csl-block"],[(Text, Text)]
_) -> (forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>)
(Text
_,[Text
"csl-left-margin"],[(Text, Text)]
_) -> (forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>)
(Text
_,[Text
"csl-indent"],[(Text, Text)]
_) -> (forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<>)
Attr
_ -> forall a. a -> a
id
forall a b. (a -> b) -> a -> b
$ case MarkdownVariant
variant of
MarkdownVariant
PlainText -> Doc Text
contents
MarkdownVariant
Markua -> Doc Text
"`" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"`" forall a. Semigroup a => a -> a -> a
<> WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts Attr
attrs
MarkdownVariant
_ | Attr
attrs forall a. Eq a => a -> a -> Bool
== Attr
nullAttr -> Doc Text
contents
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_bracketed_spans WriterOptions
opts ->
let attrs' :: Doc Text
attrs' = if Attr
attrs forall a. Eq a => a -> a -> Bool
/= Attr
nullAttr
then WriterOptions -> Attr -> Doc Text
attrsToMarkdown WriterOptions
opts Attr
attrs
else forall a. Doc a
empty
in Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"]" forall a. Semigroup a => a -> a -> a
<> Doc Text
attrs'
| 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_native_spans WriterOptions
opts ->
forall a. HasChars a => a -> Attr -> Doc a
tagWithAttrs Text
"span" Attr
attrs forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"</span>"
| Bool
otherwise -> Doc Text
contents
inlineToMarkdown WriterOptions
_ (Emph []) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
inlineToMarkdown WriterOptions
opts (Emph [Inline]
lst) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case MarkdownVariant
variant of
MarkdownVariant
PlainText
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_gutenberg WriterOptions
opts -> Doc Text
"_" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"_"
| Bool
otherwise -> Doc Text
contents
MarkdownVariant
_ -> Doc Text
"*" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"*"
inlineToMarkdown WriterOptions
_ (Underline []) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
inlineToMarkdown WriterOptions
opts (Underline [Inline]
lst) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
case MarkdownVariant
variant of
MarkdownVariant
PlainText -> forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
contents
MarkdownVariant
_ | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_bracketed_spans WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"]" forall a. Semigroup a => a -> a -> a
<> Doc Text
"{.underline}"
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_native_spans WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasChars a => a -> Attr -> Doc a
tagWithAttrs Text
"span" (Text
"", [Text
"underline"], [])
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents
forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"</span>"
| 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
$ Doc Text
"<u>" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"</u>"
| Bool
otherwise -> forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts ([Inline] -> Inline
Emph [Inline]
lst)
inlineToMarkdown WriterOptions
_ (Strong []) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
inlineToMarkdown WriterOptions
opts (Strong [Inline]
lst) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
case MarkdownVariant
variant of
MarkdownVariant
PlainText ->
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts forall a b. (a -> b) -> a -> b
$
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_gutenberg WriterOptions
opts
then forall a. Walkable Inline a => a -> a
capitalize [Inline]
lst
else [Inline]
lst
MarkdownVariant
_ -> do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"**" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"**"
inlineToMarkdown WriterOptions
_ (Strikeout []) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
inlineToMarkdown WriterOptions
opts (Strikeout [Inline]
lst) = do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
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_strikeout WriterOptions
opts
then Doc Text
"~~" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"~~"
else if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts
then Doc Text
"<s>" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"</s>"
else Doc Text
contents
inlineToMarkdown WriterOptions
_ (Superscript []) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
inlineToMarkdown WriterOptions
opts (Superscript [Inline]
lst) =
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\WriterEnv
env -> WriterEnv
env {envEscapeSpaces :: Bool
envEscapeSpaces = WriterEnv -> MarkdownVariant
envVariant WriterEnv
env forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markdown}) forall a b. (a -> b) -> a -> b
$ do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_superscript WriterOptions
opts
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"^" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"^"
else if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"<sup>" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"</sup>"
else
case forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Inline -> Maybe Inline
toSuperscriptInline [Inline]
lst of
Just [Inline]
xs' | Bool -> Bool
not (WriterOptions -> Bool
writerPreferAscii WriterOptions
opts)
-> forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
xs'
Maybe [Inline]
_ -> do
let rendered :: Text
rendered = forall a. HasChars a => Maybe Int -> Doc a -> a
render forall a. Maybe a
Nothing Doc Text
contents
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
case forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Char -> Maybe Char
toSuperscript (Text -> String
T.unpack Text
rendered) of
Just String
r -> forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
r
Maybe String
Nothing -> forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Text
"^(" forall a. Semigroup a => a -> a -> a
<> Text
rendered forall a. Semigroup a => a -> a -> a
<> Text
")"
inlineToMarkdown WriterOptions
_ (Subscript []) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
empty
inlineToMarkdown WriterOptions
opts (Subscript [Inline]
lst) =
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\WriterEnv
env -> WriterEnv
env {envEscapeSpaces :: Bool
envEscapeSpaces = WriterEnv -> MarkdownVariant
envVariant WriterEnv
env forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markdown}) forall a b. (a -> b) -> a -> b
$ do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_subscript WriterOptions
opts
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"~" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"~"
else if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"<sub>" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"</sub>"
else
case forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Inline -> Maybe Inline
toSubscriptInline [Inline]
lst of
Just [Inline]
xs' | Bool -> Bool
not (WriterOptions -> Bool
writerPreferAscii WriterOptions
opts)
-> forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
xs'
Maybe [Inline]
_ -> do
let rendered :: Text
rendered = forall a. HasChars a => Maybe Int -> Doc a -> a
render forall a. Maybe a
Nothing Doc Text
contents
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
case forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Char -> Maybe Char
toSuperscript (Text -> String
T.unpack Text
rendered) of
Just String
r -> forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
r
Maybe String
Nothing -> forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Text
"_(" forall a. Semigroup a => a -> a -> a
<> Text
rendered forall a. Semigroup a => a -> a -> a
<> Text
")"
inlineToMarkdown WriterOptions
opts (SmallCaps [Inline]
lst) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
if MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
/= MarkdownVariant
PlainText Bool -> Bool -> Bool
&&
(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_native_spans WriterOptions
opts)
then forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts (Attr -> [Inline] -> Inline
Span (Text
"",[Text
"smallcaps"],[]) [Inline]
lst)
else forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts forall a b. (a -> b) -> a -> b
$ forall a. Walkable Inline a => a -> a
capitalize [Inline]
lst
inlineToMarkdown WriterOptions
opts (Quoted QuoteType
SingleQuote [Inline]
lst) = do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
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_smart WriterOptions
opts
then Doc Text
"'" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"'"
else
if WriterOptions -> Bool
writerPreferAscii WriterOptions
opts
then Doc Text
"‘" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"’"
else Doc Text
"‘" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"’"
inlineToMarkdown WriterOptions
opts (Quoted QuoteType
DoubleQuote [Inline]
lst) = do
Doc Text
contents <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
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_smart WriterOptions
opts
then Doc Text
"\"" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"\""
else
if WriterOptions -> Bool
writerPreferAscii WriterOptions
opts
then Doc Text
"“" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"”"
else Doc Text
"“" forall a. Semigroup a => a -> a -> a
<> Doc Text
contents forall a. Semigroup a => a -> a -> a
<> Doc Text
"”"
inlineToMarkdown WriterOptions
opts (Code Attr
attr Text
str) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let tickGroups :: [Text]
tickGroups = forall a. (a -> Bool) -> [a] -> [a]
filter ((Char -> Bool) -> Text -> Bool
T.any (forall a. Eq a => a -> a -> Bool
== Char
'`')) forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.group Text
str
let longest :: Int
longest = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 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 forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Text -> Int
T.length [Text]
tickGroups
let marker :: Text
marker = Int -> Text -> Text
T.replicate (Int
longest forall a. Num a => a -> a -> a
+ Int
1) Text
"`"
let spacer :: Text
spacer = if Int
longest forall a. Eq a => a -> a -> Bool
== Int
0 then Text
"" else Text
" "
let attrsEnabled :: Bool
attrsEnabled = forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_inline_code_attributes WriterOptions
opts Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_attributes WriterOptions
opts
let attrs :: Doc Text
attrs = case MarkdownVariant
variant of
MarkdownVariant
Markua -> WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts Attr
attr
MarkdownVariant
_ -> if Bool
attrsEnabled Bool -> Bool -> Bool
&& Attr
attr forall a. Eq a => a -> a -> Bool
/= Attr
nullAttr
then WriterOptions -> Attr -> Doc Text
attrsToMarkdown WriterOptions
opts Attr
attr
else forall a. Doc a
empty
case MarkdownVariant
variant of
MarkdownVariant
PlainText -> 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
MarkdownVariant
_ -> 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
marker forall a. Semigroup a => a -> a -> a
<> Text
spacer forall a. Semigroup a => a -> a -> a
<> Text
str forall a. Semigroup a => a -> a -> a
<> Text
spacer forall a. Semigroup a => a -> a -> a
<> Text
marker) forall a. Semigroup a => a -> a -> a
<> Doc Text
attrs
inlineToMarkdown WriterOptions
opts (Str Text
str) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let str' :: Text
str' = case MarkdownVariant
variant of
MarkdownVariant
Markua -> Text -> Text
escapeMarkuaString Text
str
MarkdownVariant
_ -> (if WriterOptions -> Bool
writerPreferAscii WriterOptions
opts
then Text -> Text
toHtml5Entities
else forall a. a -> a
id) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_smart WriterOptions
opts
then WriterOptions -> Text -> Text
unsmartify WriterOptions
opts
else forall a. a -> a
id) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(if MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText
then forall a. a -> a
id
else WriterOptions -> Text -> Text
escapeText WriterOptions
opts) forall a b. (a -> b) -> a -> b
$ Text
str
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'
inlineToMarkdown WriterOptions
opts (Math MathType
InlineMath Text
str) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
case () of
()
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"`" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> Doc Text
"`" forall a. Semigroup a => a -> a -> a
<> Doc Text
"$"
| Bool
otherwise -> case WriterOptions -> HTMLMathMethod
writerHTMLMathMethod WriterOptions
opts of
WebTeX Text
url ->
let str' :: Text
str' = Text -> Text
T.strip Text
str
in forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts
(Attr -> [Inline] -> (Text, Text) -> Inline
Image Attr
nullAttr [Text -> Inline
Str Text
str'] (Text
url forall a. Semigroup a => a -> a -> a
<> Text -> Text
urlEncode Text
str', Text
str'))
HTMLMathMethod
_ | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_tex_math_dollars WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"$" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> Doc Text
"$"
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_tex_math_single_backslash WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"\\(" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> Doc Text
"\\)"
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_tex_math_double_backslash WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"\\\\(" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> Doc Text
"\\\\)"
| Bool
otherwise ->
forall (m :: * -> *).
PandocMonad m =>
MathType -> Text -> m [Inline]
texMathToInlines MathType
InlineMath Text
str forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(if MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText then [Inline] -> [Inline]
makeMathPlainer else forall a. a -> a
id)
inlineToMarkdown WriterOptions
opts (Math MathType
DisplayMath Text
str) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
case () of
()
_ | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua -> do
let attributes :: Doc Text
attributes = WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts (Attr -> (Text, Text) -> Attr
addKeyValueToAttr (Text
"",[],[])
(Text
"format", Text
"latex"))
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
<> Doc Text
attributes 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
"```" 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
<> forall a. HasChars a => a -> Doc a
literal Text
"```" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
blankline
| Bool
otherwise -> case WriterOptions -> HTMLMathMethod
writerHTMLMathMethod WriterOptions
opts of
WebTeX Text
url ->
let str' :: Text
str' = Text -> Text
T.strip Text
str
in (\Doc Text
x -> forall a. Doc a
blankline forall a. Semigroup a => a -> a -> a
<> Doc Text
x 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 -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts (Attr -> [Inline] -> (Text, Text) -> Inline
Image Attr
nullAttr [Text -> Inline
Str Text
str']
(Text
url forall a. Semigroup a => a -> a -> a
<> Text -> Text
urlEncode Text
str', Text
str'))
HTMLMathMethod
_ | forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_tex_math_dollars WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"$$" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> Doc Text
"$$"
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_tex_math_single_backslash WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"\\[" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> Doc Text
"\\]"
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_tex_math_double_backslash WriterOptions
opts ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"\\\\[" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
str forall a. Semigroup a => a -> a -> a
<> Doc Text
"\\\\]"
| Bool
otherwise -> (\Doc Text
x -> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> Doc Text
x forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap`
(forall (m :: * -> *).
PandocMonad m =>
MathType -> Text -> m [Inline]
texMathToInlines MathType
DisplayMath Text
str forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts)
inlineToMarkdown WriterOptions
opts il :: Inline
il@(RawInline Format
f Text
str) = do
let tickGroups :: [Text]
tickGroups = forall a. (a -> Bool) -> [a] -> [a]
filter ((Char -> Bool) -> Text -> Bool
T.any (forall a. Eq a => a -> a -> Bool
== Char
'`')) forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.group Text
str
let numticks :: Int
numticks = Int
1 forall a. Num a => a -> a -> a
+ forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (forall a. [a] -> Maybe (NonEmpty a)
nonEmpty (forall a b. (a -> b) -> [a] -> [b]
map Text -> Int
T.length [Text]
tickGroups))
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let Format Text
fmt = Format
f
let rawAttribInline :: MD m (Doc Text)
rawAttribInline = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
forall a. HasChars a => a -> Doc a
literal (Int -> Text -> Text
T.replicate Int
numticks Text
"`") 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. HasChars a => a -> Doc a
literal (Int -> Text -> Text
T.replicate Int
numticks Text
"`") 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. HasChars a => a -> Doc a
literal Text
fmt forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"}"
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 (Inline -> LogMessage
InlineNotRendered Inline
il)
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
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
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
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)
rawAttribInline
| 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
| 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
MarkdownVariant
_ -> MD m (Doc Text)
renderEmpty
inlineToMarkdown WriterOptions
opts Inline
LineBreak = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
if MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
PlainText Bool -> Bool -> Bool
|| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_hard_line_breaks WriterOptions
opts
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Doc a
cr
else 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_escaped_line_breaks WriterOptions
opts
then Doc Text
"\\" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
else Doc Text
" " forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
inlineToMarkdown WriterOptions
_ Inline
Space = do
Bool
escapeSpaces <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> Bool
envEscapeSpaces
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
escapeSpaces then Doc Text
"\\ " else forall a. Doc a
space
inlineToMarkdown WriterOptions
opts Inline
SoftBreak = do
Bool
escapeSpaces <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> Bool
envEscapeSpaces
let space' :: Doc Text
space' = if Bool
escapeSpaces then Doc Text
"\\ " else forall a. Doc a
space
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case WriterOptions -> WrapOption
writerWrapText WriterOptions
opts of
WrapOption
WrapNone -> Doc Text
space'
WrapOption
WrapAuto -> Doc Text
space'
WrapOption
WrapPreserve -> forall a. Doc a
cr
inlineToMarkdown WriterOptions
opts (Cite [] [Inline]
lst) = forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
inlineToMarkdown WriterOptions
opts (Cite (Citation
c:[Citation]
cs) [Inline]
lst)
| Bool -> Bool
not (forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_citations WriterOptions
opts) = forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
lst
| Bool
otherwise =
if Citation -> CitationMode
citationMode Citation
c forall a. Eq a => a -> a -> Bool
== CitationMode
AuthorInText
then do
Doc Text
suffs <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts forall a b. (a -> b) -> a -> b
$ Citation -> [Inline]
citationSuffix Citation
c
[Doc Text]
rest <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {m :: * -> *}.
PandocMonad m =>
Citation -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
convertOne [Citation]
cs
let inbr :: Doc Text
inbr = Doc Text
suffs forall a. Semigroup a => a -> a -> a
<>
(if Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Citation -> [Inline]
citationSuffix Citation
c)) Bool -> Bool -> Bool
&& Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Doc Text]
rest)
then forall a. HasChars a => String -> Doc a
text String
";"
else forall a. Monoid a => a
mempty)
forall a. Doc a -> Doc a -> Doc a
<+> [Doc Text] -> Doc Text
joincits [Doc Text]
rest
br :: Doc Text
br = if forall a. Doc a -> Bool
isEmpty Doc Text
inbr then forall a. Doc a
empty else forall a. HasChars a => Char -> Doc a
char Char
'[' forall a. Semigroup a => a -> a -> a
<> Doc Text
inbr forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => Char -> Doc a
char Char
']'
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}. (ToSources a, Semigroup a, IsString a) => a -> a
maybeInBraces (Citation -> Text
citationId Citation
c)) forall a. Doc a -> Doc a -> Doc a
<+> Doc Text
br
else do
[Doc Text]
cits <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {m :: * -> *}.
PandocMonad m =>
Citation -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
convertOne (Citation
cforall a. a -> [a] -> [a]
:[Citation]
cs)
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
<> [Doc Text] -> Doc Text
joincits [Doc Text]
cits forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
"]"
where
maybeInBraces :: a -> a
maybeInBraces a
key =
case forall t st a.
ToSources t =>
Parsec Sources st a -> st -> t -> Either PandocError a
readWith (forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char, HasLastStrPosition st) =>
Bool -> ParsecT s st m (Bool, Text)
citeKey Bool
False forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m ()
spaces forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof)
ParserState
defaultParserState (a
"@" forall a. Semigroup a => a -> a -> a
<> a
key) of
Left PandocError
_ -> a
"{" forall a. Semigroup a => a -> a -> a
<> a
key forall a. Semigroup a => a -> a -> a
<> a
"}"
Right ()
_ -> a
key
joincits :: [Doc Text] -> Doc Text
joincits = forall a. [Doc a] -> Doc a
hcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
intersperse (forall a. HasChars a => a -> Doc a
literal Text
"; ") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Doc a -> Bool
isEmpty)
convertOne :: Citation -> ReaderT WriterEnv (StateT WriterState m) (Doc Text)
convertOne Citation { citationId :: Citation -> Text
citationId = Text
k
, citationPrefix :: Citation -> [Inline]
citationPrefix = [Inline]
pinlines
, citationSuffix :: Citation -> [Inline]
citationSuffix = [Inline]
sinlines
, citationMode :: Citation -> CitationMode
citationMode = CitationMode
m }
= do
Doc Text
pdoc <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
pinlines
Doc Text
sdoc <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
sinlines
let k' :: Doc Text
k' = forall a. HasChars a => a -> Doc a
literal (forall {a}. IsString a => CitationMode -> a
modekey CitationMode
m forall a. Semigroup a => a -> a -> a
<> Text
"@" forall a. Semigroup a => a -> a -> a
<> forall {a}. (ToSources a, Semigroup a, IsString a) => a -> a
maybeInBraces Text
k)
r :: Doc Text
r = case [Inline]
sinlines of
Str (Text -> Maybe (Char, Text)
T.uncons -> Just (Char
y,Text
_)):[Inline]
_
| Char
y forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (String
",;]@" :: String) -> Doc Text
k' forall a. Semigroup a => a -> a -> a
<> Doc Text
sdoc
Inline
Space:[Inline]
_ -> Doc Text
k' forall a. Semigroup a => a -> a -> a
<> Doc Text
sdoc
[Inline]
_ -> Doc Text
k' forall a. Doc a -> Doc a -> Doc a
<+> Doc Text
sdoc
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
pdoc forall a. Doc a -> Doc a -> Doc a
<+> Doc Text
r
modekey :: CitationMode -> a
modekey CitationMode
SuppressAuthor = a
"-"
modekey CitationMode
_ = a
""
inlineToMarkdown WriterOptions
opts lnk :: Inline
lnk@(Link attr :: Attr
attr@(Text
ident,[Text]
classes,[(Text, Text)]
kvs) [Inline]
txt (Text
src, Text
tit)) = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
Doc Text
linktext <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
txt
let linktitle :: Doc Text
linktitle = if Text -> Bool
T.null Text
tit
then forall a. Doc a
empty
else forall a. HasChars a => a -> Doc a
literal forall a b. (a -> b) -> a -> b
$ Text
" \"" forall a. Semigroup a => a -> a -> a
<> Text
tit forall a. Semigroup a => a -> a -> a
<> Text
"\""
let srcSuffix :: Text
srcSuffix = forall a. a -> Maybe a -> a
fromMaybe Text
src (Text -> Text -> Maybe Text
T.stripPrefix Text
"mailto:" Text
src)
let useAuto :: Bool
useAuto = Text -> Bool
isURI Text
src Bool -> Bool -> Bool
&&
Text -> Bool
T.null Text
ident Bool -> Bool -> Bool
&&
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Text, Text)]
kvs Bool -> Bool -> Bool
&&
(forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
classes Bool -> Bool -> Bool
|| [Text]
classes forall a. Eq a => a -> a -> Bool
== [Text
"uri"] Bool -> Bool -> Bool
|| [Text]
classes forall a. Eq a => a -> a -> Bool
== [Text
"email"]) Bool -> Bool -> Bool
&&
case [Inline]
txt of
[Str Text
s] | Text -> Text
escapeURI Text
s forall a. Eq a => a -> a -> Bool
== Text
srcSuffix -> Bool
True
[Inline]
_ -> Bool
False
let useWikilink :: Bool
useWikilink = Text
tit forall a. Eq a => a -> a -> Bool
== Text
"wikilink" Bool -> Bool -> Bool
&&
(forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_wikilinks_title_after_pipe WriterOptions
opts Bool -> Bool -> Bool
||
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_wikilinks_title_before_pipe WriterOptions
opts)
let useRefLinks :: Bool
useRefLinks = WriterOptions -> Bool
writerReferenceLinks WriterOptions
opts Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
useAuto
Bool
shortcutable <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> Bool
envRefShortcutable
let useShortcutRefLinks :: Bool
useShortcutRefLinks = Bool
shortcutable 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_shortcut_reference_links WriterOptions
opts)
Doc Text
reftext <- if Bool
useRefLinks
then forall a. HasChars a => a -> Doc a
literal forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
PandocMonad m =>
Attr -> Doc Text -> (Text, Text) -> MD m Text
getReference Attr
attr Doc Text
linktext (Text
src, Text
tit)
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty
case MarkdownVariant
variant of
MarkdownVariant
PlainText
| Bool
useAuto -> 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
srcSuffix
| Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
linktext
MarkdownVariant
Markua
| Text -> Bool
T.null Text
tit -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
result forall a. Semigroup a => a -> a -> a
<> WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts Attr
attr
| Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
result forall a. Semigroup a => a -> a -> a
<> WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts Attr
attributes
where result :: Doc Text
result = Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
linktext 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
src) forall a. Semigroup a => a -> a -> a
<> Doc Text
")"
attributes :: Attr
attributes = Attr -> (Text, Text) -> Attr
addKeyValueToAttr Attr
attr (Text
"title", Text
tit)
MarkdownVariant
_ | Text
src forall a. Eq a => a -> a -> Bool
== forall a. Walkable Inline a => a -> Text
stringify [Inline]
txt Bool -> Bool -> Bool
&& Bool
useWikilink ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"[[" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal (forall a. Walkable Inline a => a -> Text
stringify [Inline]
txt) forall a. Semigroup a => a -> a -> a
<> Doc Text
"]]"
| Bool
useAuto -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"<" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
srcSuffix forall a. Semigroup a => a -> a -> a
<> Doc Text
">"
| Bool
useWikilink Bool -> Bool -> Bool
&& forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_wikilinks_title_after_pipe WriterOptions
opts -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
Doc Text
"[[" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal Text
src forall a. Semigroup a => a -> a -> a
<> Doc Text
"|" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal (forall a. Walkable Inline a => a -> Text
stringify [Inline]
txt) forall a. Semigroup a => a -> a -> a
<> Doc Text
"]]"
| Bool
useWikilink Bool -> Bool -> Bool
&& forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_wikilinks_title_before_pipe WriterOptions
opts -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
Doc Text
"[[" forall a. Semigroup a => a -> a -> a
<> forall a. HasChars a => a -> Doc a
literal (forall a. Walkable Inline a => a -> Text
stringify [Inline]
txt) 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
src forall a. Semigroup a => a -> a -> a
<> Doc Text
"]]"
| Bool
useRefLinks ->
let first :: Doc Text
first = Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
linktext forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
second :: Doc Text
second = if Doc Text -> Key
getKey Doc Text
linktext forall a. Eq a => a -> a -> Bool
== Doc Text -> Key
getKey Doc Text
reftext
then if Bool
useShortcutRefLinks
then Doc Text
""
else Doc Text
"[]"
else Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
reftext forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
first forall a. Semigroup a => a -> a -> a
<> Doc Text
second
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts
, Bool -> Bool
not (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)
, Attr
attr forall a. Eq a => a -> a -> Bool
/= Attr
nullAttr ->
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 [[Inline] -> Block
Plain [Inline
lnk]])
| Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
linktext 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
src forall a. Semigroup a => a -> a -> a
<> Doc Text
linktitle forall a. Semigroup a => a -> a -> a
<> Doc Text
")" forall a. Semigroup a => a -> a -> a
<>
WriterOptions -> Attr -> Doc Text
linkAttributes WriterOptions
opts Attr
attr
inlineToMarkdown WriterOptions
opts img :: Inline
img@(Image Attr
attr [Inline]
alternate (Text
source, Text
tit))
| forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_raw_html WriterOptions
opts Bool -> Bool -> Bool
&&
Bool -> Bool
not (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) Bool -> Bool -> Bool
&&
Attr
attr forall a. Eq a => a -> a -> Bool
/= Attr
nullAttr =
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 [[Inline] -> Block
Plain [Inline
img]])
| Bool
otherwise = do
MarkdownVariant
variant <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WriterEnv -> MarkdownVariant
envVariant
let txt :: [Inline]
txt = if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
alternate Bool -> Bool -> Bool
|| [Inline]
alternate forall a. Eq a => a -> a -> Bool
== [Text -> Inline
Str Text
source]
then [Text -> Inline
Str Text
""]
else [Inline]
alternate
Doc Text
linkPart <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> MD m (Doc Text)
inlineToMarkdown WriterOptions
opts (Attr -> [Inline] -> (Text, Text) -> Inline
Link Attr
attr [Inline]
txt (Text
source, Text
tit))
Doc Text
alt <- forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> MD m (Doc Text)
inlineListToMarkdown WriterOptions
opts [Inline]
alternate
let attributes :: Doc Text
attributes | MarkdownVariant
variant forall a. Eq a => a -> a -> Bool
== MarkdownVariant
Markua = WriterOptions -> Attr -> Doc Text
attrsToMarkua WriterOptions
opts forall a b. (a -> b) -> a -> b
$
Attr -> (Text, Text) -> Attr
addKeyValueToAttr (Attr -> (Text, Text) -> Attr
addKeyValueToAttr Attr
attr (Text
"title", Text
tit))
(Text
"alt", forall a. HasChars a => Maybe Int -> Doc a -> a
render (forall a. a -> Maybe a
Just (WriterOptions -> Int
writerColumns WriterOptions
opts)) Doc Text
alt)
| Bool
otherwise = forall a. Doc a
empty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case MarkdownVariant
variant of
MarkdownVariant
PlainText -> Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
linkPart forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
MarkdownVariant
Markua -> forall a. Doc a
cr forall a. Semigroup a => a -> a -> a
<> Doc Text
attributes 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
"![](" forall a. Semigroup a => a -> a -> a
<>
forall a. HasChars a => a -> Doc a
literal Text
source forall a. Semigroup a => a -> a -> a
<> Doc Text
")" forall a. Semigroup a => a -> a -> a
<> forall a. Doc a
cr
MarkdownVariant
_ -> Doc Text
"!" forall a. Semigroup a => a -> a -> a
<> Doc Text
linkPart
inlineToMarkdown WriterOptions
opts (Note [Block]
contents) = do
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
st -> WriterState
st{ stNotes :: Notes
stNotes = [Block]
contents forall a. a -> [a] -> [a]
: WriterState -> Notes
stNotes WriterState
st })
WriterState
st <- forall s (m :: * -> *). MonadState s m => m s
get
let ref :: Doc Text
ref = 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 (WriterState -> Int
stNoteNum WriterState
st forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. Foldable t => t a -> Int
length (WriterState -> Notes
stNotes WriterState
st) forall a. Num a => a -> a -> a
- Int
1)
if forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_footnotes WriterOptions
opts
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"[^" forall a. Semigroup a => a -> a -> a
<> Doc Text
ref forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Doc Text
"[" forall a. Semigroup a => a -> a -> a
<> Doc Text
ref forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
makeMathPlainer :: [Inline] -> [Inline]
makeMathPlainer :: [Inline] -> [Inline]
makeMathPlainer = forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
go
where
go :: Inline -> Inline
go (Emph [Inline]
xs) = Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline]
xs
go Inline
x = Inline
x
toSubscriptInline :: Inline -> Maybe Inline
toSubscriptInline :: Inline -> Maybe Inline
toSubscriptInline Inline
Space = forall a. a -> Maybe a
Just Inline
Space
toSubscriptInline (Span Attr
attr [Inline]
ils) = Attr -> [Inline] -> Inline
Span Attr
attr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Inline -> Maybe Inline
toSubscriptInline [Inline]
ils
toSubscriptInline (Str Text
s) = Text -> Inline
Str forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Char -> Maybe Char
toSubscript (Text -> String
T.unpack Text
s)
toSubscriptInline Inline
LineBreak = forall a. a -> Maybe a
Just Inline
LineBreak
toSubscriptInline Inline
SoftBreak = forall a. a -> Maybe a
Just Inline
SoftBreak
toSubscriptInline Inline
_ = forall a. Maybe a
Nothing
toSuperscriptInline :: Inline -> Maybe Inline
toSuperscriptInline :: Inline -> Maybe Inline
toSuperscriptInline Inline
Space = forall a. a -> Maybe a
Just Inline
Space
toSuperscriptInline (Span Attr
attr [Inline]
ils) = Attr -> [Inline] -> Inline
Span Attr
attr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Inline -> Maybe Inline
toSuperscriptInline [Inline]
ils
toSuperscriptInline (Str Text
s) = Text -> Inline
Str forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Char -> Maybe Char
toSuperscript (Text -> String
T.unpack Text
s)
toSuperscriptInline Inline
LineBreak = forall a. a -> Maybe a
Just Inline
LineBreak
toSuperscriptInline Inline
SoftBreak = forall a. a -> Maybe a
Just Inline
SoftBreak
toSuperscriptInline Inline
_ = forall a. Maybe a
Nothing