    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE ViewPatterns      #-}
    {- |
       Module      : Text.Pandoc.Writers.MediaWiki
       Copyright   : Copyright (C) 2008-2020 John MacFarlane
       License     : GNU GPL, version 2 or above

       Maintainer  : John MacFarlane <jgm@berkeley.edu>
       Stability   : alpha
       Portability : portable

    Conversion of 'Pandoc' documents to MediaWiki markup.

    MediaWiki:  <http://www.mediawiki.org/wiki/MediaWiki>
    -}
    module Text.Pandoc.Writers.MediaWiki ( writeMediaWiki, highlightingLangs ) where
    import Control.Monad.Reader
    import Control.Monad.State.Strict
    import Data.Maybe (fromMaybe)
    import qualified Data.Set as Set
    import Data.Text (Text)
    import qualified Data.Text as T
    import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
    import Text.Pandoc.Definition
    import Text.Pandoc.ImageSize
    import Text.Pandoc.Logging
    import Text.Pandoc.Options
    import Text.DocLayout (render, literal)
    import Text.Pandoc.Shared
    import Text.Pandoc.Templates (renderTemplate)
    import Text.Pandoc.Writers.Shared
    import Text.Pandoc.XML (escapeStringForXML)

    data WriterState = WriterState {
        WriterState -> Bool
    stNotes   :: Bool            -- True if there are notes
      , WriterState -> WriterOptions
    stOptions :: WriterOptions   -- writer options
      }

    data WriterReader = WriterReader {
        WriterReader -> WriterOptions
    options   :: WriterOptions -- Writer options
      , WriterReader -> [Char]
    listLevel :: [Char]        -- String at beginning of list items, e.g. "**"
      , WriterReader -> Bool
    useTags   :: Bool          -- True if we should use HTML tags because we're in a complex list
      }

    type MediaWikiWriter m = ReaderT WriterReader (StateT WriterState m)

    -- | Convert Pandoc to MediaWiki.
    writeMediaWiki :: PandocMonad m => WriterOptions -> Pandoc -> m Text
    writeMediaWiki :: WriterOptions -> Pandoc -> m Text
    writeMediaWiki opts :: WriterOptions
    opts document :: Pandoc
    document =
      let initialState :: WriterState
    initialState = WriterState :: Bool -> WriterOptions -> WriterState
    WriterState { stNotes :: Bool
    stNotes = Bool
    False, stOptions :: WriterOptions
    stOptions = WriterOptions
    opts }
          env :: WriterReader
    env = WriterReader :: WriterOptions -> [Char] -> Bool -> WriterReader
    WriterReader { options :: WriterOptions
    options = WriterOptions
    opts, listLevel :: [Char]
    listLevel = [], useTags :: Bool
    useTags = Bool
    False }
      in  StateT WriterState m Text -> WriterState -> m Text
    forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
    evalStateT (ReaderT WriterReader (StateT WriterState m) Text
    -> WriterReader -> StateT WriterState m Text
    forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
    runReaderT (Pandoc -> ReaderT WriterReader (StateT WriterState m) Text
    forall (m :: * -> *).
    PandocMonad m =>
    Pandoc -> MediaWikiWriter m Text
    pandocToMediaWiki Pandoc
    document) WriterReader
    env) WriterState
    initialState

    -- | Return MediaWiki representation of document.
    pandocToMediaWiki :: PandocMonad m => Pandoc -> MediaWikiWriter m Text
    pandocToMediaWiki :: Pandoc -> MediaWikiWriter m Text
    pandocToMediaWiki (Pandoc meta :: Meta
    meta blocks :: [Block]
    blocks) = do
      WriterOptions
    opts <- (WriterReader -> WriterOptions)
    -> ReaderT WriterReader (StateT WriterState m) WriterOptions
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> WriterOptions
    options
      Context Text
    metadata <- WriterOptions
    -> ([Block]
        -> ReaderT WriterReader (StateT WriterState m) (Doc Text))
    -> ([Inline]
        -> ReaderT WriterReader (StateT WriterState m) (Doc Text))
    -> Meta
    -> ReaderT WriterReader (StateT WriterState m) (Context Text)
    forall (m :: * -> *) a.
    (Monad m, TemplateTarget a) =>
    WriterOptions
    -> ([Block] -> m (Doc a))
    -> ([Inline] -> m (Doc a))
    -> Meta
    -> m (Context a)
    metaToContext WriterOptions
    opts
                  ((Text -> Doc Text)
    -> MediaWikiWriter m Text
    -> ReaderT WriterReader (StateT WriterState m) (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> (Text -> Text) -> Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Text
    trimr) (MediaWikiWriter m Text
     -> ReaderT WriterReader (StateT WriterState m) (Doc Text))
    -> ([Block] -> MediaWikiWriter m Text)
    -> [Block]
    -> ReaderT WriterReader (StateT WriterState m) (Doc Text)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki)
                  ((Text -> Doc Text)
    -> MediaWikiWriter m Text
    -> ReaderT WriterReader (StateT WriterState m) (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> (Text -> Text) -> Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Text
    trimr) (MediaWikiWriter m Text
     -> ReaderT WriterReader (StateT WriterState m) (Doc Text))
    -> ([Inline] -> MediaWikiWriter m Text)
    -> [Inline]
    -> ReaderT WriterReader (StateT WriterState m) (Doc Text)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki)
                  Meta
    meta
      Text
    body <- [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki [Block]
    blocks
      Bool
    notesExist <- (WriterState -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stNotes
      let notes :: Text
    notes = if Bool
    notesExist
                     then "\n<references />"
                     else ""
      let main :: Text
    main = Text
    body Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    notes
      let context :: Context Text
    context = Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "body" Text
    main
                    (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "toc" (WriterOptions -> Bool
    writerTableOfContents WriterOptions
    opts) Context Text
    metadata
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $
        case WriterOptions -> Maybe (Template Text)
    writerTemplate WriterOptions
    opts of
             Nothing  -> Text
    main
             Just tpl :: Template Text
    tpl -> Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing (Doc Text -> Text) -> Doc Text -> Text
    forall a b. (a -> b) -> a -> b
    $ Template Text -> Context Text -> Doc Text
    forall a b.
    (TemplateTarget a, ToContext a b) =>
    Template a -> b -> Doc a
    renderTemplate Template Text
    tpl Context Text
    context

    -- | Escape special characters for MediaWiki.
    escapeText :: Text -> Text
    escapeText :: Text -> Text
    escapeText =  Text -> Text
    escapeStringForXML

    -- | Convert Pandoc block element to MediaWiki.
    blockToMediaWiki :: PandocMonad m
                     => Block         -- ^ Block element
                     -> MediaWikiWriter m Text

    blockToMediaWiki :: Block -> MediaWikiWriter m Text
    blockToMediaWiki Null = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return ""

    blockToMediaWiki (Div attrs :: Attr
    attrs bs :: [Block]
    bs) = do
      Text
    contents <- [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki [Block]
    bs
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing (Text -> Attr -> Doc Text
    forall a. HasChars a => Text -> Attr -> Doc a
    tagWithAttrs "div" Attr
    attrs) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                         Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</div>"

    blockToMediaWiki (Plain inlines :: [Inline]
    inlines) =
      [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    inlines

    -- title beginning with fig: indicates that the image is a figure
    blockToMediaWiki (Para [Image attr :: Attr
    attr txt :: [Inline]
    txt (src :: Text
    src,Text -> Text -> Maybe Text
    T.stripPrefix "fig:" -> Just tit :: Text
    tit)]) = do
      Text
    capt <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    txt
      Text
    img  <- Attr -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    Attr -> MediaWikiWriter m Text
    imageToMediaWiki Attr
    attr
      let opt :: Text
    opt = if Text -> Bool
    T.null Text
    tit
                   then
                     if Text -> Bool
    T.null Text
    capt
                        then ""
                        else "alt=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    capt
                   else "alt=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    tit
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "[[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                Text -> [Text] -> Text
    T.intercalate "|"
                ((Text -> Bool) -> [Text] -> [Text]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (Bool -> Bool
    not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Bool
    T.null) ["File:" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    src
                                     , "thumb"
                                     , "none"
                                     , Text
    img
                                     , Text
    opt
                                     , Text
    capt
                                     ]) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                "]]\n"

    blockToMediaWiki (Para inlines :: [Inline]
    inlines) = do
      Bool
    tags <- (WriterReader -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> Bool
    useTags
      [Char]
    lev <- (WriterReader -> [Char])
    -> ReaderT WriterReader (StateT WriterState m) [Char]
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> [Char]
    listLevel
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    inlines
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ if Bool
    tags
                  then  "<p>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</p>"
                  else Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> if [Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    lev then "\n" else ""

    blockToMediaWiki (LineBlock lns :: [[Inline]]
    lns) =
      Block -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    Block -> MediaWikiWriter m Text
    blockToMediaWiki (Block -> MediaWikiWriter m Text)
    -> Block -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ [[Inline]] -> Block
    linesToPara [[Inline]]
    lns

    blockToMediaWiki b :: Block
    b@(RawBlock f :: Format
    f str :: Text
    str)
      | Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "mediawiki" = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    str
      | Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "html"      = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    str
      | Bool
    otherwise               = "" Text
    -> ReaderT WriterReader (StateT WriterState m) ()
    -> MediaWikiWriter m Text
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ LogMessage -> ReaderT WriterReader (StateT WriterState m) ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (Block -> LogMessage
    BlockNotRendered Block
    b)

    blockToMediaWiki HorizontalRule = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return "\n-----\n"

    blockToMediaWiki (Header level :: Int
    level _ inlines :: [Inline]
    inlines) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    inlines
      let eqs :: Text
    eqs = Int -> Text -> Text
    T.replicate Int
    level "="
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ Text
    eqs Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    eqs Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n"

    blockToMediaWiki (CodeBlock (_,classes :: [Text]
    classes,_) str :: Text
    str) = do
      let at :: Set Text
    at  = [Text] -> Set Text
    forall a. Ord a => [a] -> Set a
    Set.fromList [Text]
    classes Set Text -> Set Text -> Set Text
    forall a. Ord a => Set a -> Set a -> Set a
    `Set.intersection` Set Text
    highlightingLangs
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $
        case Set Text -> [Text]
    forall a. Set a -> [a]
    Set.toList Set Text
    at of
           [] -> "<pre" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> (if [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    classes
                               then ">"
                               else " class=\"" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> [Text] -> Text
    T.unwords [Text]
    classes Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\">") Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                 Text -> Text
    escapeText Text
    str Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</pre>"
           (l :: Text
    l:_) -> "<source lang=\"" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    l Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\">" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    str Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</source>"
                -- note:  no escape!  even for <!

    blockToMediaWiki (BlockQuote blocks :: [Block]
    blocks) = do
      Text
    contents <- [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki [Block]
    blocks
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<blockquote>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</blockquote>"

    blockToMediaWiki (Table capt :: [Inline]
    capt aligns :: [Alignment]
    aligns widths :: [Double]
    widths headers :: [[Block]]
    headers rows' :: [[[Block]]]
    rows') = do
      Text
    caption <- if [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    capt
                    then Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return ""
                    else do
                       Text
    c <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    capt
                       Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "|+ " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    trimr Text
    c Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n"
      let headless :: Bool
    headless = ([Block] -> Bool) -> [[Block]] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all [Block] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [[Block]]
    headers
      let allrows :: [[[Block]]]
    allrows = if Bool
    headless then [[[Block]]]
    rows' else [[Block]]
    headers[[Block]] -> [[[Block]]] -> [[[Block]]]
    forall a. a -> [a] -> [a]
    :[[[Block]]]
    rows'
      Text
    tableBody <- Text -> [Text] -> Text
    T.intercalate "|-\n" ([Text] -> Text)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> MediaWikiWriter m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    `fmap`
                    ((Int, [[Block]]) -> MediaWikiWriter m Text)
    -> [(Int, [[Block]])]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (Bool
    -> [Alignment]
    -> [Double]
    -> (Int, [[Block]])
    -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    Bool
    -> [Alignment]
    -> [Double]
    -> (Int, [[Block]])
    -> MediaWikiWriter m Text
    tableRowToMediaWiki Bool
    headless [Alignment]
    aligns [Double]
    widths)
                         ([Int] -> [[[Block]]] -> [(Int, [[Block]])]
    forall a b. [a] -> [b] -> [(a, b)]
    zip [1..] [[[Block]]]
    allrows)
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "{|\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    caption Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    tableBody Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "|}\n"

    blockToMediaWiki x :: Block
    x@(BulletList items :: [[Block]]
    items) = do
      Bool
    tags <-
        (Bool -> Bool -> Bool
    || Bool -> Bool
    not (Block -> Bool
    isSimpleList Block
    x)) (Bool -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (WriterReader -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> Bool
    useTags
      if Bool
    tags
         then do
            [Text]
    contents <- (WriterReader -> WriterReader)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
    local (\ s :: WriterReader
    s -> WriterReader
    s { useTags :: Bool
    useTags = Bool
    True }) (ReaderT WriterReader (StateT WriterState m) [Text]
     -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ ([Block] -> MediaWikiWriter m Text)
    -> [[Block]] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    listItemToMediaWiki [[Block]]
    items
            Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<ul>\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> [Text] -> Text
    vcat [Text]
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</ul>\n"
         else do
            [Char]
    lev <- (WriterReader -> [Char])
    -> ReaderT WriterReader (StateT WriterState m) [Char]
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> [Char]
    listLevel
            [Text]
    contents <- (WriterReader -> WriterReader)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
    local (\s :: WriterReader
    s -> WriterReader
    s { listLevel :: [Char]
    listLevel = WriterReader -> [Char]
    listLevel WriterReader
    s [Char] -> [Char] -> [Char]
    forall a. Semigroup a => a -> a -> a
    <> "*" }) (ReaderT WriterReader (StateT WriterState m) [Text]
     -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ ([Block] -> MediaWikiWriter m Text)
    -> [[Block]] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    listItemToMediaWiki [[Block]]
    items
            Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ [Text] -> Text
    vcat [Text]
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> if [Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    lev then "\n" else ""

    blockToMediaWiki x :: Block
    x@(OrderedList attribs :: ListAttributes
    attribs items :: [[Block]]
    items) = do
      Bool
    tags <-
        (Bool -> Bool -> Bool
    || Bool -> Bool
    not (Block -> Bool
    isSimpleList Block
    x)) (Bool -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (WriterReader -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> Bool
    useTags
      if Bool
    tags
         then do
            [Text]
    contents <- (WriterReader -> WriterReader)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
    local (\s :: WriterReader
    s -> WriterReader
    s { useTags :: Bool
    useTags = Bool
    True }) (ReaderT WriterReader (StateT WriterState m) [Text]
     -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ ([Block] -> MediaWikiWriter m Text)
    -> [[Block]] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    listItemToMediaWiki [[Block]]
    items
            Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<ol" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> ListAttributes -> Text
    listAttribsToText ListAttributes
    attribs Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> ">\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> [Text] -> Text
    vcat [Text]
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</ol>\n"
         else do
            [Char]
    lev <- (WriterReader -> [Char])
    -> ReaderT WriterReader (StateT WriterState m) [Char]
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> [Char]
    listLevel
            [Text]
    contents <- (WriterReader -> WriterReader)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
    local (\s :: WriterReader
    s -> WriterReader
    s { listLevel :: [Char]
    listLevel = WriterReader -> [Char]
    listLevel WriterReader
    s [Char] -> [Char] -> [Char]
    forall a. Semigroup a => a -> a -> a
    <> "#" }) (ReaderT WriterReader (StateT WriterState m) [Text]
     -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ ([Block] -> MediaWikiWriter m Text)
    -> [[Block]] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    listItemToMediaWiki [[Block]]
    items
            Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ [Text] -> Text
    vcat [Text]
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> if [Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    lev then "\n" else ""

    blockToMediaWiki x :: Block
    x@(DefinitionList items :: [([Inline], [[Block]])]
    items) = do
      Bool
    tags <-
        (Bool -> Bool -> Bool
    || Bool -> Bool
    not (Block -> Bool
    isSimpleList Block
    x)) (Bool -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (WriterReader -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> Bool
    useTags
      if Bool
    tags
         then do
            [Text]
    contents <- (WriterReader -> WriterReader)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
    local (\s :: WriterReader
    s -> WriterReader
    s { useTags :: Bool
    useTags = Bool
    True }) (ReaderT WriterReader (StateT WriterState m) [Text]
     -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ (([Inline], [[Block]]) -> MediaWikiWriter m Text)
    -> [([Inline], [[Block]])]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM ([Inline], [[Block]]) -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    ([Inline], [[Block]]) -> MediaWikiWriter m Text
    definitionListItemToMediaWiki [([Inline], [[Block]])]
    items
            Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<dl>\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> [Text] -> Text
    vcat [Text]
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</dl>\n"
         else do
            [Char]
    lev <- (WriterReader -> [Char])
    -> ReaderT WriterReader (StateT WriterState m) [Char]
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> [Char]
    listLevel
            [Text]
    contents <- (WriterReader -> WriterReader)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
    local (\s :: WriterReader
    s -> WriterReader
    s { listLevel :: [Char]
    listLevel = WriterReader -> [Char]
    listLevel WriterReader
    s [Char] -> [Char] -> [Char]
    forall a. Semigroup a => a -> a -> a
    <> ";" }) (ReaderT WriterReader (StateT WriterState m) [Text]
     -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ (([Inline], [[Block]]) -> MediaWikiWriter m Text)
    -> [([Inline], [[Block]])]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM ([Inline], [[Block]]) -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    ([Inline], [[Block]]) -> MediaWikiWriter m Text
    definitionListItemToMediaWiki [([Inline], [[Block]])]
    items
            Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ [Text] -> Text
    vcat [Text]
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> if [Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    lev then "\n" else ""

    -- Auxiliary functions for lists:

    -- | Convert ordered list attributes to HTML attribute string
    listAttribsToText :: ListAttributes -> Text
    listAttribsToText :: ListAttributes -> Text
    listAttribsToText (startnum :: Int
    startnum, numstyle :: ListNumberStyle
    numstyle, _) =
      let numstyle' :: Text
    numstyle' = Text -> Text
    camelCaseToHyphenated (Text -> Text) -> Text -> Text
    forall a b. (a -> b) -> a -> b
    $ ListNumberStyle -> Text
    forall a. Show a => a -> Text
    tshow ListNumberStyle
    numstyle
      in  (if Int
    startnum Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    /= 1
              then " start=\"" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Int -> Text
    forall a. Show a => a -> Text
    tshow Int
    startnum Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\""
              else "") Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
          (if ListNumberStyle
    numstyle ListNumberStyle -> ListNumberStyle -> Bool
    forall a. Eq a => a -> a -> Bool
    /= ListNumberStyle
    DefaultStyle
              then " style=\"list-style-type: " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    numstyle' Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> ";\""
              else "")

    -- | Convert bullet or ordered list item (list of blocks) to MediaWiki.
    listItemToMediaWiki :: PandocMonad m => [Block] -> MediaWikiWriter m Text
    listItemToMediaWiki :: [Block] -> MediaWikiWriter m Text
    listItemToMediaWiki items :: [Block]
    items = do
      Text
    contents <- [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki [Block]
    items
      Bool
    tags <- (WriterReader -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> Bool
    useTags
      if Bool
    tags
         then Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<li>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</li>"
         else do
           [Char]
    marker <- (WriterReader -> [Char])
    -> ReaderT WriterReader (StateT WriterState m) [Char]
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> [Char]
    listLevel
           Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ [Char] -> Text
    T.pack [Char]
    marker Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents

    -- | Convert definition list item (label, list of blocks) to MediaWiki.
    definitionListItemToMediaWiki :: PandocMonad m
                                  => ([Inline],[[Block]])
                                  -> MediaWikiWriter m Text
    definitionListItemToMediaWiki :: ([Inline], [[Block]]) -> MediaWikiWriter m Text
    definitionListItemToMediaWiki (label :: [Inline]
    label, items :: [[Block]]
    items) = do
      Text
    labelText <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    label
      [Text]
    contents <- ([Block] -> MediaWikiWriter m Text)
    -> [[Block]] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki [[Block]]
    items
      Bool
    tags <- (WriterReader -> Bool)
    -> ReaderT WriterReader (StateT WriterState m) Bool
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> Bool
    useTags
      if Bool
    tags
         then Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<dt>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    labelText Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</dt>\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
               Text -> [Text] -> Text
    T.intercalate "\n" ((Text -> Text) -> [Text] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\d :: Text
    d -> "<dd>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    d Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</dd>") [Text]
    contents)
         else do
           [Char]
    marker <- (WriterReader -> [Char])
    -> ReaderT WriterReader (StateT WriterState m) [Char]
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> [Char]
    listLevel
           Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ [Char] -> Text
    T.pack [Char]
    marker Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    labelText Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
               Text -> [Text] -> Text
    T.intercalate "\n" ((Text -> Text) -> [Text] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\d :: Text
    d -> [Char] -> Text
    T.pack ([Char] -> [Char]
    forall a. [a] -> [a]
    init [Char]
    marker) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> ": " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    d) [Text]
    contents)

    -- | True if the list can be handled by simple wiki markup, False if HTML tags will be needed.
    isSimpleList :: Block -> Bool
    isSimpleList :: Block -> Bool
    isSimpleList x :: Block
    x =
      case Block
    x of
           BulletList items :: [[Block]]
    items                 -> ([Block] -> Bool) -> [[Block]] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all [Block] -> Bool
    isSimpleListItem [[Block]]
    items
           OrderedList (num :: Int
    num, sty :: ListNumberStyle
    sty, _) items :: [[Block]]
    items  -> ([Block] -> Bool) -> [[Block]] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all [Block] -> Bool
    isSimpleListItem [[Block]]
    items Bool -> Bool -> Bool
    &&
                                                Int
    num Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1 Bool -> Bool -> Bool
    && ListNumberStyle
    sty ListNumberStyle -> [ListNumberStyle] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [ListNumberStyle
    DefaultStyle, ListNumberStyle
    Decimal]
           DefinitionList items :: [([Inline], [[Block]])]
    items             -> ([Block] -> Bool) -> [[Block]] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all [Block] -> Bool
    isSimpleListItem ([[Block]] -> Bool) -> [[Block]] -> Bool
    forall a b. (a -> b) -> a -> b
    $ (([Inline], [[Block]]) -> [[Block]])
    -> [([Inline], [[Block]])] -> [[Block]]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap ([Inline], [[Block]]) -> [[Block]]
    forall a b. (a, b) -> b
    snd [([Inline], [[Block]])]
    items
           _                                -> Bool
    False

    -- | True if list item can be handled with the simple wiki syntax.  False if
    --   HTML tags will be needed.
    isSimpleListItem :: [Block] -> Bool
    isSimpleListItem :: [Block] -> Bool
    isSimpleListItem []  = Bool
    True
    isSimpleListItem [x :: Block
    x] =
      case Block
    x of
           Plain _          -> Bool
    True
           Para  _          -> Bool
    True
           BulletList _     -> Block -> Bool
    isSimpleList Block
    x
           OrderedList _ _  -> Block -> Bool
    isSimpleList Block
    x
           DefinitionList _ -> Block -> Bool
    isSimpleList Block
    x
           _                -> Bool
    False
    isSimpleListItem [x :: Block
    x, y :: Block
    y] | Block -> Bool
    isPlainOrPara Block
    x =
      case Block
    y of
           BulletList _     -> Block -> Bool
    isSimpleList Block
    y
           OrderedList _ _  -> Block -> Bool
    isSimpleList Block
    y
           DefinitionList _ -> Block -> Bool
    isSimpleList Block
    y
           _                -> Bool
    False
    isSimpleListItem _ = Bool
    False

    isPlainOrPara :: Block -> Bool
    isPlainOrPara :: Block -> Bool
    isPlainOrPara (Plain _) = Bool
    True
    isPlainOrPara (Para  _) = Bool
    True
    isPlainOrPara _         = Bool
    False

    -- | Concatenates strings with line breaks between them.
    vcat :: [Text] -> Text
    vcat :: [Text] -> Text
    vcat = Text -> [Text] -> Text
    T.intercalate "\n"

    -- Auxiliary functions for tables:

    tableRowToMediaWiki :: PandocMonad m
                        => Bool
                        -> [Alignment]
                        -> [Double]
                        -> (Int, [[Block]])
                        -> MediaWikiWriter m Text
    tableRowToMediaWiki :: Bool
    -> [Alignment]
    -> [Double]
    -> (Int, [[Block]])
    -> MediaWikiWriter m Text
    tableRowToMediaWiki headless :: Bool
    headless alignments :: [Alignment]
    alignments widths :: [Double]
    widths (rownum :: Int
    rownum, cells :: [[Block]]
    cells) = do
      [Text]
    cells' <- ((Alignment, Double, [Block]) -> MediaWikiWriter m Text)
    -> [(Alignment, Double, [Block])]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (Bool
    -> Int -> (Alignment, Double, [Block]) -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    Bool
    -> Int -> (Alignment, Double, [Block]) -> MediaWikiWriter m Text
    tableCellToMediaWiki Bool
    headless Int
    rownum)
              ([(Alignment, Double, [Block])]
     -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> [(Alignment, Double, [Block])]
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ [Alignment]
    -> [Double] -> [[Block]] -> [(Alignment, Double, [Block])]
    forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
    zip3 [Alignment]
    alignments [Double]
    widths [[Block]]
    cells
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ [Text] -> Text
    T.unlines [Text]
    cells'

    tableCellToMediaWiki :: PandocMonad m
                         => Bool
                         -> Int
                         -> (Alignment, Double, [Block])
                         -> MediaWikiWriter m Text
    tableCellToMediaWiki :: Bool
    -> Int -> (Alignment, Double, [Block]) -> MediaWikiWriter m Text
    tableCellToMediaWiki headless :: Bool
    headless rownum :: Int
    rownum (alignment :: Alignment
    alignment, width :: Double
    width, bs :: [Block]
    bs) = do
      Text
    contents <- [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki [Block]
    bs
      let marker :: Text
    marker = if Int
    rownum Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1 Bool -> Bool -> Bool
    && Bool -> Bool
    not Bool
    headless then "!" else "|"
      let percent :: a -> Text
    percent w :: a
    w = Integer -> Text
    forall a. Show a => a -> Text
    tshow (a -> Integer
    forall a b. (RealFrac a, Integral b) => a -> b
    truncate (100a -> a -> a
    forall a. Num a => a -> a -> a
    *a
    w) :: Integer) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "%"
      let attrs :: [Text]
    attrs = ["align=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    forall a. Show a => a -> Text
    tshow (Alignment -> Text
    alignmentToText Alignment
    alignment) |
                     Alignment
    alignment Alignment -> Alignment -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Alignment
    AlignDefault Bool -> Bool -> Bool
    && Alignment
    alignment Alignment -> Alignment -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Alignment
    AlignLeft] [Text] -> [Text] -> [Text]
    forall a. Semigroup a => a -> a -> a
    <>
                  ["width=\"" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Double -> Text
    forall a. RealFrac a => a -> Text
    percent Double
    width Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\"" |
                     Double
    width Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    /= 0.0 Bool -> Bool -> Bool
    && Int
    rownum Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1]
      let attr :: Text
    attr = if [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    attrs
                    then ""
                    else [Text] -> Text
    T.unwords [Text]
    attrs Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "|"
      let sep :: Text
    sep = case [Block]
    bs of
                     [Plain _] -> " "
                     [Para  _] -> " "
                     []        -> ""
                     _         -> "\n"
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ Text
    marker Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    attr Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    sep Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    trimr Text
    contents

    alignmentToText :: Alignment -> Text
    alignmentToText :: Alignment -> Text
    alignmentToText alignment :: Alignment
    alignment = case Alignment
    alignment of
                                     AlignLeft    -> "left"
                                     AlignRight   -> "right"
                                     AlignCenter  -> "center"
                                     AlignDefault -> "left"

    imageToMediaWiki :: PandocMonad m => Attr -> MediaWikiWriter m Text
    imageToMediaWiki :: Attr -> MediaWikiWriter m Text
    imageToMediaWiki attr :: Attr
    attr = do
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> ReaderT WriterReader (StateT WriterState m) WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      let (_, cls :: [Text]
    cls, _) = Attr
    attr
          toPx :: Maybe Dimension -> Maybe Text
    toPx = (Dimension -> Text) -> Maybe Dimension -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (WriterOptions -> Dimension -> Text
    showInPixel WriterOptions
    opts) (Maybe Dimension -> Maybe Text)
    -> (Maybe Dimension -> Maybe Dimension)
    -> Maybe Dimension
    -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe Dimension -> Maybe Dimension
    checkPct
          checkPct :: Maybe Dimension -> Maybe Dimension
    checkPct (Just (Percent _)) = Maybe Dimension
    forall a. Maybe a
    Nothing
          checkPct maybeDim :: Maybe Dimension
    maybeDim           = Maybe Dimension
    maybeDim
          go :: Maybe a -> Maybe a -> a
    go (Just w :: a
    w) Nothing  = a
    w a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> "px"
          go (Just w :: a
    w) (Just h :: a
    h) = a
    w a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> "x" a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> a
    h a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> "px"
          go Nothing  (Just h :: a
    h) = "x" a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> a
    h a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> "px"
          go Nothing  Nothing  = ""
          dims :: Text
    dims = Maybe Text -> Maybe Text -> Text
    forall a. (Semigroup a, IsString a) => Maybe a -> Maybe a -> a
    go (Maybe Dimension -> Maybe Text
    toPx (Maybe Dimension -> Maybe Text) -> Maybe Dimension -> Maybe Text
    forall a b. (a -> b) -> a -> b
    $ Direction -> Attr -> Maybe Dimension
    dimension Direction
    Width Attr
    attr) (Maybe Dimension -> Maybe Text
    toPx (Maybe Dimension -> Maybe Text) -> Maybe Dimension -> Maybe Text
    forall a b. (a -> b) -> a -> b
    $ Direction -> Attr -> Maybe Dimension
    dimension Direction
    Height Attr
    attr)
          classes :: Text
    classes = if [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    cls
                       then ""
                       else "class=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> [Text] -> Text
    T.unwords [Text]
    cls
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ Text -> [Text] -> Text
    T.intercalate "|" ([Text] -> Text) -> [Text] -> Text
    forall a b. (a -> b) -> a -> b
    $ (Text -> Bool) -> [Text] -> [Text]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (Bool -> Bool
    not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Bool
    T.null) [Text
    dims, Text
    classes]

    -- | Convert list of Pandoc block elements to MediaWiki.
    blockListToMediaWiki :: PandocMonad m
                         => [Block]       -- ^ List of block elements
                         -> MediaWikiWriter m Text
    blockListToMediaWiki :: [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki blocks :: [Block]
    blocks =
      [Text] -> Text
    vcat ([Text] -> Text)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> MediaWikiWriter m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Block -> MediaWikiWriter m Text)
    -> [Block] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM Block -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    Block -> MediaWikiWriter m Text
    blockToMediaWiki [Block]
    blocks

    -- | Convert list of Pandoc inline elements to MediaWiki.
    inlineListToMediaWiki :: PandocMonad m => [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki :: [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki lst :: [Inline]
    lst =
      ([Text] -> Text)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> MediaWikiWriter m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap [Text] -> Text
    T.concat (ReaderT WriterReader (StateT WriterState m) [Text]
     -> MediaWikiWriter m Text)
    -> ReaderT WriterReader (StateT WriterState m) [Text]
    -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ (Inline -> MediaWikiWriter m Text)
    -> [Inline] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM Inline -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    Inline -> MediaWikiWriter m Text
    inlineToMediaWiki ([Inline] -> ReaderT WriterReader (StateT WriterState m) [Text])
    -> [Inline] -> ReaderT WriterReader (StateT WriterState m) [Text]
    forall a b. (a -> b) -> a -> b
    $ [Inline] -> [Inline]
    fixup [Inline]
    lst
        where
         fixup :: [Inline] -> [Inline]
    fixup [] = []
         fixup (Str t :: Text
    t : x :: Inline
    x : xs :: [Inline]
    xs)
           | Bool -> Bool
    not (Text -> Bool
    T.null Text
    t) Bool -> Bool -> Bool
    && Text -> Char
    T.last Text
    t Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '['
           , Inline -> Bool
    isLinkOrImage Inline
    x =
              Text -> Inline
    Str Text
    t Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Format -> Text -> Inline
    RawInline (Text -> Format
    Format "mediawiki") "<nowiki/>" Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixup [Inline]
    xs
         fixup (x :: Inline
    x:xs :: [Inline]
    xs) = Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixup [Inline]
    xs
         isLinkOrImage :: Inline -> Bool
    isLinkOrImage Link{}  = Bool
    True
         isLinkOrImage Image{} = Bool
    True
         isLinkOrImage _         = Bool
    False

    -- | Convert Pandoc inline element to MediaWiki.
    inlineToMediaWiki :: PandocMonad m => Inline -> MediaWikiWriter m Text

    inlineToMediaWiki :: Inline -> MediaWikiWriter m Text
    inlineToMediaWiki (Span attrs :: Attr
    attrs ils :: [Inline]
    ils) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    ils
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing (Text -> Attr -> Doc Text
    forall a. HasChars a => Text -> Attr -> Doc a
    tagWithAttrs "span" Attr
    attrs) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</span>"

    inlineToMediaWiki (Emph lst :: [Inline]
    lst) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "''" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "''"

    inlineToMediaWiki (Strong lst :: [Inline]
    lst) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "'''" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "'''"

    inlineToMediaWiki (Strikeout lst :: [Inline]
    lst) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<s>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</s>"

    inlineToMediaWiki (Superscript lst :: [Inline]
    lst) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<sup>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</sup>"

    inlineToMediaWiki (Subscript lst :: [Inline]
    lst) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<sub>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</sub>"

    inlineToMediaWiki (SmallCaps lst :: [Inline]
    lst) = [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst

    inlineToMediaWiki (Quoted SingleQuote lst :: [Inline]
    lst) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "\8216" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\8217"

    inlineToMediaWiki (Quoted DoubleQuote lst :: [Inline]
    lst) = do
      Text
    contents <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "\8220" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    contents Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\8221"

    inlineToMediaWiki (Cite _  lst :: [Inline]
    lst) = [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    lst

    inlineToMediaWiki (Code _ str :: Text
    str) =
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<code>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    escapeText Text
    str Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</code>"

    inlineToMediaWiki (Str str :: Text
    str) = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Text
    escapeText Text
    str

    inlineToMediaWiki (Math mt :: MathType
    mt str :: Text
    str) = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $
      "<math display=\"" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
      (if MathType
    mt MathType -> MathType -> Bool
    forall a. Eq a => a -> a -> Bool
    == MathType
    DisplayMath then "block" else "inline") Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
      "\">" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    str Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</math>"
      -- note:  str should NOT be escaped

    inlineToMediaWiki il :: Inline
    il@(RawInline f :: Format
    f str :: Text
    str)
      | Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "mediawiki" = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    str
      | Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "html"      = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    str
      | Bool
    otherwise               = "" Text
    -> ReaderT WriterReader (StateT WriterState m) ()
    -> MediaWikiWriter m Text
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ LogMessage -> ReaderT WriterReader (StateT WriterState m) ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (Inline -> LogMessage
    InlineNotRendered Inline
    il)

    inlineToMediaWiki LineBreak = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return "<br />\n"

    inlineToMediaWiki SoftBreak = do
      WrapOption
    wrapText <- (WriterState -> WrapOption)
    -> ReaderT WriterReader (StateT WriterState m) WrapOption
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets (WriterOptions -> WrapOption
    writerWrapText (WriterOptions -> WrapOption)
    -> (WriterState -> WriterOptions) -> WriterState -> WrapOption
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . WriterState -> WriterOptions
    stOptions)
      [Char]
    listlevel <- (WriterReader -> [Char])
    -> ReaderT WriterReader (StateT WriterState m) [Char]
    forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
    asks WriterReader -> [Char]
    listLevel
      case WrapOption
    wrapText of
           WrapAuto     -> Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return " "
           WrapNone     -> Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return " "
           WrapPreserve -> if [Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    listlevel
                              then Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return "\n"
                              else Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return " "

    inlineToMediaWiki Space = Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return " "

    inlineToMediaWiki (Link _ txt :: [Inline]
    txt (src :: Text
    src, _)) = do
      Text
    label <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    txt
      case [Inline]
    txt of
         [Str s :: Text
    s] | Text -> Bool
    isURI Text
    src Bool -> Bool -> Bool
    && Text -> Text
    escapeURI Text
    s Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    src -> Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    src
         _  -> Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ if Text -> Bool
    isURI Text
    src
                  then "[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    src Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    label Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "]"
                  else "[[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    src' Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "|" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    label Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "]]"
                         -- with leading / it's a link to a help page
                         where src' :: Text
    src' = Text -> Maybe Text -> Text
    forall a. a -> Maybe a -> a
    fromMaybe Text
    src (Maybe Text -> Text) -> Maybe Text -> Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Maybe Text
    T.stripPrefix "/" Text
    src

    inlineToMediaWiki (Image attr :: Attr
    attr alt :: [Inline]
    alt (source :: Text
    source, tit :: Text
    tit)) = do
      Text
    img  <- Attr -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    Attr -> MediaWikiWriter m Text
    imageToMediaWiki Attr
    attr
      Text
    alt' <- [Inline] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> MediaWikiWriter m Text
    inlineListToMediaWiki [Inline]
    alt
      let txt :: Text
    txt = if Text -> Bool
    T.null Text
    alt'
                   then if Text -> Bool
    T.null Text
    tit
                           then ""
                           else Text
    tit
                   else Text
    alt'
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "[[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
               Text -> [Text] -> Text
    T.intercalate "|"
               ((Text -> Bool) -> [Text] -> [Text]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (Bool -> Bool
    not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Bool
    T.null)
                [ "File:" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    source
                , Text
    img
                , Text
    txt
                ]) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "]]"

    inlineToMediaWiki (Note contents :: [Block]
    contents) = do
      Text
    contents' <- [Block] -> MediaWikiWriter m Text
    forall (m :: * -> *).
    PandocMonad m =>
    [Block] -> MediaWikiWriter m Text
    blockListToMediaWiki [Block]
    contents
      (WriterState -> WriterState)
    -> ReaderT WriterReader (StateT WriterState m) ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s { stNotes :: Bool
    stNotes = Bool
    True })
      Text -> MediaWikiWriter m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> MediaWikiWriter m Text) -> Text -> MediaWikiWriter m Text
    forall a b. (a -> b) -> a -> b
    $ "<ref>" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    stripTrailingNewlines Text
    contents' Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "</ref>"
      -- note - does not work for notes with multiple blocks

    highlightingLangs :: Set.Set Text
    highlightingLangs :: Set Text
    highlightingLangs = [Text] -> Set Text
    forall a. Ord a => [a] -> Set a
    Set.fromList [
      "abap",
      "abl",
      "abnf",
      "aconf",
      "actionscript",
      "actionscript3",
      "ada",
      "ada2005",
      "ada95",
      "adl",
      "agda",
      "ahk",
      "alloy",
      "ambienttalk",
      "ambienttalk/2",
      "antlr",
      "antlr-actionscript",
      "antlr-as",
      "antlr-c#",
      "antlr-cpp",
      "antlr-csharp",
      "antlr-java",
      "antlr-objc",
      "antlr-perl",
      "antlr-python",
      "antlr-rb",
      "antlr-ruby",
      "apache",
      "apacheconf",
      "apl",
      "applescript",
      "arduino",
      "arexx",
      "as",
      "as3",
      "asm",
      "aspectj",
      "aspx-cs",
      "aspx-vb",
      "asy",
      "asymptote",
      "at",
      "autohotkey",
      "autoit",
      "awk",
      "b3d",
      "basemake",
      "bash",
      "basic",
      "bat",
      "batch",
      "bbcode",
      "because",
      "befunge",
      "bf",
      "blitzbasic",
      "blitzmax",
      "bmax",
      "bnf",
      "boo",
      "boogie",
      "bplus",
      "brainfuck",
      "bro",
      "bsdmake",
      "bugs",
      "c",
      "c#",
      "c++",
      "c++-objdumb",
      "c-objdump",
      "ca65",
      "cadl",
      "camkes",
      "cbmbas",
      "ceylon",
      "cf3",
      "cfc",
      "cfengine3",
      "cfg",
      "cfm",
      "cfs",
      "chai",
      "chaiscript",
      "chapel",
      "cheetah",
      "chpl",
      "cirru",
      "cl",
      "clay",
      "clipper",
      "clj",
      "cljs",
      "clojure",
      "clojurescript",
      "cmake",
      "cobol",
      "cobolfree",
      "coffee",
      "coffee-script",
      "coffeescript",
      "common-lisp",
      "componentpascal",
      "console",
      "control",
      "coq",
      "cp",
      "cpp",
      "cpp-objdump",
      "cpsa",
      "crmsh",
      "croc",
      "cry",
      "cryptol",
      "csh",
      "csharp",
      "csound",
      "csound-csd",
      "csound-document",
      "csound-orc",
      "csound-sco",
      "csound-score",
      "css",
      "css+django",
      "css+erb",
      "css+genshi",
      "css+genshitext",
      "css+jinja",
      "css+lasso",
      "css+mako",
      "css+mozpreproc",
      "css+myghty",
      "css+php",
      "css+ruby",
      "css+smarty",
      "cu",
      "cucumber",
      "cuda",
      "cxx-objdump",
      "cypher",
      "cython",
      "d",
      "d-objdump",
      "dart",
      "debcontrol",
      "debsources",
      "delphi",
      "dg",
      "diff",
      "django",
      "docker",
      "dockerfile",
      "dosbatch",
      "doscon",
      "dosini",
      "dpatch",
      "dtd",
      "duby",
      "duel",
      "dylan",
      "dylan-console",
      "dylan-lid",
      "dylan-repl",
      "earl-grey",
      "earlgrey",
      "easytrieve",
      "ebnf",
      "ec",
      "ecl",
      "eg",
      "eiffel",
      "elisp",
      "elixir",
      "elm",
      "emacs",
      "erb",
      "erl",
      "erlang",
      "evoque",
      "ex",
      "exs",
      "ezhil",
      "factor",
      "fan",
      "fancy",
      "felix",
      "fish",
      "fishshell",
      "flx",
      "fortran",
      "fortranfixed",
      "foxpro",
      "fsharp",
      "fy",
      "gap",
      "gas",
      "gawk",
      "genshi",
      "genshitext",
      "gherkin",
      "glsl",
      "gnuplot",
      "go",
      "golo",
      "gooddata-cl",
      "gosu",
      "groff",
      "groovy",
      "gst",
      "haml",
      "handlebars",
      "haskell",
      "haxe",
      "haxeml",
      "hexdump",
      "hs",
      "html",
      "html+cheetah",
      "html+django",
      "html+erb",
      "html+evoque",
      "html+genshi",
      "html+handlebars",
      "html+jinja",
      "html+kid",
      "html+lasso",
      "html+mako",
      "html+myghty",
      "html+php",
      "html+ruby",
      "html+smarty",
      "html+spitfire",
      "html+twig",
      "html+velocity",
      "htmlcheetah",
      "htmldjango",
      "http",
      "hx",
      "hxml",
      "hxsl",
      "hy",
      "hybris",
      "hylang",
      "i6",
      "i6t",
      "i7",
      "idl",
      "idl4",
      "idr",
      "idris",
      "iex",
      "igor",
      "igorpro",
      "ik",
      "inform6",
      "inform7",
      "ini",
      "io",
      "ioke",
      "irb",
      "irc",
      "isabelle",
      "j",
      "jade",
      "jags",
      "jasmin",
      "jasminxt",
      "java",
      "javascript",
      "javascript+cheetah",
      "javascript+django",
      "javascript+erb",
      "javascript+genshi",
      "javascript+genshitext",
      "javascript+jinja",
      "javascript+lasso",
      "javascript+mako",
      "javascript+mozpreproc",
      "javascript+myghty",
      "javascript+php",
      "javascript+ruby",
      "javascript+smarty",
      "javascript+spitfire",
      "jbst",
      "jcl",
      "jinja",
      "jl",
      "jlcon",
      "jproperties",
      "js",
      "js+cheetah",
      "js+django",
      "js+erb",
      "js+genshi",
      "js+genshitext",
      "js+jinja",
      "js+lasso",
      "js+mako",
      "js+myghty",
      "js+php",
      "js+ruby",
      "js+smarty",
      "js+spitfire",
      "json",
      "json-ld",
      "jsonld",
      "jsonml+bst",
      "jsp",
      "julia",
      "kal",
      "kconfig",
      "kernel-config",
      "kid",
      "koka",
      "kotlin",
      "ksh",
      "lagda",
      "lasso",
      "lassoscript",
      "latex",
      "lcry",
      "lcryptol",
      "lean",
      "less",
      "lhaskell",
      "lhs",
      "lid",
      "lidr",
      "lidris",
      "lighttpd",
      "lighty",
      "limbo",
      "linux-config",
      "liquid",
      "lisp",
      "literate-agda",
      "literate-cryptol",
      "literate-haskell",
      "literate-idris",
      "live-script",
      "livescript",
      "llvm",
      "logos",
      "logtalk",
      "lsl",
      "lua",
      "m2",
      "make",
      "makefile",
      "mako",
      "man",
      "maql",
      "mask",
      "mason",
      "mathematica",
      "matlab",
      "matlabsession",
      "mawk",
      "menuconfig",
      "mf",
      "minid",
      "mma",
      "modelica",
      "modula2",
      "moin",
      "monkey",
      "moo",
      "moocode",
      "moon",
      "moonscript",
      "mozhashpreproc",
      "mozpercentpreproc",
      "mq4",
      "mq5",
      "mql",
      "mql4",
      "mql5",
      "msc",
      "mscgen",
      "mupad",
      "mxml",
      "myghty",
      "mysql",
      "nasm",
      "nawk",
      "nb",
      "nemerle",
      "nesc",
      "newlisp",
      "newspeak",
      "nginx",
      "nim",
      "nimrod",
      "nit",
      "nix",
      "nixos",
      "nroff",
      "nsh",
      "nsi",
      "nsis",
      "numpy",
      "obj-c",
      "obj-c++",
      "obj-j",
      "objc",
      "objc++",
      "objdump",
      "objdump-nasm",
      "objective-c",
      "objective-c++",
      "objective-j",
      "objectivec",
      "objectivec++",
      "objectivej",
      "objectpascal",
      "objj",
      "ocaml",
      "octave",
      "odin",
      "ooc",
      "opa",
      "openbugs",
      "openedge",
      "pacmanconf",
      "pan",
      "parasail",
      "pas",
      "pascal",
      "pawn",
      "pcmk",
      "perl",
      "perl6",
      "php",
      "php3",
      "php4",
      "php5",
      "pig",
      "pike",
      "pkgconfig",
      "pl",
      "pl6",
      "plpgsql",
      "po",
      "posh",
      "postgres",
      "postgres-console",
      "postgresql",
      "postgresql-console",
      "postscr",
      "postscript",
      "pot",
      "pov",
      "powershell",
      "praat",
      "progress",
      "prolog",
      "properties",
      "proto",
      "protobuf",
      "ps1",
      "ps1con",
      "psm1",
      "psql",
      "puppet",
      "py",
      "py3",
      "py3tb",
      "pycon",
      "pypy",
      "pypylog",
      "pyrex",
      "pytb",
      "python",
      "python3",
      "pyx",
      "qbasic",
      "qbs",
      "qml",
      "qvt",
      "qvto",
      "r",
      "racket",
      "ragel",
      "ragel-c",
      "ragel-cpp",
      "ragel-d",
      "ragel-em",
      "ragel-java",
      "ragel-objc",
      "ragel-rb",
      "ragel-ruby",
      "raw",
      "rb",
      "rbcon",
      "rconsole",
      "rd",
      "rebol",
      "red",
      "red/system",
      "redcode",
      "registry",
      "resource",
      "resourcebundle",
      "rest",
      "restructuredtext",
      "rexx",
      "rhtml",
      "rkt",
      "roboconf-graph",
      "roboconf-instances",
      "robotframework",
      "rout",
      "rql",
      "rsl",
      "rst",
      "rts",
      "ruby",
      "rust",
      "s",
      "sage",
      "salt",
      "sass",
      "sc",
      "scala",
      "scaml",
      "scheme",
      "scilab",
      "scm",
      "scss",
      "sh",
      "shell",
      "shell-session",
      "shen",
      "slim",
      "sls",
      "smali",
      "smalltalk",
      "smarty",
      "sml",
      "snobol",
      "sources.list",
      "sourceslist",
      "sp",
      "sparql",
      "spec",
      "spitfire",
      "splus",
      "sql",
      "sqlite3",
      "squeak",
      "squid",
      "squid.conf",
      "squidconf",
      "ssp",
      "st",
      "stan",
      "supercollider",
      "sv",
      "swift",
      "swig",
      "systemverilog",
      "tads3",
      "tap",
      "tcl",
      "tcsh",
      "tcshcon",
      "tea",
      "termcap",
      "terminfo",
      "terraform",
      "tex",
      "text",
      "tf",
      "thrift",
      "todotxt",
      "trac-wiki",
      "trafficscript",
      "treetop",
      "ts",
      "turtle",
      "twig",
      "typescript",
      "udiff",
      "urbiscript",
      "v",
      "vala",
      "vapi",
      "vb.net",
      "vbnet",
      "vctreestatus",
      "velocity",
      "verilog",
      "vfp",
      "vgl",
      "vhdl",
      "vim",
      "winbatch",
      "winbugs",
      "x10",
      "xbase",
      "xml",
      "xml+cheetah",
      "xml+django",
      "xml+erb",
      "xml+evoque",
      "xml+genshi",
      "xml+jinja",
      "xml+kid",
      "xml+lasso",
      "xml+mako",
      "xml+myghty",
      "xml+php",
      "xml+ruby",
      "xml+smarty",
      "xml+spitfire",
      "xml+velocity",
      "xq",
      "xql",
      "xqm",
      "xquery",
      "xqy",
      "xslt",
      "xten",
      "xtend",
      "xul+mozpreproc",
      "yaml",
      "yaml+jinja",
      "zephir" ]