{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Reflex.Dom.Pandoc.Document
  ( elPandoc,
    elPandocInlines,
    elPandocBlocks,
    Config (..),
    PandocRawNode (..),
    defaultConfig,
  )
where

import Control.Monad (guard, void)
import Control.Monad.Reader
  ( MonadReader (ask),
    MonadTrans (lift),
    ReaderT (runReaderT),
  )
import Data.Bool (bool)
import qualified Data.Map as Map
import Data.Map.Strict (Map)
import Data.Text (Text)
import qualified Data.Text as T
import Reflex.Dom.Core hiding (Link, Space, mapAccum)
import Reflex.Dom.Pandoc.Footnotes
import Reflex.Dom.Pandoc.Raw (PandocRawNode (..), elPandocRawNodeSafe)
import Reflex.Dom.Pandoc.SyntaxHighlighting (elCodeHighlighted)
import Reflex.Dom.Pandoc.Util (elPandocAttr, headerElement, plainify, renderAttr, sansEmptyAttrs)
import Text.Pandoc.Definition

data Config t m a = Config
  { -- | Custom link renderer.
    Config t m a
-> m a -> Text -> Map Text Text -> Maybe [Inline] -> m a
_config_renderLink ::
      m a ->
      -- Link URL
      Text ->
      -- Link attributes, including "title"
      Map Text Text ->
      -- Inner body of the link. Nothing if same as URL (i.e., an autolink)
      Maybe [Inline] ->
      m a,
    -- | How to render code blocks
    Config t m a -> m () -> Attr -> Text -> m ()
_config_renderCode ::
      m () ->
      Attr ->
      Text ->
      m (),
    -- | How to render raw nodes
    Config t m a -> PandocRawNode -> m a
_config_renderRaw ::
      PandocRawNode ->
      m a
  }

defaultConfig :: DomBuilder t m => Config t m ()
defaultConfig :: Config t m ()
defaultConfig =
  (m () -> Text -> Map Text Text -> Maybe [Inline] -> m ())
-> (m () -> Attr -> Text -> m ())
-> (PandocRawNode -> m ())
-> Config t m ()
forall t (m :: * -> *) a.
(m a -> Text -> Map Text Text -> Maybe [Inline] -> m a)
-> (m () -> Attr -> Text -> m ())
-> (PandocRawNode -> m a)
-> Config t m a
Config
    (\m ()
f Text
_ Map Text Text
_ Maybe [Inline]
_ -> m ()
f m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
    (\m ()
f Attr
_ Text
_ -> m ()
f)
    PandocRawNode -> m ()
forall t (m :: * -> *). DomBuilder t m => PandocRawNode -> m ()
elPandocRawNodeSafe

-- | Convert Markdown to HTML
elPandoc :: forall t m a. (DomBuilder t m, Monoid a) => Config t m a -> Pandoc -> m a
elPandoc :: Config t m a -> Pandoc -> m a
elPandoc Config t m a
cfg doc :: Pandoc
doc@(Pandoc Meta
_meta [Block]
blocks) = do
  let fs :: Footnotes
fs = Pandoc -> Footnotes
queryFootnotes Pandoc
doc
  a
x <- (ReaderT Footnotes m a -> Footnotes -> m a)
-> Footnotes -> ReaderT Footnotes m a -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT Footnotes m a -> Footnotes -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT Footnotes
fs (ReaderT Footnotes m a -> m a) -> ReaderT Footnotes m a -> m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
blocks
  (a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
<>) (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Block] -> m a) -> Footnotes -> m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
([Block] -> m a) -> Footnotes -> m a
renderFootnotes (ReaderT Footnotes m a -> m a
forall t (m :: * -> *) a.
DomBuilder t m =>
ReaderT Footnotes m a -> m a
sansFootnotes (ReaderT Footnotes m a -> m a)
-> ([Block] -> ReaderT Footnotes m a) -> [Block] -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg) Footnotes
fs

-- | Render list of Pandoc inlines
elPandocInlines :: DomBuilder t m => [Inline] -> m ()
elPandocInlines :: [Inline] -> m ()
elPandocInlines = m () -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m () -> m ()) -> ([Inline] -> m ()) -> [Inline] -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReaderT Footnotes m () -> m ()
forall t (m :: * -> *) a.
DomBuilder t m =>
ReaderT Footnotes m a -> m a
sansFootnotes (ReaderT Footnotes m () -> m ())
-> ([Inline] -> ReaderT Footnotes m ()) -> [Inline] -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config t m () -> [Inline] -> ReaderT Footnotes m ()
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m ()
forall t (m :: * -> *). DomBuilder t m => Config t m ()
defaultConfig

-- | Render list of Pandoc Blocks
elPandocBlocks :: DomBuilder t m => [Block] -> m ()
elPandocBlocks :: [Block] -> m ()
elPandocBlocks = m () -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m () -> m ()) -> ([Block] -> m ()) -> [Block] -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReaderT Footnotes m () -> m ()
forall t (m :: * -> *) a.
DomBuilder t m =>
ReaderT Footnotes m a -> m a
sansFootnotes (ReaderT Footnotes m () -> m ())
-> ([Block] -> ReaderT Footnotes m ()) -> [Block] -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config t m () -> [Block] -> ReaderT Footnotes m ()
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m ()
forall t (m :: * -> *). DomBuilder t m => Config t m ()
defaultConfig

mapAccum :: (Monoid b, Applicative f) => (a -> f b) -> [a] -> f b
mapAccum :: (a -> f b) -> [a] -> f b
mapAccum a -> f b
f =
  ([b] -> b) -> f [b] -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [b] -> b
forall a. Monoid a => [a] -> a
mconcat (f [b] -> f b) -> ([a] -> f [b]) -> [a] -> f b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f b) -> [a] -> f [b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f

renderBlocks :: (DomBuilder t m, Monoid a) => Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks :: Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg =
  (Block -> ReaderT Footnotes m a)
-> [Block] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum ((Block -> ReaderT Footnotes m a)
 -> [Block] -> ReaderT Footnotes m a)
-> (Block -> ReaderT Footnotes m a)
-> [Block]
-> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> Block -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> Block -> ReaderT Footnotes m a
renderBlock Config t m a
cfg

renderBlock :: (DomBuilder t m, Monoid a) => Config t m a -> Block -> ReaderT Footnotes m a
renderBlock :: Config t m a -> Block -> ReaderT Footnotes m a
renderBlock Config t m a
cfg = \case
  -- Pandoc parses github tasklist as this structure.
  Plain (Str Text
"☐" : Inline
Space : [Inline]
is) -> Bool -> ReaderT Footnotes m ()
forall t (f :: * -> *). DomBuilder t f => Bool -> f ()
checkboxEl Bool
False ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
is
  Plain (Str Text
"☒" : Inline
Space : [Inline]
is) -> Bool -> ReaderT Footnotes m ()
forall t (f :: * -> *). DomBuilder t f => Bool -> f ()
checkboxEl Bool
True ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
is
  Para (Str Text
"☐" : Inline
Space : [Inline]
is) -> Bool -> ReaderT Footnotes m ()
forall t (f :: * -> *). DomBuilder t f => Bool -> f ()
checkboxEl Bool
False ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
is
  Para (Str Text
"☒" : Inline
Space : [Inline]
is) -> Bool -> ReaderT Footnotes m ()
forall t (f :: * -> *). DomBuilder t f => Bool -> f ()
checkboxEl Bool
True ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
is
  Plain [Inline]
xs ->
    Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Para [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"p" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  LineBlock [[Inline]]
xss ->
    (([Inline] -> ReaderT Footnotes m a)
 -> [[Inline]] -> ReaderT Footnotes m a)
-> [[Inline]]
-> ([Inline] -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ([Inline] -> ReaderT Footnotes m a)
-> [[Inline]] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [[Inline]]
xss (([Inline] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> ([Inline] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \[Inline]
xs -> do
      Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs ReaderT Footnotes m a
-> ReaderT Footnotes m () -> ReaderT Footnotes m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"\n"
  CodeBlock Attr
attr Text
x -> do
    m () -> ReaderT Footnotes m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ReaderT Footnotes m ()) -> m () -> ReaderT Footnotes m ()
forall a b. (a -> b) -> a -> b
$ Config t m a -> m () -> Attr -> Text -> m ()
forall t (m :: * -> *) a.
Config t m a -> m () -> Attr -> Text -> m ()
_config_renderCode Config t m a
cfg (Attr -> Text -> m ()
forall t (m :: * -> *). DomBuilder t m => Attr -> Text -> m ()
elCodeHighlighted Attr
attr Text
x) Attr
attr Text
x
    a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  RawBlock Format
fmt Text
x ->
    m a -> ReaderT Footnotes m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT Footnotes m a) -> m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> PandocRawNode -> m a
forall t (m :: * -> *) a. Config t m a -> PandocRawNode -> m a
_config_renderRaw Config t m a
cfg (Format -> Text -> PandocRawNode
PandocRawNode_Block Format
fmt Text
x) m a -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  BlockQuote [Block]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"blockquote" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
xs
  OrderedList (Int
idx, ListNumberStyle
style, ListNumberDelim
_delim) [[Block]]
xss ->
    -- delimStyle is not supported in HTML or in Semantic UI
    Text
-> Map Text Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Map Text Text -> m a -> m a
elAttr Text
"ol" (ListNumberStyle -> Map Text Text
listStyle ListNumberStyle
style Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<> Int -> Map Text Text
forall a a.
(Monoid a, At a, IsString (Index a), Show a, Eq a, Num a,
 IxValue a ~ Text) =>
a -> a
startFrom Int
idx) (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
      (([Block] -> ReaderT Footnotes m a)
 -> [[Block]] -> ReaderT Footnotes m a)
-> [[Block]]
-> ([Block] -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ([Block] -> ReaderT Footnotes m a)
-> [[Block]] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [[Block]]
xss (([Block] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> ([Block] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \[Block]
xs -> do
        Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"li" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
xs
  BulletList [[Block]]
xss ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"ul" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ (([Block] -> ReaderT Footnotes m a)
 -> [[Block]] -> ReaderT Footnotes m a)
-> [[Block]]
-> ([Block] -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ([Block] -> ReaderT Footnotes m a)
-> [[Block]] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [[Block]]
xss (([Block] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> ([Block] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \[Block]
xs -> Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"li" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
xs
  DefinitionList [([Inline], [[Block]])]
defs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"dl" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$
      ((([Inline], [[Block]]) -> ReaderT Footnotes m a)
 -> [([Inline], [[Block]])] -> ReaderT Footnotes m a)
-> [([Inline], [[Block]])]
-> (([Inline], [[Block]]) -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (([Inline], [[Block]]) -> ReaderT Footnotes m a)
-> [([Inline], [[Block]])] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [([Inline], [[Block]])]
defs ((([Inline], [[Block]]) -> ReaderT Footnotes m a)
 -> ReaderT Footnotes m a)
-> (([Inline], [[Block]]) -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \([Inline]
term, [[Block]]
descList) -> do
        a
x <- Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"dt" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
term
        (a -> a) -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
<>) (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$
          (([Block] -> ReaderT Footnotes m a)
 -> [[Block]] -> ReaderT Footnotes m a)
-> [[Block]]
-> ([Block] -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ([Block] -> ReaderT Footnotes m a)
-> [[Block]] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [[Block]]
descList (([Block] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> ([Block] -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \[Block]
desc ->
            Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"dd" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
desc
  Header Int
level Attr
attr [Inline]
xs ->
    Text -> Attr -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Attr -> m a -> m a
elPandocAttr (Int -> Text
headerElement Int
level) Attr
attr (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
      Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Block
HorizontalRule ->
    Text -> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"hr" ReaderT Footnotes m ()
forall (m :: * -> *). Monad m => m ()
blank ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Table Attr
attr Caption
_captions [ColSpec]
_colSpec (TableHead Attr
_ [Row]
hrows) [TableBody]
tbodys TableFoot
_tfoot -> do
    -- TODO: Apply captions, colSpec, etc.
    Text -> Attr -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Attr -> m a -> m a
elPandocAttr Text
"table" Attr
attr (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
      a
x <- Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"thead" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
        ((Row -> ReaderT Footnotes m a) -> [Row] -> ReaderT Footnotes m a)
-> [Row] -> (Row -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Row -> ReaderT Footnotes m a) -> [Row] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [Row]
hrows ((Row -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> (Row -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \(Row Attr
_ [Cell]
cells) -> do
          Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"tr" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
            ((Cell -> ReaderT Footnotes m a)
 -> [Cell] -> ReaderT Footnotes m a)
-> [Cell]
-> (Cell -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Cell -> ReaderT Footnotes m a) -> [Cell] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [Cell]
cells ((Cell -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> (Cell -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \(Cell Attr
_ Alignment
_ RowSpan
_ ColSpan
_ [Block]
blks) ->
              Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"th" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
blks
      (a -> a) -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
<>) (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$
        ((TableBody -> ReaderT Footnotes m a)
 -> [TableBody] -> ReaderT Footnotes m a)
-> [TableBody]
-> (TableBody -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (TableBody -> ReaderT Footnotes m a)
-> [TableBody] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [TableBody]
tbodys ((TableBody -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> (TableBody -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \(TableBody Attr
_ RowHeadColumns
_ [Row]
_ [Row]
rows) ->
          Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"tbody" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
            ((Row -> ReaderT Footnotes m a) -> [Row] -> ReaderT Footnotes m a)
-> [Row] -> (Row -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Row -> ReaderT Footnotes m a) -> [Row] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [Row]
rows ((Row -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> (Row -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \(Row Attr
_ [Cell]
cells) ->
              Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"tr" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
                ((Cell -> ReaderT Footnotes m a)
 -> [Cell] -> ReaderT Footnotes m a)
-> [Cell]
-> (Cell -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Cell -> ReaderT Footnotes m a) -> [Cell] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum [Cell]
cells ((Cell -> ReaderT Footnotes m a) -> ReaderT Footnotes m a)
-> (Cell -> ReaderT Footnotes m a) -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ \(Cell Attr
_ Alignment
_ RowSpan
_ ColSpan
_ [Block]
blks) ->
                  Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"td" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
blks
  Div Attr
attr [Block]
xs ->
    Text -> Attr -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Attr -> m a -> m a
elPandocAttr Text
"div" Attr
attr (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$
      Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
xs
  Block
Null ->
    ReaderT Footnotes m ()
forall (m :: * -> *). Monad m => m ()
blank ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  where
    checkboxEl :: Bool -> f ()
checkboxEl Bool
checked = do
      let attrs :: Map Text Text
attrs =
            [Map Text Text] -> Map Text Text
forall a. Monoid a => [a] -> a
mconcat ([Map Text Text] -> Map Text Text)
-> [Map Text Text] -> Map Text Text
forall a b. (a -> b) -> a -> b
$
              [ Index (Map Text Text)
"type" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: IxValue (Map Text Text)
"checkbox",
                Index (Map Text Text)
"disabled" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: IxValue (Map Text Text)
"True",
                Map Text Text -> Map Text Text -> Bool -> Map Text Text
forall a. a -> a -> Bool -> a
bool Map Text Text
forall a. Monoid a => a
mempty (Index (Map Text Text)
"checked" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: IxValue (Map Text Text)
"True") Bool
checked
              ]
      f () -> f ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$ Text -> Map Text Text -> f () -> f ()
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Map Text Text -> m a -> m a
elAttr Text
"input" Map Text Text
attrs f ()
forall (m :: * -> *). Monad m => m ()
blank
    startFrom :: a -> a
startFrom a
idx = a -> a -> Bool -> a
forall a. a -> a -> Bool -> a
bool a
forall a. Monoid a => a
mempty (Index a
"start" Index a -> IxValue a -> a
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: String -> Text
T.pack (a -> String
forall a. Show a => a -> String
show a
idx)) (a
idx a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
1)
    listStyle :: ListNumberStyle -> Map Text Text
listStyle = \case
      ListNumberStyle
LowerRoman -> Index (Map Text Text)
"type" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: IxValue (Map Text Text)
"i"
      ListNumberStyle
UpperRoman -> Index (Map Text Text)
"type" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: IxValue (Map Text Text)
"I"
      ListNumberStyle
LowerAlpha -> Index (Map Text Text)
"type" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: IxValue (Map Text Text)
"a"
      ListNumberStyle
UpperAlpha -> Index (Map Text Text)
"type" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: IxValue (Map Text Text)
"A"
      ListNumberStyle
_ -> Map Text Text
forall a. Monoid a => a
mempty

renderInlines :: (DomBuilder t m, Monoid a) => Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines :: Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg =
  (Inline -> ReaderT Footnotes m a)
-> [Inline] -> ReaderT Footnotes m a
forall b (f :: * -> *) a.
(Monoid b, Applicative f) =>
(a -> f b) -> [a] -> f b
mapAccum ((Inline -> ReaderT Footnotes m a)
 -> [Inline] -> ReaderT Footnotes m a)
-> (Inline -> ReaderT Footnotes m a)
-> [Inline]
-> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> Inline -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> Inline -> ReaderT Footnotes m a
renderInline Config t m a
cfg

renderInline :: (DomBuilder t m, Monoid a) => Config t m a -> Inline -> ReaderT Footnotes m a
renderInline :: Config t m a -> Inline -> ReaderT Footnotes m a
renderInline Config t m a
cfg = \case
  Str Text
x ->
    Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
x ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Emph [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"em" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Strong [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"strong" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Underline [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"u" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Strikeout [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"strike" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Superscript [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"sup" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Subscript [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"sub" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  SmallCaps [Inline]
xs ->
    Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"small" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Quoted QuoteType
qt [Inline]
xs ->
    (ReaderT Footnotes m a -> QuoteType -> ReaderT Footnotes m a)
-> QuoteType -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT Footnotes m a -> QuoteType -> ReaderT Footnotes m a
forall t (m :: * -> *) b. DomBuilder t m => m b -> QuoteType -> m b
inQuotes QuoteType
qt (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  Cite [Citation]
_ [Inline]
_ -> do
    Text -> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"pre" (ReaderT Footnotes m () -> ReaderT Footnotes m ())
-> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall a b. (a -> b) -> a -> b
$ Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"error[reflex-doc-pandoc]: Pandoc Cite is not handled"
    a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Code Attr
attr Text
x ->
    Text -> Attr -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Attr -> m a -> m a
elPandocAttr Text
"code" Attr
attr (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ do
      Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
x
      a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Inline
Space ->
    Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
" " ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Inline
SoftBreak ->
    Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
" " ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Inline
LineBreak ->
    Text -> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall t (m :: * -> *) a. DomBuilder t m => Text -> m a -> m a
el Text
"br" ReaderT Footnotes m ()
forall (m :: * -> *). Monad m => m ()
blank ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  RawInline Format
fmt Text
x ->
    m a -> ReaderT Footnotes m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT Footnotes m a) -> m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> PandocRawNode -> m a
forall t (m :: * -> *) a. Config t m a -> PandocRawNode -> m a
_config_renderRaw Config t m a
cfg (Format -> Text -> PandocRawNode
PandocRawNode_Inline Format
fmt Text
x) m a -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Math MathType
mathType Text
s -> do
    -- http://docs.mathjax.org/en/latest/basic/mathematics.html#tex-and-latex-input
    case MathType
mathType of
      MathType
InlineMath ->
        Text -> Text -> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Text -> m a -> m a
elClass Text
"span" Text
"math inline" (ReaderT Footnotes m () -> ReaderT Footnotes m ())
-> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall a b. (a -> b) -> a -> b
$ Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text (Text -> ReaderT Footnotes m ()) -> Text -> ReaderT Footnotes m ()
forall a b. (a -> b) -> a -> b
$ Text
"\\(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
s Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\\)"
      MathType
DisplayMath ->
        Text -> Text -> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Text -> m a -> m a
elClass Text
"span" Text
"math display" (ReaderT Footnotes m () -> ReaderT Footnotes m ())
-> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall a b. (a -> b) -> a -> b
$ Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"$$" ReaderT Footnotes m ()
-> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
s ReaderT Footnotes m ()
-> ReaderT Footnotes m () -> ReaderT Footnotes m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"$$"
    a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Link Attr
attr [Inline]
xs (Text
lUrl, Text
lTitle) -> do
    let attrMap :: Map Text Text
attrMap = Attr -> Map Text Text
renderAttr Attr
attr
        defaultRender :: ReaderT Footnotes m a
defaultRender = do
          let attr' :: Map Text Text
attr' = Map Text Text -> Map Text Text
forall k. Map k Text -> Map k Text
sansEmptyAttrs (Map Text Text -> Map Text Text) -> Map Text Text -> Map Text Text
forall a b. (a -> b) -> a -> b
$ Map Text Text
attrMap Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<> Index (Map Text Text)
"href" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: Text
IxValue (Map Text Text)
lUrl Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<> Index (Map Text Text)
"title" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: Text
IxValue (Map Text Text)
lTitle
          Text
-> Map Text Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Map Text Text -> m a -> m a
elAttr Text
"a" Map Text Text
attr' (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
    Footnotes
fns <- ReaderT Footnotes m Footnotes
forall r (m :: * -> *). MonadReader r m => m r
ask
    let minner :: Maybe [Inline]
minner = do
          Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ [Inline]
xs [Inline] -> [Inline] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Text -> Inline
Str Text
lUrl]
          [Inline] -> Maybe [Inline]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Inline]
xs
    m a -> ReaderT Footnotes m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT Footnotes m a) -> m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$
      Config t m a
-> m a -> Text -> Map Text Text -> Maybe [Inline] -> m a
forall t (m :: * -> *) a.
Config t m a
-> m a -> Text -> Map Text Text -> Maybe [Inline] -> m a
_config_renderLink
        Config t m a
cfg
        (ReaderT Footnotes m a -> Footnotes -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT Footnotes m a
defaultRender Footnotes
fns)
        Text
lUrl
        (Map Text Text
attrMap Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<> Index (Map Text Text)
"title" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: Text
IxValue (Map Text Text)
lTitle)
        Maybe [Inline]
minner
  Image Attr
attr [Inline]
xs (Text, Text)
target -> do
    Text
-> Map Text Text
-> ReaderT Footnotes m ()
-> ReaderT Footnotes m ()
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Map Text Text -> m a -> m a
elAttr Text
"img" (Attr -> [Inline] -> (Text, Text) -> Map Text Text
imageAttrs Attr
attr [Inline]
xs (Text, Text)
target) ReaderT Footnotes m ()
forall (m :: * -> *). Monad m => m ()
blank ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Note [Block]
xs -> do
    Footnotes
fs :: Footnotes <- ReaderT Footnotes m Footnotes
forall r (m :: * -> *). MonadReader r m => m r
ask
    case Footnote -> Footnotes -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ([Block] -> Footnote
mkFootnote [Block]
xs) Footnotes
fs of
      Maybe Int
Nothing ->
        -- No footnote in the global map (this means that the user has
        -- defined a footnote inside a footnote); just put the whole thing in
        -- aside.
        Text -> Text -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Text -> m a -> m a
elClass Text
"aside" Text
"footnote-inline" (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$ Config t m a -> [Block] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Block] -> ReaderT Footnotes m a
renderBlocks Config t m a
cfg [Block]
xs
      Just Int
idx ->
        Int -> ReaderT Footnotes m ()
forall t (m :: * -> *). DomBuilder t m => Int -> m ()
renderFootnoteRef Int
idx ReaderT Footnotes m ()
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ReaderT Footnotes m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  Span Attr
attr [Inline]
xs ->
    Text -> Attr -> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
DomBuilder t m =>
Text -> Attr -> m a -> m a
elPandocAttr Text
"span" Attr
attr (ReaderT Footnotes m a -> ReaderT Footnotes m a)
-> ReaderT Footnotes m a -> ReaderT Footnotes m a
forall a b. (a -> b) -> a -> b
$
      Config t m a -> [Inline] -> ReaderT Footnotes m a
forall t (m :: * -> *) a.
(DomBuilder t m, Monoid a) =>
Config t m a -> [Inline] -> ReaderT Footnotes m a
renderInlines Config t m a
cfg [Inline]
xs
  where
    inQuotes :: m b -> QuoteType -> m b
inQuotes m b
w = \case
      QuoteType
SingleQuote -> Text -> m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"‘" m () -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m b
w m b -> m () -> m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"’"
      QuoteType
DoubleQuote -> Text -> m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"“" m () -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m b
w m b -> m () -> m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> m ()
forall t (m :: * -> *). DomBuilder t m => Text -> m ()
text Text
"”"
    -- Pandoc stores Img's alt text as [Inline]
    imageAttrs :: Attr -> [Inline] -> (Text, Text) -> Map Text Text
imageAttrs Attr
attr [Inline]
imgInlines (Text
iUrl, Text
iTitle) =
      Map Text Text -> Map Text Text
forall k. Map k Text -> Map k Text
sansEmptyAttrs (Map Text Text -> Map Text Text) -> Map Text Text -> Map Text Text
forall a b. (a -> b) -> a -> b
$ Attr -> Map Text Text
renderAttr Attr
attr Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<> (Index (Map Text Text)
"src" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: Text
IxValue (Map Text Text)
iUrl Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<> Index (Map Text Text)
"title" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: Text
IxValue (Map Text Text)
iTitle Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<> Index (Map Text Text)
"alt" Index (Map Text Text) -> IxValue (Map Text Text) -> Map Text Text
forall m. (At m, Monoid m) => Index m -> IxValue m -> m
=: [Inline] -> Text
plainify [Inline]
imgInlines)