{-
pandoc-crossref is a pandoc filter for numbering figures,
equations, tables and cross-references to them.
Copyright (C) 2015  Nikolay Yakimov <root@livid.pp.ru>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-}

{-# LANGUAGE Rank2Types, OverloadedStrings #-}
module Text.Pandoc.CrossRef.References.Blocks
  ( replaceAll
  ) where

import Text.Pandoc.Definition
import qualified Text.Pandoc.Builder as B
import Text.Pandoc.Shared (stringify, blocksToInlines)
import Text.Pandoc.Walk (walk)
import Control.Monad.State hiding (get, modify)
import Data.List
import Data.Maybe
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Text.Read as T

import Data.Accessor
import Data.Accessor.Monad.Trans.State
import Text.Pandoc.CrossRef.References.Types
import Text.Pandoc.CrossRef.Util.Util
import Text.Pandoc.CrossRef.Util.Options
import Text.Pandoc.CrossRef.Util.Template
import Control.Applicative
import Prelude
import Data.Default

replaceAll :: (Data a) => Options -> a -> WS a
replaceAll :: Options -> a -> WS a
replaceAll Options
opts =
    GenRR (StateT References Identity)
-> GenericM (StateT References Identity)
forall (m :: * -> *). Monad m => GenRR m -> GenericM m
runReplace ((Block -> StateT References Identity (ReplacedResult Block))
-> a -> StateT References Identity (ReplacedResult a)
forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(b -> m (ReplacedResult b)) -> a -> m (ReplacedResult a)
mkRR (Options
-> Block -> StateT References Identity (ReplacedResult Block)
replaceBlock Options
opts)
      (a -> StateT References Identity (ReplacedResult a))
-> (Inline -> StateT References Identity (ReplacedResult Inline))
-> a
-> StateT References Identity (ReplacedResult a)
forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(a -> m (ReplacedResult a))
-> (b -> m (ReplacedResult b)) -> a -> m (ReplacedResult a)
`extRR` Options
-> Inline -> StateT References Identity (ReplacedResult Inline)
replaceInline Options
opts
      (a -> StateT References Identity (ReplacedResult a))
-> ([Inline]
    -> StateT References Identity (ReplacedResult [Inline]))
-> a
-> StateT References Identity (ReplacedResult a)
forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(a -> m (ReplacedResult a))
-> (b -> m (ReplacedResult b)) -> a -> m (ReplacedResult a)
`extRR` Options
-> [Inline] -> StateT References Identity (ReplacedResult [Inline])
replaceInlineMany Options
opts
      )
  (a -> WS a) -> (a -> a) -> a -> WS a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
runSplitMath
  (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((Block -> Block) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT Block -> Block
divBlocks (a -> a) -> ([Inline] -> [Inline]) -> a -> a
forall a b.
(Typeable a, Typeable b) =>
(a -> a) -> (b -> b) -> a -> a
`extT` Options -> [Inline] -> [Inline]
spanInlines Options
opts)
  where
    runSplitMath :: a -> a
runSplitMath | Options -> Bool
tableEqns Options
opts
                 , Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Maybe Format -> Bool
isLatexFormat (Options -> Maybe Format
outFormat Options
opts)
                 = (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere (([Block] -> [Block]) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT [Block] -> [Block]
splitMath)
                 | Bool
otherwise = a -> a
forall a. a -> a
id

simpleTable :: [Alignment] -> [ColWidth] -> [[[Block]]] -> Block
simpleTable :: [Alignment] -> [ColWidth] -> [[[Block]]] -> Block
simpleTable [Alignment]
align [ColWidth]
width [[[Block]]]
bod = Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
nullAttr Caption
noCaption ([Alignment] -> [ColWidth] -> [ColSpec]
forall a b. [a] -> [b] -> [(a, b)]
zip [Alignment]
align [ColWidth]
width)
  TableHead
noTableHead [[[[Block]]] -> TableBody
mkBody [[[Block]]]
bod] TableFoot
noTableFoot
  where
  mkBody :: [[[Block]]] -> TableBody
mkBody [[[Block]]]
xs = Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr (Int -> RowHeadColumns
RowHeadColumns Int
0) [] (([[Block]] -> Row) -> [[[Block]]] -> [Row]
forall a b. (a -> b) -> [a] -> [b]
map [[Block]] -> Row
mkRow [[[Block]]]
xs)
  mkRow :: [[Block]] -> Row
mkRow [[Block]]
xs = Attr -> [Cell] -> Row
Row Attr
nullAttr (([Block] -> Cell) -> [[Block]] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map [Block] -> Cell
mkCell [[Block]]
xs)
  mkCell :: [Block] -> Cell
mkCell [Block]
xs = Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
nullAttr Alignment
AlignDefault (Int -> RowSpan
RowSpan Int
0) (Int -> ColSpan
ColSpan Int
0) [Block]
xs
  noCaption :: Caption
noCaption = Maybe [Inline] -> [Block] -> Caption
Caption Maybe [Inline]
forall a. Maybe a
Nothing [Block]
forall a. Monoid a => a
mempty
  noTableHead :: TableHead
noTableHead = Attr -> [Row] -> TableHead
TableHead Attr
nullAttr []
  noTableFoot :: TableFoot
noTableFoot = Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr []

replaceBlock :: Options -> Block -> WS (ReplacedResult Block)
replaceBlock :: Options
-> Block -> StateT References Identity (ReplacedResult Block)
replaceBlock Options
opts (Header Int
n (Text
label, [Text]
cls, [(Text, Text)]
attrs) [Inline]
text')
  = do
    let label' :: Text
label' = if Options -> Bool
autoSectionLabels Options
opts Bool -> Bool -> Bool
&& Bool -> Bool
not (Text
"sec:" Text -> Text -> Bool
`T.isPrefixOf` Text
label)
                 then Text
"sec:"Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
label
                 else Text
label
    Bool
-> StateT References Identity () -> StateT References Identity ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text
"unnumbered" Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cls) (StateT References Identity () -> StateT References Identity ())
-> StateT References Identity () -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$ do
      T References Index
-> (Index -> Index) -> StateT References Identity ()
forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify T References Index
curChap ((Index -> Index) -> StateT References Identity ())
-> (Index -> Index) -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$ \Index
cc ->
        let ln :: Int
ln = Index -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Index
cc
            cl :: Int -> Maybe Text
cl Int
i = Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs Maybe Text -> Maybe Text -> Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Options -> Int -> Int -> Maybe Text
customHeadingLabel Options
opts Int
n Int
i Maybe Text -> Maybe Text -> Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Options -> Text -> Int -> Maybe Text
customLabel Options
opts Text
"sec" Int
i
            inc :: Index -> Index
inc Index
l = let i :: Int
i = (Int, Maybe Text) -> Int
forall a b. (a, b) -> a
fst (Index -> (Int, Maybe Text)
forall a. [a] -> a
last Index
l) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 in Index -> Index
forall a. [a] -> [a]
init Index
l Index -> Index -> Index
forall a. Semigroup a => a -> a -> a
<> [(Int
i, Int -> Maybe Text
cl Int
i)]
            cc' :: Index
cc' | Int
ln Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = Index -> Index
inc (Index -> Index) -> Index -> Index
forall a b. (a -> b) -> a -> b
$ Int -> Index -> Index
forall a. Int -> [a] -> [a]
take Int
n Index
cc
                | Int
ln Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = Index -> Index
inc Index
cc
                | Bool
otherwise = Index
cc Index -> Index -> Index
forall a. Semigroup a => a -> a -> a
<> Int -> Index -> Index
forall a. Int -> [a] -> [a]
take (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
lnInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ([Int] -> [Maybe Text] -> Index
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1,Int
1..] ([Maybe Text] -> Index) -> [Maybe Text] -> Index
forall a b. (a -> b) -> a -> b
$ Maybe Text -> [Maybe Text]
forall a. a -> [a]
repeat Maybe Text
forall a. Maybe a
Nothing) Index -> Index -> Index
forall a. Semigroup a => a -> a -> a
<> [(Int
1,Int -> Maybe Text
cl Int
1)]
        in Index
cc'
      Bool
-> StateT References Identity () -> StateT References Identity ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text
"sec:" Text -> Text -> Bool
`T.isPrefixOf` Text
label') (StateT References Identity () -> StateT References Identity ())
-> StateT References Identity () -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$ do
        Index
index  <- T References Index -> StateT References Identity Index
forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T References Index
curChap
        T References RefMap
-> (RefMap -> RefMap) -> StateT References Identity ()
forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify T References RefMap
secRefs ((RefMap -> RefMap) -> StateT References Identity ())
-> (RefMap -> RefMap) -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$ Text -> RefRec -> RefMap -> RefMap
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
label' RefRec :: Index -> [Inline] -> Maybe Index -> RefRec
RefRec {
          refIndex :: Index
refIndex=Index
index
        , refTitle :: [Inline]
refTitle= [Inline]
text'
        , refSubfigure :: Maybe Index
refSubfigure = Maybe Index
forall a. Maybe a
Nothing
        }
    Index
cc <- T References Index -> StateT References Identity Index
forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T References Index
curChap
    let textCC :: [Inline]
textCC | Options -> Bool
numberSections Options
opts
               , Options -> Int
sectionsDepth Options
opts Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
               Bool -> Bool -> Bool
|| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= if Options -> Int
sectionsDepth Options
opts Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then Options -> Int
chaptersDepth Options
opts else Options -> Int
sectionsDepth Options
opts
               , Text
"unnumbered" Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
cls
               = Map Text [Inline] -> Template -> [Inline]
applyTemplate' ([(Text, [Inline])] -> Map Text [Inline]
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList [
                    (Text
"i", [Text -> Inline
Str (Text -> [Text] -> Text
T.intercalate Text
"." ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ ((Int, Maybe Text) -> Text) -> Index -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Maybe Text) -> Text
forall a. Show a => (a, Maybe Text) -> Text
show' Index
cc)])
                  , (Text
"n", [Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1])
                  , (Text
"t", [Inline]
text')
                  ]) (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
secHeaderTemplate Options
opts
               | Bool
otherwise = [Inline]
text'
        show' :: (a, Maybe Text) -> Text
show' (a
_, Just Text
s) = Text
s
        show' (a
i, Maybe Text
Nothing) = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ a -> String
forall a. Show a => a -> String
show a
i
    Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Int -> Attr -> [Inline] -> Block
Header Int
n (Text
label', [Text]
cls, [(Text, Text)]
attrs) [Inline]
textCC
-- subfigures
replaceBlock Options
opts (Div (Text
label,[Text]
cls,[(Text, Text)]
attrs) [Block]
images)
  | Text
"fig:" Text -> Text -> Bool
`T.isPrefixOf` Text
label
  , Para [Inline]
caption <- [Block] -> Block
forall a. [a] -> a
last [Block]
images
  = do
    [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts (Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label) (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
caption T References RefMap
imgRefs
    let ([Block]
cont, References
st) = State References [Block] -> References -> ([Block], References)
forall s a. State s a -> s -> (a, s)
runState (GenRR (StateT References Identity)
-> GenericM (StateT References Identity)
forall (m :: * -> *). Monad m => GenRR m -> GenericM m
runReplace (([Inline] -> StateT References Identity (ReplacedResult [Inline]))
-> a -> StateT References Identity (ReplacedResult a)
forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(b -> m (ReplacedResult b)) -> a -> m (ReplacedResult a)
mkRR (([Inline] -> StateT References Identity (ReplacedResult [Inline]))
 -> a -> StateT References Identity (ReplacedResult a))
-> ([Inline]
    -> StateT References Identity (ReplacedResult [Inline]))
-> a
-> StateT References Identity (ReplacedResult a)
forall a b. (a -> b) -> a -> b
$ Options
-> [Inline] -> StateT References Identity (ReplacedResult [Inline])
replaceSubfigs Options
opts') ([Block] -> State References [Block])
-> [Block] -> State References [Block]
forall a b. (a -> b) -> a -> b
$ [Block] -> [Block]
forall a. [a] -> [a]
init [Block]
images) References
forall a. Default a => a
def
        collectedCaptions :: [Inline]
collectedCaptions = Many Inline -> [Inline]
forall a. Many a -> [a]
B.toList (Many Inline -> [Inline]) -> Many Inline -> [Inline]
forall a b. (a -> b) -> a -> b
$
            Many Inline -> [Many Inline] -> Many Inline
forall a (f :: * -> *).
(Eq a, Monoid a, Foldable f) =>
a -> f a -> a
intercalate' ([Inline] -> Many Inline
forall a. [a] -> Many a
B.fromList ([Inline] -> Many Inline) -> [Inline] -> Many Inline
forall a b. (a -> b) -> a -> b
$ Options -> [Inline]
ccsDelim Options
opts)
          ([Many Inline] -> Many Inline) -> [Many Inline] -> Many Inline
forall a b. (a -> b) -> a -> b
$ ((Text, RefRec) -> Many Inline)
-> [(Text, RefRec)] -> [Many Inline]
forall a b. (a -> b) -> [a] -> [b]
map ([Inline] -> Many Inline
forall a. [a] -> Many a
B.fromList ([Inline] -> Many Inline)
-> ((Text, RefRec) -> [Inline]) -> (Text, RefRec) -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RefRec -> [Inline]
collectCaps (RefRec -> [Inline])
-> ((Text, RefRec) -> RefRec) -> (Text, RefRec) -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, RefRec) -> RefRec
forall a b. (a, b) -> b
snd)
          ([(Text, RefRec)] -> [Many Inline])
-> [(Text, RefRec)] -> [Many Inline]
forall a b. (a -> b) -> a -> b
$ ((Text, RefRec) -> Index) -> [(Text, RefRec)] -> [(Text, RefRec)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (RefRec -> Index
refIndex (RefRec -> Index)
-> ((Text, RefRec) -> RefRec) -> (Text, RefRec) -> Index
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, RefRec) -> RefRec
forall a b. (a, b) -> b
snd)
          ([(Text, RefRec)] -> [(Text, RefRec)])
-> [(Text, RefRec)] -> [(Text, RefRec)]
forall a b. (a -> b) -> a -> b
$ ((Text, RefRec) -> Bool) -> [(Text, RefRec)] -> [(Text, RefRec)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ((Text, RefRec) -> Bool) -> (Text, RefRec) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Inline] -> Bool)
-> ((Text, RefRec) -> [Inline]) -> (Text, RefRec) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RefRec -> [Inline]
refTitle (RefRec -> [Inline])
-> ((Text, RefRec) -> RefRec) -> (Text, RefRec) -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, RefRec) -> RefRec
forall a b. (a, b) -> b
snd)
          ([(Text, RefRec)] -> [(Text, RefRec)])
-> [(Text, RefRec)] -> [(Text, RefRec)]
forall a b. (a -> b) -> a -> b
$ RefMap -> [(Text, RefRec)]
forall k a. Map k a -> [(k, a)]
M.toList
          (RefMap -> [(Text, RefRec)]) -> RefMap -> [(Text, RefRec)]
forall a b. (a -> b) -> a -> b
$ References -> RefMap
imgRefs_ References
st
        collectCaps :: RefRec -> [Inline]
collectCaps RefRec
v =
              [Inline] -> [Inline] -> Template -> [Inline]
applyTemplate
                ([Inline] -> Index -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
opts) (RefRec -> Index
refIndex RefRec
v))
                (RefRec -> [Inline]
refTitle RefRec
v)
                (Options -> Template
ccsTemplate Options
opts)
        vars :: Map Text [Inline]
vars = [(Text, [Inline])] -> Map Text [Inline]
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
                  [ (Text
"ccs", [Inline]
collectedCaptions)
                  , (Text
"i", [Inline]
idxStr)
                  , (Text
"t", [Inline]
caption)
                  ]
        capt :: [Inline]
capt = Map Text [Inline] -> Template -> [Inline]
applyTemplate' Map Text [Inline]
vars (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
subfigureTemplate Options
opts
    RefRec
lastRef <- Maybe RefRec -> RefRec
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe RefRec -> RefRec)
-> (RefMap -> Maybe RefRec) -> RefMap -> RefRec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> RefMap -> Maybe RefRec
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
label (RefMap -> RefRec)
-> StateT References Identity RefMap
-> StateT References Identity RefRec
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T References RefMap -> StateT References Identity RefMap
forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T References RefMap
imgRefs
    T References RefMap
-> (RefMap -> RefMap) -> StateT References Identity ()
forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify T References RefMap
imgRefs ((RefMap -> RefMap) -> StateT References Identity ())
-> (RefMap -> RefMap) -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$ \RefMap
old ->
        RefMap -> RefMap -> RefMap
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union
          RefMap
old
          ((RefRec -> RefRec) -> RefMap -> RefMap
forall a b k. (a -> b) -> Map k a -> Map k b
M.map (\RefRec
v -> RefRec
v{refIndex :: Index
refIndex = RefRec -> Index
refIndex RefRec
lastRef, refSubfigure :: Maybe Index
refSubfigure = Index -> Maybe Index
forall a. a -> Maybe a
Just (Index -> Maybe Index) -> Index -> Maybe Index
forall a b. (a -> b) -> a -> b
$ RefRec -> Index
refIndex RefRec
v})
          (RefMap -> RefMap) -> RefMap -> RefMap
forall a b. (a -> b) -> a -> b
$ References -> RefMap
imgRefs_ References
st)
    case Options -> Maybe Format
outFormat Options
opts of
          Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
            Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div Attr
nullAttr ([Block] -> Block) -> [Block] -> Block
forall a b. (a -> b) -> a -> b
$
              [ Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\begin{figure}\n\\centering" ]
              [Block] -> [Block] -> [Block]
forall a. Semigroup a => a -> a -> a
<> [Block]
cont [Block] -> [Block] -> [Block]
forall a. Semigroup a => a -> a -> a
<>
              [ [Inline] -> Block
Para [Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"\\caption"
                       , Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline]
caption]
              , Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") (Text -> Block) -> Text -> Block
forall a b. (a -> b) -> a -> b
$ Text -> Text
mkLaTeXLabel Text
label
              , Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\end{figure}"]
          Maybe Format
_  -> Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, Text
"subfigures"Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:[Text]
cls, [(Text, Text)]
attrs) ([Block] -> Block) -> [Block] -> Block
forall a b. (a -> b) -> a -> b
$ [Block] -> [Inline] -> [Block]
toTable [Block]
cont [Inline]
capt
  where
    opts' :: Options
opts' = Options
opts
              { figureTemplate :: Template
figureTemplate = Options -> Template
subfigureChildTemplate Options
opts
              , customLabel :: Text -> Int -> Maybe Text
customLabel = \Text
r Int
i -> Options -> Text -> Int -> Maybe Text
customLabel Options
opts (Text
"sub"Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
r) Int
i
              }
    toTable :: [Block] -> [Inline] -> [Block]
    toTable :: [Block] -> [Inline] -> [Block]
toTable [Block]
blks [Inline]
capt
      | Options -> Bool
subfigGrid Options
opts = [ [Alignment] -> [ColWidth] -> [[[Block]]] -> Block
simpleTable [Alignment]
align ((Double -> ColWidth) -> [Double] -> [ColWidth]
forall a b. (a -> b) -> [a] -> [b]
map Double -> ColWidth
ColWidth [Double]
widths) ((Block -> [[Block]]) -> [Block] -> [[[Block]]]
forall a b. (a -> b) -> [a] -> [b]
map Block -> [[Block]]
blkToRow [Block]
blks)
                          , Options -> Text -> [Inline] -> Block
mkCaption Options
opts Text
"Image Caption" [Inline]
capt]
      | Bool
otherwise = [Block]
blks [Block] -> [Block] -> [Block]
forall a. Semigroup a => a -> a -> a
<> [Options -> Text -> [Inline] -> Block
mkCaption Options
opts Text
"Image Caption" [Inline]
capt]
      where
        align :: [Alignment]
align | Para [Inline]
ils:[Block]
_ <- [Block]
blks = Int -> Alignment -> [Alignment]
forall a. Int -> a -> [a]
replicate ([Double] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Double] -> Int) -> [Double] -> Int
forall a b. (a -> b) -> a -> b
$ (Inline -> Maybe Double) -> [Inline] -> [Double]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Inline -> Maybe Double
getWidth [Inline]
ils) Alignment
AlignCenter
              | Bool
otherwise = String -> [Alignment]
forall a. HasCallStack => String -> a
error String
"Misformatted subfigures block"
        widths :: [Double]
widths | Para [Inline]
ils:[Block]
_ <- [Block]
blks
               = [Double] -> [Double]
fixZeros ([Double] -> [Double]) -> [Double] -> [Double]
forall a b. (a -> b) -> a -> b
$ (Inline -> Maybe Double) -> [Inline] -> [Double]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Inline -> Maybe Double
getWidth [Inline]
ils
               | Bool
otherwise = String -> [Double]
forall a. HasCallStack => String -> a
error String
"Misformatted subfigures block"
        getWidth :: Inline -> Maybe Double
getWidth (Image (Text
_id, [Text]
_class, [(Text, Text)]
as) [Inline]
_ (Text, Text)
_)
          = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double) -> Double -> Maybe Double
forall a b. (a -> b) -> a -> b
$ Double -> (Text -> Double) -> Maybe Text -> Double
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Double
0 Text -> Double
percToDouble (Maybe Text -> Double) -> Maybe Text -> Double
forall a b. (a -> b) -> a -> b
$ Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"width" [(Text, Text)]
as
        getWidth Inline
_ = Maybe Double
forall a. Maybe a
Nothing
        fixZeros :: [Double] -> [Double]
        fixZeros :: [Double] -> [Double]
fixZeros [Double]
ws
          = let nz :: Int
nz = [Double] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Double] -> Int) -> [Double] -> Int
forall a b. (a -> b) -> a -> b
$ (Double -> Bool) -> [Double] -> [Double]
forall a. (a -> Bool) -> [a] -> [a]
filter (Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0) [Double]
ws
                rzw :: Double
rzw = (Double
0.99 Double -> Double -> Double
forall a. Num a => a -> a -> a
- [Double] -> Double
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Double]
ws) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nz
            in if Int
nzInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
0
               then (Double -> Double) -> [Double] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (\Double
x -> if Double
x Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 then Double
rzw else Double
x) [Double]
ws
               else [Double]
ws
        percToDouble :: T.Text -> Double
        percToDouble :: Text -> Double
percToDouble Text
percs
          | Right (Double
perc, Text
"%") <- Reader Double
T.double Text
percs
          = Double
percDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
100.0
          | Bool
otherwise = String -> Double
forall a. HasCallStack => String -> a
error String
"Only percent allowed in subfigure width!"
        blkToRow :: Block -> [[Block]]
        blkToRow :: Block -> [[Block]]
blkToRow (Para [Inline]
inls) = (Inline -> Maybe [Block]) -> [Inline] -> [[Block]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Inline -> Maybe [Block]
inlToCell [Inline]
inls
        blkToRow Block
x = [[Block
x]]
        inlToCell :: Inline -> Maybe [Block]
        inlToCell :: Inline -> Maybe [Block]
inlToCell (Image (Text
id', [Text]
cs, [(Text, Text)]
as) [Inline]
txt (Text, Text)
tgt)  = [Block] -> Maybe [Block]
forall a. a -> Maybe a
Just [[Inline] -> Block
Para [Attr -> [Inline] -> (Text, Text) -> Inline
Image (Text
id', [Text]
cs, [(Text, Text)] -> [(Text, Text)]
forall a b. (IsString a, IsString b, Eq a) => [(a, b)] -> [(a, b)]
setW [(Text, Text)]
as) [Inline]
txt (Text, Text)
tgt]]
        inlToCell Inline
_ = Maybe [Block]
forall a. Maybe a
Nothing
        setW :: [(a, b)] -> [(a, b)]
setW [(a, b)]
as = (a
"width", b
"100%")(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:((a, b) -> Bool) -> [(a, b)] -> [(a, b)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
"width") (a -> Bool) -> ((a, b) -> a) -> (a, b) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, b) -> a
forall a b. (a, b) -> a
fst) [(a, b)]
as
replaceBlock Options
opts (Div divOps :: Attr
divOps@(Text
label,[Text]
_,[(Text, Text)]
attrs) [Table Attr
tattr (Caption Maybe [Inline]
short (Block
btitle:[Block]
rest)) [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot])
  | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
title
  , Text
"tbl:" Text -> Text -> Bool
`T.isPrefixOf` Text
label
  = do
    [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts (Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label) (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
title T References RefMap
tblRefs
    let title' :: [Inline]
title' =
          case Options -> Maybe Format
outFormat Options
opts of
              Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
                Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") (Text -> Text
mkLaTeXLabel Text
label) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
title
              Maybe Format
_  -> [Inline] -> [Inline] -> Template -> [Inline]
applyTemplate [Inline]
idxStr [Inline]
title (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
tableTemplate Options
opts
        caption' :: Caption
caption' = Maybe [Inline] -> [Block] -> Caption
Caption Maybe [Inline]
short ([Inline] -> [Inline] -> Block -> Block
walkReplaceInlines [Inline]
title' [Inline]
title Block
btitleBlock -> [Block] -> [Block]
forall a. a -> [a] -> [a]
:[Block]
rest)
    Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div Attr
divOps [Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
tattr Caption
caption' [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot]
  where title :: [Inline]
title = [Block] -> [Inline]
blocksToInlines [Block
btitle]
replaceBlock Options
opts (Table divOps :: Attr
divOps@(Text
label,[Text]
_,[(Text, Text)]
attrs) (Caption Maybe [Inline]
short (Block
btitle:[Block]
rest)) [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot)
  | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
title
  , Text
"tbl:" Text -> Text -> Bool
`T.isPrefixOf` Text
label
  = do
    [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts (Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label) (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
title T References RefMap
tblRefs
    let title' :: [Inline]
title' =
          case Options -> Maybe Format
outFormat Options
opts of
              Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
                Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") (Text -> Text
mkLaTeXLabel Text
label) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
title
              Maybe Format
_  -> [Inline] -> [Inline] -> Template -> [Inline]
applyTemplate [Inline]
idxStr [Inline]
title (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
tableTemplate Options
opts
        caption' :: Caption
caption' = Maybe [Inline] -> [Block] -> Caption
Caption Maybe [Inline]
short ([Inline] -> [Inline] -> Block -> Block
walkReplaceInlines [Inline]
title' [Inline]
title Block
btitleBlock -> [Block] -> [Block]
forall a. a -> [a] -> [a]
:[Block]
rest)
    Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
divOps Caption
caption' [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot
  where title :: [Inline]
title = [Block] -> [Inline]
blocksToInlines [Block
btitle]
replaceBlock Options
opts cb :: Block
cb@(CodeBlock (Text
label, [Text]
classes, [(Text, Text)]
attrs) Text
code)
  | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
T.null Text
label
  , Text
"lst:" Text -> Text -> Bool
`T.isPrefixOf` Text
label
  , Just Text
caption <- Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"caption" [(Text, Text)]
attrs
  = case Options -> Maybe Format
outFormat Options
opts of
      Maybe Format
f
        --if used with listings package,nothing shoud be done
        | Maybe Format -> Bool
isLatexFormat Maybe Format
f, Options -> Bool
listings Options
opts -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceNoRecurse
        --if not using listings, however, wrap it in a codelisting environment
        | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
          Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div Attr
nullAttr [
              Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\begin{codelisting}"
            , [Inline] -> Block
Plain [
                Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"\\caption{"
              , Text -> Inline
Str Text
caption
              , Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"}"
              ]
            , Block
cb
            , Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\end{codelisting}"
            ]
      Maybe Format
_ -> do
        let cap :: [Inline]
cap = Many Inline -> [Inline]
forall a. Many a -> [a]
B.toList (Many Inline -> [Inline]) -> Many Inline -> [Inline]
forall a b. (a -> b) -> a -> b
$ Text -> Many Inline
B.text Text
caption
        [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts (Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label) (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
cap T References RefMap
lstRefs
        let caption' :: [Inline]
caption' = [Inline] -> [Inline] -> Template -> [Inline]
applyTemplate [Inline]
idxStr [Inline]
cap (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
listingTemplate Options
opts
        Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, Text
"listing"Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:[Text]
classes, []) [
            Options -> Text -> [Inline] -> Block
mkCaption Options
opts Text
"Caption" [Inline]
caption'
          , Attr -> Text -> Block
CodeBlock (Text
"", [Text]
classes, [(Text, Text)]
attrs [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. Eq a => [a] -> [a] -> [a]
\\ [(Text
"caption", Text
caption)]) Text
code
          ]
replaceBlock Options
opts
  (Div (Text
label,Text
"listing":[Text]
_, [])
    [Para [Inline]
caption, CodeBlock (Text
"",[Text]
classes,[(Text, Text)]
attrs) Text
code])
  | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
T.null Text
label
  , Text
"lst:" Text -> Text -> Bool
`T.isPrefixOf` Text
label
  = case Options -> Maybe Format
outFormat Options
opts of
      Maybe Format
f
        --if used with listings package, return code block with caption
        | Maybe Format -> Bool
isLatexFormat Maybe Format
f, Options -> Bool
listings Options
opts ->
          Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> Text -> Block
CodeBlock (Text
label,[Text]
classes,(Text
"caption",Text -> Text
escapeLaTeX (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ [Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
caption)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
:[(Text, Text)]
attrs) Text
code
        --if not using listings, however, wrap it in a codelisting environment
        | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
          Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div Attr
nullAttr [
              Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\begin{codelisting}"
            , [Inline] -> Block
Para [
                Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"\\caption"
              , Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline]
caption
              ]
            , Attr -> Text -> Block
CodeBlock (Text
label,[Text]
classes,[(Text, Text)]
attrs) Text
code
            , Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\end{codelisting}"
            ]
      Maybe Format
_ -> do
        [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts (Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label) (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
caption T References RefMap
lstRefs
        let caption' :: [Inline]
caption' = [Inline] -> [Inline] -> Template -> [Inline]
applyTemplate [Inline]
idxStr [Inline]
caption (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
listingTemplate Options
opts
        Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, Text
"listing"Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:[Text]
classes, []) [
            Options -> Text -> [Inline] -> Block
mkCaption Options
opts Text
"Caption" [Inline]
caption'
          , Attr -> Text -> Block
CodeBlock (Text
"", [Text]
classes, [(Text, Text)]
attrs) Text
code
          ]
replaceBlock Options
opts (Para [Span Attr
attrs [Math MathType
DisplayMath Text
eq]])
  | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Maybe Format -> Bool
isLatexFormat (Options -> Maybe Format
outFormat Options
opts)
  , Options -> Bool
tableEqns Options
opts
  = do
    (Text
eq', Text
idx) <- Options -> Attr -> Text -> WS (Text, Text)
replaceEqn Options
opts Attr
attrs Text
eq
    Block -> StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> StateT References Identity (ReplacedResult Block))
-> Block -> StateT References Identity (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div Attr
attrs [
      [Alignment] -> [ColWidth] -> [[[Block]]] -> Block
simpleTable [Alignment
AlignCenter, Alignment
AlignRight] [Double -> ColWidth
ColWidth Double
0.9, Double -> ColWidth
ColWidth Double
0.09]
       [[[[Inline] -> Block
Plain [MathType -> Text -> Inline
Math MathType
DisplayMath Text
eq']], [Text -> Block
eqnNumber Text
idx]]]]
  where
  eqnNumber :: Text -> Block
eqnNumber Text
idx
    | Options -> Maybe Format
outFormat Options
opts Maybe Format -> Maybe Format -> Bool
forall a. Eq a => a -> a -> Bool
== Format -> Maybe Format
forall a. a -> Maybe a
Just (Text -> Format
Format Text
"docx")
    = Attr -> [Block] -> Block
Div Attr
nullAttr [
        Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"openxml") Text
"<w:tcPr><w:vAlign w:val=\"center\"/></w:tcPr>"
      , Block
mathIdx
      ]
    | Bool
otherwise = Block
mathIdx
    where mathIdx :: Block
mathIdx = [Inline] -> Block
Plain [MathType -> Text -> Inline
Math MathType
DisplayMath (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
idx Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"]
replaceBlock Options
_ Block
_ = StateT References Identity (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceRecurse

replaceEqn :: Options -> Attr -> T.Text -> WS (T.Text, T.Text)
replaceEqn :: Options -> Attr -> Text -> WS (Text, Text)
replaceEqn Options
opts (Text
label, [Text]
_, [(Text, Text)]
attrs) Text
eq = do
  let label' :: Either Text Text
label' | Text -> Bool
T.null Text
label = Text -> Either Text Text
forall a b. a -> Either a b
Left Text
"eq"
             | Bool
otherwise = Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label
  [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts Either Text Text
label' (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [] T References RefMap
eqnRefs
  let eq' :: Text
eq' | Options -> Bool
tableEqns Options
opts = Text
eq
          | Bool
otherwise = Text
eqText -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\\qquad("Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
idxTxtText -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
")"
      idxTxt :: Text
idxTxt = [Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
idxStr
  (Text, Text) -> WS (Text, Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
eq', Text
idxTxt)

replaceInlineMany :: Options -> [Inline] -> WS (ReplacedResult [Inline])
replaceInlineMany :: Options
-> [Inline] -> StateT References Identity (ReplacedResult [Inline])
replaceInlineMany Options
opts (Span attrs :: Attr
attrs@(Text
label,[Text]
_,[(Text, Text)]
_) [Math MathType
DisplayMath Text
eq]:[Inline]
xs)
  | Text
"eq:" Text -> Text -> Bool
`T.isPrefixOf` Text
label Bool -> Bool -> Bool
|| Text -> Bool
T.null Text
label Bool -> Bool -> Bool
&& Options -> Bool
autoEqnLabels Options
opts
  = [Inline] -> StateT References Identity (ReplacedResult [Inline])
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceRecurse ([Inline] -> StateT References Identity (ReplacedResult [Inline]))
-> ([Inline] -> [Inline])
-> [Inline]
-> StateT References Identity (ReplacedResult [Inline])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<>[Inline]
xs) ([Inline] -> StateT References Identity (ReplacedResult [Inline]))
-> WS [Inline]
-> StateT References Identity (ReplacedResult [Inline])
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< case Options -> Maybe Format
outFormat Options
opts of
      Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
        [Inline] -> WS [Inline]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"\\begin{equation}"
        , Attr -> [Inline] -> Inline
Span Attr
attrs [Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
eq]
        , Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text -> Text
mkLaTeXLabel Text
label Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\\end{equation}"]
      Maybe Format
_ -> Inline -> [Inline]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inline -> [Inline])
-> ((Text, Text) -> Inline) -> (Text, Text) -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> [Inline] -> Inline
Span Attr
attrs ([Inline] -> Inline)
-> ((Text, Text) -> [Inline]) -> (Text, Text) -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[]) (Inline -> [Inline])
-> ((Text, Text) -> Inline) -> (Text, Text) -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MathType -> Text -> Inline
Math MathType
DisplayMath (Text -> Inline)
-> ((Text, Text) -> Text) -> (Text, Text) -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Text) -> Text
forall a b. (a, b) -> a
fst ((Text, Text) -> [Inline]) -> WS (Text, Text) -> WS [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Options -> Attr -> Text -> WS (Text, Text)
replaceEqn Options
opts Attr
attrs Text
eq
replaceInlineMany Options
_ [Inline]
_ = StateT References Identity (ReplacedResult [Inline])
forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceRecurse

replaceInline :: Options -> Inline -> WS (ReplacedResult Inline)
replaceInline :: Options
-> Inline -> StateT References Identity (ReplacedResult Inline)
replaceInline Options
opts (Image attr :: Attr
attr@(Text
label,[Text]
_,[(Text, Text)]
attrs) [Inline]
alt img :: (Text, Text)
img@(Text
_, Text
tit))
  | Text
"fig:" Text -> Text -> Bool
`T.isPrefixOf` Text
label Bool -> Bool -> Bool
&& Text
"fig:" Text -> Text -> Bool
`T.isPrefixOf` Text
tit
  = do
    [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts (Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label) (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
alt T References RefMap
imgRefs
    let alt' :: [Inline]
alt' = case Options -> Maybe Format
outFormat Options
opts of
          Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f -> [Inline]
alt
          Maybe Format
_  -> [Inline] -> [Inline] -> Template -> [Inline]
applyTemplate [Inline]
idxStr [Inline]
alt (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
figureTemplate Options
opts
    Inline -> StateT References Identity (ReplacedResult Inline)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Inline -> StateT References Identity (ReplacedResult Inline))
-> Inline -> StateT References Identity (ReplacedResult Inline)
forall a b. (a -> b) -> a -> b
$ Attr -> [Inline] -> (Text, Text) -> Inline
Image Attr
attr [Inline]
alt' (Text, Text)
img
replaceInline Options
_ Inline
_ = StateT References Identity (ReplacedResult Inline)
forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceRecurse

replaceSubfigs :: Options -> [Inline] -> WS (ReplacedResult [Inline])
replaceSubfigs :: Options
-> [Inline] -> StateT References Identity (ReplacedResult [Inline])
replaceSubfigs Options
opts = ([Inline] -> StateT References Identity (ReplacedResult [Inline])
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse ([Inline] -> StateT References Identity (ReplacedResult [Inline]))
-> ([[Inline]] -> [Inline])
-> [[Inline]]
-> StateT References Identity (ReplacedResult [Inline])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Inline]] -> [Inline]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat) ([[Inline]]
 -> StateT References Identity (ReplacedResult [Inline]))
-> ([Inline] -> StateT References Identity [[Inline]])
-> [Inline]
-> StateT References Identity (ReplacedResult [Inline])
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Inline -> WS [Inline])
-> [Inline] -> StateT References Identity [[Inline]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Options -> Inline -> WS [Inline]
replaceSubfig Options
opts)

replaceSubfig :: Options -> Inline -> WS [Inline]
replaceSubfig :: Options -> Inline -> WS [Inline]
replaceSubfig Options
opts x :: Inline
x@(Image (Text
label,[Text]
cls,[(Text, Text)]
attrs) [Inline]
alt (Text
src, Text
tit))
  = do
      let label' :: Either Text Text
label' | Text
"fig:" Text -> Text -> Bool
`T.isPrefixOf` Text
label = Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label
                 | Text -> Bool
T.null Text
label = Text -> Either Text Text
forall a b. a -> Either a b
Left Text
"fig"
                 | Bool
otherwise  = Text -> Either Text Text
forall a b. b -> Either a b
Right (Text -> Either Text Text) -> Text -> Either Text Text
forall a b. (a -> b) -> a -> b
$ Text
"fig:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
label
      [Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
opts Either Text Text
label' (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
alt T References RefMap
imgRefs
      case Options -> Maybe Format
outFormat Options
opts of
        Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
          [Inline] -> WS [Inline]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> WS [Inline]) -> [Inline] -> WS [Inline]
forall a b. (a -> b) -> a -> b
$ Inline -> Text -> [Inline]
latexSubFigure Inline
x Text
label
        Maybe Format
_  ->
          let alt' :: [Inline]
alt' = [Inline] -> [Inline] -> Template -> [Inline]
applyTemplate [Inline]
idxStr [Inline]
alt (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
figureTemplate Options
opts
              tit' :: Text
tit' | Text
"nocaption" Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cls = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
tit (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Maybe Text
T.stripPrefix Text
"fig:" Text
tit
                   | Text
"fig:" Text -> Text -> Bool
`T.isPrefixOf` Text
tit = Text
tit
                   | Bool
otherwise = Text
"fig:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tit
          in [Inline] -> WS [Inline]
forall (m :: * -> *) a. Monad m => a -> m a
return [Attr -> [Inline] -> (Text, Text) -> Inline
Image (Text
label, [Text]
cls, [(Text, Text)]
attrs) [Inline]
alt' (Text
src, Text
tit')]
replaceSubfig Options
_ Inline
x = [Inline] -> WS [Inline]
forall (m :: * -> *) a. Monad m => a -> m a
return [Inline
x]

divBlocks :: Block -> Block
divBlocks :: Block -> Block
divBlocks (Table Attr
tattr (Caption Maybe [Inline]
short (Block
btitle:[Block]
rest)) [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot)
  | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
title
  , Just Text
label <- Text -> [Inline] -> Maybe Text
getRefLabel Text
"tbl" [[Inline] -> Inline
forall a. [a] -> a
last [Inline]
title]
  = Attr -> [Block] -> Block
Div (Text
label,[],[]) [
    Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
tattr (Maybe [Inline] -> [Block] -> Caption
Caption Maybe [Inline]
short ([Block] -> Caption) -> [Block] -> Caption
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline] -> Block -> Block
walkReplaceInlines ((Inline -> Bool) -> [Inline] -> [Inline]
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd Inline -> Bool
isSpace ([Inline] -> [Inline]
forall a. [a] -> [a]
init [Inline]
title)) [Inline]
title Block
btitleBlock -> [Block] -> [Block]
forall a. a -> [a] -> [a]
:[Block]
rest) [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot]
  where
    title :: [Inline]
title = [Block] -> [Inline]
blocksToInlines [Block
btitle]
divBlocks Block
x = Block
x

walkReplaceInlines :: [Inline] -> [Inline] -> Block -> Block
walkReplaceInlines :: [Inline] -> [Inline] -> Block -> Block
walkReplaceInlines [Inline]
newTitle [Inline]
title = ([Inline] -> [Inline]) -> Block -> Block
forall a b. Walkable a b => (a -> a) -> b -> b
walk [Inline] -> [Inline]
replaceInlines
  where
  replaceInlines :: [Inline] -> [Inline]
replaceInlines [Inline]
xs
    | [Inline]
xs [Inline] -> [Inline] -> Bool
forall a. Eq a => a -> a -> Bool
== [Inline]
title = [Inline]
newTitle
    | Bool
otherwise = [Inline]
xs

splitMath :: [Block] -> [Block]
splitMath :: [Block] -> [Block]
splitMath (Para [Inline]
ils:[Block]
xs)
  | [Inline] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Inline]
ils Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = ([Inline] -> Block) -> [[Inline]] -> [Block]
forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> Block
Para ([[Inline]] -> [Inline] -> [Inline] -> [[Inline]]
split [] [] [Inline]
ils) [Block] -> [Block] -> [Block]
forall a. Semigroup a => a -> a -> a
<> [Block]
xs
  where
    split :: [[Inline]] -> [Inline] -> [Inline] -> [[Inline]]
split [[Inline]]
res [Inline]
acc [] = [[Inline]] -> [[Inline]]
forall a. [a] -> [a]
reverse ([Inline] -> [Inline]
forall a. [a] -> [a]
reverse [Inline]
acc [Inline] -> [[Inline]] -> [[Inline]]
forall a. a -> [a] -> [a]
: [[Inline]]
res)
    split [[Inline]]
res [Inline]
acc (x :: Inline
x@(Span Attr
_ [Math MathType
DisplayMath Text
_]):[Inline]
ys) =
      [[Inline]] -> [Inline] -> [Inline] -> [[Inline]]
split ([Inline
x] [Inline] -> [[Inline]] -> [[Inline]]
forall a. a -> [a] -> [a]
: [Inline] -> [Inline]
forall a. [a] -> [a]
reverse ([Inline] -> [Inline]
dropSpaces [Inline]
acc) [Inline] -> [[Inline]] -> [[Inline]]
forall a. a -> [a] -> [a]
: [[Inline]]
res)
            [] ([Inline] -> [Inline]
dropSpaces [Inline]
ys)
    split [[Inline]]
res [Inline]
acc (Inline
y:[Inline]
ys) = [[Inline]] -> [Inline] -> [Inline] -> [[Inline]]
split [[Inline]]
res (Inline
yInline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[Inline]
acc) [Inline]
ys
    dropSpaces :: [Inline] -> [Inline]
dropSpaces = (Inline -> Bool) -> [Inline] -> [Inline]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Inline -> Bool
isSpace
splitMath [Block]
xs = [Block]
xs

spanInlines :: Options -> [Inline] -> [Inline]
spanInlines :: Options -> [Inline] -> [Inline]
spanInlines Options
opts (math :: Inline
math@(Math MathType
DisplayMath Text
_eq):[Inline]
ils)
  | Inline
c:[Inline]
ils' <- (Inline -> Bool) -> [Inline] -> [Inline]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Inline -> Bool
isSpace [Inline]
ils
  , Just Text
label <- Text -> [Inline] -> Maybe Text
getRefLabel Text
"eq" [Inline
c]
  = Attr -> [Inline] -> Inline
Span (Text
label,[],[]) [Inline
math]Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[Inline]
ils'
  | Options -> Bool
autoEqnLabels Options
opts
  = Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline
math]Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[Inline]
ils
spanInlines Options
_ [Inline]
x = [Inline]
x

replaceAttr :: Options -> Either T.Text T.Text -> Maybe T.Text -> [Inline] -> Accessor References RefMap -> WS [Inline]
replaceAttr :: Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> T References RefMap
-> WS [Inline]
replaceAttr Options
o Either Text Text
label Maybe Text
refLabel [Inline]
title T References RefMap
prop
  = do
    Index
chap  <- Int -> Index -> Index
forall a. Int -> [a] -> [a]
take (Options -> Int
chaptersDepth Options
o) (Index -> Index)
-> StateT References Identity Index
-> StateT References Identity Index
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` T References Index -> StateT References Identity Index
forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T References Index
curChap
    RefMap
prop' <- T References RefMap -> StateT References Identity RefMap
forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T References RefMap
prop
    let i :: Int
i = Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (RefMap -> Int
forall k a. Map k a -> Int
M.size (RefMap -> Int) -> (RefMap -> RefMap) -> RefMap -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RefRec -> Bool) -> RefMap -> RefMap
forall a k. (a -> Bool) -> Map k a -> Map k a
M.filter (\RefRec
x -> (Index
chap Index -> Index -> Bool
forall a. Eq a => a -> a -> Bool
== Index -> Index
forall a. [a] -> [a]
init (RefRec -> Index
refIndex RefRec
x)) Bool -> Bool -> Bool
&& Maybe Index -> Bool
forall a. Maybe a -> Bool
isNothing (RefRec -> Maybe Index
refSubfigure RefRec
x)) (RefMap -> Int) -> RefMap -> Int
forall a b. (a -> b) -> a -> b
$ RefMap
prop')
        index :: Index
index = Index
chap Index -> Index -> Index
forall a. Semigroup a => a -> a -> a
<> [(Int
i, Maybe Text
refLabel Maybe Text -> Maybe Text -> Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Options -> Text -> Int -> Maybe Text
customLabel Options
o Text
ref Int
i)]
        ref :: Text
ref = (Text -> Text) -> (Text -> Text) -> Either Text Text -> Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Text -> Text
forall a. a -> a
id ((Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
':')) Either Text Text
label
        label' :: Text
label' = (Text -> Text) -> (Text -> Text) -> Either Text Text -> Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Char
':' Char -> String -> String
forall a. a -> [a] -> [a]
: Index -> String
forall a. Show a => a -> String
show Index
index)) Text -> Text
forall a. a -> a
id Either Text Text
label
    Bool
-> StateT References Identity () -> StateT References Identity ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> RefMap -> Bool
forall k a. Ord k => k -> Map k a -> Bool
M.member Text
label' RefMap
prop') (StateT References Identity () -> StateT References Identity ())
-> StateT References Identity () -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$
      String -> StateT References Identity ()
forall a. HasCallStack => String -> a
error (String -> StateT References Identity ())
-> (Text -> String) -> Text -> StateT References Identity ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack (Text -> StateT References Identity ())
-> Text -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$ Text
"Duplicate label: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
label'
    T References RefMap
-> (RefMap -> RefMap) -> StateT References Identity ()
forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify T References RefMap
prop ((RefMap -> RefMap) -> StateT References Identity ())
-> (RefMap -> RefMap) -> StateT References Identity ()
forall a b. (a -> b) -> a -> b
$ Text -> RefRec -> RefMap -> RefMap
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
label' RefRec :: Index -> [Inline] -> Maybe Index -> RefRec
RefRec {
      refIndex :: Index
refIndex= Index
index
    , refTitle :: [Inline]
refTitle= [Inline]
title
    , refSubfigure :: Maybe Index
refSubfigure = Maybe Index
forall a. Maybe a
Nothing
    }
    [Inline] -> WS [Inline]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> WS [Inline]) -> [Inline] -> WS [Inline]
forall a b. (a -> b) -> a -> b
$ [Inline] -> Index -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
o) Index
index

latexSubFigure :: Inline -> T.Text -> [Inline]
latexSubFigure :: Inline -> Text -> [Inline]
latexSubFigure (Image (Text
_, [Text]
cls, [(Text, Text)]
attrs) [Inline]
alt (Text
src, Text
title)) Text
label =
  let
    title' :: Text
title' = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
title (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Maybe Text
T.stripPrefix Text
"fig:" Text
title
    texlabel :: [Inline]
texlabel | Text -> Bool
T.null Text
label = []
             | Bool
otherwise = [Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text -> Text
mkLaTeXLabel Text
label]
    texalt :: [Inline]
texalt | Text
"nocaption" Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cls  = []
           | Bool
otherwise = [[Inline]] -> [Inline]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
              [ [ Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"["]
              , [Inline]
alt
              , [ Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"]"]
              ]
    img :: Inline
img = Attr -> [Inline] -> (Text, Text) -> Inline
Image (Text
label, [Text]
cls, [(Text, Text)]
attrs) [Inline]
alt (Text
src, Text
title')
  in [[Inline]] -> [Inline]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
      [ Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"\\subfloat" ]
      , [Inline]
texalt
      , [Attr -> [Inline] -> Inline
Span Attr
nullAttr ([Inline] -> Inline) -> [Inline] -> Inline
forall a b. (a -> b) -> a -> b
$ Inline
imgInline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[Inline]
texlabel]
      ]
latexSubFigure Inline
x Text
_ = [Inline
x]

mkCaption :: Options -> T.Text -> [Inline] -> Block
mkCaption :: Options -> Text -> [Inline] -> Block
mkCaption Options
opts Text
style
  | Options -> Maybe Format
outFormat Options
opts Maybe Format -> Maybe Format -> Bool
forall a. Eq a => a -> a -> Bool
== Format -> Maybe Format
forall a. a -> Maybe a
Just (Text -> Format
Format Text
"docx") = Attr -> [Block] -> Block
Div (Text
"", [], [(Text
"custom-style", Text
style)]) ([Block] -> Block) -> ([Inline] -> [Block]) -> [Inline] -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block -> [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return (Block -> [Block]) -> ([Inline] -> Block) -> [Inline] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Block
Para
  | Bool
otherwise = [Inline] -> Block
Para