    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.Writers.Org
       Copyright   : © 2010-2015 Puneeth Chaganti <punchagan@gmail.com>
                       2010-2020 John MacFarlane <jgm@berkeley.edu>
                       2016-2020 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
       License     : GNU GPL, version 2 or above

       Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
       Stability   : alpha
       Portability : portable

    Conversion of 'Pandoc' documents to Emacs Org-Mode.

    Org-Mode:  <http://orgmode.org>
    -}
    module Text.Pandoc.Writers.Org (writeOrg) where
    import Control.Monad.State.Strict
    import Data.Char (isAlphaNum)
    import Data.List (intersect, intersperse, partition, transpose)
    import Data.Text (Text)
    import qualified Data.Text as T
    import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
    import Text.Pandoc.Definition
    import Text.Pandoc.Logging
    import Text.Pandoc.Options
    import Text.DocLayout
    import Text.Pandoc.Shared
    import Text.Pandoc.Templates (renderTemplate)
    import Text.Pandoc.Writers.Shared

    data WriterState =
      WriterState { WriterState -> [[Block]]
    stNotes   :: [[Block]]
                  , WriterState -> Bool
    stHasMath :: Bool
                  , WriterState -> WriterOptions
    stOptions :: WriterOptions
                  }

    type Org = StateT WriterState

    -- | Convert Pandoc to Org.
    writeOrg :: PandocMonad m => WriterOptions -> Pandoc -> m Text
    writeOrg :: WriterOptions -> Pandoc -> m Text
    writeOrg opts :: WriterOptions
    opts document :: Pandoc
    document = do
      let st :: WriterState
    st = WriterState :: [[Block]] -> Bool -> WriterOptions -> WriterState
    WriterState { stNotes :: [[Block]]
    stNotes = [],
                             stHasMath :: Bool
    stHasMath = Bool
    False,
                             stOptions :: WriterOptions
    stOptions = WriterOptions
    opts }
      StateT WriterState m Text -> WriterState -> m Text
    forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
    evalStateT (Pandoc -> StateT WriterState m Text
    forall (m :: * -> *). PandocMonad m => Pandoc -> Org m Text
    pandocToOrg Pandoc
    document) WriterState
    st

    -- | Return Org representation of document.
    pandocToOrg :: PandocMonad m => Pandoc -> Org m Text
    pandocToOrg :: Pandoc -> Org m Text
    pandocToOrg (Pandoc meta :: Meta
    meta blocks :: [Block]
    blocks) = 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 colwidth :: Maybe Int
    colwidth = if WriterOptions -> WrapOption
    writerWrapText WriterOptions
    opts 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
    opts
                        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
    opts
                   [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg
                   ((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] -> Org m (Doc Text)
    inlineListToOrg)
                   Meta
    meta
      Doc Text
    body <- [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [Block]
    blocks
      Doc Text
    notes <- (WriterState -> [[Block]]) -> StateT WriterState m [[Block]]
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets ([[Block]] -> [[Block]]
    forall a. [a] -> [a]
    reverse ([[Block]] -> [[Block]])
    -> (WriterState -> [[Block]]) -> WriterState -> [[Block]]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . WriterState -> [[Block]]
    stNotes) StateT WriterState m [[Block]]
    -> ([[Block]] -> StateT WriterState m (Doc Text))
    -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [[Block]] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [[Block]] -> Org m (Doc Text)
    notesToOrg
      Bool
    hasMath <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stHasMath
      let main :: Doc Text
    main = Doc Text
    body Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $+$ Doc Text
    notes
      let context :: Context Text
    context = 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) -> 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 "math" Bool
    hasMath
                  (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Context Text
    metadata
      Text -> Org m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> Org m Text) -> Text -> Org 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
    opts 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

    -- | Return Org representation of notes.
    notesToOrg :: PandocMonad m => [[Block]] -> Org m (Doc Text)
    notesToOrg :: [[Block]] -> Org m (Doc Text)
    notesToOrg notes :: [[Block]]
    notes =
      [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vsep ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> Org m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Int -> [Block] -> Org m (Doc Text))
    -> [Int] -> [[Block]] -> StateT WriterState m [Doc Text]
    forall (m :: * -> *) a b c.
    Applicative m =>
    (a -> b -> m c) -> [a] -> [b] -> m [c]
    zipWithM Int -> [Block] -> Org m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Int -> [Block] -> Org m (Doc Text)
    noteToOrg [1..] [[Block]]
    notes

    -- | Return Org representation of a note.
    noteToOrg :: PandocMonad m => Int -> [Block] -> Org m (Doc Text)
    noteToOrg :: Int -> [Block] -> Org m (Doc Text)
    noteToOrg num :: Int
    num note :: [Block]
    note = do
      Doc Text
    contents <- [Block] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [Block]
    note
      let marker :: [Char]
    marker = "[fn:" [Char] -> [Char] -> [Char]
    forall a. [a] -> [a] -> [a]
    ++ Int -> [Char]
    forall a. Show a => a -> [Char]
    show Int
    num [Char] -> [Char] -> [Char]
    forall a. [a] -> [a] -> [a]
    ++ "] "
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Int -> Doc Text -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
    hang ([Char] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Char]
    marker) ([Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text [Char]
    marker) Doc Text
    contents

    -- | Escape special characters for Org.
    escapeString :: Text -> Text
    escapeString :: Text -> Text
    escapeString = [(Char, Text)] -> Text -> Text
    escapeStringUsing
                   [ ('\x2014',"---")
                   , ('\x2013',"--")
                   , ('\x2019',"'")
                   , ('\x2026',"...")
                   ]

    isRawFormat :: Format -> Bool
    isRawFormat :: Format -> Bool
    isRawFormat f :: Format
    f =
      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 "org"

    -- | Convert Pandoc block element to Org.
    blockToOrg :: PandocMonad m
               => Block         -- ^ Block element
               -> Org m (Doc Text)
    blockToOrg :: Block -> Org m (Doc Text)
    blockToOrg Null = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    blockToOrg (Div (_,classes :: [Text]
    classes@(cls :: Text
    cls:_),kvs :: [(Text, Text)]
    kvs) bs :: [Block]
    bs) | "drawer" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes = do
      Doc Text
    contents <- [Block] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [Block]
    bs
      let drawerNameTag :: Doc Text
    drawerNameTag = ":" 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
    cls Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> ":"
      let keys :: Doc Text
    keys = [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 (\(k :: Text
    k,v :: Text
    v) ->
                           ":" 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
    k 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
    space 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
    v) [(Text, Text)]
    kvs
      let drawerEndTag :: Doc Text
    drawerEndTag = [Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text ":END:"
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    drawerNameTag Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    keys 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. Doc a -> Doc a -> Doc a
    $$ Doc Text
    contents 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. Doc a -> Doc a -> Doc a
    $$ Doc Text
    drawerEndTag Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
               Doc Text
    forall a. Doc a
    blankline
    blockToOrg (Div (ident :: Text
    ident, classes :: [Text]
    classes, kv :: [(Text, Text)]
    kv) bs :: [Block]
    bs) = do
      Doc Text
    contents <- [Block] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [Block]
    bs
      -- if one class looks like the name of a greater block then output as such:
      -- The ID, if present, is added via the #+NAME keyword; other classes and
      -- key-value pairs are kept as #+ATTR_HTML attributes.
      let isGreaterBlockClass :: Text -> Bool
    isGreaterBlockClass = (Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` ["center", "quote"]) (Text -> Bool) -> (Text -> Text) -> Text -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Text
    T.toLower
          (blockTypeCand :: [Text]
    blockTypeCand, classes' :: [Text]
    classes') = (Text -> Bool) -> [Text] -> ([Text], [Text])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    partition Text -> Bool
    isGreaterBlockClass [Text]
    classes
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ case [Text]
    blockTypeCand of
        (blockType :: Text
    blockType:classes'' :: [Text]
    classes'') ->
          Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Attr -> Doc Text
    attrHtml (Text
    ident, [Text]
    classes'' [Text] -> [Text] -> [Text]
    forall a. Semigroup a => a -> a -> a
    <> [Text]
    classes', [(Text, Text)]
    kv) Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
          "#+BEGIN_" 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
    blockType 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_" 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
    blockType Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline
        _                     ->
          -- fallback with id: add id as an anchor if present, discard classes and
          -- key-value pairs, unwrap the content.
          let contents' :: Doc Text
    contents' = if Bool -> Bool
    not (Text -> Bool
    T.null Text
    ident)
                          then "<<" 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
    ident 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
    contents
                          else Doc Text
    contents
          in Doc Text
    forall a. Doc a
    blankline 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
    $$ Doc Text
    forall a. Doc a
    blankline
    blockToOrg (Plain inlines :: [Inline]
    inlines) = [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    inlines
    -- title beginning with fig: indicates that the image is a figure
    blockToOrg (Para [Image attr :: Attr
    attr txt :: [Inline]
    txt (src :: Text
    src,tgt :: Text
    tgt)])
      | Just tit :: Text
    tit <- Text -> Text -> Maybe Text
    T.stripPrefix "fig:" Text
    tgt = do
          Doc Text
    capt <- if [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    txt
                  then Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
                  else ("#+CAPTION: " Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>) (Doc Text -> Doc Text) -> Org m (Doc Text) -> Org m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    `fmap` [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    txt
          Doc Text
    img <- Inline -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Inline -> Org m (Doc Text)
    inlineToOrg (Attr -> [Inline] -> (Text, Text) -> Inline
    Image Attr
    attr [Inline]
    txt (Text
    src,Text
    tit))
          Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    capt 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
    forall a. Doc a
    blankline
    blockToOrg (Para inlines :: [Inline]
    inlines) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    inlines
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    blankline
    blockToOrg (LineBlock lns :: [[Inline]]
    lns) = do
      let splitStanza :: [a] -> [[a]]
    splitStanza [] = []
          splitStanza xs :: [a]
    xs = case (a -> Bool) -> [a] -> ([a], [a])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    break (a -> a -> Bool
    forall a. Eq a => a -> a -> Bool
    == a
    forall a. Monoid a => a
    mempty) [a]
    xs of
            (l :: [a]
    l, [])  -> [[a]
    l]
            (l :: [a]
    l, _:r :: [a]
    r) -> [a]
    l [a] -> [[a]] -> [[a]]
    forall a. a -> [a] -> [a]
    : [a] -> [[a]]
    splitStanza [a]
    r
      let joinWithLinefeeds :: [Doc Text] -> Doc Text
    joinWithLinefeeds  = Doc Text -> Doc Text
    forall a. IsString a => Doc a -> Doc a
    nowrap (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
    forall a. Monoid a => [a] -> a
    mconcat ([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] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse Doc Text
    forall a. Doc a
    cr
      let joinWithBlankLines :: [Doc a] -> Doc a
    joinWithBlankLines = [Doc a] -> Doc a
    forall a. Monoid a => [a] -> a
    mconcat ([Doc a] -> Doc a) -> ([Doc a] -> [Doc a]) -> [Doc a] -> Doc a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Doc a -> [Doc a] -> [Doc a]
    forall a. a -> [a] -> [a]
    intersperse Doc a
    forall a. Doc a
    blankline
      let prettifyStanza :: [[Inline]] -> StateT WriterState m (Doc Text)
    prettifyStanza ls :: [[Inline]]
    ls  = [Doc Text] -> Doc Text
    joinWithLinefeeds ([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
    <$> ([Inline] -> StateT WriterState 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] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [[Inline]]
    ls
      Doc Text
    contents <- [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    joinWithBlankLines ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> Org m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ([[Inline]] -> Org 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]] -> Org m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [[Inline]] -> StateT WriterState m (Doc Text)
    prettifyStanza ([[Inline]] -> [[[Inline]]]
    forall a. (Eq a, Monoid a) => [a] -> [[a]]
    splitStanza [[Inline]]
    lns)
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "#+BEGIN_VERSE" 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
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "#+END_VERSE" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    blankline
    blockToOrg (RawBlock "html" str :: Text
    str) =
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "#+BEGIN_HTML" 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 (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_HTML" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline
    blockToOrg b :: Block
    b@(RawBlock f :: Format
    f str :: Text
    str)
      | Format -> Bool
    isRawFormat Format
    f = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str
      | Bool
    otherwise     = 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 -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    blockToOrg HorizontalRule = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "--------------" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline
    blockToOrg (Header level :: Int
    level attr :: Attr
    attr inlines :: [Inline]
    inlines) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    inlines
      let headerStr :: Doc Text
    headerStr = [Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text ([Char] -> Doc Text) -> [Char] -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ if Int
    level Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > 999 then " " else Int -> Char -> [Char]
    forall a. Int -> a -> [a]
    replicate Int
    level '*'
      let drawerStr :: Doc Text
    drawerStr = if Attr
    attr Attr -> Attr -> Bool
    forall a. Eq a => a -> a -> Bool
    == Attr
    nullAttr
                      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
    <> Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest (Int
    level Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1) (Attr -> Doc Text
    propertiesDrawer Attr
    attr)
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    headerStr 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
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    drawerStr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    blankline
    blockToOrg (CodeBlock (_,classes :: [Text]
    classes,kvs :: [(Text, Text)]
    kvs) str :: Text
    str) = do
      let startnum :: Text
    startnum = Text -> (Text -> Text) -> Maybe Text -> Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe "" (\x :: Text
    x -> " " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    trimr Text
    x) (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 "startFrom" [(Text, Text)]
    kvs
      let numberlines :: Text
    numberlines = if "numberLines" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                          then if "continuedSourceBlock" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                                 then " +n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    startnum
                                 else " -n" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    startnum
                          else ""
      let at :: [Text]
    at = (Text -> Text) -> [Text] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Text
    pandocLangToOrg [Text]
    classes [Text] -> [Text] -> [Text]
    forall a. Eq a => [a] -> [a] -> [a]
    `intersect` [Text]
    orgLangIdentifiers
      let (beg :: Text
    beg, end :: [Char]
    end) = case [Text]
    at of
                          []    -> ("#+BEGIN_EXAMPLE" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    numberlines, "#+END_EXAMPLE")
                          (x :: Text
    x:_) -> ("#+BEGIN_SRC " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    x Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    numberlines, "#+END_SRC")
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    beg 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 (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
    $$ [Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text [Char]
    end Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline
    blockToOrg (BlockQuote blocks :: [Block]
    blocks) = do
      Doc Text
    contents <- [Block] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [Block]
    blocks
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "#+BEGIN_QUOTE" 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
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ "#+END_QUOTE" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline
    blockToOrg (Table caption' :: [Inline]
    caption' _ _ headers :: [[Block]]
    headers rows :: [[[Block]]]
    rows) =  do
      Doc Text
    caption'' <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    caption'
      let caption :: Doc Text
    caption = if [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    caption'
                       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
    caption''
      [Doc Text]
    headers' <- ([Block] -> Org 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] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [[Block]]
    headers
      [[Doc Text]]
    rawRows <- ([[Block]] -> StateT WriterState 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] -> Org 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] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg) [[[Block]]]
    rows
      let numChars :: [Doc Text] -> Int
    numChars = [Int] -> Int
    forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
    maximum ([Int] -> Int) -> ([Doc Text] -> [Int]) -> [Doc Text] -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Doc Text -> Int) -> [Doc Text] -> [Int]
    forall a b. (a -> b) -> [a] -> [b]
    map Doc Text -> Int
    forall a. (IsString a, HasChars a) => Doc a -> Int
    offset
      -- FIXME: width is not being used.
      let widthsInChars :: [Int]
    widthsInChars =
           ([Doc Text] -> Int) -> [[Doc Text]] -> [Int]
    forall a b. (a -> b) -> [a] -> [b]
    map [Doc Text] -> Int
    numChars ([[Doc Text]] -> [Int]) -> [[Doc Text]] -> [Int]
    forall a b. (a -> b) -> a -> b
    $ [[Doc Text]] -> [[Doc Text]]
    forall a. [[a]] -> [[a]]
    transpose ([Doc Text]
    headers' [Doc Text] -> [[Doc Text]] -> [[Doc Text]]
    forall a. a -> [a] -> [a]
    : [[Doc Text]]
    rawRows)
      -- FIXME: Org doesn't allow blocks with height more than 1.
      let hpipeBlocks :: [Doc a] -> Doc a
    hpipeBlocks blocks :: [Doc a]
    blocks = [Doc a] -> Doc a
    forall a. [Doc a] -> Doc a
    hcat [Doc a
    beg, Doc a
    middle, Doc a
    end]
            where sep' :: Doc a
    sep'   = a -> Doc a
    forall a. HasChars a => a -> Doc a
    vfill " | "
                  beg :: Doc a
    beg    = a -> Doc a
    forall a. HasChars a => a -> Doc a
    vfill "| "
                  end :: Doc a
    end    = a -> Doc a
    forall a. HasChars a => a -> Doc a
    vfill " |"
                  middle :: Doc a
    middle = [Doc a] -> Doc a
    forall a. [Doc a] -> Doc a
    hcat ([Doc a] -> Doc a) -> [Doc a] -> Doc a
    forall a b. (a -> b) -> a -> b
    $ Doc a -> [Doc a] -> [Doc a]
    forall a. a -> [a] -> [a]
    intersperse Doc a
    sep' [Doc a]
    blocks
      let makeRow :: [Doc Text] -> Doc Text
    makeRow = [Doc Text] -> Doc Text
    forall a. HasChars a => [Doc a] -> Doc a
    hpipeBlocks ([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) -> [Int] -> [Doc Text] -> [Doc Text]
    forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
    zipWith Int -> Doc Text -> Doc Text
    forall a. HasChars a => Int -> Doc a -> Doc a
    lblock [Int]
    widthsInChars
      let head' :: Doc Text
    head' = [Doc Text] -> Doc Text
    makeRow [Doc Text]
    headers'
      [Doc Text]
    rows' <- ([[Block]] -> Org 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 (\row :: [[Block]]
    row -> do [Doc Text]
    cols <- ([Block] -> Org 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] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [[Block]]
    row
                                Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> Doc Text
    makeRow [Doc Text]
    cols) [[[Block]]]
    rows
      let border :: Char -> Doc a
    border ch :: Char
    ch = Char -> Doc a
    forall a. HasChars a => Char -> Doc a
    char '|' Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc a
    forall a. HasChars a => Char -> Doc a
    char Char
    ch Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <>
                      ([Doc a] -> Doc a
    forall a. [Doc a] -> Doc a
    hcat ([Doc a] -> Doc a) -> ([Doc a] -> [Doc a]) -> [Doc a] -> Doc a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Doc a -> [Doc a] -> [Doc a]
    forall a. a -> [a] -> [a]
    intersperse (Char -> Doc a
    forall a. HasChars a => Char -> Doc a
    char Char
    ch Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc a
    forall a. HasChars a => Char -> Doc a
    char '+' Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc a
    forall a. HasChars a => Char -> Doc a
    char Char
    ch) ([Doc a] -> Doc a) -> [Doc a] -> Doc a
    forall a b. (a -> b) -> a -> b
    $
                              (Int -> Doc a) -> [Int] -> [Doc a]
    forall a b. (a -> b) -> [a] -> [b]
    map (\l :: Int
    l -> [Char] -> Doc a
    forall a. HasChars a => [Char] -> Doc a
    text ([Char] -> Doc a) -> [Char] -> Doc a
    forall a b. (a -> b) -> a -> b
    $ Int -> Char -> [Char]
    forall a. Int -> a -> [a]
    replicate Int
    l Char
    ch) [Int]
    widthsInChars) Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <>
                      Char -> Doc a
    forall a. HasChars a => Char -> Doc a
    char Char
    ch Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc a
    forall a. HasChars a => Char -> Doc a
    char '|'
      let body :: Doc Text
    body = [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat [Doc Text]
    rows'
      let head'' :: 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]]
    headers
                      then Doc Text
    forall a. Doc a
    empty
                      else Doc Text
    head' Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    border '-'
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    head'' Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    body 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. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline
    blockToOrg (BulletList items :: [[Block]]
    items) = do
      [Doc Text]
    contents <- ([Block] -> Org 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] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    bulletListItemToOrg [[Block]]
    items
      -- ensure that sublists have preceding blank line
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
               (if [[Block]] -> Bool
    isTightList [[Block]]
    items then [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat else [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vsep) [Doc Text]
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
               Doc Text
    forall a. Doc a
    blankline
    blockToOrg (OrderedList (start :: Int
    start, _, delim :: ListNumberDelim
    delim) items :: [[Block]]
    items) = do
      let delim' :: ListNumberDelim
    delim' = case ListNumberDelim
    delim of
                        TwoParens -> ListNumberDelim
    OneParen
                        x :: ListNumberDelim
    x         -> ListNumberDelim
    x
      let markers :: [Text]
    markers = Int -> [Text] -> [Text]
    forall a. Int -> [a] -> [a]
    take ([[Block]] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [[Block]]
    items) ([Text] -> [Text]) -> [Text] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ (Int, ListNumberStyle, ListNumberDelim) -> [Text]
    orderedListMarkers
                                          (Int
    start, ListNumberStyle
    Decimal, ListNumberDelim
    delim')
      let maxMarkerLength :: Int
    maxMarkerLength = [Int] -> Int
    forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
    maximum ([Int] -> Int) -> [Int] -> Int
    forall a b. (a -> b) -> a -> b
    $ (Text -> Int) -> [Text] -> [Int]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Int
    T.length [Text]
    markers
      let markers' :: [Text]
    markers' = (Text -> Text) -> [Text] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\m :: Text
    m -> let s :: Int
    s = Int
    maxMarkerLength Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Text -> Int
    T.length Text
    m
                                in  Text
    m Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Int -> Text -> Text
    T.replicate Int
    s " ") [Text]
    markers
      [Doc Text]
    contents <- (Text -> [Block] -> Org m (Doc Text))
    -> [Text] -> [[Block]] -> StateT WriterState m [Doc Text]
    forall (m :: * -> *) a b c.
    Applicative m =>
    (a -> b -> m c) -> [a] -> [b] -> m [c]
    zipWithM Text -> [Block] -> Org m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> [Block] -> Org m (Doc Text)
    orderedListItemToOrg [Text]
    markers' [[Block]]
    items
      -- ensure that sublists have preceding blank line
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    forall a. Doc a
    blankline Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
               (if [[Block]] -> Bool
    isTightList [[Block]]
    items then [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat else [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vsep) [Doc Text]
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
               Doc Text
    forall a. Doc a
    blankline
    blockToOrg (DefinitionList items :: [([Inline], [[Block]])]
    items) = do
      [Doc Text]
    contents <- (([Inline], [[Block]]) -> Org 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]]) -> Org m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    ([Inline], [[Block]]) -> Org m (Doc Text)
    definitionListItemToOrg [([Inline], [[Block]])]
    items
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat [Doc Text]
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    blankline

    -- | Convert bullet list item (list of blocks) to Org.
    bulletListItemToOrg :: PandocMonad m => [Block] -> Org m (Doc Text)
    bulletListItemToOrg :: [Block] -> Org m (Doc Text)
    bulletListItemToOrg items :: [Block]
    items = do
      Doc Text
    contents <- [Block] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [Block]
    items
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Int -> Doc Text -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
    hang 2 "- " Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
              if [Block] -> Bool
    endsWithPlain [Block]
    items
                 then Doc Text
    forall a. Doc a
    cr
                 else Doc Text
    forall a. Doc a
    blankline

    -- | Convert ordered list item (a list of blocks) to Org.
    orderedListItemToOrg :: PandocMonad m
                         => Text   -- ^ marker for list item
                         -> [Block]  -- ^ list item (list of blocks)
                         -> Org m (Doc Text)
    orderedListItemToOrg :: Text -> [Block] -> Org m (Doc Text)
    orderedListItemToOrg marker :: Text
    marker items :: [Block]
    items = do
      Doc Text
    contents <- [Block] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [Block]
    items
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Int -> Doc Text -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
    hang (Text -> Int
    T.length Text
    marker Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1) (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    marker Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    space) Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
              if [Block] -> Bool
    endsWithPlain [Block]
    items
                 then Doc Text
    forall a. Doc a
    cr
                 else Doc Text
    forall a. Doc a
    blankline

    -- | Convert definition list item (label, list of blocks) to Org.
    definitionListItemToOrg :: PandocMonad m
                            => ([Inline], [[Block]]) -> Org m (Doc Text)
    definitionListItemToOrg :: ([Inline], [[Block]]) -> Org m (Doc Text)
    definitionListItemToOrg (label :: [Inline]
    label, defs :: [[Block]]
    defs) = do
      Doc Text
    label' <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    label
      Doc Text
    contents <- [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> Org m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ([Block] -> Org 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] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> Org m (Doc Text)
    blockListToOrg [[Block]]
    defs
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Int -> Doc Text -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a -> Doc a
    hang 2 "- " (Doc Text
    label' 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
    contents) Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
          if [[Block]] -> Bool
    isTightList [[Block]]
    defs
             then Doc Text
    forall a. Doc a
    cr
             else Doc Text
    forall a. Doc a
    blankline

    -- | Convert list of key/value pairs to Org :PROPERTIES: drawer.
    propertiesDrawer :: Attr -> Doc Text
    propertiesDrawer :: Attr -> Doc Text
    propertiesDrawer (ident :: Text
    ident, classes :: [Text]
    classes, kv :: [(Text, Text)]
    kv) =
      let
        drawerStart :: Doc Text
    drawerStart = [Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text ":PROPERTIES:"
        drawerEnd :: Doc Text
    drawerEnd   = [Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text ":END:"
        kv' :: [(Text, Text)]
    kv'  = if [Text]
    classes [Text] -> [Text] -> Bool
    forall a. Eq a => a -> a -> Bool
    == [Text]
    forall a. Monoid a => a
    mempty then [(Text, Text)]
    kv  else ("CLASS", [Text] -> Text
    T.unwords [Text]
    classes)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
    forall a. a -> [a] -> [a]
    :[(Text, Text)]
    kv
        kv'' :: [(Text, Text)]
    kv'' = if Text
    ident Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    forall a. Monoid a => a
    mempty   then [(Text, Text)]
    kv' else ("CUSTOM_ID", Text
    ident)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
    forall a. a -> [a] -> [a]
    :[(Text, Text)]
    kv'
        properties :: Doc Text
    properties = [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 (Text, Text) -> Doc Text
    kvToOrgProperty [(Text, Text)]
    kv''
      in
        Doc Text
    drawerStart 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
    properties 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
    drawerEnd
     where
       kvToOrgProperty :: (Text, Text) -> Doc Text
       kvToOrgProperty :: (Text, Text) -> Doc Text
    kvToOrgProperty (key :: Text
    key, value :: Text
    value) =
         [Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text ":" 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
    key Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> [Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text ": " 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
    value Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr

    attrHtml :: Attr -> Doc Text
    attrHtml :: Attr -> Doc Text
    attrHtml (""   , []     , []) = Doc Text
    forall a. Monoid a => a
    mempty
    attrHtml (ident :: Text
    ident, classes :: [Text]
    classes, kvs :: [(Text, Text)]
    kvs) =
      let
        name :: Doc Text
    name = if Text -> Bool
    T.null Text
    ident then Doc Text
    forall a. Monoid a => a
    mempty else "#+NAME: " 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
    ident Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr
        keyword :: Doc Text
    keyword = "#+ATTR_HTML"
        classKv :: (Text, Text)
    classKv = ("class", [Text] -> Text
    T.unwords [Text]
    classes)
        kvStrings :: [Text]
    kvStrings = ((Text, Text) -> Text) -> [(Text, Text)] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\(k :: Text
    k,v :: Text
    v) -> ":" Text -> Text -> Text
    forall a. Semigroup 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) ((Text, Text)
    classKv(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
    forall a. a -> [a] -> [a]
    :[(Text, Text)]
    kvs)
      in Doc Text
    name Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    keyword 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
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal ([Text] -> Text
    T.unwords [Text]
    kvStrings) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr

    -- | Convert list of Pandoc block elements to Org.
    blockListToOrg :: PandocMonad m
                   => [Block]       -- ^ List of block elements
                   -> Org m (Doc Text)
    blockListToOrg :: [Block] -> Org m (Doc Text)
    blockListToOrg blocks :: [Block]
    blocks = [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> Org m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Block -> Org 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 -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> Org m (Doc Text)
    blockToOrg [Block]
    blocks

    -- | Convert list of Pandoc inline elements to Org.
    inlineListToOrg :: PandocMonad m
                    => [Inline]
                    -> Org m (Doc Text)
    inlineListToOrg :: [Inline] -> Org m (Doc Text)
    inlineListToOrg lst :: [Inline]
    lst = [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    hcat ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text] -> Org m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Inline -> Org 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 -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Inline -> Org m (Doc Text)
    inlineToOrg ([Inline] -> [Inline]
    fixMarkers [Inline]
    lst)
      where fixMarkers :: [Inline] -> [Inline]
    fixMarkers [] = []  -- prevent note refs and list markers from wrapping, see #4171
            fixMarkers (Space : x :: Inline
    x : rest :: [Inline]
    rest) | Inline -> Bool
    shouldFix Inline
    x =
              Text -> Inline
    Str " " Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixMarkers [Inline]
    rest
            fixMarkers (SoftBreak : x :: Inline
    x : rest :: [Inline]
    rest) | Inline -> Bool
    shouldFix Inline
    x =
              Text -> Inline
    Str " " Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixMarkers [Inline]
    rest
            fixMarkers (x :: Inline
    x : rest :: [Inline]
    rest) = Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixMarkers [Inline]
    rest

            shouldFix :: Inline -> Bool
    shouldFix Note{} = Bool
    True -- Prevent footnotes
            shouldFix (Str "-") = Bool
    True -- Prevent bullet list items
            -- TODO: prevent ordered list items
            shouldFix _ = Bool
    False

    -- | Convert Pandoc inline element to Org.
    inlineToOrg :: PandocMonad m => Inline -> Org m (Doc Text)
    inlineToOrg :: Inline -> Org m (Doc Text)
    inlineToOrg (Span (uid :: Text
    uid, [], []) []) =
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org 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
    uid Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> ">>"
    inlineToOrg (Span _ lst :: [Inline]
    lst) =
      [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
    inlineToOrg (Emph lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "/" 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
    <> "/"
    inlineToOrg (Strong lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "*" 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
    <> "*"
    inlineToOrg (Strikeout lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "+" 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
    <> "+"
    inlineToOrg (Superscript lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "^{" 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
    <> "}"
    inlineToOrg (Subscript lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "_{" 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
    <> "}"
    inlineToOrg (SmallCaps lst :: [Inline]
    lst) = [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
    inlineToOrg (Quoted SingleQuote lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "'" 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
    <> "'"
    inlineToOrg (Quoted DoubleQuote lst :: [Inline]
    lst) = do
      Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "\"" 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
    <> "\""
    inlineToOrg (Cite _  lst :: [Inline]
    lst) = [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    lst
    inlineToOrg (Code _ str :: Text
    str) = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org 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
    str Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "="
    inlineToOrg (Str str :: Text
    str) = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text))
    -> (Text -> Doc Text) -> Text -> Org m (Doc Text)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Org m (Doc Text)) -> Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Text
    escapeString Text
    str
    inlineToOrg (Math t :: MathType
    t str :: Text
    str) = 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{ stHasMath :: Bool
    stHasMath = Bool
    True }
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ if MathType
    t MathType -> MathType -> Bool
    forall a. Eq a => a -> a -> Bool
    == MathType
    InlineMath
                  then "$" 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
    str Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "$"
                  else "$$" 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
    str Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "$$"
    inlineToOrg il :: Inline
    il@(RawInline f :: Format
    f str :: Text
    str)
      | Format -> Bool
    isRawFormat Format
    f = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str
      | Bool
    otherwise     = 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 -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    inlineToOrg LineBreak = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([Char] -> Doc Text
    forall a. HasChars a => [Char] -> Doc a
    text "\\\\" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr)
    inlineToOrg Space = Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
    inlineToOrg 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
           WrapPreserve -> Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    cr
           WrapAuto     -> Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
           WrapNone     -> Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
    inlineToOrg (Link _ txt :: [Inline]
    txt (src :: Text
    src, _)) =
      case [Inline]
    txt of
            [Str x :: Text
    x] | Text -> Text
    escapeURI Text
    x Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    src ->  -- autolink
                 Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org 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
    orgPath Text
    x) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "]]"
            _ -> do Doc Text
    contents <- [Inline] -> Org m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> Org m (Doc Text)
    inlineListToOrg [Inline]
    txt
                    Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org 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
    orgPath Text
    src) 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
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "]]"
    inlineToOrg (Image _ _ (source :: Text
    source, _)) =
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org 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
    orgPath Text
    source) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "]]"
    inlineToOrg (Note contents :: [Block]
    contents) = do
      -- add to notes in state
      [[Block]]
    notes <- (WriterState -> [[Block]]) -> StateT WriterState m [[Block]]
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> [[Block]]
    stNotes
      (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 :: [[Block]]
    stNotes = [Block]
    contents[Block] -> [[Block]] -> [[Block]]
    forall a. a -> [a] -> [a]
    :[[Block]]
    notes }
      let ref :: Text
    ref = Int -> Text
    forall a. Show a => a -> Text
    tshow (Int -> Text) -> Int -> Text
    forall a b. (a -> b) -> a -> b
    $ [[Block]] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [[Block]]
    notes Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1
      Doc Text -> Org m (Doc Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Org m (Doc Text)) -> Doc Text -> Org m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ "[fn:" 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
    ref Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> "]"

    orgPath :: Text -> Text
    orgPath :: Text -> Text
    orgPath src :: Text
    src = case Text -> Maybe (Char, Text)
    T.uncons Text
    src of
      Nothing            -> ""             -- wiki link
      Just ('#', _)      -> Text
    src            -- internal link
      _ | Text -> Bool
    isUrl Text
    src      -> Text
    src
      _ | Text -> Bool
    isFilePath Text
    src -> Text
    src
      _                  -> "file:" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    src
      where
        isFilePath :: Text -> Bool
        isFilePath :: Text -> Bool
    isFilePath cs :: Text
    cs = (Text -> Bool) -> [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (Text -> Text -> Bool
    `T.isPrefixOf` Text
    cs) ["/", "./", "../", "file:"]

        isUrl :: Text -> Bool
        isUrl :: Text -> Bool
    isUrl cs :: Text
    cs =
          let (scheme :: Text
    scheme, path :: Text
    path) = (Char -> Bool) -> Text -> (Text, Text)
    T.break (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == ':') Text
    cs
          in (Char -> Bool) -> Text -> Bool
    T.all (\c :: Char
    c -> Char -> Bool
    isAlphaNum Char
    c Bool -> Bool -> Bool
    || Char
    c Char -> Text -> Bool
    `elemText` ".-") Text
    scheme
             Bool -> Bool -> Bool
    && Bool -> Bool
    not (Text -> Bool
    T.null Text
    path)

    -- | Translate from pandoc's programming language identifiers to those used by
    -- org-mode.
    pandocLangToOrg :: Text -> Text
    pandocLangToOrg :: Text -> Text
    pandocLangToOrg cs :: Text
    cs =
      case Text
    cs of
        "c"          -> "C"
        "cpp"        -> "C++"
        "commonlisp" -> "lisp"
        "r"          -> "R"
        "bash"       -> "sh"
        _            -> Text
    cs

    -- | List of language identifiers recognized by org-mode.
    orgLangIdentifiers :: [Text]
    orgLangIdentifiers :: [Text]
    orgLangIdentifiers =
      [ "asymptote", "awk", "C", "C++", "clojure", "css", "d", "ditaa", "dot"
      , "calc", "emacs-lisp", "fortran", "gnuplot", "haskell", "java", "js"
      , "latex", "ledger", "lisp", "lilypond", "matlab", "mscgen", "ocaml"
      , "octave", "org", "oz", "perl", "plantuml", "processing", "python", "R"
      , "ruby", "sass", "scheme", "screen", "sed", "sh", "sql", "sqlite"
      ]