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

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

    Conversion of 'Pandoc' format into LaTeX.
    -}
    module Text.Pandoc.Writers.LaTeX (
        writeLaTeX
      , writeBeamer
      ) where
    import Control.Applicative ((<|>))
    import Control.Monad.State.Strict
    import Data.Monoid (Any(..))
    import Data.Char (isAlphaNum, isAscii, isDigit, isLetter, isSpace,
                      isPunctuation, ord)
    import Data.List (foldl', intersperse, nubBy, (\\), uncons)
    import Data.Maybe (catMaybes, fromMaybe, isJust, mapMaybe, isNothing)
    import qualified Data.Map as M
    import Data.Text (Text)
    import qualified Data.Text as T
    import Network.URI (unEscapeString)
    import Text.DocTemplates (FromContext(lookupContext), renderTemplate,
                              Val(..), Context(..))
    import Text.Pandoc.BCP47 (Lang (..), getLang, renderLang)
    import Text.Pandoc.Class.PandocMonad (PandocMonad, report, toLang)
    import Text.Pandoc.Definition
    import Text.Pandoc.Highlighting (formatLaTeXBlock, formatLaTeXInline, highlight,
                                     styleToLaTeX, toListingsLanguage)
    import Text.Pandoc.ImageSize
    import Text.Pandoc.Logging
    import Text.Pandoc.Options
    import Text.DocLayout
    import Text.Pandoc.Shared
    import Text.Pandoc.Slides
    import Text.Pandoc.Walk
    import Text.Pandoc.Writers.Shared
    import Text.Printf (printf)
    import qualified Data.Text.Normalize as Normalize

    data WriterState =
      WriterState { WriterState -> Bool
    stInNote        :: Bool          -- true if we're in a note
                  , WriterState -> Bool
    stInQuote       :: Bool          -- true if in a blockquote
                  , WriterState -> Bool
    stExternalNotes :: Bool          -- true if in context where
                                                     -- we need to store footnotes
                  , WriterState -> Bool
    stInMinipage    :: Bool          -- true if in minipage
                  , WriterState -> Bool
    stInHeading     :: Bool          -- true if in a section heading
                  , WriterState -> Bool
    stInItem        :: Bool          -- true if in \item[..]
                  , WriterState -> [Doc Text]
    stNotes         :: [Doc Text]    -- notes in a minipage
                  , WriterState -> Int
    stOLLevel       :: Int           -- level of ordered list nesting
                  , WriterState -> WriterOptions
    stOptions       :: WriterOptions -- writer options, so they don't have to be parameter
                  , WriterState -> Bool
    stVerbInNote    :: Bool          -- true if document has verbatim text in note
                  , WriterState -> Bool
    stTable         :: Bool          -- true if document has a table
                  , WriterState -> Bool
    stStrikeout     :: Bool          -- true if document has strikeout
                  , WriterState -> Bool
    stUrl           :: Bool          -- true if document has visible URL link
                  , WriterState -> Bool
    stGraphics      :: Bool          -- true if document contains images
                  , WriterState -> Bool
    stLHS           :: Bool          -- true if document has literate haskell code
                  , WriterState -> Bool
    stHasChapters   :: Bool          -- true if document has chapters
                  , WriterState -> Bool
    stCsquotes      :: Bool          -- true if document uses csquotes
                  , WriterState -> Bool
    stHighlighting  :: Bool          -- true if document has highlighted code
                  , WriterState -> Bool
    stIncremental   :: Bool          -- true if beamer lists should be displayed bit by bit
                  , WriterState -> [Text]
    stInternalLinks :: [Text]      -- list of internal link targets
                  , WriterState -> Bool
    stBeamer        :: Bool          -- produce beamer
                  , WriterState -> Bool
    stEmptyLine     :: Bool          -- true if no content on line
                  , WriterState -> Bool
    stHasCslRefs    :: Bool          -- has a Div with class refs
                  , WriterState -> Bool
    stCslHangingIndent :: Bool       -- use hanging indent for bib
                  }

    startingState :: WriterOptions -> WriterState
    startingState :: WriterOptions -> WriterState
    startingState options :: WriterOptions
    options = WriterState :: Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> [Doc Text]
    -> Int
    -> WriterOptions
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> [Text]
    -> Bool
    -> Bool
    -> Bool
    -> Bool
    -> WriterState
    WriterState {
                      stInNote :: Bool
    stInNote = Bool
    False
                    , stInQuote :: Bool
    stInQuote = Bool
    False
                    , stExternalNotes :: Bool
    stExternalNotes = Bool
    False
                    , stInHeading :: Bool
    stInHeading = Bool
    False
                    , stInMinipage :: Bool
    stInMinipage = Bool
    False
                    , stInItem :: Bool
    stInItem = Bool
    False
                    , stNotes :: [Doc Text]
    stNotes = []
                    , stOLLevel :: Int
    stOLLevel = 1
                    , stOptions :: WriterOptions
    stOptions = WriterOptions
    options
                    , stVerbInNote :: Bool
    stVerbInNote = Bool
    False
                    , stTable :: Bool
    stTable = Bool
    False
                    , stStrikeout :: Bool
    stStrikeout = Bool
    False
                    , stUrl :: Bool
    stUrl = Bool
    False
                    , stGraphics :: Bool
    stGraphics = Bool
    False
                    , stLHS :: Bool
    stLHS = Bool
    False
                    , stHasChapters :: Bool
    stHasChapters = case WriterOptions -> TopLevelDivision
    writerTopLevelDivision WriterOptions
    options of
                                        TopLevelPart    -> Bool
    True
                                        TopLevelChapter -> Bool
    True
                                        _               -> Bool
    False
                    , stCsquotes :: Bool
    stCsquotes = Bool
    False
                    , stHighlighting :: Bool
    stHighlighting = Bool
    False
                    , stIncremental :: Bool
    stIncremental = WriterOptions -> Bool
    writerIncremental WriterOptions
    options
                    , stInternalLinks :: [Text]
    stInternalLinks = []
                    , stBeamer :: Bool
    stBeamer = Bool
    False
                    , stEmptyLine :: Bool
    stEmptyLine = Bool
    True
                    , stHasCslRefs :: Bool
    stHasCslRefs = Bool
    False
                    , stCslHangingIndent :: Bool
    stCslHangingIndent = Bool
    False }

    -- | Convert Pandoc to LaTeX.
    writeLaTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
    writeLaTeX :: WriterOptions -> Pandoc -> m Text
    writeLaTeX options :: WriterOptions
    options document :: Pandoc
    document =
      StateT WriterState m Text -> WriterState -> m Text
    forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
    evalStateT (WriterOptions -> Pandoc -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX WriterOptions
    options Pandoc
    document) (WriterState -> m Text) -> WriterState -> m Text
    forall a b. (a -> b) -> a -> b
    $
        WriterOptions -> WriterState
    startingState WriterOptions
    options

    -- | Convert Pandoc to LaTeX Beamer.
    writeBeamer :: PandocMonad m => WriterOptions -> Pandoc -> m Text
    writeBeamer :: WriterOptions -> Pandoc -> m Text
    writeBeamer options :: WriterOptions
    options document :: Pandoc
    document =
      StateT WriterState m Text -> WriterState -> m Text
    forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
    evalStateT (WriterOptions -> Pandoc -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX WriterOptions
    options Pandoc
    document) (WriterState -> m Text) -> WriterState -> m Text
    forall a b. (a -> b) -> a -> b
    $
        (WriterOptions -> WriterState
    startingState WriterOptions
    options){ stBeamer :: Bool
    stBeamer = Bool
    True }

    type LW m = StateT WriterState m

    pandocToLaTeX :: PandocMonad m
                  => WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX :: WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX options :: WriterOptions
    options (Pandoc meta :: Meta
    meta blocks :: [Block]
    blocks) = do
      -- Strip off final 'references' header if --natbib or --biblatex
      let method :: CiteMethod
    method = WriterOptions -> CiteMethod
    writerCiteMethod WriterOptions
    options
      let blocks' :: [Block]
    blocks' = if CiteMethod
    method CiteMethod -> CiteMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    == CiteMethod
    Biblatex Bool -> Bool -> Bool
    || CiteMethod
    method CiteMethod -> CiteMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    == CiteMethod
    Natbib
                       then case [Block] -> [Block]
    forall a. [a] -> [a]
    reverse [Block]
    blocks of
                                 Div ("refs",_,_) _:xs :: [Block]
    xs -> [Block] -> [Block]
    forall a. [a] -> [a]
    reverse [Block]
    xs
                                 _                     -> [Block]
    blocks
                       else [Block]
    blocks
      -- see if there are internal links
      let isInternalLink :: Inline -> [Text]
    isInternalLink (Link _ _ (s :: Text
    s,_))
            | Just ('#', xs :: Text
    xs) <- Text -> Maybe (Char, Text)
    T.uncons Text
    s = [Text
    xs]
          isInternalLink _                 = []
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stInternalLinks :: [Text]
    stInternalLinks = (Inline -> [Text]) -> [Block] -> [Text]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query Inline -> [Text]
    isInternalLink [Block]
    blocks' }
      let colwidth :: Maybe Int
    colwidth = if WriterOptions -> WrapOption
    writerWrapText WriterOptions
    options WrapOption -> WrapOption -> Bool
    forall a. Eq a => a -> a -> Bool
    == WrapOption
    WrapAuto
                        then Int -> Maybe Int
    forall a. a -> Maybe a
    Just (Int -> Maybe Int) -> Int -> Maybe Int
    forall a b. (a -> b) -> a -> b
    $ WriterOptions -> Int
    writerColumns WriterOptions
    options
                        else Maybe Int
    forall a. Maybe a
    Nothing
      Context Text
    metadata <- WriterOptions
    -> ([Block] -> StateT WriterState m (Doc Text))
    -> ([Inline] -> StateT WriterState m (Doc Text))
    -> Meta
    -> 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
    options
                  [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX
                  ((Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Doc Text -> Doc Text
    forall a. Doc a -> Doc a
    chomp (StateT WriterState m (Doc Text)
     -> StateT WriterState m (Doc Text))
    -> ([Inline] -> StateT WriterState m (Doc Text))
    -> [Inline]
    -> StateT WriterState m (Doc Text)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX)
                  Meta
    meta
      let chaptersClasses :: [Text]
    chaptersClasses = ["memoir","book","report","scrreprt","scrbook","extreport","extbook","tufte-book"]
      let frontmatterClasses :: [Text]
    frontmatterClasses = ["memoir","book","scrbook","extbook","tufte-book"]
      -- these have \frontmatter etc.
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      let documentClass :: Text
    documentClass =
            case Text -> Context Text -> Maybe Text
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    lookupContext "documentclass" (WriterOptions -> Context Text
    writerVariables WriterOptions
    options) Maybe Text -> Maybe Text -> Maybe Text
    forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
    `mplus`
                  (MetaValue -> Text
    forall a. Walkable Inline a => a -> Text
    stringify (MetaValue -> Text) -> Maybe MetaValue -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Meta -> Maybe MetaValue
    lookupMeta "documentclass" Meta
    meta) of
                     Just x :: Text
    x -> Text
    x
                     Nothing | Bool
    beamer    -> "beamer"
                             | Bool
    otherwise -> case WriterOptions -> TopLevelDivision
    writerTopLevelDivision WriterOptions
    options of
                                              TopLevelPart    -> "book"
                                              TopLevelChapter -> "book"
                                              _               -> "article"
      Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when (Text
    documentClass Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    chaptersClasses) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $
         (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stHasChapters :: Bool
    stHasChapters = Bool
    True }
      case Text -> Text
    T.toLower (Text -> Text) -> (Doc Text -> Text) -> Doc Text -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . 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) -> Maybe (Doc Text) -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField "csquotes" Context Text
    metadata of
         Nothing      -> () -> StateT WriterState m ()
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
         Just "false" -> () -> StateT WriterState m ()
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
         Just _       -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{stCsquotes :: Bool
    stCsquotes = Bool
    True}
      let (blocks'' :: [Block]
    blocks'', lastHeader :: [Inline]
    lastHeader) = if WriterOptions -> CiteMethod
    writerCiteMethod WriterOptions
    options CiteMethod -> CiteMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    == CiteMethod
    Citeproc then
                                     ([Block]
    blocks', [])
                                   else case [Block] -> [Block]
    forall a. [a] -> [a]
    reverse [Block]
    blocks' of
                                     Header 1 _ il :: [Inline]
    il : _ -> ([Block] -> [Block]
    forall a. [a] -> [a]
    init [Block]
    blocks', [Inline]
    il)
                                     _                 -> ([Block]
    blocks', [])
      [Block]
    blocks''' <- if Bool
    beamer
                      then [Block] -> LW m [Block]
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m [Block]
    toSlides [Block]
    blocks''
                      else [Block] -> LW m [Block]
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([Block] -> LW m [Block]) -> [Block] -> LW m [Block]
    forall a b. (a -> b) -> a -> b
    $ Bool -> Maybe Int -> [Block] -> [Block]
    makeSections Bool
    False Maybe Int
    forall a. Maybe a
    Nothing [Block]
    blocks''
      Doc Text
    main <- [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    blocks'''
      Doc Text
    biblioTitle <- [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lastHeader
      WriterState
    st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
      Text
    titleMeta <- StringContext -> Text -> LW m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString (Text -> LW m Text) -> Text -> LW m Text
    forall a b. (a -> b) -> a -> b
    $ [Inline] -> Text
    forall a. Walkable Inline a => a -> Text
    stringify ([Inline] -> Text) -> [Inline] -> Text
    forall a b. (a -> b) -> a -> b
    $ Meta -> [Inline]
    docTitle Meta
    meta
      [Text]
    authorsMeta <- ([Inline] -> LW m Text)
    -> [[Inline]] -> StateT WriterState m [Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (StringContext -> Text -> LW m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString (Text -> LW m Text) -> ([Inline] -> Text) -> [Inline] -> LW m Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> Text
    forall a. Walkable Inline a => a -> Text
    stringify) ([[Inline]] -> StateT WriterState m [Text])
    -> [[Inline]] -> StateT WriterState m [Text]
    forall a b. (a -> b) -> a -> b
    $ Meta -> [[Inline]]
    docAuthors Meta
    meta
      [Lang]
    docLangs <- [Maybe Lang] -> [Lang]
    forall a. [Maybe a] -> [a]
    catMaybes ([Maybe Lang] -> [Lang])
    -> StateT WriterState m [Maybe Lang] -> StateT WriterState m [Lang]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
          (Text -> StateT WriterState m (Maybe Lang))
    -> [Text] -> StateT WriterState m [Maybe Lang]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (Maybe Text -> StateT WriterState m (Maybe Lang)
    forall (m :: * -> *). PandocMonad m => Maybe Text -> m (Maybe Lang)
    toLang (Maybe Text -> StateT WriterState m (Maybe Lang))
    -> (Text -> Maybe Text)
    -> Text
    -> StateT WriterState m (Maybe Lang)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Text
    forall a. a -> Maybe a
    Just) ([Text] -> [Text]
    forall a. Ord a => [a] -> [a]
    ordNub ((Block -> [Text]) -> [Block] -> [Text]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Block -> [Text]
    extract "lang") [Block]
    blocks))
      let hasStringValue :: Text -> Bool
    hasStringValue x :: Text
    x = Maybe (Doc Text) -> Bool
    forall a. Maybe a -> Bool
    isJust (Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    x Context Text
    metadata :: Maybe (Doc Text))
      let geometryFromMargins :: Doc Text
    geometryFromMargins = [Doc Text] -> Doc Text
    forall a. Monoid a => [a] -> a
    mconcat ([Doc Text] -> Doc Text) -> [Doc Text] -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse ("," :: Doc Text) ([Doc Text] -> [Doc Text]) -> [Doc Text] -> [Doc Text]
    forall a b. (a -> b) -> a -> b
    $
                                ((Doc Text, Text) -> Maybe (Doc Text))
    -> [(Doc Text, Text)] -> [Doc Text]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe (\(x :: Doc Text
    x,y :: Text
    y) ->
                                    ((Doc Text
    x Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "=") Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>) (Doc Text -> Doc Text) -> Maybe (Doc Text) -> Maybe (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    y Context Text
    metadata)
                                  [("lmargin","margin-left")
                                  ,("rmargin","margin-right")
                                  ,("tmargin","margin-top")
                                  ,("bmargin","margin-bottom")
                                  ]
      let toPolyObj :: Lang -> Val Text
          toPolyObj :: Lang -> Val Text
    toPolyObj lang :: Lang
    lang = Context Text -> Val Text
    forall a. Context a -> Val a
    MapVal (Context Text -> Val Text) -> Context Text -> Val Text
    forall a b. (a -> b) -> a -> b
    $ Map Text (Val Text) -> Context Text
    forall a. Map Text (Val a) -> Context a
    Context (Map Text (Val Text) -> Context Text)
    -> Map Text (Val Text) -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                            [(Text, Val Text)] -> Map Text (Val Text)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList [ ("name" , Doc Text -> Val Text
    forall a. Doc a -> Val a
    SimpleVal (Doc Text -> Val Text) -> Doc Text -> Val Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    name)
                                       , ("options" , Doc Text -> Val Text
    forall a. Doc a -> Val a
    SimpleVal (Doc Text -> Val Text) -> Doc Text -> Val Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    opts) ]
            where
              (name :: Text
    name, opts :: Text
    opts) = Lang -> (Text, Text)
    toPolyglossia Lang
    lang
      Maybe Lang
    mblang <- Maybe Text -> StateT WriterState m (Maybe Lang)
    forall (m :: * -> *). PandocMonad m => Maybe Text -> m (Maybe Lang)
    toLang (Maybe Text -> StateT WriterState m (Maybe Lang))
    -> Maybe Text -> StateT WriterState m (Maybe Lang)
    forall a b. (a -> b) -> a -> b
    $ case WriterOptions -> Meta -> Maybe Text
    getLang WriterOptions
    options Meta
    meta of
                              Just l :: Text
    l -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    l
                              Nothing | [Lang] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Lang]
    docLangs -> Maybe Text
    forall a. Maybe a
    Nothing
                                      | Bool
    otherwise     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "en"
      -- we need a default here since lang is used in template conditionals

      let dirs :: [Text]
    dirs = (Block -> [Text]) -> [Block] -> [Text]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Block -> [Text]
    extract "dir") [Block]
    blocks

      let context :: Context Text
    context  =  Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "toc" (WriterOptions -> Bool
    writerTableOfContents WriterOptions
    options) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "toc-depth" (Int -> Text
    forall a. Show a => a -> Text
    tshow
                                            (WriterOptions -> Int
    writerTOCDepth WriterOptions
    options Int -> Int -> Int
    forall a. Num a => a -> a -> a
    -
                                                  if WriterState -> Bool
    stHasChapters WriterState
    st
                                                     then 1
                                                     else 0)) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "body" Doc Text
    main (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "title-meta" Text
    titleMeta (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "author-meta"
                            (Text -> [Text] -> Text
    T.intercalate "; " [Text]
    authorsMeta) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "documentclass" Text
    documentClass (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 "verbatim-in-note" (WriterState -> Bool
    stVerbInNote WriterState
    st) (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 "tables" (WriterState -> Bool
    stTable WriterState
    st) (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 "strikeout" (WriterState -> Bool
    stStrikeout WriterState
    st) (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 "url" (WriterState -> Bool
    stUrl WriterState
    st) (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 "numbersections" (WriterOptions -> Bool
    writerNumberSections WriterOptions
    options) (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 "lhs" (WriterState -> Bool
    stLHS WriterState
    st) (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 "graphics" (WriterState -> Bool
    stGraphics WriterState
    st) (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 "has-chapters" (WriterState -> Bool
    stHasChapters WriterState
    st) (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 "has-frontmatter" (Text
    documentClass Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    frontmatterClasses) (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 "listings" (WriterOptions -> Bool
    writerListings WriterOptions
    options Bool -> Bool -> Bool
    || WriterState -> Bool
    stLHS WriterState
    st) (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 "beamer" Bool
    beamer (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (if WriterState -> Bool
    stHighlighting WriterState
    st
                          then case WriterOptions -> Maybe Style
    writerHighlightStyle WriterOptions
    options of
                                    Just sty :: Style
    sty ->
                                       Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "highlighting-macros"
                                          (Text -> Text
    T.stripEnd (Text -> Text) -> Text -> Text
    forall a b. (a -> b) -> a -> b
    $ Style -> Text
    styleToLaTeX Style
    sty)
                                    Nothing -> Context Text -> Context Text
    forall a. a -> a
    id
                          else Context Text -> Context Text
    forall a. a -> a
    id) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (case WriterOptions -> CiteMethod
    writerCiteMethod WriterOptions
    options of
                             Natbib   -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "biblio-title" Doc Text
    biblioTitle (Context Text -> Context Text)
    -> (Context Text -> Context Text) -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                                         Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "natbib" Bool
    True
                             Biblatex -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "biblio-title" Doc Text
    biblioTitle (Context Text -> Context Text)
    -> (Context Text -> Context Text) -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                                         Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "biblatex" Bool
    True
                             _        -> Context Text -> Context Text
    forall a. a -> a
    id) (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 "colorlinks" ((Text -> Bool) -> [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any Text -> Bool
    hasStringValue
                               ["citecolor", "urlcolor", "linkcolor", "toccolor",
                                "filecolor"]) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (if [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    dirs
                         then Context Text -> Context Text
    forall a. a -> a
    id
                         else Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "dir" ("ltr" :: Text)) (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 "section-titles" Bool
    True (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 "csl-refs" (WriterState -> Bool
    stHasCslRefs WriterState
    st) (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 "csl-hanging-indent" (WriterState -> Bool
    stCslHangingIndent WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "geometry" Doc Text
    geometryFromMargins (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (case Text -> Maybe (Char, Text)
    T.uncons (Text -> Maybe (Char, Text))
    -> (Doc Text -> Text) -> Doc Text -> Maybe (Char, Text)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing (Doc Text -> Maybe (Char, Text))
    -> Maybe (Doc Text) -> Maybe (Maybe (Char, Text))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                            Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField "papersize" Context Text
    metadata of
                          -- uppercase a4, a5, etc.
                          Just (Just ('A', ds :: Text
    ds))
                            | Bool -> Bool
    not (Text -> Bool
    T.null Text
    ds) Bool -> Bool -> Bool
    && (Char -> Bool) -> Text -> Bool
    T.all Char -> Bool
    isDigit Text
    ds
                              -> Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    resetField "papersize" ("a" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ds)
                          _   -> Context Text -> Context Text
    forall a. a -> a
    id)
                      Context Text
    metadata
      let context' :: Context Text
    context' =
              -- note: lang is used in some conditionals in the template,
              -- so we need to set it if we have any babel/polyglossia:
              (Context Text -> Context Text)
    -> (Lang -> Context Text -> Context Text)
    -> Maybe Lang
    -> Context Text
    -> Context Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Context Text -> Context Text
    forall a. a -> a
    id (\l :: Lang
    l -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "lang"
                          (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Lang -> Text
    renderLang Lang
    l)) Maybe Lang
    mblang
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ (Context Text -> Context Text)
    -> (Lang -> Context Text -> Context Text)
    -> Maybe Lang
    -> Context Text
    -> Context Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Context Text -> Context Text
    forall a. a -> a
    id (\l :: Lang
    l -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "babel-lang"
                          (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Lang -> Text
    toBabel Lang
    l)) Maybe Lang
    mblang
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Text -> [Doc Text] -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "babel-otherlangs"
                 ((Lang -> Doc Text) -> [Lang] -> [Doc Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> (Lang -> Text) -> Lang -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Lang -> Text
    toBabel) [Lang]
    docLangs)
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "babel-newcommands" ([Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat ([Doc Text] -> Doc Text) -> [Doc Text] -> Doc Text
    forall a b. (a -> b) -> a -> b
    $
               ((Text, Text) -> Doc Text) -> [(Text, Text)] -> [Doc Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\(poly :: Text
    poly, babel :: Text
    babel) -> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $
                -- \textspanish and \textgalician are already used by babel
                -- save them as \oritext... and let babel use that
                if Text
    poly Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` ["spanish", "galician"]
                   then "\\let\\oritext" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\\text" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                        "\\AddBabelHook{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}{beforeextras}" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                          "{\\renewcommand{\\text" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}{\\oritext"
                          Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}}\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                        "\\AddBabelHook{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}{afterextras}" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                          "{\\renewcommand{\\text" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}[2][]{\\foreignlanguage{"
                          Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}{##2}}}"
                   else (if Text
    poly Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == "latin" -- see #4161
                            then "\\providecommand{\\textlatin}{}\n\\renewcommand"
                            else "\\newcommand") Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "{\\text" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                        "}[2][]{\\foreignlanguage{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    babel Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}{#2}}\n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                        "\\newenvironment{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    poly Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                        "}[2][]{\\begin{otherlanguage}{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                        Text
    babel Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}}{\\end{otherlanguage}}"
                )
                -- eliminate duplicates that have same polyglossia name
                ([(Text, Text)] -> [Doc Text]) -> [(Text, Text)] -> [Doc Text]
    forall a b. (a -> b) -> a -> b
    $ ((Text, Text) -> (Text, Text) -> Bool)
    -> [(Text, Text)] -> [(Text, Text)]
    forall a. (a -> a -> Bool) -> [a] -> [a]
    nubBy (\a :: (Text, Text)
    a b :: (Text, Text)
    b -> (Text, Text) -> Text
    forall a b. (a, b) -> a
    fst (Text, Text)
    a Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == (Text, Text) -> Text
    forall a b. (a, b) -> a
    fst (Text, Text)
    b)
                -- find polyglossia and babel names of languages used in the document
                ([(Text, Text)] -> [(Text, Text)])
    -> [(Text, Text)] -> [(Text, Text)]
    forall a b. (a -> b) -> a -> b
    $ (Lang -> (Text, Text)) -> [Lang] -> [(Text, Text)]
    forall a b. (a -> b) -> [a] -> [b]
    map (\l :: Lang
    l -> ((Text, Text) -> Text
    forall a b. (a, b) -> a
    fst ((Text, Text) -> Text) -> (Text, Text) -> Text
    forall a b. (a -> b) -> a -> b
    $ Lang -> (Text, Text)
    toPolyglossia Lang
    l, Lang -> Text
    toBabel Lang
    l)) [Lang]
    docLangs
              )
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ (Context Text -> Context Text)
    -> (Lang -> Context Text -> Context Text)
    -> Maybe Lang
    -> Context Text
    -> Context Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Context Text -> Context Text
    forall a. a -> a
    id (Text -> Val Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "polyglossia-lang" (Val Text -> Context Text -> Context Text)
    -> (Lang -> Val Text) -> Lang -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Lang -> Val Text
    toPolyObj) Maybe Lang
    mblang
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Val Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField "polyglossia-otherlangs"
                 ([Val Text] -> Val Text
    forall a. [Val a] -> Val a
    ListVal ((Lang -> Val Text) -> [Lang] -> [Val Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Lang -> Val Text
    toPolyObj [Lang]
    docLangs :: [Val Text]))
            (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 "latex-dir-rtl"
               ((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) -> Maybe (Doc Text) -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField "dir" Context Text
    context) Maybe Text -> Maybe Text -> Bool
    forall a. Eq a => a -> a -> Bool
    ==
                   Text -> Maybe Text
    forall a. a -> Maybe a
    Just ("rtl" :: Text)) Context Text
    context
      Text -> LW m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> LW m Text) -> Text -> LW 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
    colwidth (Doc Text -> Text) -> Doc Text -> Text
    forall a b. (a -> b) -> a -> b
    $
        case WriterOptions -> Maybe (Template Text)
    writerTemplate WriterOptions
    options of
           Nothing  -> Doc Text
    main
           Just tpl :: Template Text
    tpl -> 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'

    data StringContext = TextString
                       | URLString
                       | CodeString
                       deriving (StringContext -> StringContext -> Bool
    (StringContext -> StringContext -> Bool)
    -> (StringContext -> StringContext -> Bool) -> Eq StringContext
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    /= :: StringContext -> StringContext -> Bool
    $c/= :: StringContext -> StringContext -> Bool
    == :: StringContext -> StringContext -> Bool
    $c== :: StringContext -> StringContext -> Bool
    Eq)

    -- escape things as needed for LaTeX
    stringToLaTeX :: PandocMonad m => StringContext -> Text -> LW m Text
    stringToLaTeX :: StringContext -> Text -> LW m Text
    stringToLaTeX context :: StringContext
    context zs :: Text
    zs = do
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      Text -> LW m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> LW m Text) -> Text -> LW m Text
    forall a b. (a -> b) -> a -> b
    $ String -> Text
    T.pack (String -> Text) -> String -> Text
    forall a b. (a -> b) -> a -> b
    $
        (Char -> String -> String) -> String -> String -> String
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr (WriterOptions -> StringContext -> Char -> String -> String
    go WriterOptions
    opts StringContext
    context) String
    forall a. Monoid a => a
    mempty (String -> String) -> String -> String
    forall a b. (a -> b) -> a -> b
    $ Text -> String
    T.unpack (Text -> String) -> Text -> String
    forall a b. (a -> b) -> a -> b
    $
        if WriterOptions -> Bool
    writerPreferAscii WriterOptions
    opts
           then NormalizationMode -> Text -> Text
    Normalize.normalize NormalizationMode
    Normalize.NFD Text
    zs
           else Text
    zs
     where
      go :: WriterOptions -> StringContext -> Char -> String -> String
      go :: WriterOptions -> StringContext -> Char -> String -> String
    go opts :: WriterOptions
    opts ctx :: StringContext
    ctx x :: Char
    x xs :: String
    xs   =
        let ligatures :: Bool
    ligatures = Extension -> WriterOptions -> Bool
    forall a. HasSyntaxExtensions a => Extension -> a -> Bool
    isEnabled Extension
    Ext_smart WriterOptions
    opts Bool -> Bool -> Bool
    && StringContext
    ctx StringContext -> StringContext -> Bool
    forall a. Eq a => a -> a -> Bool
    == StringContext
    TextString
            isUrl :: Bool
    isUrl = StringContext
    ctx StringContext -> StringContext -> Bool
    forall a. Eq a => a -> a -> Bool
    == StringContext
    URLString
            mbAccentCmd :: Maybe String
    mbAccentCmd =
              if WriterOptions -> Bool
    writerPreferAscii WriterOptions
    opts Bool -> Bool -> Bool
    && StringContext
    ctx StringContext -> StringContext -> Bool
    forall a. Eq a => a -> a -> Bool
    == StringContext
    TextString
                 then String -> Maybe (Char, String)
    forall a. [a] -> Maybe (a, [a])
    uncons String
    xs Maybe (Char, String)
    -> ((Char, String) -> Maybe String) -> Maybe String
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= \(c :: Char
    c,_) -> Char -> Map Char String -> Maybe String
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Char
    c Map Char String
    accents
                 else Maybe String
    forall a. Maybe a
    Nothing
            emits :: String -> String
    emits s :: String
    s =
              case Maybe String
    mbAccentCmd of
                   Just cmd :: String
    cmd ->
                     String
    cmd String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "{" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    s String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "}" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> Int -> String -> String
    forall a. Int -> [a] -> [a]
    drop 1 String
    xs -- drop combining accent
                   Nothing  -> String
    s String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs
            emitc :: Char -> String
    emitc c :: Char
    c =
              case Maybe String
    mbAccentCmd of
                   Just cmd :: String
    cmd ->
                     String
    cmd String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "{" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> [Char
    c] String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "}" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> Int -> String -> String
    forall a. Int -> [a] -> [a]
    drop 1 String
    xs -- drop combining accent
                   Nothing  -> Char
    c Char -> String -> String
    forall a. a -> [a] -> [a]
    : String
    xs
            emitcseq :: String -> String
    emitcseq cs :: String
    cs =
              case String
    xs of
                c :: Char
    c:_ | Char -> Bool
    isLetter Char
    c
                    , StringContext
    ctx StringContext -> StringContext -> Bool
    forall a. Eq a => a -> a -> Bool
    == StringContext
    TextString
                                 -> String
    cs String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> " " String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs
                    | Char -> Bool
    isSpace Char
    c  -> String
    cs String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "{}" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs
                    | StringContext
    ctx StringContext -> StringContext -> Bool
    forall a. Eq a => a -> a -> Bool
    == StringContext
    TextString
                                 -> String
    cs String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs
                _ -> String
    cs String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "{}" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs
            emitquote :: String -> String
    emitquote cs :: String
    cs =
              case String
    xs of
                '`':_  -> String
    cs String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "\\," String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs -- add thin space
                '\'':_ -> String
    cs String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> "\\," String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs -- add thin space
                _      -> String
    cs String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    xs
        in case Char
    x of
             '?' | Bool
    ligatures ->  -- avoid ?` ligature
               case String
    xs of
                 '`':_ -> String -> String
    emits "?{}"
                 _     -> Char -> String
    emitc Char
    x
             '!' | Bool
    ligatures ->  -- avoid !` ligature
               case String
    xs of
                 '`':_ -> String -> String
    emits "!{}"
                 _     -> Char -> String
    emitc Char
    x
             '{' -> String -> String
    emits "\\{"
             '}' -> String -> String
    emits "\\}"
             '`' | StringContext
    ctx StringContext -> StringContext -> Bool
    forall a. Eq a => a -> a -> Bool
    == StringContext
    CodeString -> String -> String
    emitcseq "\\textasciigrave"
             '$' | Bool -> Bool
    not Bool
    isUrl -> String -> String
    emits "\\$"
             '%' -> String -> String
    emits "\\%"
             '&' -> String -> String
    emits "\\&"
             '_' | Bool -> Bool
    not Bool
    isUrl -> String -> String
    emits "\\_"
             '#' -> String -> String
    emits "\\#"
             '-' | Bool -> Bool
    not Bool
    isUrl -> case String
    xs of
                         -- prevent adjacent hyphens from forming ligatures
                         ('-':_) -> String -> String
    emits "-\\/"
                         _       -> Char -> String
    emitc '-'
             '~' | Bool -> Bool
    not Bool
    isUrl -> String -> String
    emitcseq "\\textasciitilde"
             '^' -> String -> String
    emits "\\^{}"
             '\\'| Bool
    isUrl     -> Char -> String
    emitc '/' -- NB. / works as path sep even on Windows
                 | Bool
    otherwise -> String -> String
    emitcseq "\\textbackslash"
             '|' | Bool -> Bool
    not Bool
    isUrl -> String -> String
    emitcseq "\\textbar"
             '<' -> String -> String
    emitcseq "\\textless"
             '>' -> String -> String
    emitcseq "\\textgreater"
             '[' -> String -> String
    emits "{[}"  -- to avoid interpretation as
             ']' -> String -> String
    emits "{]}"  -- optional arguments
             '\'' | StringContext
    ctx StringContext -> StringContext -> Bool
    forall a. Eq a => a -> a -> Bool
    == StringContext
    CodeString -> String -> String
    emitcseq "\\textquotesingle"
             '\160' -> String -> String
    emits "~"
             '\x200B' -> String -> String
    emits "\\hspace{0pt}"  -- zero-width space
             '\x202F' -> String -> String
    emits "\\,"
             '\x2026' -> String -> String
    emitcseq "\\ldots"
             '\x2018' | Bool
    ligatures -> String -> String
    emitquote "`"
             '\x2019' | Bool
    ligatures -> String -> String
    emitquote "'"
             '\x201C' | Bool
    ligatures -> String -> String
    emitquote "``"
             '\x201D' | Bool
    ligatures -> String -> String
    emitquote "''"
             '\x2014' | Bool
    ligatures -> String -> String
    emits "---"
             '\x2013' | Bool
    ligatures -> String -> String
    emits "--"
             _ | WriterOptions -> Bool
    writerPreferAscii WriterOptions
    opts
                 -> case Char
    x of
                      'ı' -> String -> String
    emitcseq "\\i"
                      'ȷ' -> String -> String
    emitcseq "\\j"
                      'å' -> String -> String
    emitcseq "\\aa"
                      'Å' -> String -> String
    emitcseq "\\AA"
                      'ß' -> String -> String
    emitcseq "\\ss"
                      'ø' -> String -> String
    emitcseq "\\o"
                      'Ø' -> String -> String
    emitcseq "\\O"
                      'Ł' -> String -> String
    emitcseq "\\L"
                      'ł' -> String -> String
    emitcseq "\\l"
                      'æ' -> String -> String
    emitcseq "\\ae"
                      'Æ' -> String -> String
    emitcseq "\\AE"
                      'œ' -> String -> String
    emitcseq "\\oe"
                      'Œ' -> String -> String
    emitcseq "\\OE"
                      '£' -> String -> String
    emitcseq "\\pounds"
                      '€' -> String -> String
    emitcseq "\\euro"
                      '©' -> String -> String
    emitcseq "\\copyright"
                      _   -> Char -> String
    emitc Char
    x
               | Bool
    otherwise -> Char -> String
    emitc Char
    x

    accents :: M.Map Char String
    accents :: Map Char String
    accents = [(Char, String)] -> Map Char String
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [ ('\779' , "\\H")
      , ('\768' , "\\`")
      , ('\769' , "\\'")
      , ('\770' , "\\^")
      , ('\771' , "\\~")
      , ('\776' , "\\\"")
      , ('\775' , "\\.")
      , ('\772' , "\\=")
      , ('\781' , "\\|")
      , ('\817' , "\\b")
      , ('\807' , "\\c")
      , ('\783' , "\\G")
      , ('\777' , "\\h")
      , ('\803' , "\\d")
      , ('\785' , "\\f")
      , ('\778' , "\\r")
      , ('\865' , "\\t")
      , ('\782' , "\\U")
      , ('\780' , "\\v")
      , ('\774' , "\\u")
      , ('\808' , "\\k")
      , ('\785' , "\\newtie")
      , ('\8413', "\\textcircled")
      ]

    toLabel :: PandocMonad m => Text -> LW m Text
    toLabel :: Text -> LW m Text
    toLabel z :: Text
    z = Text -> Text
    go (Text -> Text) -> LW m Text -> LW m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    `fmap` StringContext -> Text -> LW m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    URLString Text
    z
     where
       go :: Text -> Text
    go = (Char -> Text) -> Text -> Text
    T.concatMap ((Char -> Text) -> Text -> Text) -> (Char -> Text) -> Text -> Text
    forall a b. (a -> b) -> a -> b
    $ \x :: Char
    x -> case Char
    x of
         _ | (Char -> Bool
    isLetter Char
    x Bool -> Bool -> Bool
    || Char -> Bool
    isDigit Char
    x) Bool -> Bool -> Bool
    && Char -> Bool
    isAscii Char
    x -> Char -> Text
    T.singleton Char
    x
           | Char
    x Char -> Text -> Bool
    `elemText` "_-+=:;." -> Char -> Text
    T.singleton Char
    x
           | Bool
    otherwise -> String -> Text
    T.pack (String -> Text) -> String -> Text
    forall a b. (a -> b) -> a -> b
    $ "ux" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String -> Int -> String
    forall r. PrintfType r => String -> r
    printf "%x" (Char -> Int
    ord Char
    x)

    -- | Puts contents into LaTeX command.
    inCmd :: Text -> Doc Text -> Doc Text
    inCmd :: Text -> Doc Text -> Doc Text
    inCmd cmd :: Text
    cmd contents :: Doc Text
    contents = Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '\\' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    cmd Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    contents

    toSlides :: PandocMonad m => [Block] -> LW m [Block]
    toSlides :: [Block] -> LW m [Block]
    toSlides bs :: [Block]
    bs = do
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      let slideLevel :: Int
    slideLevel = Int -> Maybe Int -> Int
    forall a. a -> Maybe a -> a
    fromMaybe ([Block] -> Int
    getSlideLevel [Block]
    bs) (Maybe Int -> Int) -> Maybe Int -> Int
    forall a b. (a -> b) -> a -> b
    $ WriterOptions -> Maybe Int
    writerSlideLevel WriterOptions
    opts
      let bs' :: [Block]
    bs' = Int -> [Block] -> [Block]
    prepSlides Int
    slideLevel [Block]
    bs
      (Block -> StateT WriterState m Block) -> [Block] -> LW m [Block]
    forall a b (m :: * -> *).
    (Walkable a b, Monad m, Applicative m, Functor m) =>
    (a -> m a) -> b -> m b
    walkM (Int -> Block -> StateT WriterState m Block
    forall (m :: * -> *). PandocMonad m => Int -> Block -> LW m Block
    elementToBeamer Int
    slideLevel) ([Block] -> LW m [Block]) -> [Block] -> LW m [Block]
    forall a b. (a -> b) -> a -> b
    $ Bool -> Maybe Int -> [Block] -> [Block]
    makeSections Bool
    False Maybe Int
    forall a. Maybe a
    Nothing [Block]
    bs'

    -- this creates section slides and marks slides with class "slide","block"
    elementToBeamer :: PandocMonad m => Int -> Block -> LW m Block
    elementToBeamer :: Int -> Block -> LW m Block
    elementToBeamer slideLevel :: Int
    slideLevel (Div (ident :: Text
    ident,"section":dclasses :: [Text]
    dclasses,dkvs :: [(Text, Text)]
    dkvs)
                                  xs :: [Block]
    xs@(h :: Block
    h@(Header lvl :: Int
    lvl _ _) : ys :: [Block]
    ys))
      | Int
    lvl Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >  Int
    slideLevel
        = Block -> LW m Block
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Block -> LW m Block) -> Block -> LW m Block
    forall a b. (a -> b) -> a -> b
    $ Attr -> [Block] -> Block
    Div (Text
    ident,"block"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) [Block]
    xs
      | Int
    lvl Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <  Int
    slideLevel
        = do let isSlide :: Block -> Bool
    isSlide (Div (_,"slide":_,_) _)   = Bool
    True
                 isSlide (Div (_,"section":_,_) _) = Bool
    True
                 isSlide _                         = Bool
    False
             let (titleBs :: [Block]
    titleBs, slideBs :: [Block]
    slideBs) = (Block -> Bool) -> [Block] -> ([Block], [Block])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    break Block -> Bool
    isSlide [Block]
    ys
             Block -> LW m Block
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Block -> LW m Block) -> Block -> LW m Block
    forall a b. (a -> b) -> a -> b
    $
               if [Block] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Block]
    titleBs
                  then Attr -> [Block] -> Block
    Div (Text
    ident,"section"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) [Block]
    xs
                  else Attr -> [Block] -> Block
    Div (Text
    ident,"section"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs)
                        (Block
    h Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : Attr -> [Block] -> Block
    Div ("","slide"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) (Block
    hBlock -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    :[Block]
    titleBs) Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    slideBs)
      | Bool
    otherwise
        = Block -> LW m Block
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Block -> LW m Block) -> Block -> LW m Block
    forall a b. (a -> b) -> a -> b
    $ Attr -> [Block] -> Block
    Div (Text
    ident,"slide"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) [Block]
    xs
    elementToBeamer _ x :: Block
    x = Block -> LW m Block
    forall (m :: * -> *) a. Monad m => a -> m a
    return Block
    x

    isListBlock :: Block -> Bool
    isListBlock :: Block -> Bool
    isListBlock (BulletList _)     = Bool
    True
    isListBlock (OrderedList _ _)  = Bool
    True
    isListBlock (DefinitionList _) = Bool
    True
    isListBlock _                  = Bool
    False

    -- | Convert Pandoc block element to LaTeX.
    blockToLaTeX :: PandocMonad m
                 => Block     -- ^ Block to convert
                 -> LW m (Doc Text)
    blockToLaTeX :: Block -> LW m (Doc Text)
    blockToLaTeX Null = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    blockToLaTeX (Div attr :: Attr
    attr@(identifier :: Text
    identifier,"block":_,_) (Header _ _ ils :: [Inline]
    ils : bs :: [Block]
    bs)) = do
      Text
    ref <- Text -> LW m Text
    forall (m :: * -> *). PandocMonad m => Text -> LW m Text
    toLabel Text
    identifier
      let anchor :: Doc Text
    anchor = if Text -> Bool
    T.null Text
    identifier
                      then Doc Text
    forall a. Doc a
    empty
                      else Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\protect\\hypertarget" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                           Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    ref) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    forall a. Doc a
    empty
      Doc Text
    title' <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    ils
      Doc Text
    contents <- [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    bs
      Attr -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Attr -> Doc Text -> LW m (Doc Text)
    wrapDiv Attr
    attr (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ ("\\begin{block}" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    title' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    anchor) Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                     Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\end{block}"
    blockToLaTeX (Div (identifier :: Text
    identifier,"slide":dclasses :: [Text]
    dclasses,dkvs :: [(Text, Text)]
    dkvs)
                   (Header _ (_,hclasses :: [Text]
    hclasses,hkvs :: [(Text, Text)]
    hkvs) ils :: [Inline]
    ils : bs :: [Block]
    bs)) = do
      -- note: [fragile] is required or verbatim breaks
      let hasCodeBlock :: Block -> [Bool]
    hasCodeBlock (CodeBlock _ _) = [Bool
    True]
          hasCodeBlock _               = []
      let hasCode :: Inline -> [Bool]
    hasCode (Code _ _) = [Bool
    True]
          hasCode _          = []
      let classes :: [Text]
    classes = [Text] -> [Text]
    forall a. Ord a => [a] -> [a]
    ordNub ([Text] -> [Text]) -> [Text] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ [Text]
    dclasses [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++ [Text]
    hclasses
      let kvs :: [(Text, Text)]
    kvs = [(Text, Text)] -> [(Text, Text)]
    forall a. Ord a => [a] -> [a]
    ordNub ([(Text, Text)] -> [(Text, Text)])
    -> [(Text, Text)] -> [(Text, Text)]
    forall a b. (a -> b) -> a -> b
    $ [(Text, Text)]
    dkvs [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
    forall a. [a] -> [a] -> [a]
    ++ [(Text, Text)]
    hkvs
      let fragile :: Bool
    fragile = "fragile" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes Bool -> Bool -> Bool
    ||
                    Bool -> Bool
    not ([Bool] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null ([Bool] -> Bool) -> [Bool] -> Bool
    forall a b. (a -> b) -> a -> b
    $ (Block -> [Bool]) -> [Block] -> [Bool]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query Block -> [Bool]
    hasCodeBlock [Block]
    bs [Bool] -> [Bool] -> [Bool]
    forall a. [a] -> [a] -> [a]
    ++ (Inline -> [Bool]) -> [Block] -> [Bool]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query Inline -> [Bool]
    hasCode [Block]
    bs)
      let frameoptions :: [Text]
    frameoptions = ["allowdisplaybreaks", "allowframebreaks", "fragile",
                          "b", "c", "t", "environment",
                          "label", "plain", "shrink", "standout",
                          "noframenumbering"]
      let optionslist :: [Text]
    optionslist = ["fragile" | Bool
    fragile
                                   , Maybe Text -> Bool
    forall a. Maybe a -> Bool
    isNothing (Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "fragile" [(Text, Text)]
    kvs)
                                   , "fragile" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` [Text]
    classes] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                        [Text
    k | Text
    k <- [Text]
    classes, Text
    k Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    frameoptions] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                        [Text
    k Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    v | (k :: Text
    k,v :: Text
    v) <- [(Text, Text)]
    kvs, Text
    k Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    frameoptions]
      let options :: Doc Text
    options = if [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    optionslist
                       then Doc Text
    forall a. Doc a
    empty
                       else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> [Text] -> Text
    T.intercalate "," [Text]
    optionslist))
      Doc Text
    slideTitle <- if [Inline]
    ils [Inline] -> [Inline] -> Bool
    forall a. Eq a => a -> a -> Bool
    == [Text -> Inline
    Str "\0"] -- marker for hrule
                       then Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
                       else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    ils
      Text
    ref <- Text -> LW m Text
    forall (m :: * -> *). PandocMonad m => Text -> LW m Text
    toLabel Text
    identifier
      let slideAnchor :: Doc Text
    slideAnchor = if Text -> Bool
    T.null Text
    identifier
                           then Doc Text
    forall a. Doc a
    empty
                           else Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\protect\\hypertarget" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    ref) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    forall a. Doc a
    empty
      Doc Text
    contents <- [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    bs LW m (Doc Text) -> (Doc Text -> LW m (Doc Text)) -> LW m (Doc Text)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Attr -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Attr -> Doc Text -> LW m (Doc Text)
    wrapDiv (Text
    identifier,[Text]
    classes,[(Text, Text)]
    kvs)
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ ("\\begin{frame}" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    options Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    slideTitle Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    slideAnchor) Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                 Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\end{frame}"
    blockToLaTeX (Div (identifier :: Text
    identifier@(Text -> Maybe (Char, Text)
    T.uncons -> Just (_,_)),dclasses :: [Text]
    dclasses,dkvs :: [(Text, Text)]
    dkvs)
                   (Header lvl :: Int
    lvl ("",hclasses :: [Text]
    hclasses,hkvs :: [(Text, Text)]
    hkvs) ils :: [Inline]
    ils : bs :: [Block]
    bs)) =
      -- move identifier from div to header
      Block -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX (Attr -> [Block] -> Block
    Div ("",[Text]
    dclasses,[(Text, Text)]
    dkvs)
                   (Int -> Attr -> [Inline] -> Block
    Header Int
    lvl (Text
    identifier,[Text]
    hclasses,[(Text, Text)]
    hkvs) [Inline]
    ils Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    bs))
    blockToLaTeX (Div (identifier :: Text
    identifier,classes :: [Text]
    classes,kvs :: [(Text, Text)]
    kvs) bs :: [Block]
    bs) = do
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      Bool
    oldIncremental <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stIncremental
      if Bool
    beamer Bool -> Bool -> Bool
    && "incremental" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
         then (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stIncremental :: Bool
    stIncremental = Bool
    True }
         else Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when (Bool
    beamer Bool -> Bool -> Bool
    && "nonincremental" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $
                 (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st { stIncremental :: Bool
    stIncremental = Bool
    False }
      Doc Text
    result <- if Text
    identifier Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == "refs"
                   then do
                     Doc Text
    inner <- [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    bs
                     (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stHasCslRefs :: Bool
    stHasCslRefs = Bool
    True
                                       , stCslHangingIndent :: Bool
    stCslHangingIndent =
                                          "hanging-indent" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes }
                     Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\begin{cslreferences}" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                              Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                              "\\end{cslreferences}"
                   else [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    bs
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stIncremental :: Bool
    stIncremental = Bool
    oldIncremental }
      Doc Text
    linkAnchor' <- Bool -> Text -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> Text -> Doc Text -> LW m (Doc Text)
    hypertarget Bool
    True Text
    identifier Doc Text
    forall a. Doc a
    empty
      -- see #2704 for the motivation for adding \leavevmode:
      let linkAnchor :: Doc Text
    linkAnchor =
            case [Block]
    bs of
              Para _ : _
                | Bool -> Bool
    not (Doc Text -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc Text
    linkAnchor')
                  -> "\\leavevmode" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    linkAnchor' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "%"
              _ -> Doc Text
    linkAnchor'
          wrapNotes :: Doc Text -> Doc Text
    wrapNotes txt :: Doc Text
    txt = if Bool
    beamer Bool -> Bool -> Bool
    && "notes" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                             then "\\note" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    txt -- speaker notes
                             else Doc Text
    linkAnchor Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    txt
      Doc Text -> Doc Text
    wrapNotes (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Attr -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Attr -> Doc Text -> LW m (Doc Text)
    wrapDiv (Text
    identifier,[Text]
    classes,[(Text, Text)]
    kvs) Doc Text
    result
    blockToLaTeX (Plain lst :: [Inline]
    lst) =
      [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    -- title beginning with fig: indicates that the image is a figure
    blockToLaTeX (Para [Image attr :: Attr
    attr@(ident :: Text
    ident, _, _) txt :: [Inline]
    txt (src :: Text
    src,tgt :: Text
    tgt)])
      | Just tit :: Text
    tit <- Text -> Text -> Maybe Text
    T.stripPrefix "fig:" Text
    tgt
      = do
          (capt :: Doc Text
    capt, captForLof :: Doc Text
    captForLof, footnotes :: Doc Text
    footnotes) <- Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
    getCaption Bool
    True [Inline]
    txt
          Doc Text
    lab <- Text -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Text -> LW m (Doc Text)
    labelFor Text
    ident
          let caption :: Doc Text
    caption = "\\caption" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    captForLof Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    capt Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    lab
          Doc Text
    img <- Inline -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Inline -> LW m (Doc Text)
    inlineToLaTeX (Attr -> [Inline] -> (Text, Text) -> Inline
    Image Attr
    attr [Inline]
    txt (Text
    src,Text
    tit))
          Doc Text
    innards <- Bool -> Text -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> Text -> Doc Text -> LW m (Doc Text)
    hypertarget Bool
    True Text
    ident (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $
                       "\\centering" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    img Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    caption Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr
          let figure :: Doc Text
    figure = Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\begin{figure}" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    innards Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\end{figure}"
          WriterState
    st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
          Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ (if WriterState -> Bool
    stInMinipage WriterState
    st
                     -- can't have figures in notes or minipage (here, table cell)
                     -- http://www.tex.ac.uk/FAQ-ouparmd.html
                    then Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\begin{center}" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    img Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $+$ Doc Text
    capt Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                           "\\end{center}"
                    else Doc Text
    figure) Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    footnotes
    -- . . . indicates pause in beamer slides
    blockToLaTeX (Para [Str ".",Space,Str ".",Space,Str "."]) = do
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      if Bool
    beamer
         then Block -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX (Format -> Text -> Block
    RawBlock "latex" "\\pause")
         else [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Text -> Inline
    Str ".",Inline
    Space,Text -> Inline
    Str ".",Inline
    Space,Text -> Inline
    Str "."]
    blockToLaTeX (Para lst :: [Inline]
    lst) =
      [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    blockToLaTeX (LineBlock lns :: [[Inline]]
    lns) =
      Block -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX (Block -> LW m (Doc Text)) -> Block -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ [[Inline]] -> Block
    linesToPara [[Inline]]
    lns
    blockToLaTeX (BlockQuote lst :: [Block]
    lst) = do
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      case [Block]
    lst of
           [b :: Block
    b] | Bool
    beamer Bool -> Bool -> Bool
    && Block -> Bool
    isListBlock Block
    b -> do
             Bool
    oldIncremental <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stIncremental
             (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stIncremental :: Bool
    stIncremental = Bool -> Bool
    not Bool
    oldIncremental }
             Doc Text
    result <- Block -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX Block
    b
             (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stIncremental :: Bool
    stIncremental = Bool
    oldIncremental }
             Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    result
           _ -> do
             Bool
    oldInQuote <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInQuote
             (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s{stInQuote :: Bool
    stInQuote = Bool
    True})
             Doc Text
    contents <- [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    lst
             (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s{stInQuote :: Bool
    stInQuote = Bool
    oldInQuote})
             Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\begin{quote}" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\end{quote}"
    blockToLaTeX (CodeBlock (identifier :: Text
    identifier,classes :: [Text]
    classes,keyvalAttr :: [(Text, Text)]
    keyvalAttr) str :: Text
    str) = do
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      Doc Text
    lab <- Text -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Text -> LW m (Doc Text)
    labelFor Text
    identifier
      Doc Text
    linkAnchor' <- Bool -> Text -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> Text -> Doc Text -> LW m (Doc Text)
    hypertarget Bool
    True Text
    identifier Doc Text
    lab
      let linkAnchor :: Doc Text
    linkAnchor = if Doc Text -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc Text
    linkAnchor'
                          then Doc Text
    forall a. Doc a
    empty
                          else Doc Text
    linkAnchor' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "%"
      let lhsCodeBlock :: LW m (Doc Text)
    lhsCodeBlock = do
            (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stLHS :: Bool
    stLHS = Bool
    True }
            Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    forall a. Doc a -> Doc a
    flush (Doc Text
    linkAnchor Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\begin{code}" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                                "\\end{code}") Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    cr
      let rawCodeBlock :: LW m (Doc Text)
    rawCodeBlock = do
            WriterState
    st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
            Text
    env <- if WriterState -> Bool
    stInNote WriterState
    st
                      then (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s{ stVerbInNote :: Bool
    stVerbInNote = Bool
    True }) StateT WriterState m () -> LW m Text -> LW m Text
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >>
                           Text -> LW m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return "Verbatim"
                      else Text -> LW m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return "verbatim"
            Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    forall a. Doc a -> Doc a
    flush (Doc Text
    linkAnchor Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal ("\\begin{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    env Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}") Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                     Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal ("\\end{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    env Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}")) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr
      let listingsCodeBlock :: LW m (Doc Text)
    listingsCodeBlock = do
            WriterState
    st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
            Text
    ref <- Text -> LW m Text
    forall (m :: * -> *). PandocMonad m => Text -> LW m Text
    toLabel Text
    identifier
            let params :: [Text]
    params = if WriterOptions -> Bool
    writerListings (WriterState -> WriterOptions
    stOptions WriterState
    st)
                         then (case [Text] -> Maybe Text
    getListingsLanguage [Text]
    classes of
                                    Just l :: Text
    l  -> [ "language=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    mbBraced Text
    l ]
                                    Nothing -> []) [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                              [ "numbers=left" | "numberLines" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                                 Bool -> Bool -> Bool
    || "number" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                                 Bool -> Bool -> Bool
    || "number-lines" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes ] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                              [ (if Text
    key Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == "startFrom"
                                    then "firstnumber"
                                    else Text
    key) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    mbBraced Text
    attr |
                                    (key :: Text
    key,attr :: Text
    attr) <- [(Text, Text)]
    keyvalAttr,
                                    Text
    key Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` ["exports", "tangle", "results"]
                                    -- see #4889
                              ] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                              ["label=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ref | Bool -> Bool
    not (Text -> Bool
    T.null Text
    identifier)]

                         else []
                printParams :: Doc Text
    printParams
                    | [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    params = Doc Text
    forall a. Doc a
    empty
                    | Bool
    otherwise   = Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    hcat (Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse ", "
                          ((Text -> Doc Text) -> [Text] -> [Doc Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal [Text]
    params))
            Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    forall a. Doc a -> Doc a
    flush ("\\begin{lstlisting}" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    printParams Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                     "\\end{lstlisting}") Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    cr
      let highlightedCodeBlock :: LW m (Doc Text)
    highlightedCodeBlock =
            case SyntaxMap
    -> (FormatOptions -> [SourceLine] -> Text)
    -> Attr
    -> Text
    -> Either Text Text
    forall a.
    SyntaxMap
    -> (FormatOptions -> [SourceLine] -> a)
    -> Attr
    -> Text
    -> Either Text a
    highlight (WriterOptions -> SyntaxMap
    writerSyntaxMap WriterOptions
    opts)
                     FormatOptions -> [SourceLine] -> Text
    formatLaTeXBlock ("",[Text]
    classes,[(Text, Text)]
    keyvalAttr) Text
    str of
                   Left msg :: Text
    msg -> do
                     Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (Text -> Bool
    T.null Text
    msg) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $
                       LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> LogMessage
    CouldNotHighlight Text
    msg
                     LW m (Doc Text)
    rawCodeBlock
                   Right h :: Text
    h -> do
                      WriterState
    st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
                      Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when (WriterState -> Bool
    stInNote WriterState
    st) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s{ stVerbInNote :: Bool
    stVerbInNote = Bool
    True })
                      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s{ stHighlighting :: Bool
    stHighlighting = Bool
    True })
                      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Doc Text
    forall a. Doc a -> Doc a
    flush (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    linkAnchor Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Text -> String
    T.unpack Text
    h))
      case () of
         _ | Extension -> WriterOptions -> Bool
    forall a. HasSyntaxExtensions a => Extension -> a -> Bool
    isEnabled Extension
    Ext_literate_haskell WriterOptions
    opts Bool -> Bool -> Bool
    && "haskell" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes Bool -> Bool -> Bool
    &&
             "literate" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes           -> LW m (Doc Text)
    lhsCodeBlock
           | WriterOptions -> Bool
    writerListings WriterOptions
    opts                 -> LW m (Doc Text)
    listingsCodeBlock
           | Bool -> Bool
    not ([Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    classes) Bool -> Bool -> Bool
    && Maybe Style -> Bool
    forall a. Maybe a -> Bool
    isJust (WriterOptions -> Maybe Style
    writerHighlightStyle WriterOptions
    opts)
                                                 -> LW m (Doc Text)
    highlightedCodeBlock
           | Bool
    otherwise                           -> LW m (Doc Text)
    rawCodeBlock
    blockToLaTeX b :: Block
    b@(RawBlock f :: Format
    f x :: Text
    x) = do
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      if Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "latex" Bool -> Bool -> Bool
    || Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "tex" Bool -> Bool -> Bool
    ||
           (Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "beamer" Bool -> Bool -> Bool
    && Bool
    beamer)
         then Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    x
         else do
           LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Block -> LogMessage
    BlockNotRendered Block
    b
           Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    blockToLaTeX (BulletList []) = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty  -- otherwise latex error
    blockToLaTeX (BulletList lst :: [[Block]]
    lst) = do
      Bool
    incremental <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stIncremental
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      let inc :: String
    inc = if Bool
    beamer Bool -> Bool -> Bool
    && Bool
    incremental then "[<+->]" else ""
      [Doc Text]
    items <- ([Block] -> LW m (Doc Text))
    -> [[Block]] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    listItemToLaTeX [[Block]]
    lst
      let spacing :: Doc Text
    spacing = if [[Block]] -> Bool
    isTightList [[Block]]
    lst
                       then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\tightlist"
                       else Doc Text
    forall a. Doc a
    empty
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text ("\\begin{itemize}" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    inc) Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    spacing Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat [Doc Text]
    items Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                 "\\end{itemize}"
    blockToLaTeX (OrderedList _ []) = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty -- otherwise latex error
    blockToLaTeX (OrderedList (start :: Int
    start, numstyle :: ListNumberStyle
    numstyle, numdelim :: ListNumberDelim
    numdelim) lst :: [[Block]]
    lst) = do
      WriterState
    st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
      let inc :: String
    inc = if WriterState -> Bool
    stBeamer WriterState
    st Bool -> Bool -> Bool
    && WriterState -> Bool
    stIncremental WriterState
    st then "[<+->]" else ""
      let oldlevel :: Int
    oldlevel = WriterState -> Int
    stOLLevel WriterState
    st
      WriterState -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => s -> m ()
    put (WriterState -> StateT WriterState m ())
    -> WriterState -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ WriterState
    st {stOLLevel :: Int
    stOLLevel = Int
    oldlevel Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1}
      [Doc Text]
    items <- ([Block] -> LW m (Doc Text))
    -> [[Block]] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    listItemToLaTeX [[Block]]
    lst
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s {stOLLevel :: Int
    stOLLevel = Int
    oldlevel})
      let beamer :: Bool
    beamer = WriterState -> Bool
    stBeamer WriterState
    st
      let tostyle :: Doc a -> Doc a
    tostyle x :: Doc a
    x = case ListNumberStyle
    numstyle of
                           Decimal      -> "\\arabic" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           UpperRoman   -> "\\Roman" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           LowerRoman   -> "\\roman" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           UpperAlpha   -> "\\Alph" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           LowerAlpha   -> "\\alph" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           Example      -> "\\arabic" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           DefaultStyle -> "\\arabic" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
      let todelim :: Doc a -> Doc a
    todelim x :: Doc a
    x = case ListNumberDelim
    numdelim of
                           OneParen  -> Doc a
    x Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> ")"
                           TwoParens -> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    parens Doc a
    x
                           Period    -> Doc a
    x Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> "."
                           _         -> Doc a
    x Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> "."
      let exemplar :: Doc Text
    exemplar = case ListNumberStyle
    numstyle of
                           Decimal      -> "1"
                           UpperRoman   -> "I"
                           LowerRoman   -> "i"
                           UpperAlpha   -> "A"
                           LowerAlpha   -> "a"
                           Example      -> "1"
                           DefaultStyle -> "1"
      let enum :: Doc Text
    enum = Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ "enum" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    T.toLower (Int -> Text
    toRomanNumeral Int
    oldlevel)
      let stylecommand :: Doc Text
    stylecommand
            | ListNumberStyle
    numstyle ListNumberStyle -> ListNumberStyle -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberStyle
    DefaultStyle Bool -> Bool -> Bool
    && ListNumberDelim
    numdelim ListNumberDelim -> ListNumberDelim -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberDelim
    DefaultDelim = Doc Text
    forall a. Doc a
    empty
            | Bool
    beamer Bool -> Bool -> Bool
    && ListNumberStyle
    numstyle ListNumberStyle -> ListNumberStyle -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberStyle
    Decimal Bool -> Bool -> Bool
    && ListNumberDelim
    numdelim ListNumberDelim -> ListNumberDelim -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberDelim
    Period = Doc Text
    forall a. Doc a
    empty
            | Bool
    beamer = Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    todelim Doc Text
    exemplar)
            | Bool
    otherwise = "\\def" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\label" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    enum Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
              Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    todelim (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    tostyle Doc Text
    enum)
      let resetcounter :: Doc Text
    resetcounter = if Int
    start Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1 Bool -> Bool -> Bool
    || Int
    oldlevel Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > 4
                            then Doc Text
    forall a. Doc a
    empty
                            else "\\setcounter" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    enum Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                  Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (String -> Doc Text) -> String -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Int -> String
    forall a. Show a => a -> String
    show (Int -> String) -> Int -> String
    forall a b. (a -> b) -> a -> b
    $ Int
    start Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 1)
      let spacing :: Doc Text
    spacing = if [[Block]] -> Bool
    isTightList [[Block]]
    lst
                       then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\tightlist"
                       else Doc Text
    forall a. Doc a
    empty
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text ("\\begin{enumerate}" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    inc)
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    stylecommand
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    resetcounter
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    spacing
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat [Doc Text]
    items
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\end{enumerate}"
    blockToLaTeX (DefinitionList []) = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    blockToLaTeX (DefinitionList lst :: [([Inline], [[Block]])]
    lst) = do
      Bool
    incremental <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stIncremental
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      let inc :: String
    inc = if Bool
    beamer Bool -> Bool -> Bool
    && Bool
    incremental then "[<+->]" else ""
      [Doc Text]
    items <- (([Inline], [[Block]]) -> LW m (Doc Text))
    -> [([Inline], [[Block]])] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM ([Inline], [[Block]]) -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    ([Inline], [[Block]]) -> LW m (Doc Text)
    defListItemToLaTeX [([Inline], [[Block]])]
    lst
      let spacing :: Doc Text
    spacing = if (([Inline], [[Block]]) -> Bool) -> [([Inline], [[Block]])] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all ([[Block]] -> Bool
    isTightList ([[Block]] -> Bool)
    -> (([Inline], [[Block]]) -> [[Block]])
    -> ([Inline], [[Block]])
    -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ([Inline], [[Block]]) -> [[Block]]
    forall a b. (a, b) -> b
    snd) [([Inline], [[Block]])]
    lst
                       then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\tightlist"
                       else Doc Text
    forall a. Doc a
    empty
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text ("\\begin{description}" String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    inc) Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    spacing Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat [Doc Text]
    items Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                   "\\end{description}"
    blockToLaTeX HorizontalRule =
                Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return
      "\\begin{center}\\rule{0.5\\linewidth}{0.5pt}\\end{center}"
    blockToLaTeX (Header level :: Int
    level (id' :: Text
    id',classes :: [Text]
    classes,_) lst :: [Inline]
    lst) = do
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{stInHeading :: Bool
    stInHeading = Bool
    True}
      Doc Text
    hdr <- [Text] -> Text -> Int -> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [Text] -> Text -> Int -> [Inline] -> LW m (Doc Text)
    sectionHeader [Text]
    classes Text
    id' Int
    level [Inline]
    lst
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{stInHeading :: Bool
    stInHeading = Bool
    False}
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    hdr
    blockToLaTeX (Table caption :: [Inline]
    caption aligns :: [Alignment]
    aligns widths :: [Double]
    widths heads :: [[Block]]
    heads rows :: [[[Block]]]
    rows) = do
      (captionText :: Doc Text
    captionText, captForLof :: Doc Text
    captForLof, captNotes :: Doc Text
    captNotes) <- Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
    getCaption Bool
    False [Inline]
    caption
      let toHeaders :: [[Block]] -> StateT WriterState m (Doc Text)
    toHeaders hs :: [[Block]]
    hs = do Doc Text
    contents <- Bool
    -> [Alignment]
    -> [Double]
    -> [[Block]]
    -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Alignment] -> [Double] -> [[Block]] -> LW m (Doc Text)
    tableRowToLaTeX Bool
    True [Alignment]
    aligns [Double]
    widths [[Block]]
    hs
                            Doc Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return ("\\toprule" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\midrule")
      let removeNote :: Inline -> Inline
    removeNote (Note _) = Attr -> [Inline] -> Inline
    Span ("", [], []) []
          removeNote x :: Inline
    x        = Inline
    x
      Doc Text
    firsthead <- if Doc Text -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc Text
    captionText Bool -> Bool -> Bool
    || ([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]]
    heads
                      then Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
                      else (Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\endfirsthead") (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [[Block]] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [[Block]] -> StateT WriterState m (Doc Text)
    toHeaders [[Block]]
    heads
      Doc Text
    head' <- if ([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]]
    heads
                  then Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return "\\toprule"
                  -- avoid duplicate notes in head and firsthead:
                  else [[Block]] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [[Block]] -> StateT WriterState m (Doc Text)
    toHeaders (if Doc Text -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc Text
    firsthead
                                     then [[Block]]
    heads
                                     else (Inline -> Inline) -> [[Block]] -> [[Block]]
    forall a b. Walkable a b => (a -> a) -> b -> b
    walk Inline -> Inline
    removeNote [[Block]]
    heads)
      let capt :: Doc Text
    capt = if Doc Text -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc Text
    captionText
                    then Doc Text
    forall a. Doc a
    empty
                    else "\\caption" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    captForLof Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    captionText
                             Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\tabularnewline"
      [Doc Text]
    rows' <- ([[Block]] -> LW m (Doc Text))
    -> [[[Block]]] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (Bool -> [Alignment] -> [Double] -> [[Block]] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Alignment] -> [Double] -> [[Block]] -> LW m (Doc Text)
    tableRowToLaTeX Bool
    False [Alignment]
    aligns [Double]
    widths) [[[Block]]]
    rows
      let colDescriptors :: Doc Text
    colDescriptors = Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ [Text] -> Text
    T.concat ([Text] -> Text) -> [Text] -> Text
    forall a b. (a -> b) -> a -> b
    $ (Alignment -> Text) -> [Alignment] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Alignment -> Text
    toColDescriptor [Alignment]
    aligns
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stTable :: Bool
    stTable = Bool
    True }
      Doc Text
    notes <- [Doc Text] -> Doc Text
    notesToLaTeX ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (WriterState -> [Doc Text]) -> StateT WriterState m [Doc Text]
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> [Doc Text]
    stNotes
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\begin{longtable}[]" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                  Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces ("@{}" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    colDescriptors Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "@{}")
                  -- the @{} removes extra space at beginning and end
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    capt
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    firsthead
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    head'
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\endhead"
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat [Doc Text]
    rows'
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\bottomrule"
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "\\end{longtable}"
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    captNotes
             Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    notes

    getCaption :: PandocMonad m
               => Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
    getCaption :: Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
    getCaption externalNotes :: Bool
    externalNotes txt :: [Inline]
    txt = do
      Bool
    oldExternalNotes <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stExternalNotes
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stExternalNotes :: Bool
    stExternalNotes = Bool
    externalNotes, stNotes :: [Doc Text]
    stNotes = [] }
      Doc Text
    capt <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    txt
      Doc Text
    footnotes <- if Bool
    externalNotes
                      then [Doc Text] -> Doc Text
    notesToLaTeX ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (WriterState -> [Doc Text]) -> StateT WriterState m [Doc Text]
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> [Doc Text]
    stNotes
                      else Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stExternalNotes :: Bool
    stExternalNotes = Bool
    oldExternalNotes, stNotes :: [Doc Text]
    stNotes = [] }
      -- We can't have footnotes in the list of figures/tables, so remove them:
      let getNote :: Inline -> Any
    getNote (Note _) = Bool -> Any
    Any Bool
    True
          getNote _        = Bool -> Any
    Any Bool
    False
      let hasNotes :: [Inline] -> Bool
    hasNotes = Any -> Bool
    getAny (Any -> Bool) -> ([Inline] -> Any) -> [Inline] -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Inline -> Any) -> [Inline] -> Any
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query Inline -> Any
    getNote
      Doc Text
    captForLof <- if [Inline] -> Bool
    hasNotes [Inline]
    txt
                       then Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX ((Inline -> Inline) -> [Inline] -> [Inline]
    forall a b. Walkable a b => (a -> a) -> b -> b
    walk Inline -> Inline
    deNote [Inline]
    txt)
                       else Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
      (Doc Text, Doc Text, Doc Text)
    -> LW m (Doc Text, Doc Text, Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text
    capt, Doc Text
    captForLof, Doc Text
    footnotes)

    toColDescriptor :: Alignment -> Text
    toColDescriptor :: Alignment -> Text
    toColDescriptor align :: Alignment
    align =
      case Alignment
    align of
             AlignLeft    -> "l"
             AlignRight   -> "r"
             AlignCenter  -> "c"
             AlignDefault -> "l"

    blockListToLaTeX :: PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX :: [Block] -> LW m (Doc Text)
    blockListToLaTeX lst :: [Block]
    lst =
      [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vsep ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    `fmap` (Block -> LW m (Doc Text))
    -> [Block] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (\b :: Block
    b -> Bool -> LW m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    True LW m () -> LW m (Doc Text) -> LW m (Doc Text)
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Block -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX Block
    b) [Block]
    lst

    tableRowToLaTeX :: PandocMonad m
                    => Bool
                    -> [Alignment]
                    -> [Double]
                    -> [[Block]]
                    -> LW m (Doc Text)
    tableRowToLaTeX :: Bool -> [Alignment] -> [Double] -> [[Block]] -> LW m (Doc Text)
    tableRowToLaTeX header :: Bool
    header aligns :: [Alignment]
    aligns widths :: [Double]
    widths cols :: [[Block]]
    cols = do
      -- scale factor compensates for extra space between columns
      -- so the whole table isn't larger than columnwidth
      let scaleFactor :: Double
    scaleFactor = 0.97 Double -> Double -> Double
    forall a. Floating a => a -> a -> a
    ** Int -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral ([Alignment] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Alignment]
    aligns)
      let isSimple :: [Block] -> Bool
    isSimple [Plain _] = Bool
    True
          isSimple [Para  _] = Bool
    True
          isSimple []        = Bool
    True
          isSimple _         = Bool
    False
      -- simple tables have to have simple cells:
      let widths' :: [Double]
    widths' = if (Double -> Bool) -> [Double] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all (Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == 0) [Double]
    widths Bool -> Bool -> Bool
    && Bool -> Bool
    not (([Block] -> Bool) -> [[Block]] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all [Block] -> Bool
    isSimple [[Block]]
    cols)
                       then Int -> Double -> [Double]
    forall a. Int -> a -> [a]
    replicate ([Alignment] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Alignment]
    aligns)
                              (Double
    scaleFactor Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Int -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral ([Alignment] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Alignment]
    aligns))
                       else (Double -> Double) -> [Double] -> [Double]
    forall a b. (a -> b) -> [a] -> [b]
    map (Double
    scaleFactor Double -> Double -> Double
    forall a. Num a => a -> a -> a
    *) [Double]
    widths
      [Doc Text]
    cells <- ((Double, Alignment, [Block]) -> LW m (Doc Text))
    -> [(Double, Alignment, [Block])]
    -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (Bool -> (Double, Alignment, [Block]) -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> (Double, Alignment, [Block]) -> LW m (Doc Text)
    tableCellToLaTeX Bool
    header) ([(Double, Alignment, [Block])] -> StateT WriterState m [Doc Text])
    -> [(Double, Alignment, [Block])]
    -> StateT WriterState m [Doc Text]
    forall a b. (a -> b) -> a -> b
    $ [Double]
    -> [Alignment] -> [[Block]] -> [(Double, Alignment, [Block])]
    forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
    zip3 [Double]
    widths' [Alignment]
    aligns [[Block]]
    cols
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    hsep (Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse "&" [Doc Text]
    cells) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\tabularnewline"

    -- For simple latex tables (without minipages or parboxes),
    -- we need to go to some lengths to get line breaks working:
    -- as LineBreak bs = \vtop{\hbox{\strut as}\hbox{\strut bs}}.
    fixLineBreaks :: Block -> Block
    fixLineBreaks :: Block -> Block
    fixLineBreaks (Para ils :: [Inline]
    ils)  = [Inline] -> Block
    Para ([Inline] -> Block) -> [Inline] -> Block
    forall a b. (a -> b) -> a -> b
    $ [Inline] -> [Inline]
    fixLineBreaks' [Inline]
    ils
    fixLineBreaks (Plain ils :: [Inline]
    ils) = [Inline] -> Block
    Plain ([Inline] -> Block) -> [Inline] -> Block
    forall a b. (a -> b) -> a -> b
    $ [Inline] -> [Inline]
    fixLineBreaks' [Inline]
    ils
    fixLineBreaks x :: Block
    x           = Block
    x

    fixLineBreaks' :: [Inline] -> [Inline]
    fixLineBreaks' :: [Inline] -> [Inline]
    fixLineBreaks' ils :: [Inline]
    ils = case (Inline -> Bool) -> [Inline] -> [[Inline]]
    forall a. (a -> Bool) -> [a] -> [[a]]
    splitBy (Inline -> Inline -> Bool
    forall a. Eq a => a -> a -> Bool
    == Inline
    LineBreak) [Inline]
    ils of
                           []     -> []
                           [xs :: [Inline]
    xs]   -> [Inline]
    xs
                           chunks :: [[Inline]]
    chunks -> Format -> Text -> Inline
    RawInline "tex" "\\vtop{" Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :
                                     ([Inline] -> [Inline]) -> [[Inline]] -> [Inline]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap [Inline] -> [Inline]
    tohbox [[Inline]]
    chunks [Inline] -> [Inline] -> [Inline]
    forall a. Semigroup a => a -> a -> a
    <>
                                     [Format -> Text -> Inline
    RawInline "tex" "}"]
      where tohbox :: [Inline] -> [Inline]
    tohbox ys :: [Inline]
    ys = Format -> Text -> Inline
    RawInline "tex" "\\hbox{\\strut " Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline]
    ys [Inline] -> [Inline] -> [Inline]
    forall a. Semigroup a => a -> a -> a
    <>
                        [Format -> Text -> Inline
    RawInline "tex" "}"]

    -- We also change display math to inline math, since display
    -- math breaks in simple tables.
    displayMathToInline :: Inline -> Inline
    displayMathToInline :: Inline -> Inline
    displayMathToInline (Math DisplayMath x :: Text
    x) = MathType -> Text -> Inline
    Math MathType
    InlineMath Text
    x
    displayMathToInline x :: Inline
    x                    = Inline
    x

    tableCellToLaTeX :: PandocMonad m => Bool -> (Double, Alignment, [Block])
                     -> LW m (Doc Text)
    tableCellToLaTeX :: Bool -> (Double, Alignment, [Block]) -> LW m (Doc Text)
    tableCellToLaTeX _      (0,     _,     blocks :: [Block]
    blocks) =
      [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX ([Block] -> LW m (Doc Text)) -> [Block] -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ (Block -> Block) -> [Block] -> [Block]
    forall a b. Walkable a b => (a -> a) -> b -> b
    walk Block -> Block
    fixLineBreaks ([Block] -> [Block]) -> [Block] -> [Block]
    forall a b. (a -> b) -> a -> b
    $ (Inline -> Inline) -> [Block] -> [Block]
    forall a b. Walkable a b => (a -> a) -> b -> b
    walk Inline -> Inline
    displayMathToInline [Block]
    blocks
    tableCellToLaTeX header :: Bool
    header (width :: Double
    width, align :: Alignment
    align, blocks :: [Block]
    blocks) = do
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      Bool
    externalNotes <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stExternalNotes
      Bool
    inMinipage <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInMinipage
      -- See #5367 -- footnotehyper/footnote don't work in beamer,
      -- so we need to produce the notes outside the table...
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stExternalNotes :: Bool
    stExternalNotes = Bool
    beamer,
                          stInMinipage :: Bool
    stInMinipage = Bool
    True }
      Doc Text
    cellContents <- [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    blocks
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stExternalNotes :: Bool
    stExternalNotes = Bool
    externalNotes,
                          stInMinipage :: Bool
    stInMinipage = Bool
    inMinipage }
      let valign :: Doc Text
    valign = String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (String -> Doc Text) -> String -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ if Bool
    header then "[b]" else "[t]"
      let halign :: Doc Text
    halign = case Alignment
    align of
                   AlignLeft    -> "\\raggedright"
                   AlignRight   -> "\\raggedleft"
                   AlignCenter  -> "\\centering"
                   AlignDefault -> "\\raggedright"
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\begin{minipage}" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    valign Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
               Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (String -> Double -> String
    forall r. PrintfType r => String -> r
    printf "%.2f\\columnwidth" Double
    width)) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
               Doc Text
    halign Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    cellContents Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\strut" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
               "\\end{minipage}"

    notesToLaTeX :: [Doc Text] -> Doc Text
    notesToLaTeX :: [Doc Text] -> Doc Text
    notesToLaTeX [] = Doc Text
    forall a. Doc a
    empty
    notesToLaTeX ns :: [Doc Text]
    ns = (case [Doc Text] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Doc Text]
    ns of
                                  n :: Int
    n | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > 1 -> "\\addtocounter" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                               Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces "footnote" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                               Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (String -> Doc Text) -> String -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Int -> String
    forall a. Show a => a -> String
    show (Int -> String) -> Int -> String
    forall a b. (a -> b) -> a -> b
    $ 1 Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    n)
                                    | Bool
    otherwise -> Doc Text
    forall a. Doc a
    empty)
                       Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                       [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat (Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse
                         ("\\addtocounter" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces "footnote" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces "1")
                         ([Doc Text] -> [Doc Text]) -> [Doc Text] -> [Doc Text]
    forall a b. (a -> b) -> a -> b
    $ (Doc Text -> Doc Text) -> [Doc Text] -> [Doc Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\x :: Doc Text
    x -> "\\footnotetext" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    x)
                         ([Doc Text] -> [Doc Text]) -> [Doc Text] -> [Doc Text]
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> [Doc Text]
    forall a. [a] -> [a]
    reverse [Doc Text]
    ns)

    listItemToLaTeX :: PandocMonad m => [Block] -> LW m (Doc Text)
    listItemToLaTeX :: [Block] -> LW m (Doc Text)
    listItemToLaTeX lst :: [Block]
    lst
      -- we need to put some text before a header if it's the first
      -- element in an item. This will look ugly in LaTeX regardless, but
      -- this will keep the typesetter from throwing an error.
      | (Header{} :_) <- [Block]
    lst =
        (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\item ~" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$) (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest 2 (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    lst
      | Plain (Str "☐":Space:is :: [Inline]
    is) : bs :: [Block]
    bs <- [Block]
    lst = Bool -> [Inline] -> [Block] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Inline] -> [Block] -> StateT WriterState m (Doc Text)
    taskListItem Bool
    False [Inline]
    is [Block]
    bs
      | Plain (Str "☒":Space:is :: [Inline]
    is) : bs :: [Block]
    bs <- [Block]
    lst = Bool -> [Inline] -> [Block] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Inline] -> [Block] -> StateT WriterState m (Doc Text)
    taskListItem Bool
    True  [Inline]
    is [Block]
    bs
      | Para  (Str "☐":Space:is :: [Inline]
    is) : bs :: [Block]
    bs <- [Block]
    lst = Bool -> [Inline] -> [Block] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Inline] -> [Block] -> StateT WriterState m (Doc Text)
    taskListItem Bool
    False [Inline]
    is [Block]
    bs
      | Para  (Str "☒":Space:is :: [Inline]
    is) : bs :: [Block]
    bs <- [Block]
    lst = Bool -> [Inline] -> [Block] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Inline] -> [Block] -> StateT WriterState m (Doc Text)
    taskListItem Bool
    True  [Inline]
    is [Block]
    bs
      | Bool
    otherwise = (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$) (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest 2 (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    lst
      where
        taskListItem :: Bool -> [Inline] -> [Block] -> StateT WriterState m (Doc Text)
    taskListItem checked :: Bool
    checked is :: [Inline]
    is bs :: [Block]
    bs = do
          let checkbox :: Doc Text
    checkbox  = if Bool
    checked
                          then "$\\boxtimes$"
                          else "$\\square$"
          Doc Text
    isContents <- [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    is
          Doc Text
    bsContents <- [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    bs
          Doc Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> StateT WriterState m (Doc Text))
    -> Doc Text -> StateT WriterState m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    checkbox
            Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest 2 (Doc Text
    isContents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $+$ Doc Text
    bsContents)

    defListItemToLaTeX :: PandocMonad m => ([Inline], [[Block]]) -> LW m (Doc Text)
    defListItemToLaTeX :: ([Inline], [[Block]]) -> LW m (Doc Text)
    defListItemToLaTeX (term :: [Inline]
    term, defs :: [[Block]]
    defs) = do
        -- needed to turn off 'listings' because it breaks inside \item[...]:
        (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{stInItem :: Bool
    stInItem = Bool
    True}
        Doc Text
    term' <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    term
        (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{stInItem :: Bool
    stInItem = Bool
    False}
        -- put braces around term if it contains an internal link,
        -- since otherwise we get bad bracket interactions: \item[\hyperref[..]
        let isInternalLink :: Inline -> Bool
    isInternalLink (Link _ _ (src :: Text
    src,_))
              | Just ('#', _) <- Text -> Maybe (Char, Text)
    T.uncons Text
    src = Bool
    True
            isInternalLink _                  = Bool
    False
        let term'' :: Doc Text
    term'' = if (Inline -> Bool) -> [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any Inline -> Bool
    isInternalLink [Inline]
    term
                        then Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    term'
                        else Doc Text
    term'
        Doc Text
    def'  <- ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> LW m (Doc Text)
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vsep (StateT WriterState m [Doc Text] -> LW m (Doc Text))
    -> StateT WriterState m [Doc Text] -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ ([Block] -> LW m (Doc Text))
    -> [[Block]] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [[Block]]
    defs
        Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ case [[Block]]
    defs of
         ((Header{} : _) : _)    ->
           "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    term'' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> " ~ " Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    def'
         ((CodeBlock{} : _) : _) -> -- see #4662
           "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    term'' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> " ~ " Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    def'
         _                       ->
           "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    term'' Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    def'

    -- | Craft the section header, inserting the secton reference, if supplied.
    sectionHeader :: PandocMonad m
                  => [Text]  -- classes
                  -> Text
                  -> Int
                  -> [Inline]
                  -> LW m (Doc Text)
    sectionHeader :: [Text] -> Text -> Int -> [Inline] -> LW m (Doc Text)
    sectionHeader classes :: [Text]
    classes ident :: Text
    ident level :: Int
    level lst :: [Inline]
    lst = do
      let unnumbered :: Bool
    unnumbered = "unnumbered" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
      let unlisted :: Bool
    unlisted = "unlisted" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
      Doc Text
    txt <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
      Text
    plain <- StringContext -> Text -> LW m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString (Text -> LW m Text) -> Text -> LW m Text
    forall a b. (a -> b) -> a -> b
    $ [Text] -> Text
    T.concat ([Text] -> Text) -> [Text] -> Text
    forall a b. (a -> b) -> a -> b
    $ (Inline -> Text) -> [Inline] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Inline -> Text
    forall a. Walkable Inline a => a -> Text
    stringify [Inline]
    lst
      let removeInvalidInline :: Inline -> [Inline]
    removeInvalidInline (Note _)             = []
          removeInvalidInline (Span (id' :: Text
    id', _, _) _) | Bool -> Bool
    not (Text -> Bool
    T.null Text
    id') = []
          removeInvalidInline Image{}            = []
          removeInvalidInline x :: Inline
    x                    = [Inline
    x]
      let lstNoNotes :: [Inline]
    lstNoNotes = (Inline -> [Inline] -> [Inline])
    -> [Inline] -> [Inline] -> [Inline]
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr ([Inline] -> [Inline] -> [Inline]
    forall a. Monoid a => a -> a -> a
    mappend ([Inline] -> [Inline] -> [Inline])
    -> (Inline -> [Inline]) -> Inline -> [Inline] -> [Inline]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (\x :: Inline
    x -> (Inline -> [Inline]) -> Inline -> [Inline]
    forall a b (m :: * -> *).
    (Walkable a b, Monad m, Applicative m, Functor m) =>
    (a -> m a) -> b -> m b
    walkM Inline -> [Inline]
    removeInvalidInline Inline
    x)) [Inline]
    forall a. Monoid a => a
    mempty [Inline]
    lst
      Doc Text
    txtNoNotes <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lstNoNotes
      -- footnotes in sections don't work (except for starred variants)
      -- unless you specify an optional argument:
      -- \section[mysec]{mysec\footnote{blah}}
      Doc Text
    optional <- if Bool
    unnumbered Bool -> Bool -> Bool
    || [Inline]
    lstNoNotes [Inline] -> [Inline] -> Bool
    forall a. Eq a => a -> a -> Bool
    == [Inline]
    lst Bool -> Bool -> Bool
    || [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    lstNoNotes
                     then Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
                     else
                       Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    txtNoNotes
      let contents :: Doc Text
    contents = if Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing Doc Text
    txt Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    plain
                        then Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    txt
                        else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\texorpdfstring"
                             Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    txt
                             Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    plain))
      Bool
    book <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stHasChapters
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      let topLevelDivision :: TopLevelDivision
    topLevelDivision = if Bool
    book Bool -> Bool -> Bool
    && WriterOptions -> TopLevelDivision
    writerTopLevelDivision WriterOptions
    opts TopLevelDivision -> TopLevelDivision -> Bool
    forall a. Eq a => a -> a -> Bool
    == TopLevelDivision
    TopLevelDefault
                             then TopLevelDivision
    TopLevelChapter
                             else WriterOptions -> TopLevelDivision
    writerTopLevelDivision WriterOptions
    opts
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      let level' :: Int
    level' = if Bool
    beamer Bool -> Bool -> Bool
    &&
                      TopLevelDivision
    topLevelDivision TopLevelDivision -> [TopLevelDivision] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [TopLevelDivision
    TopLevelPart, TopLevelDivision
    TopLevelChapter]
                   -- beamer has parts but no chapters
                   then if Int
    level Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1 then -1 else Int
    level Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 1
                   else case TopLevelDivision
    topLevelDivision of
                          TopLevelPart    -> Int
    level Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 2
                          TopLevelChapter -> Int
    level Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 1
                          TopLevelSection -> Int
    level
                          TopLevelDefault -> Int
    level
      let sectionType :: String
    sectionType = case Int
    level' of
                              -1 -> "part"
                              0  -> "chapter"
                              1  -> "section"
                              2  -> "subsection"
                              3  -> "subsubsection"
                              4  -> "paragraph"
                              5  -> "subparagraph"
                              _  -> ""
      Bool
    inQuote <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInQuote
      let prefix :: Doc Text
    prefix = if Bool
    inQuote Bool -> Bool -> Bool
    && Int
    level' Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 4
                      then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\mbox{}%"
                      -- needed for \paragraph, \subparagraph in quote environment
                      -- see http://tex.stackexchange.com/questions/169830/
                      else Doc Text
    forall a. Doc a
    empty
      Doc Text
    lab <- Text -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Text -> LW m (Doc Text)
    labelFor Text
    ident
      let star :: Doc Text
    star = if Bool
    unnumbered then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "*" else Doc Text
    forall a. Doc a
    empty
      let stuffing :: Doc Text
    stuffing = Doc Text
    star Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    optional Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    contents
      Doc Text
    stuffing' <- Bool -> Text -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> Text -> Doc Text -> LW m (Doc Text)
    hypertarget Bool
    True Text
    ident (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $
                      String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text ('\\'Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    sectionType) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    stuffing Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    lab
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ if Int
    level' Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > 5
                  then Doc Text
    txt
                  else Doc Text
    prefix Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    stuffing'
                       Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ if Bool
    unnumbered Bool -> Bool -> Bool
    && Bool -> Bool
    not Bool
    unlisted
                             then "\\addcontentsline{toc}" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                    Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    sectionType) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                    Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    txtNoNotes
                             else Doc Text
    forall a. Doc a
    empty

    wrapDiv :: PandocMonad m => Attr -> Doc Text -> LW m (Doc Text)
    wrapDiv :: Attr -> Doc Text -> LW m (Doc Text)
    wrapDiv (_,classes :: [Text]
    classes,kvs :: [(Text, Text)]
    kvs) t :: Doc Text
    t = do
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      let align :: Doc Text -> Doc Text -> Doc Text
    align dir :: Doc Text
    dir txt :: Doc Text
    txt = Text -> Doc Text -> Doc Text
    inCmd "begin" Doc Text
    dir Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    txt Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text -> Doc Text
    inCmd "end" Doc Text
    dir
      Maybe Lang
    lang <- Maybe Text -> StateT WriterState m (Maybe Lang)
    forall (m :: * -> *). PandocMonad m => Maybe Text -> m (Maybe Lang)
    toLang (Maybe Text -> StateT WriterState m (Maybe Lang))
    -> Maybe Text -> StateT WriterState m (Maybe Lang)
    forall a b. (a -> b) -> a -> b
    $ Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "lang" [(Text, Text)]
    kvs
      let wrapColumns :: Doc Text -> Doc Text
    wrapColumns = if Bool
    beamer Bool -> Bool -> Bool
    && "columns" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                        then \contents :: Doc Text
    contents ->
                               Text -> Doc Text -> Doc Text
    inCmd "begin" "columns" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets "T"
                               Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    contents
                               Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text -> Doc Text
    inCmd "end" "columns"
                        else Doc Text -> Doc Text
    forall a. a -> a
    id
          wrapColumn :: Doc Text -> Doc Text
    wrapColumn  = if Bool
    beamer Bool -> Bool -> Bool
    && "column" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                        then \contents :: Doc Text
    contents ->
                               let w :: Text
    w = Text -> (Text -> Text) -> Maybe Text -> Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe "0.48" Text -> Text
    fromPct (Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "width" [(Text, Text)]
    kvs)
                               in  Text -> Doc Text -> Doc Text
    inCmd "begin" "column" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                   Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    w Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\textwidth")
                                   Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    contents
                                   Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text -> Doc Text
    inCmd "end" "column"
                        else Doc Text -> Doc Text
    forall a. a -> a
    id
          fromPct :: Text -> Text
    fromPct xs :: Text
    xs =
            case Text -> Maybe (Text, Char)
    T.unsnoc Text
    xs of
              Just (ds :: Text
    ds, '%') -> case Text -> Maybe Double
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead Text
    ds of
                                  Just digits :: Double
    digits -> Double -> Text
    forall a. RealFloat a => a -> Text
    showFl (Double
    digits Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / 100 :: Double)
                                  Nothing -> Text
    xs
              _              -> Text
    xs
          wrapDir :: Doc Text -> Doc Text
    wrapDir = case Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "dir" [(Text, Text)]
    kvs of
                      Just "rtl" -> Doc Text -> Doc Text -> Doc Text
    align "RTL"
                      Just "ltr" -> Doc Text -> Doc Text -> Doc Text
    align "LTR"
                      _          -> Doc Text -> Doc Text
    forall a. a -> a
    id
          wrapLang :: Doc Text -> Doc Text
    wrapLang txt :: Doc Text
    txt = case Maybe Lang
    lang of
                           Just lng :: Lang
    lng -> let (l :: Text
    l, o :: Text
    o) = Lang -> (Text, Text)
    toPolyglossiaEnv Lang
    lng
                                           ops :: Doc Text
    ops = if Text -> Bool
    T.null Text
    o
                                                 then ""
                                                 else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    o
                                       in  Text -> Doc Text -> Doc Text
    inCmd "begin" (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    l) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    ops
                                           Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    txt Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    blankline
                                           Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text -> Doc Text
    inCmd "end" (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    l)
                           Nothing  -> Doc Text
    txt
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    wrapColumns (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Doc Text -> Doc Text
    wrapColumn (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Doc Text -> Doc Text
    wrapDir (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Doc Text -> Doc Text
    wrapLang (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    t

    hypertarget :: PandocMonad m => Bool -> Text -> Doc Text -> LW m (Doc Text)
    hypertarget :: Bool -> Text -> Doc Text -> LW m (Doc Text)
    hypertarget _ "" x :: Doc Text
    x    = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    x
    hypertarget addnewline :: Bool
    addnewline ident :: Text
    ident x :: Doc Text
    x = do
      Doc Text
    ref <- Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> StateT WriterState m Text -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    `fmap` Text -> StateT WriterState m Text
    forall (m :: * -> *). PandocMonad m => Text -> LW m Text
    toLabel Text
    ident
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\hypertarget"
                  Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    ref
                  Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces ((if Bool
    addnewline Bool -> Bool -> Bool
    && Bool -> Bool
    not (Doc Text -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc Text
    x)
                                 then "%" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr
                                 else Doc Text
    forall a. Doc a
    empty) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    x)

    labelFor :: PandocMonad m => Text -> LW m (Doc Text)
    labelFor :: Text -> LW m (Doc Text)
    labelFor ""    = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    labelFor ident :: Text
    ident = do
      Doc Text
    ref <- Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> StateT WriterState m Text -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    `fmap` Text -> StateT WriterState m Text
    forall (m :: * -> *). PandocMonad m => Text -> LW m Text
    toLabel Text
    ident
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\label" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    ref

    -- | Convert list of inline elements to LaTeX.
    inlineListToLaTeX :: PandocMonad m
                      => [Inline]  -- ^ Inlines to convert
                      -> LW m (Doc Text)
    inlineListToLaTeX :: [Inline] -> LW m (Doc Text)
    inlineListToLaTeX lst :: [Inline]
    lst = [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    hcat ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
      (Inline -> LW m (Doc Text))
    -> [Inline] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM Inline -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Inline -> LW m (Doc Text)
    inlineToLaTeX ([Inline] -> [Inline]
    fixLineInitialSpaces ([Inline] -> [Inline])
    -> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> [Inline]
    fixInitialLineBreaks ([Inline] -> [Inline]) -> [Inline] -> [Inline]
    forall a b. (a -> b) -> a -> b
    $ [Inline]
    lst)
        -- nonbreaking spaces (~) in LaTeX don't work after line breaks,
        -- so we turn nbsps after hard breaks to \hspace commands.
        -- this is mostly used in verse.
     where fixLineInitialSpaces :: [Inline] -> [Inline]
    fixLineInitialSpaces [] = []
           fixLineInitialSpaces (LineBreak : Str s :: Text
    s : xs :: [Inline]
    xs)
             | Just ('\160', _) <- Text -> Maybe (Char, Text)
    T.uncons Text
    s
             = Inline
    LineBreak Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Text -> [Inline]
    fixNbsps Text
    s [Inline] -> [Inline] -> [Inline]
    forall a. Semigroup a => a -> a -> a
    <> [Inline] -> [Inline]
    fixLineInitialSpaces [Inline]
    xs
           fixLineInitialSpaces (x :: Inline
    x:xs :: [Inline]
    xs) = Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixLineInitialSpaces [Inline]
    xs
           fixNbsps :: Text -> [Inline]
    fixNbsps s :: Text
    s = let (ys :: Text
    ys,zs :: Text
    zs) = (Char -> Bool) -> Text -> (Text, Text)
    T.span (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    =='\160') Text
    s
                        in  Int -> Inline -> [Inline]
    forall a. Int -> a -> [a]
    replicate (Text -> Int
    T.length Text
    ys) Inline
    hspace [Inline] -> [Inline] -> [Inline]
    forall a. Semigroup a => a -> a -> a
    <> [Text -> Inline
    Str Text
    zs]
           hspace :: Inline
    hspace = Format -> Text -> Inline
    RawInline "latex" "\\hspace*{0.333em}"
           -- We need \hfill\break for a line break at the start
           -- of a paragraph. See #5591.
           fixInitialLineBreaks :: [Inline] -> [Inline]
    fixInitialLineBreaks (LineBreak:xs :: [Inline]
    xs) =
             Format -> Text -> Inline
    RawInline (Text -> Format
    Format "latex") "\\hfill\\break\n" Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :
               [Inline] -> [Inline]
    fixInitialLineBreaks [Inline]
    xs
           fixInitialLineBreaks xs :: [Inline]
    xs = [Inline]
    xs

    isQuoted :: Inline -> Bool
    isQuoted :: Inline -> Bool
    isQuoted (Quoted _ _) = Bool
    True
    isQuoted _            = Bool
    False

    -- | Convert inline element to LaTeX
    inlineToLaTeX :: PandocMonad m
                  => Inline    -- ^ Inline to convert
                  -> LW m (Doc Text)
    inlineToLaTeX :: Inline -> LW m (Doc Text)
    inlineToLaTeX (Span (id' :: Text
    id',classes :: [Text]
    classes,kvs :: [(Text, Text)]
    kvs) ils :: [Inline]
    ils) = do
      Doc Text
    linkAnchor <- Bool -> Text -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> Text -> Doc Text -> LW m (Doc Text)
    hypertarget Bool
    False Text
    id' Doc Text
    forall a. Doc a
    empty
      Maybe Lang
    lang <- Maybe Text -> StateT WriterState m (Maybe Lang)
    forall (m :: * -> *). PandocMonad m => Maybe Text -> m (Maybe Lang)
    toLang (Maybe Text -> StateT WriterState m (Maybe Lang))
    -> Maybe Text -> StateT WriterState m (Maybe Lang)
    forall a b. (a -> b) -> a -> b
    $ Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "lang" [(Text, Text)]
    kvs
      let cmds :: [Text]
    cmds = ["textup" | "csl-no-emph" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                 ["textnormal" | "csl-no-strong" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes Bool -> Bool -> Bool
    ||
                                 "csl-no-smallcaps" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                 ["RL" | ("dir", "rtl") (Text, Text) -> [(Text, Text)] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [(Text, Text)]
    kvs] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                 ["LR" | ("dir", "ltr") (Text, Text) -> [(Text, Text)] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [(Text, Text)]
    kvs] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                 (case Maybe Lang
    lang of
                    Just lng :: Lang
    lng -> let (l :: Text
    l, o :: Text
    o) = Lang -> (Text, Text)
    toPolyglossia Lang
    lng
                                    ops :: Text
    ops = if Text -> Bool
    T.null Text
    o then "" else "[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    o Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "]"
                                in  ["text" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    l Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ops]
                    Nothing  -> [])
      Doc Text
    contents <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    ils
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ (if Text -> Bool
    T.null Text
    id'
                   then Doc Text
    forall a. Doc a
    empty
                   else "\\protect" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    linkAnchor) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
               (if [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    cmds
                   then Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    contents
                   else (Text -> Doc Text -> Doc Text) -> Doc Text -> [Text] -> Doc Text
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Text -> Doc Text -> Doc Text
    inCmd Doc Text
    contents [Text]
    cmds)
    inlineToLaTeX (Emph lst :: [Inline]
    lst) = Text -> Doc Text -> Doc Text
    inCmd "emph" (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Strong lst :: [Inline]
    lst) = Text -> Doc Text -> Doc Text
    inCmd "textbf" (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Strikeout lst :: [Inline]
    lst) = do
      -- we need to protect VERB in an mbox or we get an error
      -- see #1294
      -- with regular texttt we don't get an error, but we get
      -- incorrect results if there is a space, see #5529
      Doc Text
    contents <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX ([Inline] -> LW m (Doc Text)) -> [Inline] -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ ([Inline] -> [Inline]) -> [Inline] -> [Inline]
    forall a b. Walkable a b => (a -> a) -> b -> b
    walk ((Inline -> [Inline]) -> [Inline] -> [Inline]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap Inline -> [Inline]
    protectCode) [Inline]
    lst
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stStrikeout :: Bool
    stStrikeout = Bool
    True }
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text -> Doc Text
    inCmd "sout" Doc Text
    contents
    inlineToLaTeX (Superscript lst :: [Inline]
    lst) =
      Text -> Doc Text -> Doc Text
    inCmd "textsuperscript" (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Subscript lst :: [Inline]
    lst) =
      Text -> Doc Text -> Doc Text
    inCmd "textsubscript" (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (SmallCaps lst :: [Inline]
    lst) =
      Text -> Doc Text -> Doc Text
    inCmd "textsc"(Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Cite cits :: [Citation]
    cits lst :: [Inline]
    lst) = do
      WriterState
    st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
      let opts :: WriterOptions
    opts = WriterState -> WriterOptions
    stOptions WriterState
    st
      case WriterOptions -> CiteMethod
    writerCiteMethod WriterOptions
    opts of
         Natbib   -> [Citation] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [Citation] -> LW m (Doc Text)
    citationsToNatbib [Citation]
    cits
         Biblatex -> [Citation] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [Citation] -> LW m (Doc Text)
    citationsToBiblatex [Citation]
    cits
         _        -> [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst

    inlineToLaTeX (Code (_,classes :: [Text]
    classes,kvs :: [(Text, Text)]
    kvs) str :: Text
    str) = do
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      Bool
    inHeading <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInHeading
      Bool
    inItem <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInItem
      let listingsCode :: LW m (Doc Text)
    listingsCode = do
            let listingsopts :: [(Text, Text)]
    listingsopts = (case [Text] -> Maybe Text
    getListingsLanguage [Text]
    classes of
                                    Just l :: Text
    l  -> (("language", Text -> Text
    mbBraced Text
    l)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
    forall a. a -> [a] -> [a]
    :)
                                    Nothing -> [(Text, Text)] -> [(Text, Text)]
    forall a. a -> a
    id)
                               [(Text
    k,Text
    v) | (k :: Text
    k,v :: Text
    v) <- [(Text, Text)]
    kvs
                                      , Text
    k Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` ["exports","tangle","results"]]
            let listingsopt :: Text
    listingsopt = if [(Text, Text)] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [(Text, Text)]
    listingsopts
                                 then ""
                                 else "[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                                      Text -> [Text] -> Text
    T.intercalate ", "
                                      (((Text, Text) -> Text) -> [(Text, Text)] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\(k :: Text
    k,v :: Text
    v) -> Text
    k Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    v)
                                       [(Text, Text)]
    listingsopts) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "]"
            Bool
    inNote <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInNote
            Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when Bool
    inNote (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stVerbInNote :: Bool
    stVerbInNote = Bool
    True }
            let chr :: Char
    chr = case "!\"'()*,-./:;?@" String -> String -> String
    forall a. Eq a => [a] -> [a] -> [a]
    \\ Text -> String
    T.unpack Text
    str of
                           (c :: Char
    c:_) -> Char
    c
                           []    -> '!'
            let str' :: Text
    str' = [(Char, Text)] -> Text -> Text
    escapeStringUsing (String -> [(Char, Text)]
    backslashEscapes "\\{}%~_&#") Text
    str
            -- we always put lstinline in a dummy 'passthrough' command
            -- (defined in the default template) so that we don't have
            -- to change the way we escape characters depending on whether
            -- the lstinline is inside another command.  See #1629:
            Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ "\\passthrough{\\lstinline" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                            Text
    listingsopt Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Text
    T.singleton Char
    chr Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    str' Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Text
    T.singleton Char
    chr Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}"
      let rawCode :: LW m (Doc Text)
    rawCode = (Text -> Doc Text) -> StateT WriterState m Text -> LW m (Doc Text)
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM (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
    . (\s :: Text
    s -> "\\texttt{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    escapeSpaces Text
    s Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}"))
                     (StateT WriterState m Text -> LW m (Doc Text))
    -> StateT WriterState m Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    CodeString Text
    str
                    where escapeSpaces :: Text -> Text
    escapeSpaces = (Char -> Text) -> Text -> Text
    T.concatMap
                             (\c :: Char
    c -> if Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == ' ' then "\\ " else Char -> Text
    T.singleton Char
    c)
      let highlightCode :: LW m (Doc Text)
    highlightCode =
            case SyntaxMap
    -> (FormatOptions -> [SourceLine] -> Text)
    -> Attr
    -> Text
    -> Either Text Text
    forall a.
    SyntaxMap
    -> (FormatOptions -> [SourceLine] -> a)
    -> Attr
    -> Text
    -> Either Text a
    highlight (WriterOptions -> SyntaxMap
    writerSyntaxMap WriterOptions
    opts)
                     FormatOptions -> [SourceLine] -> Text
    formatLaTeXInline ("",[Text]
    classes,[]) Text
    str of
                   Left msg :: Text
    msg -> do
                     Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (Text -> Bool
    T.null Text
    msg) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> LogMessage
    CouldNotHighlight Text
    msg
                     LW m (Doc Text)
    rawCode
                   Right h :: Text
    h -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\st :: WriterState
    st -> WriterState
    st{ stHighlighting :: Bool
    stHighlighting = Bool
    True }) StateT WriterState m () -> LW m (Doc Text) -> LW m (Doc Text)
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >>
                              Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Text -> String
    T.unpack Text
    h))
      case () of
         _ | Bool
    inHeading Bool -> Bool -> Bool
    || Bool
    inItem  -> LW m (Doc Text)
    rawCode  -- see #5574
           | WriterOptions -> Bool
    writerListings WriterOptions
    opts  -> LW m (Doc Text)
    listingsCode
           | Maybe Style -> Bool
    forall a. Maybe a -> Bool
    isJust (WriterOptions -> Maybe Style
    writerHighlightStyle WriterOptions
    opts) Bool -> Bool -> Bool
    && Bool -> Bool
    not ([Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    classes)
                                  -> LW m (Doc Text)
    highlightCode
           | Bool
    otherwise            -> LW m (Doc Text)
    rawCode
    inlineToLaTeX (Quoted qt :: QuoteType
    qt lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
      Bool
    csquotes <- (WriterState -> Bool)
    -> StateT WriterState m WriterState -> StateT WriterState m Bool
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM WriterState -> Bool
    stCsquotes StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      if Bool
    csquotes
         then Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\enquote" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    contents
         else do
           let s1 :: Doc Text
    s1 = if Bool -> Bool
    not ([Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    lst) Bool -> Bool -> Bool
    && Inline -> Bool
    isQuoted ([Inline] -> Inline
    forall a. [a] -> a
    head [Inline]
    lst)
                       then "\\,"
                       else Doc Text
    forall a. Doc a
    empty
           let s2 :: Doc Text
    s2 = if Bool -> Bool
    not ([Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    lst) Bool -> Bool -> Bool
    && Inline -> Bool
    isQuoted ([Inline] -> Inline
    forall a. [a] -> a
    last [Inline]
    lst)
                       then "\\,"
                       else Doc Text
    forall a. Doc a
    empty
           let inner :: Doc Text
    inner = Doc Text
    s1 Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    s2
           Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ case QuoteType
    qt of
                    DoubleQuote ->
                       if Extension -> WriterOptions -> Bool
    forall a. HasSyntaxExtensions a => Extension -> a -> Bool
    isEnabled Extension
    Ext_smart WriterOptions
    opts
                          then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "``" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "''"
                          else Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '\x201C' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '\x201D'
                    SingleQuote ->
                       if Extension -> WriterOptions -> Bool
    forall a. HasSyntaxExtensions a => Extension -> a -> Bool
    isEnabled Extension
    Ext_smart WriterOptions
    opts
                          then Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '`' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '\''
                          else Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '\x2018' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '\x2019'
    inlineToLaTeX (Str str :: Text
    str) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      (Text -> Doc Text) -> StateT WriterState m Text -> LW m (Doc Text)
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (StateT WriterState m Text -> LW m (Doc Text))
    -> StateT WriterState m Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString Text
    str
    inlineToLaTeX (Math InlineMath str :: Text
    str) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\(" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Text
    handleMathComment Text
    str) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\)"
    inlineToLaTeX (Math DisplayMath str :: Text
    str) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\[" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Text
    handleMathComment Text
    str) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\]"
    inlineToLaTeX il :: Inline
    il@(RawInline f :: Format
    f str :: Text
    str) = do
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      if Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "latex" Bool -> Bool -> Bool
    || Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "tex" Bool -> Bool -> Bool
    ||
            (Format
    f Format -> Format -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Format
    Format "beamer" Bool -> Bool -> Bool
    && Bool
    beamer)
         then do
           Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
           Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str
         else do
           LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Inline -> LogMessage
    InlineNotRendered Inline
    il
           Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    inlineToLaTeX LineBreak = do
      Bool
    emptyLine <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stEmptyLine
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    True
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ (if Bool
    emptyLine then "~" else "") Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\\\" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr
    inlineToLaTeX SoftBreak = do
      WrapOption
    wrapText <- (WriterState -> WrapOption) -> 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)
      case WrapOption
    wrapText of
           WrapAuto     -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
           WrapNone     -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
           WrapPreserve -> Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    cr
    inlineToLaTeX Space = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
    inlineToLaTeX (Link _ txt :: [Inline]
    txt (src :: Text
    src,_))
      | Just ('#', ident :: Text
    ident) <- Text -> Maybe (Char, Text)
    T.uncons Text
    src
      = do
          Doc Text
    contents <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    txt
          Text
    lab <- Text -> StateT WriterState m Text
    forall (m :: * -> *). PandocMonad m => Text -> LW m Text
    toLabel Text
    ident
          Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\protect\\hyperlink" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    lab) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    contents
      | Bool
    otherwise =
      case [Inline]
    txt of
            [Str x :: Text
    x] | String -> String
    unEscapeString (Text -> String
    T.unpack Text
    x) String -> String -> Bool
    forall a. Eq a => a -> a -> Bool
    == String -> String
    unEscapeString (Text -> String
    T.unpack Text
    src) ->  -- autolink
                 do (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stUrl :: Bool
    stUrl = Bool
    True }
                    Text
    src' <- StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    URLString (Text -> Text
    escapeURI Text
    src)
                    Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ "\\url{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    src' Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}"
            [Str x :: Text
    x] | Just rest :: Text
    rest <- Text -> Text -> Maybe Text
    T.stripPrefix "mailto:" Text
    src,
                      String -> String
    unEscapeString (Text -> String
    T.unpack Text
    x) String -> String -> Bool
    forall a. Eq a => a -> a -> Bool
    == String -> String
    unEscapeString (Text -> String
    T.unpack Text
    rest) -> -- email autolink
                 do (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stUrl :: Bool
    stUrl = Bool
    True }
                    Text
    src' <- StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    URLString (Text -> Text
    escapeURI Text
    src)
                    Doc Text
    contents <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    txt
                    Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\href" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    src') Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                       Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces ("\\nolinkurl" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    contents)
            _ -> do Doc Text
    contents <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    txt
                    Text
    src' <- StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    URLString (Text -> Text
    escapeURI Text
    src)
                    Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal ("\\href{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    src' Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}{") Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                             Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char '}'
    inlineToLaTeX il :: Inline
    il@(Image _ _ (src :: Text
    src, _))
      | Just _ <- Text -> Text -> Maybe Text
    T.stripPrefix "data:" Text
    src = do
          LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Inline -> LogMessage
    InlineNotRendered Inline
    il
          Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    inlineToLaTeX (Image attr :: Attr
    attr _ (source :: Text
    source, _)) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: WriterState
    s -> WriterState
    s{ stGraphics :: Bool
    stGraphics = Bool
    True }
      WriterOptions
    opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      let showDim :: Direction -> [Doc Text]
    showDim dir :: Direction
    dir = let d :: Doc Text
    d = String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Direction -> String
    forall a. Show a => a -> String
    show Direction
    dir) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "="
                        in case Direction -> Attr -> Maybe Dimension
    dimension Direction
    dir Attr
    attr of
                             Just (Pixel a :: Integer
    a)   ->
                               [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (WriterOptions -> Dimension -> Text
    showInInch WriterOptions
    opts (Integer -> Dimension
    Pixel Integer
    a)) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "in"]
                             Just (Percent a :: Double
    a) ->
                               [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Double -> Text
    forall a. RealFloat a => a -> Text
    showFl (Double
    a Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / 100)) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                 case Direction
    dir of
                                    Width  -> "\\textwidth"
                                    Height -> "\\textheight"
                               ]
                             Just dim :: Dimension
    dim         ->
                               [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Dimension -> String
    forall a. Show a => a -> String
    show Dimension
    dim)]
                             Nothing          ->
                               case Direction
    dir of
                                    Width | Maybe Dimension -> Bool
    forall a. Maybe a -> Bool
    isJust (Direction -> Attr -> Maybe Dimension
    dimension Direction
    Height Attr
    attr) ->
                                      [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\textwidth"]
                                    Height | Maybe Dimension -> Bool
    forall a. Maybe a -> Bool
    isJust (Direction -> Attr -> Maybe Dimension
    dimension Direction
    Width Attr
    attr) ->
                                      [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "\\textheight"]
                                    _ -> []
          dimList :: [Doc Text]
    dimList = Direction -> [Doc Text]
    showDim Direction
    Width [Doc Text] -> [Doc Text] -> [Doc Text]
    forall a. Semigroup a => a -> a -> a
    <> Direction -> [Doc Text]
    showDim Direction
    Height
          dims :: Doc Text
    dims = if [Doc Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Doc Text]
    dimList
                    then Doc Text
    forall a. Doc a
    empty
                    else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> Doc Text
    forall a. Monoid a => [a] -> a
    mconcat (Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse "," [Doc Text]
    dimList)
          source' :: Text
    source' = if Text -> Bool
    isURI Text
    source
                       then Text
    source
                       else String -> Text
    T.pack (String -> Text) -> String -> Text
    forall a b. (a -> b) -> a -> b
    $ String -> String
    unEscapeString (String -> String) -> String -> String
    forall a b. (a -> b) -> a -> b
    $ Text -> String
    T.unpack Text
    source
      Text
    source'' <- StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    URLString Text
    source'
      Bool
    inHeading <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInHeading
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $
        (if Bool
    inHeading then "\\protect\\includegraphics" else "\\includegraphics") Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
        Doc Text
    dims Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    source'')
    inlineToLaTeX (Note contents :: [Block]
    contents) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      Bool
    externalNotes <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stExternalNotes
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s{stInNote :: Bool
    stInNote = Bool
    True, stExternalNotes :: Bool
    stExternalNotes = Bool
    True})
      Doc Text
    contents' <- [Block] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    contents
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\s :: WriterState
    s -> WriterState
    s {stInNote :: Bool
    stInNote = Bool
    False, stExternalNotes :: Bool
    stExternalNotes = Bool
    externalNotes})
      let optnl :: Doc a
    optnl = case [Block] -> [Block]
    forall a. [a] -> [a]
    reverse [Block]
    contents of
                       (CodeBlock _ _ : _) -> Doc a
    forall a. Doc a
    cr
                       _                   -> Doc a
    forall a. Doc a
    empty
      let noteContents :: Doc Text
    noteContents = Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest 2 Doc Text
    contents' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    optnl
      Bool
    beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      -- in beamer slides, display footnote from current overlay forward
      let beamerMark :: Doc Text
    beamerMark = if Bool
    beamer
                          then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "<.->"
                          else Doc Text
    forall a. Doc a
    empty
      if Bool
    externalNotes
         then do
           (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stNotes :: [Doc Text]
    stNotes = Doc Text
    noteContents Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    : WriterState -> [Doc Text]
    stNotes WriterState
    st }
           Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return "\\footnotemark{}"
           -- note: a \n before } needed when note ends with a Verbatim environment
           else Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\\footnote" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    beamerMark Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    noteContents

    -- A comment at the end of math needs to be followed by a newline,
    -- or the closing delimiter gets swallowed.
    handleMathComment :: Text -> Text
    handleMathComment :: Text -> Text
    handleMathComment s :: Text
    s =
      let (_, ys :: Text
    ys) = (Char -> Bool) -> Text -> (Text, Text)
    T.break (\c :: Char
    c -> Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '\n' Bool -> Bool -> Bool
    || Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '%') (Text -> (Text, Text)) -> Text -> (Text, Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Text
    T.reverse Text
    s -- no T.breakEnd
      in  case Text -> Maybe (Char, Text)
    T.uncons Text
    ys of
            Just ('%', ys' :: Text
    ys') -> case Text -> Maybe (Char, Text)
    T.uncons Text
    ys' of
              Just ('\\', _) -> Text
    s
              _              -> Text
    s Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n"
            _                -> Text
    s

    protectCode :: Inline -> [Inline]
    protectCode :: Inline -> [Inline]
    protectCode x :: Inline
    x@(Code _ _) = [Text -> Inline
    ltx "\\mbox{" , Inline
    x , Text -> Inline
    ltx "}"]
      where ltx :: Text -> Inline
    ltx = Format -> Text -> Inline
    RawInline (Text -> Format
    Format "latex")
    protectCode x :: Inline
    x = [Inline
    x]

    setEmptyLine :: PandocMonad m => Bool -> LW m ()
    setEmptyLine :: Bool -> LW m ()
    setEmptyLine b :: Bool
    b = (WriterState -> WriterState) -> LW m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> LW m ())
    -> (WriterState -> WriterState) -> LW m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: WriterState
    st -> WriterState
    st{ stEmptyLine :: Bool
    stEmptyLine = Bool
    b }

    citationsToNatbib :: PandocMonad m => [Citation] -> LW m (Doc Text)
    citationsToNatbib :: [Citation] -> LW m (Doc Text)
    citationsToNatbib
                [one :: Citation
    one]
      = Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand Text
    c [Inline]
    p [Inline]
    s Text
    k
      where
        Citation { citationId :: Citation -> Text
    citationId = Text
    k
                 , citationPrefix :: Citation -> [Inline]
    citationPrefix = [Inline]
    p
                 , citationSuffix :: Citation -> [Inline]
    citationSuffix = [Inline]
    s
                 , citationMode :: Citation -> CitationMode
    citationMode = CitationMode
    m
                 }
          = Citation
    one
        c :: Text
    c = case CitationMode
    m of
                 AuthorInText   -> "citet"
                 SuppressAuthor -> "citeyearpar"
                 NormalCitation -> "citep"

    citationsToNatbib cits :: [Citation]
    cits
      | [Citation] -> Bool
    noPrefix ([Citation] -> [Citation]
    forall a. [a] -> [a]
    tail [Citation]
    cits) Bool -> Bool -> Bool
    && [Citation] -> Bool
    noSuffix ([Citation] -> [Citation]
    forall a. [a] -> [a]
    init [Citation]
    cits) Bool -> Bool -> Bool
    && CitationMode -> [Citation] -> Bool
    forall (t :: * -> *).
    Foldable t =>
    CitationMode -> t Citation -> Bool
    ismode CitationMode
    NormalCitation [Citation]
    cits
      = Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand "citep" [Inline]
    p [Inline]
    s Text
    ks
      where
         noPrefix :: [Citation] -> Bool
    noPrefix  = (Citation -> Bool) -> [Citation] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all ([Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null ([Inline] -> Bool) -> (Citation -> [Inline]) -> Citation -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Citation -> [Inline]
    citationPrefix)
         noSuffix :: [Citation] -> Bool
    noSuffix  = (Citation -> Bool) -> [Citation] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all ([Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null ([Inline] -> Bool) -> (Citation -> [Inline]) -> Citation -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Citation -> [Inline]
    citationSuffix)
         ismode :: CitationMode -> t Citation -> Bool
    ismode m :: CitationMode
    m  = (Citation -> Bool) -> t Citation -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all (CitationMode -> CitationMode -> Bool
    forall a. Eq a => a -> a -> Bool
    (==) CitationMode
    m  (CitationMode -> Bool)
    -> (Citation -> CitationMode) -> Citation -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Citation -> CitationMode
    citationMode)
         p :: [Inline]
    p         = Citation -> [Inline]
    citationPrefix  (Citation -> [Inline]) -> Citation -> [Inline]
    forall a b. (a -> b) -> a -> b
    $
                     [Citation] -> Citation
    forall a. [a] -> a
    head [Citation]
    cits
         s :: [Inline]
    s         = Citation -> [Inline]
    citationSuffix  (Citation -> [Inline]) -> Citation -> [Inline]
    forall a b. (a -> b) -> a -> b
    $
                     [Citation] -> Citation
    forall a. [a] -> a
    last [Citation]
    cits
         ks :: Text
    ks        = Text -> [Text] -> Text
    T.intercalate ", " ([Text] -> Text) -> [Text] -> Text
    forall a b. (a -> b) -> a -> b
    $ (Citation -> Text) -> [Citation] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Citation -> Text
    citationId [Citation]
    cits

    citationsToNatbib (c :: Citation
    c:cs :: [Citation]
    cs) | Citation -> CitationMode
    citationMode Citation
    c CitationMode -> CitationMode -> Bool
    forall a. Eq a => a -> a -> Bool
    == CitationMode
    AuthorInText = do
         Doc Text
    author <- Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand "citeauthor" [] [] (Citation -> Text
    citationId Citation
    c)
         Doc Text
    cits   <- [Citation] -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [Citation] -> LW m (Doc Text)
    citationsToNatbib (Citation
    c { citationMode :: CitationMode
    citationMode = CitationMode
    SuppressAuthor } Citation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    : [Citation]
    cs)
         Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    author Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    <+> Doc Text
    cits

    citationsToNatbib cits :: [Citation]
    cits = do
      [Doc Text]
    cits' <- (Citation -> LW m (Doc Text))
    -> [Citation] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM Citation -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Citation -> LW m (Doc Text)
    convertOne [Citation]
    cits
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "\\citetext{" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> (Doc Text -> Doc Text -> Doc Text)
    -> Doc Text -> [Doc Text] -> Doc Text
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl' Doc Text -> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a -> Doc a
    combineTwo Doc Text
    forall a. Doc a
    empty [Doc Text]
    cits' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text "}"
      where
        combineTwo :: Doc a -> Doc a -> Doc a
    combineTwo a :: Doc a
    a b :: Doc a
    b | Doc a -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc a
    a = Doc a
    b
                       | Bool
    otherwise = Doc a
    a Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> String -> Doc a
    forall a. HasChars a => String -> Doc a
    text "; " Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a
    b
        convertOne :: Citation -> LW m (Doc Text)
    convertOne Citation { citationId :: Citation -> Text
    citationId = Text
    k
                            , citationPrefix :: Citation -> [Inline]
    citationPrefix = [Inline]
    p
                            , citationSuffix :: Citation -> [Inline]
    citationSuffix = [Inline]
    s
                            , citationMode :: Citation -> CitationMode
    citationMode = CitationMode
    m
                            }
            = case CitationMode
    m of
                   AuthorInText   -> Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand "citealt"  [Inline]
    p [Inline]
    s Text
    k
                   SuppressAuthor -> Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand "citeyear" [Inline]
    p [Inline]
    s Text
    k
                   NormalCitation -> Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand "citealp"  [Inline]
    p [Inline]
    s Text
    k

    citeCommand :: PandocMonad m
                => Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand :: Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand c :: Text
    c p :: [Inline]
    p s :: [Inline]
    s k :: Text
    k = do
      Doc Text
    args <- [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeArguments [Inline]
    p [Inline]
    s Text
    k
      Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal ("\\" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    c) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    args

    type Prefix = [Inline]
    type Suffix = [Inline]
    type CiteId = Text
    data CiteGroup = CiteGroup Prefix Suffix [CiteId]

    citeArgumentsList :: PandocMonad m
                  => CiteGroup -> LW m (Doc Text)
    citeArgumentsList :: CiteGroup -> LW m (Doc Text)
    citeArgumentsList (CiteGroup _ _ []) = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    citeArgumentsList (CiteGroup pfxs :: [Inline]
    pfxs sfxs :: [Inline]
    sfxs ids :: [Text]
    ids) = do
          Doc Text
    pdoc <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    pfxs
          Doc Text
    sdoc <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    sfxs'
          Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ (Doc Text -> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a -> Doc a
    optargs Doc Text
    pdoc Doc Text
    sdoc) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                  (Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> [Text] -> Text
    T.intercalate "," ([Text] -> [Text]
    forall a. [a] -> [a]
    reverse [Text]
    ids))))
          where sfxs' :: [Inline]
    sfxs' = [Inline] -> [Inline]
    stripLocatorBraces ([Inline] -> [Inline]) -> [Inline] -> [Inline]
    forall a b. (a -> b) -> a -> b
    $ case [Inline]
    sfxs of
                    (Str t :: Text
    t : r :: [Inline]
    r) -> case Text -> Maybe (Char, Text)
    T.uncons Text
    t of
                      Just (x :: Char
    x, xs :: Text
    xs)
                        | Text -> Bool
    T.null Text
    xs
                        , Char -> Bool
    isPunctuation Char
    x -> (Inline -> Bool) -> [Inline] -> [Inline]
    forall a. (a -> Bool) -> [a] -> [a]
    dropWhile (Inline -> Inline -> Bool
    forall a. Eq a => a -> a -> Bool
    == Inline
    Space) [Inline]
    r
                        | Char -> Bool
    isPunctuation Char
    x -> Text -> Inline
    Str Text
    xs Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline]
    r
                      _ -> [Inline]
    sfxs
                    _   -> [Inline]
    sfxs
                optargs :: Doc a -> Doc a -> Doc a
    optargs pdoc :: Doc a
    pdoc sdoc :: Doc a
    sdoc = case (Doc a -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc a
    pdoc, Doc a -> Bool
    forall a. Doc a -> Bool
    isEmpty Doc a
    sdoc) of
                     (True, True ) -> Doc a
    forall a. Doc a
    empty
                     (True, False) -> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc a
    sdoc
                     (_   , _    ) -> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc a
    pdoc Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc a
    sdoc

    citeArguments :: PandocMonad m
                  => [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeArguments :: [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeArguments p :: [Inline]
    p s :: [Inline]
    s k :: Text
    k = CiteGroup -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => CiteGroup -> LW m (Doc Text)
    citeArgumentsList ([Inline] -> [Inline] -> [Text] -> CiteGroup
    CiteGroup [Inline]
    p [Inline]
    s [Text
    k])

    -- strip off {} used to define locator in pandoc-citeproc; see #5722
    stripLocatorBraces :: [Inline] -> [Inline]
    stripLocatorBraces :: [Inline] -> [Inline]
    stripLocatorBraces = (Inline -> Inline) -> [Inline] -> [Inline]
    forall a b. Walkable a b => (a -> a) -> b -> b
    walk Inline -> Inline
    go
      where go :: Inline -> Inline
    go (Str xs :: Text
    xs) = Text -> Inline
    Str (Text -> Inline) -> Text -> Inline
    forall a b. (a -> b) -> a -> b
    $ (Char -> Bool) -> Text -> Text
    T.filter (\c :: Char
    c -> Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= '{' Bool -> Bool -> Bool
    && Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= '}') Text
    xs
            go x :: Inline
    x        = Inline
    x

    citationsToBiblatex :: PandocMonad m => [Citation] -> LW m (Doc Text)
    citationsToBiblatex :: [Citation] -> LW m (Doc Text)
    citationsToBiblatex
                [one :: Citation
    one]
      = Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
    citeCommand Text
    cmd [Inline]
    p [Inline]
    s Text
    k
        where
           Citation { citationId :: Citation -> Text
    citationId = Text
    k
                    , citationPrefix :: Citation -> [Inline]
    citationPrefix = [Inline]
    p
                    , citationSuffix :: Citation -> [Inline]
    citationSuffix = [Inline]
    s
                    , citationMode :: Citation -> CitationMode
    citationMode = CitationMode
    m
                    } = Citation
    one
           cmd :: Text
    cmd = case CitationMode
    m of
                      SuppressAuthor -> "autocite*"
                      AuthorInText   -> "textcite"
                      NormalCitation -> "autocite"

    citationsToBiblatex (c :: Citation
    c:cs :: [Citation]
    cs)
      | (Citation -> Bool) -> [Citation] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all (\cit :: Citation
    cit -> [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null (Citation -> [Inline]
    citationPrefix Citation
    cit) Bool -> Bool -> Bool
    && [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null (Citation -> [Inline]
    citationSuffix Citation
    cit)) (Citation
    cCitation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    :[Citation]
    cs)
        = do
          let cmd :: String
    cmd = case Citation -> CitationMode
    citationMode Citation
    c of
                        SuppressAuthor -> "\\autocite*"
                        AuthorInText   -> "\\textcite"
                        NormalCitation -> "\\autocite"
          Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    cmd Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                   Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> [Text] -> Text
    T.intercalate "," ((Citation -> Text) -> [Citation] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Citation -> Text
    citationId (Citation
    cCitation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    :[Citation]
    cs))))
      | Bool
    otherwise
        = do
          let cmd :: String
    cmd = case Citation -> CitationMode
    citationMode Citation
    c of
                        SuppressAuthor -> "\\autocites*"
                        AuthorInText   -> "\\textcites"
                        NormalCitation -> "\\autocites"

          [Doc Text]
    groups <- (CiteGroup -> LW m (Doc Text))
    -> [CiteGroup] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM CiteGroup -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => CiteGroup -> LW m (Doc Text)
    citeArgumentsList ([CiteGroup] -> [CiteGroup]
    forall a. [a] -> [a]
    reverse (([CiteGroup] -> Citation -> [CiteGroup])
    -> [CiteGroup] -> [Citation] -> [CiteGroup]
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl' [CiteGroup] -> Citation -> [CiteGroup]
    grouper [] (Citation
    cCitation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    :[Citation]
    cs)))

          Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    cmd Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> ([Doc Text] -> Doc Text
    forall a. Monoid a => [a] -> a
    mconcat [Doc Text]
    groups)

      where grouper :: [CiteGroup] -> Citation -> [CiteGroup]
    grouper prev :: [CiteGroup]
    prev cit :: Citation
    cit = case [CiteGroup]
    prev of
             ((CiteGroup oPfx :: [Inline]
    oPfx oSfx :: [Inline]
    oSfx ids :: [Text]
    ids):rest :: [CiteGroup]
    rest)
                 | [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    oSfx Bool -> Bool -> Bool
    && [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    pfx -> ([Inline] -> [Inline] -> [Text] -> CiteGroup
    CiteGroup [Inline]
    oPfx [Inline]
    sfx (Text
    cidText -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    ids))CiteGroup -> [CiteGroup] -> [CiteGroup]
    forall a. a -> [a] -> [a]
    :[CiteGroup]
    rest
             _ -> ([Inline] -> [Inline] -> [Text] -> CiteGroup
    CiteGroup [Inline]
    pfx [Inline]
    sfx [Text
    cid])CiteGroup -> [CiteGroup] -> [CiteGroup]
    forall a. a -> [a] -> [a]
    :[CiteGroup]
    prev
             where pfx :: [Inline]
    pfx = Citation -> [Inline]
    citationPrefix Citation
    cit
                   sfx :: [Inline]
    sfx = Citation -> [Inline]
    citationSuffix Citation
    cit
                   cid :: Text
    cid = Citation -> Text
    citationId Citation
    cit

    citationsToBiblatex _ = Doc Text -> LW m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty

    -- Determine listings language from list of class attributes.
    getListingsLanguage :: [Text] -> Maybe Text
    getListingsLanguage :: [Text] -> Maybe Text
    getListingsLanguage xs :: [Text]
    xs
      = (Text -> Maybe Text -> Maybe Text)
    -> Maybe Text -> [Text] -> Maybe Text
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr (Maybe Text -> Maybe Text -> Maybe Text
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    (<|>) (Maybe Text -> Maybe Text -> Maybe Text)
    -> (Text -> Maybe Text) -> Text -> Maybe Text -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Text
    toListingsLanguage) Maybe Text
    forall a. Maybe a
    Nothing [Text]
    xs

    mbBraced :: Text -> Text
    mbBraced :: Text -> Text
    mbBraced x :: Text
    x = if Bool -> Bool
    not ((Char -> Bool) -> Text -> Bool
    T.all Char -> Bool
    isAlphaNum Text
    x)
                    then "{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    x Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "}"
                    else Text
    x

    -- Extract a key from divs and spans
    extract :: Text -> Block -> [Text]
    extract :: Text -> Block -> [Text]
    extract key :: Text
    key (Div attr :: Attr
    attr _)     = Text -> Attr -> [Text]
    lookKey Text
    key Attr
    attr
    extract key :: Text
    key (Plain ils :: [Inline]
    ils)      = (Inline -> [Text]) -> [Inline] -> [Text]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Inline -> [Text]
    extractInline Text
    key) [Inline]
    ils
    extract key :: Text
    key (Para ils :: [Inline]
    ils)       = (Inline -> [Text]) -> [Inline] -> [Text]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Inline -> [Text]
    extractInline Text
    key) [Inline]
    ils
    extract key :: Text
    key (Header _ _ ils :: [Inline]
    ils) = (Inline -> [Text]) -> [Inline] -> [Text]
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Inline -> [Text]
    extractInline Text
    key) [Inline]
    ils
    extract _ _                  = []

    -- Extract a key from spans
    extractInline :: Text -> Inline -> [Text]
    extractInline :: Text -> Inline -> [Text]
    extractInline key :: Text
    key (Span attr :: Attr
    attr _) = Text -> Attr -> [Text]
    lookKey Text
    key Attr
    attr
    extractInline _ _               = []

    -- Look up a key in an attribute and give a list of its values
    lookKey :: Text -> Attr -> [Text]
    lookKey :: Text -> Attr -> [Text]
    lookKey key :: Text
    key (_,_,kvs :: [(Text, Text)]
    kvs) =  [Text] -> (Text -> [Text]) -> Maybe Text -> [Text]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] Text -> [Text]
    T.words (Maybe Text -> [Text]) -> Maybe Text -> [Text]
    forall a b. (a -> b) -> a -> b
    $ Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    key [(Text, Text)]
    kvs

    -- In environments \Arabic instead of \arabic is used
    toPolyglossiaEnv :: Lang -> (Text, Text)
    toPolyglossiaEnv :: Lang -> (Text, Text)
    toPolyglossiaEnv l :: Lang
    l =
      case Lang -> (Text, Text)
    toPolyglossia Lang
    l of
        ("arabic", o :: Text
    o) -> ("Arabic", Text
    o)
        x :: (Text, Text)
    x             -> (Text, Text)
    x

    -- Takes a list of the constituents of a BCP 47 language code and
    -- converts it to a Polyglossia (language, options) tuple
    -- http://mirrors.ctan.org/macros/latex/contrib/polyglossia/polyglossia.pdf
    toPolyglossia :: Lang -> (Text, Text)
    toPolyglossia :: Lang -> (Text, Text)
    toPolyglossia (Lang "ar" _ "DZ" _)        = ("arabic", "locale=algeria")
    toPolyglossia (Lang "ar" _ "IQ" _)        = ("arabic", "locale=mashriq")
    toPolyglossia (Lang "ar" _ "JO" _)        = ("arabic", "locale=mashriq")
    toPolyglossia (Lang "ar" _ "LB" _)        = ("arabic", "locale=mashriq")
    toPolyglossia (Lang "ar" _ "LY" _)        = ("arabic", "locale=libya")
    toPolyglossia (Lang "ar" _ "MA" _)        = ("arabic", "locale=morocco")
    toPolyglossia (Lang "ar" _ "MR" _)        = ("arabic", "locale=mauritania")
    toPolyglossia (Lang "ar" _ "PS" _)        = ("arabic", "locale=mashriq")
    toPolyglossia (Lang "ar" _ "SY" _)        = ("arabic", "locale=mashriq")
    toPolyglossia (Lang "ar" _ "TN" _)        = ("arabic", "locale=tunisia")
    toPolyglossia (Lang "de" _ _ vars :: [Text]
    vars)
      | "1901" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                    = ("german", "spelling=old")
    toPolyglossia (Lang "de" _ "AT" vars :: [Text]
    vars)
      | "1901" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                    = ("german", "variant=austrian, spelling=old")
    toPolyglossia (Lang "de" _ "AT" _)        = ("german", "variant=austrian")
    toPolyglossia (Lang "de" _ "CH" vars :: [Text]
    vars)
      | "1901" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                    = ("german", "variant=swiss, spelling=old")
    toPolyglossia (Lang "de" _ "CH" _)        = ("german", "variant=swiss")
    toPolyglossia (Lang "de" _ _ _)           = ("german", "")
    toPolyglossia (Lang "dsb" _ _ _)          = ("lsorbian", "")
    toPolyglossia (Lang "el" _ "polyton" _)   = ("greek",   "variant=poly")
    toPolyglossia (Lang "en" _ "AU" _)        = ("english", "variant=australian")
    toPolyglossia (Lang "en" _ "CA" _)        = ("english", "variant=canadian")
    toPolyglossia (Lang "en" _ "GB" _)        = ("english", "variant=british")
    toPolyglossia (Lang "en" _ "NZ" _)        = ("english", "variant=newzealand")
    toPolyglossia (Lang "en" _ "UK" _)        = ("english", "variant=british")
    toPolyglossia (Lang "en" _ "US" _)        = ("english", "variant=american")
    toPolyglossia (Lang "grc" _ _ _)          = ("greek",   "variant=ancient")
    toPolyglossia (Lang "hsb" _ _  _)         = ("usorbian", "")
    toPolyglossia (Lang "la" _ _ vars :: [Text]
    vars)
      | "x-classic" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars               = ("latin", "variant=classic")
    toPolyglossia (Lang "sl" _ _ _)           = ("slovenian", "")
    toPolyglossia x :: Lang
    x                           = (Lang -> Text
    commonFromBcp47 Lang
    x, "")

    -- Takes a list of the constituents of a BCP 47 language code and
    -- converts it to a Babel language string.
    -- http://mirrors.ctan.org/macros/latex/required/babel/base/babel.pdf
    -- List of supported languages (slightly outdated):
    -- http://tug.ctan.org/language/hyph-utf8/doc/generic/hyph-utf8/hyphenation.pdf
    toBabel :: Lang -> Text
    toBabel :: Lang -> Text
    toBabel (Lang "de" _ "AT" vars :: [Text]
    vars)
      | "1901" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                  = "austrian"
      | Bool
    otherwise                           = "naustrian"
    toBabel (Lang "de" _ "CH" vars :: [Text]
    vars)
      | "1901" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                  = "swissgerman"
      | Bool
    otherwise                           = "nswissgerman"
    toBabel (Lang "de" _ _ vars :: [Text]
    vars)
      | "1901" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                  = "german"
      | Bool
    otherwise                           = "ngerman"
    toBabel (Lang "dsb" _ _ _)              = "lowersorbian"
    toBabel (Lang "el" _ _ vars :: [Text]
    vars)
      | "polyton" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars               = "polutonikogreek"
    toBabel (Lang "en" _ "AU" _)            = "australian"
    toBabel (Lang "en" _ "CA" _)            = "canadian"
    toBabel (Lang "en" _ "GB" _)            = "british"
    toBabel (Lang "en" _ "NZ" _)            = "newzealand"
    toBabel (Lang "en" _ "UK" _)            = "british"
    toBabel (Lang "en" _ "US" _)            = "american"
    toBabel (Lang "fr" _ "CA" _)            = "canadien"
    toBabel (Lang "fra" _ _ vars :: [Text]
    vars)
      | "aca" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                   = "acadian"
    toBabel (Lang "grc" _ _ _)              = "polutonikogreek"
    toBabel (Lang "hsb" _ _ _)              = "uppersorbian"
    toBabel (Lang "la" _ _ vars :: [Text]
    vars)
      | "x-classic" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars             = "classiclatin"
    toBabel (Lang "sl" _ _ _)               = "slovene"
    toBabel x :: Lang
    x                               = Lang -> Text
    commonFromBcp47 Lang
    x

    -- Takes a list of the constituents of a BCP 47 language code
    -- and converts it to a string shared by Babel and Polyglossia.
    -- https://tools.ietf.org/html/bcp47#section-2.1
    commonFromBcp47 :: Lang -> Text
    commonFromBcp47 :: Lang -> Text
    commonFromBcp47 (Lang "pt" _ "BR" _)            = "brazil"
    -- Note: documentation says "brazilian" works too, but it doesn't seem to work
    -- on some systems.  See #2953.
    commonFromBcp47 (Lang "sr" "Cyrl" _ _)          = "serbianc"
    commonFromBcp47 (Lang "zh" "Latn" _ vars :: [Text]
    vars)
      | "pinyin" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    vars                        = "pinyin"
    commonFromBcp47 (Lang l :: Text
    l _ _ _) = Text -> Text
    forall a p. (Eq a, IsString a, IsString p) => a -> p
    fromIso Text
    l
      where
        fromIso :: a -> p
    fromIso "af"  = "afrikaans"
        fromIso "am"  = "amharic"
        fromIso "ar"  = "arabic"
        fromIso "as"  = "assamese"
        fromIso "ast" = "asturian"
        fromIso "bg"  = "bulgarian"
        fromIso "bn"  = "bengali"
        fromIso "bo"  = "tibetan"
        fromIso "br"  = "breton"
        fromIso "ca"  = "catalan"
        fromIso "cy"  = "welsh"
        fromIso "cs"  = "czech"
        fromIso "cop" = "coptic"
        fromIso "da"  = "danish"
        fromIso "dv"  = "divehi"
        fromIso "el"  = "greek"
        fromIso "en"  = "english"
        fromIso "eo"  = "esperanto"
        fromIso "es"  = "spanish"
        fromIso "et"  = "estonian"
        fromIso "eu"  = "basque"
        fromIso "fa"  = "farsi"
        fromIso "fi"  = "finnish"
        fromIso "fr"  = "french"
        fromIso "fur" = "friulan"
        fromIso "ga"  = "irish"
        fromIso "gd"  = "scottish"
        fromIso "gez" = "ethiopic"
        fromIso "gl"  = "galician"
        fromIso "he"  = "hebrew"
        fromIso "hi"  = "hindi"
        fromIso "hr"  = "croatian"
        fromIso "hu"  = "magyar"
        fromIso "hy"  = "armenian"
        fromIso "ia"  = "interlingua"
        fromIso "id"  = "indonesian"
        fromIso "ie"  = "interlingua"
        fromIso "is"  = "icelandic"
        fromIso "it"  = "italian"
        fromIso "jp"  = "japanese"
        fromIso "km"  = "khmer"
        fromIso "kmr" = "kurmanji"
        fromIso "kn"  = "kannada"
        fromIso "ko"  = "korean"
        fromIso "la"  = "latin"
        fromIso "lo"  = "lao"
        fromIso "lt"  = "lithuanian"
        fromIso "lv"  = "latvian"
        fromIso "ml"  = "malayalam"
        fromIso "mn"  = "mongolian"
        fromIso "mr"  = "marathi"
        fromIso "nb"  = "norsk"
        fromIso "nl"  = "dutch"
        fromIso "nn"  = "nynorsk"
        fromIso "no"  = "norsk"
        fromIso "nqo" = "nko"
        fromIso "oc"  = "occitan"
        fromIso "pa"  = "panjabi"
        fromIso "pl"  = "polish"
        fromIso "pms" = "piedmontese"
        fromIso "pt"  = "portuguese"
        fromIso "rm"  = "romansh"
        fromIso "ro"  = "romanian"
        fromIso "ru"  = "russian"
        fromIso "sa"  = "sanskrit"
        fromIso "se"  = "samin"
        fromIso "sk"  = "slovak"
        fromIso "sq"  = "albanian"
        fromIso "sr"  = "serbian"
        fromIso "sv"  = "swedish"
        fromIso "syr" = "syriac"
        fromIso "ta"  = "tamil"
        fromIso "te"  = "telugu"
        fromIso "th"  = "thai"
        fromIso "ti"  = "ethiopic"
        fromIso "tk"  = "turkmen"
        fromIso "tr"  = "turkish"
        fromIso "uk"  = "ukrainian"
        fromIso "ur"  = "urdu"
        fromIso "vi"  = "vietnamese"
        fromIso _     = ""