{-# LANGUAGE Rank2Types, OverloadedStrings, FlexibleContexts #-}
module Text.Pandoc.CrossRef.References.Blocks
( replaceAll
) where
import Control.Monad.State hiding (get, modify)
import Data.List
import qualified Data.Map as M
import Data.Maybe
import qualified Data.Text as T
import qualified Data.Text.Read as T
import qualified Text.Pandoc.Builder as B
import Text.Pandoc.Definition
import Text.Pandoc.Shared (blocksToInlines, stringify)
import Text.Pandoc.Walk (walk)
import Control.Applicative
import Data.Default
import Lens.Micro
import Lens.Micro.Mtl
import Text.Pandoc.CrossRef.References.Types
import Text.Pandoc.CrossRef.Util.Options
import Text.Pandoc.CrossRef.Util.Template
import Text.Pandoc.CrossRef.Util.Util
replaceAll :: (Data a) => Options -> a -> WS a
replaceAll :: forall a. Data a => Options -> a -> WS a
replaceAll Options
opts =
forall (m :: * -> *). Monad m => GenRR m -> GenericM m
runReplace (forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(b -> m (ReplacedResult b)) -> a -> m (ReplacedResult a)
mkRR (Options -> Block -> WS (ReplacedResult Block)
replaceBlock Options
opts)
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 -> WS (ReplacedResult Inline)
replaceInline Options
opts
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] -> WS (ReplacedResult [Inline])
replaceInlineMany Options
opts
)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
runSplitMath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere (forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT Block -> Block
divBlocks 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 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 (forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT [Block] -> [Block]
splitMath)
| Bool
otherwise = 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 (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) [] (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 (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
1) (Int -> ColSpan
ColSpan Int
1) [Block]
xs
noCaption :: Caption
noCaption = Maybe [Inline] -> [Block] -> Caption
Caption forall a. Maybe a
Nothing forall a. Monoid a => a
mempty
noTableHead :: TableHead
noTableHead = Attr -> [Row] -> TableHead
TableHead Attr
nullAttr []
noTableFoot :: TableFoot
noTableFoot = Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr []
setLabel :: Options -> [Inline] -> [(T.Text, T.Text)] -> [(T.Text, T.Text)]
setLabel :: Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idx
| Options -> Bool
setLabelAttribute Options
opts
= ((Text
"label", forall a. Walkable Inline a => a -> Text
stringify [Inline]
idx) forall a. a -> [a] -> [a]
:)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Eq a => a -> a -> Bool
/= Text
"label") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst)
| Bool
otherwise = forall a. a -> a
id
replaceBlock :: Options -> Block -> WS (ReplacedResult Block)
replaceBlock :: Options -> Block -> WS (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:"forall a. Semigroup a => a -> a -> a
<>Text
label
else Text
label
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text
"unnumbered" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cls) forall a b. (a -> b) -> a -> b
$ do
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
modifying Lens' References [(Int, Maybe Text)]
curChap forall a b. (a -> b) -> a -> b
$ \[(Int, Maybe Text)]
cc ->
let ln :: Int
ln = forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Int, Maybe Text)]
cc
cl :: Int -> Maybe Text
cl Int
i = forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Options -> Int -> Int -> Maybe Text
customHeadingLabel Options
opts Int
n Int
i forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Options -> Text -> Int -> Maybe Text
customLabel Options
opts Text
"sec" Int
i
inc :: [(Int, Maybe Text)] -> [(Int, Maybe Text)]
inc [(Int, Maybe Text)]
l = let i :: Int
i = forall a b. (a, b) -> a
fst (forall a. [a] -> a
last [(Int, Maybe Text)]
l) forall a. Num a => a -> a -> a
+ Int
1 in forall a. [a] -> [a]
init [(Int, Maybe Text)]
l forall a. Semigroup a => a -> a -> a
<> [(Int
i, Int -> Maybe Text
cl Int
i)]
cc' :: [(Int, Maybe Text)]
cc' | Int
ln forall a. Ord a => a -> a -> Bool
> Int
n = [(Int, Maybe Text)] -> [(Int, Maybe Text)]
inc forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
take Int
n [(Int, Maybe Text)]
cc
| Int
ln forall a. Eq a => a -> a -> Bool
== Int
n = [(Int, Maybe Text)] -> [(Int, Maybe Text)]
inc [(Int, Maybe Text)]
cc
| Bool
otherwise = [(Int, Maybe Text)]
cc forall a. Semigroup a => a -> a -> a
<> forall a. Int -> [a] -> [a]
take (Int
nforall a. Num a => a -> a -> a
-Int
lnforall a. Num a => a -> a -> a
-Int
1) forall {a}. [(Int, Maybe a)]
implicitChapters forall a. Semigroup a => a -> a -> a
<> [(Int
1,Int -> Maybe Text
cl Int
1)]
implicitChapters :: [(Int, Maybe a)]
implicitChapters | Options -> Bool
numberSections Options
opts = forall a. a -> [a]
repeat (Int
1, forall a. Maybe a
Nothing)
| Bool
otherwise = forall a. a -> [a]
repeat (Int
0, forall a. Maybe a
Nothing)
in [(Int, Maybe Text)]
cc'
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text
"sec:" Text -> Text -> Bool
`T.isPrefixOf` Text
label') forall a b. (a -> b) -> a -> b
$ do
[(Int, Maybe Text)]
index <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' References [(Int, Maybe Text)]
curChap
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
modifying Lens' References RefMap
secRefs forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
label' RefRec {
refIndex :: [(Int, Maybe Text)]
refIndex=[(Int, Maybe Text)]
index
, refTitle :: [Inline]
refTitle= [Inline]
text'
, refSubfigure :: Maybe [(Int, Maybe Text)]
refSubfigure = forall a. Maybe a
Nothing
}
[(Int, Maybe Text)]
cc <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' References [(Int, Maybe Text)]
curChap
let textCC :: [Inline]
textCC | Options -> Bool
numberSections Options
opts
, Options -> Int
sectionsDepth Options
opts forall a. Ord a => a -> a -> Bool
< Int
0
Bool -> Bool -> Bool
|| Int
n forall a. Ord a => a -> a -> Bool
<= if Options -> Int
sectionsDepth Options
opts forall a. Eq a => a -> a -> Bool
== Int
0 then Options -> Int
chaptersDepth Options
opts else Options -> Int
sectionsDepth Options
opts
, Text
"unnumbered" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
cls
= forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' (forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList [
(Text
"i", [Inline]
idxStr)
, (Text
"n", [Text -> Inline
Str forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ Int
n forall a. Num a => a -> a -> a
- Int
1])
, (Text
"t", [Inline]
text')
]) forall a b. (a -> b) -> a -> b
$ Options -> Template
secHeaderTemplate Options
opts
| Bool
otherwise = [Inline]
text'
idxStr :: [Inline]
idxStr = [Inline] -> [(Int, Maybe Text)] -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
opts) [(Int, Maybe Text)]
cc
attrs' :: [(Text, Text)]
attrs' | Text
"unnumbered" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
cls
= Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs
| Bool
otherwise = [(Text, Text)]
attrs
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Int -> Attr -> [Inline] -> Block
Header Int
n (Text
label', [Text]
cls, [(Text, Text)]
attrs') [Inline]
textCC
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 <- forall a. [a] -> a
last [Block]
images
= do
[Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts (forall a b. b -> Either a b
Right Text
label) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
caption Lens' References RefMap
imgRefs
let ([Block]
cont, References
st) = forall s a. State s a -> s -> (a, s)
runState (forall (m :: * -> *). Monad m => GenRR m -> GenericM m
runReplace (forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(b -> m (ReplacedResult b)) -> a -> m (ReplacedResult a)
mkRR forall a b. (a -> b) -> a -> b
$ Options -> [Inline] -> WS (ReplacedResult [Inline])
replaceSubfigs Options
opts') forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
init [Block]
images) forall a. Default a => a
def
collectedCaptions :: [Inline]
collectedCaptions = forall a. Many a -> [a]
B.toList forall a b. (a -> b) -> a -> b
$
forall a (f :: * -> *).
(Eq a, Monoid a, Foldable f) =>
a -> f a -> a
intercalate' (forall a. [a] -> Many a
B.fromList forall a b. (a -> b) -> a -> b
$ Options -> [Inline]
ccsDelim Options
opts)
forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a. [a] -> Many a
B.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. MkTemplate a Template => RefRec -> [a]
collectCaps forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd)
forall a b. (a -> b) -> a -> b
$ forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (RefRec -> [(Int, Maybe Text)]
refIndex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd)
forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. RefRec -> [Inline]
refTitle forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd)
forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
M.toList
forall a b. (a -> b) -> a -> b
$ References
stforall s a. s -> Getting a s a -> a
^.Lens' References RefMap
imgRefs
collectCaps :: RefRec -> [a]
collectCaps RefRec
v =
forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate
([Inline] -> [(Int, Maybe Text)] -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
opts) (RefRec -> [(Int, Maybe Text)]
refIndex RefRec
v))
(RefRec -> [Inline]
refTitle RefRec
v)
(Options -> Template
ccsTemplate Options
opts)
vars :: Map Text [Inline]
vars = 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 = forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' Map Text [Inline]
vars forall a b. (a -> b) -> a -> b
$ Options -> Template
subfigureTemplate Options
opts
RefRec
lastRef <- forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
label forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' References RefMap
imgRefs
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
modifying Lens' References RefMap
imgRefs forall a b. (a -> b) -> a -> b
$ \RefMap
old ->
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union
RefMap
old
(forall a b k. (a -> b) -> Map k a -> Map k b
M.map (\RefRec
v -> RefRec
v{refIndex :: [(Int, Maybe Text)]
refIndex = RefRec -> [(Int, Maybe Text)]
refIndex RefRec
lastRef, refSubfigure :: Maybe [(Int, Maybe Text)]
refSubfigure = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ RefRec -> [(Int, Maybe Text)]
refIndex RefRec
v})
forall a b. (a -> b) -> a -> b
$ References
stforall s a. s -> Getting a s a -> a
^.Lens' References RefMap
imgRefs)
case Options -> Maybe Format
outFormat Options
opts of
Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div Attr
nullAttr forall a b. (a -> b) -> a -> b
$
[ Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\begin{pandoccrossrefsubfigures}" ]
forall a. Semigroup a => a -> a -> a
<> [Block]
cont 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] -> [Inline]
removeFootnotes [Inline]
caption)
, Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"]"
, Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline]
caption]
, Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") forall a b. (a -> b) -> a -> b
$ Text -> Text
mkLaTeXLabel Text
label
, Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\end{pandoccrossrefsubfigures}"]
Maybe Format
_ -> forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, Text
"subfigures"forall a. a -> [a] -> [a]
:[Text]
cls, Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) 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"forall a. Semigroup a => a -> a -> a
<>Text
r) Int
i
}
removeFootnotes :: [Inline] -> [Inline]
removeFootnotes = forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
removeFootnote
removeFootnote :: Inline -> Inline
removeFootnote Note{} = Text -> Inline
Str Text
""
removeFootnote Inline
x = Inline
x
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 (forall a b. (a -> b) -> [a] -> [b]
map Double -> ColWidth
ColWidth [Double]
widths) (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 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 = forall a. Int -> a -> [a]
replicate (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Inline -> Maybe Double
getWidth [Inline]
ils) Alignment
AlignCenter
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"Misformatted subfigures block"
widths :: [Double]
widths | Para [Inline]
ils:[Block]
_ <- [Block]
blks
= [Double] -> [Double]
fixZeros forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Inline -> Maybe Double
getWidth [Inline]
ils
| Bool
otherwise = 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)
_)
= forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe Double
0 Text -> Double
percToDouble forall a b. (a -> b) -> a -> b
$ forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"width" [(Text, Text)]
as
getWidth Inline
_ = forall a. Maybe a
Nothing
fixZeros :: [Double] -> [Double]
fixZeros :: [Double] -> [Double]
fixZeros [Double]
ws
= let nz :: Int
nz = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
== Double
0) [Double]
ws
rzw :: Double
rzw = (Double
0.99 forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Double]
ws) forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nz
in if Int
nzforall a. Ord a => a -> a -> Bool
>Int
0
then forall a b. (a -> b) -> [a] -> [b]
map (\Double
x -> if Double
x 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
percforall a. Fractional a => a -> a -> a
/Double
100.0
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"Only percent allowed in subfigure width!"
blkToRow :: Block -> [[Block]]
blkToRow :: Block -> [[Block]]
blkToRow (Para [Inline]
inls) = 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) = forall a. a -> Maybe a
Just [[Inline] -> Block
Para [Attr -> [Inline] -> (Text, Text) -> Inline
Image (Text
id', [Text]
cs, forall {b} {b}.
(IsString b, IsString b, Eq b) =>
[(b, b)] -> [(b, b)]
setW [(Text, Text)]
as) [Inline]
txt (Text, Text)
tgt]]
inlToCell Inline
_ = forall a. Maybe a
Nothing
setW :: [(b, b)] -> [(b, b)]
setW [(b, b)]
as = (b
"width", b
"100%")forall a. a -> [a] -> [a]
:forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Eq a => a -> a -> Bool
/=b
"width") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(b, b)]
as
replaceBlock Options
opts (Div (Text
label,[Text]
clss,[(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 forall a b. (a -> b) -> a -> b
$ 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]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts (forall a b. b -> Either a b
Right Text
label) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
title Lens' 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) forall a. a -> [a] -> [a]
: [Inline]
title
Maybe Format
_ -> forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [Inline]
idxStr [Inline]
title 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
btitleforall a. a -> [a] -> [a]
:[Block]
rest)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, [Text]
clss, Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) [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 (Text
label,[Text]
clss,[(Text, Text)]
attrs) (Caption Maybe [Inline]
short (Block
btitle:[Block]
rest)) [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot)
| Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ 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]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts (forall a b. b -> Either a b
Right Text
label) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
title Lens' 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) forall a. a -> [a] -> [a]
: [Inline]
title
Maybe Format
_ -> forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [Inline]
idxStr [Inline]
title 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
btitleforall a. a -> [a] -> [a]
:[Block]
rest)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table (Text
label, [Text]
clss, Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) 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 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 <- 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
| Maybe Format -> Bool
isLatexFormat Maybe Format
f, Options -> Bool
listings Options
opts -> forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceNoRecurse
| Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse 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 = forall a. Many a -> [a]
B.toList forall a b. (a -> b) -> a -> b
$ Text -> Many Inline
B.text Text
caption
[Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts (forall a b. b -> Either a b
Right Text
label) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
cap Lens' References RefMap
lstRefs
let caption' :: [Inline]
caption' = forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [Inline]
idxStr [Inline]
cap forall a b. (a -> b) -> a -> b
$ Options -> Template
listingTemplate Options
opts
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, Text
"listing"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, forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Eq a => a -> a -> Bool
/=Text
"caption") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$ Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) Text
code
]
replaceBlock Options
opts
(Div (Text
label,Text
"listing":[Text]
divClasses, [(Text, Text)]
divAttrs)
[Para [Inline]
caption, CodeBlock (Text
"",[Text]
cbClasses,[(Text, Text)]
cbAttrs) Text
code])
| Bool -> Bool
not 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
| Maybe Format -> Bool
isLatexFormat Maybe Format
f, Options -> Bool
listings Options
opts ->
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> Text -> Block
CodeBlock (Text
label,[Text]
classes,(Text
"caption",Text -> Text
escapeLaTeX forall a b. (a -> b) -> a -> b
$ forall a. Walkable Inline a => a -> Text
stringify [Inline]
caption)forall a. a -> [a] -> [a]
:[(Text, Text)]
attrs) Text
code
| Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse 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]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts (forall a b. b -> Either a b
Right Text
label) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
caption Lens' References RefMap
lstRefs
let caption' :: [Inline]
caption' = forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [Inline]
idxStr [Inline]
caption forall a b. (a -> b) -> a -> b
$ Options -> Template
listingTemplate Options
opts
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, Text
"listing"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, Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) Text
code
]
where attrs :: [(Text, Text)]
attrs = [(Text, Text)]
divAttrs forall a. Semigroup a => a -> a -> a
<> [(Text, Text)]
cbAttrs
classes :: [Text]
classes = forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ [Text]
divClasses forall a. Semigroup a => a -> a -> a
<> [Text]
cbClasses
replaceBlock Options
opts (Para [Span sattrs :: Attr
sattrs@(Text
label, [Text]
cls, [(Text, Text)]
attrs) [Math MathType
DisplayMath Text
eq]])
| Bool -> Bool
not 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', [Inline]
idxStr) <- Options -> Attr -> Text -> WS (Text, [Inline])
replaceEqn Options
opts Attr
sattrs Text
eq
let mathfmt :: MathType
mathfmt = if Options -> Bool
eqnBlockInlineMath Options
opts then MathType
InlineMath else MathType
DisplayMath
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label,[Text]
cls,Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) forall a b. (a -> b) -> a -> b
$
forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [MathType -> Text -> Inline
Math MathType
mathfmt forall a b. (a -> b) -> a -> b
$ forall a. Walkable Inline a => a -> Text
stringify [Inline]
idxStr] [MathType -> Text -> Inline
Math MathType
mathfmt Text
eq']
forall a b. (a -> b) -> a -> b
$ Options -> BlockTemplate
eqnBlockTemplate Options
opts
replaceBlock Options
_ Block
_ = forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceRecurse
replaceEqn :: Options -> Attr -> T.Text -> WS (T.Text, [Inline])
replaceEqn :: Options -> Attr -> Text -> WS (Text, [Inline])
replaceEqn Options
opts (Text
label, [Text]
_, [(Text, Text)]
attrs) Text
eq = do
let label' :: Either Text Text
label' | Text -> Bool
T.null Text
label = forall a b. a -> Either a b
Left Text
"eq"
| Bool
otherwise = forall a b. b -> Either a b
Right Text
label
[Inline]
idxStrRaw <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts Either Text Text
label' (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [] Lens' References RefMap
eqnRefs
let idxStr :: [Inline]
idxStr = forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' (forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList [(Text
"i", [Inline]
idxStrRaw)]) forall a b. (a -> b) -> a -> b
$ Options -> Template
eqnIndexTemplate Options
opts
eqTxt :: [Inline]
eqTxt = forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' Map Text [Inline]
eqTxtVars forall a b. (a -> b) -> a -> b
$ Options -> Template
eqnInlineTemplate Options
opts :: [Inline]
eqTxtVars :: Map Text [Inline]
eqTxtVars = forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
[ (Text
"e", [Text -> Inline
Str Text
eq])
, (Text
"i", [Inline]
idxStr)
, (Text
"ri", [Inline]
idxStrRaw)
]
eq' :: Text
eq' | Options -> Bool
tableEqns Options
opts = Text
eq
| Bool
otherwise = forall a. Walkable Inline a => a -> Text
stringify [Inline]
eqTxt
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
eq', [Inline]
idxStr)
replaceInlineMany :: Options -> [Inline] -> WS (ReplacedResult [Inline])
replaceInlineMany :: Options -> [Inline] -> WS (ReplacedResult [Inline])
replaceInlineMany Options
opts (Span spanAttr :: Attr
spanAttr@(Text
label,[Text]
clss,[(Text, Text)]
attrs) [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
= forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceRecurse forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Semigroup a => a -> a -> a
<>[Inline]
xs) 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 ->
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
spanAttr [Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
eq]
, Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") forall a b. (a -> b) -> a -> b
$ Text -> Text
mkLaTeXLabel Text
label forall a. Semigroup a => a -> a -> a
<> Text
"\\end{equation}"]
Maybe Format
_ -> do
(Text
eq', [Inline]
idxStr) <- Options -> Attr -> Text -> WS (Text, [Inline])
replaceEqn Options
opts Attr
spanAttr Text
eq
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Attr -> [Inline] -> Inline
Span (Text
label,[Text]
clss,Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) [MathType -> Text -> Inline
Math MathType
DisplayMath Text
eq']]
replaceInlineMany Options
_ [Inline]
_ = forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceRecurse
replaceInline :: Options -> Inline -> WS (ReplacedResult Inline)
replaceInline :: Options -> Inline -> WS (ReplacedResult Inline)
replaceInline Options
opts (Image (Text
label,[Text]
cls,[(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]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts (forall a b. b -> Either a b
Right Text
label) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
alt Lens' 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
_ -> forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [Inline]
idxStr [Inline]
alt forall a b. (a -> b) -> a -> b
$ Options -> Template
figureTemplate Options
opts
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Inline] -> (Text, Text) -> Inline
Image (Text
label,[Text]
cls,Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) [Inline]
alt' (Text, Text)
img
replaceInline Options
_ Inline
_ = forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceRecurse
replaceSubfigs :: Options -> [Inline] -> WS (ReplacedResult [Inline])
replaceSubfigs :: Options -> [Inline] -> WS (ReplacedResult [Inline])
replaceSubfigs Options
opts = (forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat) forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< 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 = forall a b. b -> Either a b
Right Text
label
| Text -> Bool
T.null Text
label = forall a b. a -> Either a b
Left Text
"fig"
| Bool
otherwise = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Text
"fig:" forall a. Semigroup a => a -> a -> a
<> Text
label
[Inline]
idxStr <- Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
opts Either Text Text
label' (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
attrs) [Inline]
alt Lens' References RefMap
imgRefs
case Options -> Maybe Format
outFormat Options
opts of
Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Inline -> Text -> [Inline]
latexSubFigure Inline
x Text
label
Maybe Format
_ ->
let alt' :: [Inline]
alt' = forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [Inline]
idxStr [Inline]
alt forall a b. (a -> b) -> a -> b
$ Options -> Template
figureTemplate Options
opts
tit' :: Text
tit' | Text
"nocaption" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cls = forall a. a -> Maybe a -> a
fromMaybe Text
tit 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:" forall a. Semigroup a => a -> a -> a
<> Text
tit
in forall (m :: * -> *) a. Monad m => a -> m a
return [Attr -> [Inline] -> (Text, Text) -> Inline
Image (Text
label, [Text]
cls, Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) [Inline]
alt' (Text
src, Text
tit')]
replaceSubfig Options
_ Inline
x = 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 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
title
, Just Text
label <- Text -> [Inline] -> Maybe Text
getRefLabel Text
"tbl" [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 forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline] -> Block -> Block
walkReplaceInlines (forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd Inline -> Bool
isSpace (forall a. [a] -> [a]
init [Inline]
title)) [Inline]
title Block
btitleforall 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 = forall a b. Walkable a b => (a -> a) -> b -> b
walk [Inline] -> [Inline]
replaceInlines
where
replaceInlines :: [Inline] -> [Inline]
replaceInlines [Inline]
xs
| [Inline]
xs 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)
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [Inline]
ils forall a. Ord a => a -> a -> Bool
> Int
1 = forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> Block
Para ([[Inline]] -> [Inline] -> [Inline] -> [[Inline]]
split [] [] [Inline]
ils) forall a. Semigroup a => a -> a -> a
<> [Block]
xs
where
split :: [[Inline]] -> [Inline] -> [Inline] -> [[Inline]]
split [[Inline]]
res [Inline]
acc [] = forall a. [a] -> [a]
reverse (forall a. [a] -> [a]
reverse [Inline]
acc 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] forall a. a -> [a] -> [a]
: forall a. [a] -> [a]
reverse ([Inline] -> [Inline]
dropSpaces [Inline]
acc) 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
yforall a. a -> [a] -> [a]
:[Inline]
acc) [Inline]
ys
dropSpaces :: [Inline] -> [Inline]
dropSpaces = 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' <- 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]forall a. a -> [a] -> [a]
:[Inline]
ils'
| Options -> Bool
autoEqnLabels Options
opts
= Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline
math]forall a. a -> [a] -> [a]
:[Inline]
ils
spanInlines Options
_ [Inline]
x = [Inline]
x
replaceAttr :: Options -> Either T.Text T.Text -> Maybe T.Text -> [Inline] -> Lens References References RefMap RefMap -> WS [Inline]
replaceAttr :: Options
-> Either Text Text
-> Maybe Text
-> [Inline]
-> Lens' References RefMap
-> WS [Inline]
replaceAttr Options
o Either Text Text
label Maybe Text
refLabel [Inline]
title Lens' References RefMap
prop
= do
[(Int, Maybe Text)]
chap <- forall a. Int -> [a] -> [a]
take (Options -> Int
chaptersDepth Options
o) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' References [(Int, Maybe Text)]
curChap
RefMap
prop' <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' References RefMap
prop
let i :: Int
i = Int
1forall a. Num a => a -> a -> a
+ (forall k a. Map k a -> Int
M.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a k. (a -> Bool) -> Map k a -> Map k a
M.filter (\RefRec
x -> ([(Int, Maybe Text)]
chap forall a. Eq a => a -> a -> Bool
== forall a. [a] -> [a]
init (RefRec -> [(Int, Maybe Text)]
refIndex RefRec
x)) Bool -> Bool -> Bool
&& forall a. Maybe a -> Bool
isNothing (RefRec -> Maybe [(Int, Maybe Text)]
refSubfigure RefRec
x)) forall a b. (a -> b) -> a -> b
$ RefMap
prop')
index :: [(Int, Maybe Text)]
index = [(Int, Maybe Text)]
chap forall a. Semigroup a => a -> a -> a
<> [(Int
i, Maybe Text
refLabel 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 = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> a
id ((Char -> Bool) -> Text -> Text
T.takeWhile (forall a. Eq a => a -> a -> Bool
/=Char
':')) Either Text Text
label
label' :: Text
label' = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Char
':' forall a. a -> [a] -> [a]
: forall a. Show a => a -> String
show [(Int, Maybe Text)]
index)) forall a. a -> a
id Either Text Text
label
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall k a. Ord k => k -> Map k a -> Bool
M.member Text
label' RefMap
prop') forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => String -> a
error forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack forall a b. (a -> b) -> a -> b
$ Text
"Duplicate label: " forall a. Semigroup a => a -> a -> a
<> Text
label'
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
modifying Lens' References RefMap
prop forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
label' RefRec {
refIndex :: [(Int, Maybe Text)]
refIndex= [(Int, Maybe Text)]
index
, refTitle :: [Inline]
refTitle= [Inline]
title
, refSubfigure :: Maybe [(Int, Maybe Text)]
refSubfigure = forall a. Maybe a
Nothing
}
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Inline] -> [(Int, Maybe Text)] -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
o) [(Int, Maybe Text)]
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' = forall a. a -> Maybe a -> a
fromMaybe Text
title 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") forall a b. (a -> b) -> a -> b
$ Text -> Text
mkLaTeXLabel Text
label]
texalt :: [Inline]
texalt | Text
"nocaption" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cls = []
| Bool
otherwise = 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 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 forall a b. (a -> b) -> a -> b
$ Inline
imgforall 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 forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just (Text -> Format
Format Text
"docx") = Attr -> [Block] -> Block
Div (Text
"", [], [(Text
"custom-style", Text
style)]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Block
Para
| Bool
otherwise = [Inline] -> Block
Para