{-# LANGUAGE OverloadedStrings, RecordWildCards, LambdaCase #-}
module Text.Pandoc.CrossRef.References.List (listOf) where
import Data.List
import Control.Monad.Reader
import qualified Data.Map as M
import qualified Data.Text as T
import Text.Pandoc.Definition
import Data.Maybe
import Lens.Micro.Mtl
import Text.Pandoc.CrossRef.References.Types
import Text.Pandoc.CrossRef.References.Monad
import Text.Pandoc.CrossRef.Util.Options
import Text.Pandoc.CrossRef.Util.Util
import Text.Pandoc.CrossRef.Util.Template
listOf :: [Block] -> WS [Block]
listOf :: [Block] -> WS [Block]
listOf [Block]
blocks = (Options -> Bool) -> WS Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Options -> Bool
isLatexFormat WS Bool -> (Bool -> WS [Block]) -> WS [Block]
forall a b. WS a -> (a -> WS b) -> WS b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Bool
True -> [Block] -> WS [Block]
forall a. a -> WS a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Block]
blocks
Bool
False -> case [Block]
blocks of
(RawBlock Format
fmt Text
"\\listoffigures":[Block]
xs)
| Format -> Bool
isLaTeXRawBlockFmt Format
fmt
-> Getting RefMap References RefMap -> WS RefMap
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Prefix -> Lens' References RefMap
refsAt Prefix
PfxImg) WS RefMap -> (RefMap -> WS [Block]) -> WS [Block]
forall a b. WS a -> (a -> WS b) -> WS b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text
-> (Options -> BlockTemplate)
-> (Options -> [Block])
-> [Block]
-> RefMap
-> WS [Block]
makeList Text
"fig" Options -> BlockTemplate
lofItemTemplate Options -> [Block]
lofTitle [Block]
xs
(RawBlock Format
fmt Text
"\\listoftables":[Block]
xs)
| Format -> Bool
isLaTeXRawBlockFmt Format
fmt
-> Getting RefMap References RefMap -> WS RefMap
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Prefix -> Lens' References RefMap
refsAt Prefix
PfxTbl) WS RefMap -> (RefMap -> WS [Block]) -> WS [Block]
forall a b. WS a -> (a -> WS b) -> WS b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text
-> (Options -> BlockTemplate)
-> (Options -> [Block])
-> [Block]
-> RefMap
-> WS [Block]
makeList Text
"tbl" Options -> BlockTemplate
lotItemTemplate Options -> [Block]
lotTitle [Block]
xs
(RawBlock Format
fmt Text
"\\listoflistings":[Block]
xs)
| Format -> Bool
isLaTeXRawBlockFmt Format
fmt
-> Getting RefMap References RefMap -> WS RefMap
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Prefix -> Lens' References RefMap
refsAt Prefix
PfxLst) WS RefMap -> (RefMap -> WS [Block]) -> WS [Block]
forall a b. WS a -> (a -> WS b) -> WS b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text
-> (Options -> BlockTemplate)
-> (Options -> [Block])
-> [Block]
-> RefMap
-> WS [Block]
makeList Text
"lst" Options -> BlockTemplate
lolItemTemplate Options -> [Block]
lolTitle [Block]
xs
[Block]
_ -> [Block] -> WS [Block]
forall a. a -> WS a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Block]
blocks
makeList
:: T.Text
-> (Options -> BlockTemplate)
-> (Options -> [Block])
-> [Block]
-> M.Map T.Text RefRec
-> WS [Block]
makeList :: Text
-> (Options -> BlockTemplate)
-> (Options -> [Block])
-> [Block]
-> RefMap
-> WS [Block]
makeList Text
pfx Options -> BlockTemplate
tf Options -> [Block]
titlef [Block]
xs RefMap
refs = do
Options
o <- WS Options
forall r (m :: * -> *). MonadReader r m => m r
ask
[Block] -> WS [Block]
forall a. a -> WS a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Block] -> WS [Block]) -> [Block] -> WS [Block]
forall a b. (a -> b) -> a -> b
$ Options -> [Block]
titlef Options
o [Block] -> [Block] -> [Block]
forall a. Semigroup a => a -> a -> a
<> (Attr -> [Block] -> Block
Div (Text
"", [Text
"list", Text
"list-of-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
pfx], []) (Options -> [Block]
items Options
o) Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [Block]
xs)
where
items :: Options -> [Block]
items Options
o = let is :: [Block]
is = Options -> [Block]
items' Options
o in [Block] -> Maybe [Block] -> [Block]
forall a. a -> Maybe a -> a
fromMaybe [Block]
is (Maybe [Block] -> [Block]) -> Maybe [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ Block -> [Block]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Block -> [Block]) -> Maybe Block -> Maybe [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList Maybe ([[Block]] -> Block)
forall a. Maybe a
Nothing [] [Block]
is
items' :: Options -> [Block]
items' Options
o = ((Text, RefRec) -> [Block]) -> [(Text, RefRec)] -> [Block]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Options -> RefRec -> [Block]
itemChap Options
o (RefRec -> [Block])
-> ((Text, RefRec) -> RefRec) -> (Text, RefRec) -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, RefRec) -> RefRec
forall a b. (a, b) -> b
snd) [(Text, RefRec)]
refsSorted
mergeList :: Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList Maybe ([[Block]] -> Block)
Nothing [[Block]]
acc (OrderedList ListAttributes
style [[Block]]
item : [Block]
ys) =
Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList (([[Block]] -> Block) -> Maybe ([[Block]] -> Block)
forall a. a -> Maybe a
Just (([[Block]] -> Block) -> Maybe ([[Block]] -> Block))
-> ([[Block]] -> Block) -> Maybe ([[Block]] -> Block)
forall a b. (a -> b) -> a -> b
$ ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
style) ([[Block]]
item [[Block]] -> [[Block]] -> [[Block]]
forall a. Semigroup a => a -> a -> a
<> [[Block]]
acc) [Block]
ys
mergeList Maybe ([[Block]] -> Block)
Nothing [[Block]]
acc (BulletList [[Block]]
item : [Block]
ys) =
Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList (([[Block]] -> Block) -> Maybe ([[Block]] -> Block)
forall a. a -> Maybe a
Just [[Block]] -> Block
BulletList) ([[Block]]
item [[Block]] -> [[Block]] -> [[Block]]
forall a. Semigroup a => a -> a -> a
<> [[Block]]
acc) [Block]
ys
mergeList (Just [[Block]] -> Block
cons) [[Block]]
acc (OrderedList ListAttributes
style [[Block]]
item : [Block]
ys)
| [[Block]] -> Block
cons [] Block -> Block -> Bool
forall a. Eq a => a -> a -> Bool
== ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
style [] =
Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList (([[Block]] -> Block) -> Maybe ([[Block]] -> Block)
forall a. a -> Maybe a
Just (([[Block]] -> Block) -> Maybe ([[Block]] -> Block))
-> ([[Block]] -> Block) -> Maybe ([[Block]] -> Block)
forall a b. (a -> b) -> a -> b
$ ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
style) ([[Block]]
item [[Block]] -> [[Block]] -> [[Block]]
forall a. Semigroup a => a -> a -> a
<> [[Block]]
acc) [Block]
ys
mergeList (Just [[Block]] -> Block
cons) [[Block]]
acc (BulletList [[Block]]
item : [Block]
ys)
| [[Block]] -> Block
cons [] Block -> Block -> Bool
forall a. Eq a => a -> a -> Bool
== [[Block]] -> Block
BulletList[] =
Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList (([[Block]] -> Block) -> Maybe ([[Block]] -> Block)
forall a. a -> Maybe a
Just [[Block]] -> Block
BulletList) ([[Block]]
item [[Block]] -> [[Block]] -> [[Block]]
forall a. Semigroup a => a -> a -> a
<> [[Block]]
acc) [Block]
ys
mergeList (Just [[Block]] -> Block
cons) [[Block]]
acc [] = Block -> Maybe Block
forall a. a -> Maybe a
Just (Block -> Maybe Block) -> Block -> Maybe Block
forall a b. (a -> b) -> a -> b
$ [[Block]] -> Block
cons ([[Block]] -> Block) -> [[Block]] -> Block
forall a b. (a -> b) -> a -> b
$ [[Block]] -> [[Block]]
forall a. [a] -> [a]
reverse [[Block]]
acc
mergeList Maybe ([[Block]] -> Block)
_ [[Block]]
_ [Block]
_ = Maybe Block
forall a. Maybe a
Nothing
refsSorted :: [(Text, RefRec)]
refsSorted = ((Text, RefRec) -> (Text, RefRec) -> Ordering)
-> [(Text, RefRec)] -> [(Text, RefRec)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (Text, RefRec) -> (Text, RefRec) -> Ordering
forall {a} {a}. (a, RefRec) -> (a, RefRec) -> Ordering
compare' ([(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
refs
compare' :: (a, RefRec) -> (a, RefRec) -> Ordering
compare'
(a
_,RefRec{refIndex :: RefRec -> Index
refIndex=Index
i, refSubfigure :: RefRec -> Maybe Index
refSubfigure=Maybe Index
si})
(a
_,RefRec{refIndex :: RefRec -> Index
refIndex=Index
j, refSubfigure :: RefRec -> Maybe Index
refSubfigure=Maybe Index
sj})
= (Index, Maybe Index) -> (Index, Maybe Index) -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Index
i, Maybe Index
si) (Index
j, Maybe Index
sj)
itemChap :: Options -> RefRec -> [Block]
itemChap :: Options -> RefRec -> [Block]
itemChap Options
o ref :: RefRec
ref@RefRec{[Inline]
Maybe Index
Index
refIndex :: RefRec -> Index
refSubfigure :: RefRec -> Maybe Index
refIndex :: Index
refTitle :: [Inline]
refSubfigure :: Maybe Index
refTitle :: RefRec -> [Inline]
..} = [Inline] -> [Inline] -> BlockTemplate -> [Block]
forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate (Options -> RefRec -> [Inline]
numWithChap Options
o RefRec
ref) [Inline]
refTitle (Options -> BlockTemplate
tf Options
o)
numWithChap :: Options -> RefRec -> [Inline]
numWithChap :: Options -> RefRec -> [Inline]
numWithChap Options{Bool
Int
[Inline]
[Block]
Maybe Format
Text
BlockTemplate
Template
Bool -> Int -> [Inline]
Int -> Int -> Maybe Text
Text -> Template
Text -> Int -> Maybe Text
lofItemTemplate :: Options -> BlockTemplate
lofTitle :: Options -> [Block]
lotItemTemplate :: Options -> BlockTemplate
lotTitle :: Options -> [Block]
lolItemTemplate :: Options -> BlockTemplate
lolTitle :: Options -> [Block]
cref :: Bool
chaptersDepth :: Int
listings :: Bool
codeBlockCaptions :: Bool
autoSectionLabels :: Bool
numberSections :: Bool
sectionsDepth :: Int
figPrefix :: Bool -> Int -> [Inline]
eqnPrefix :: Bool -> Int -> [Inline]
tblPrefix :: Bool -> Int -> [Inline]
lstPrefix :: Bool -> Int -> [Inline]
secPrefix :: Bool -> Int -> [Inline]
figPrefixTemplate :: Template
eqnPrefixTemplate :: Template
tblPrefixTemplate :: Template
lstPrefixTemplate :: Template
secPrefixTemplate :: Template
lofItemTemplate :: BlockTemplate
lotItemTemplate :: BlockTemplate
lolItemTemplate :: BlockTemplate
eqnBlockTemplate :: BlockTemplate
eqnBlockInlineMath :: Bool
eqnIndexTemplate :: Template
eqnInlineTemplate :: Template
refIndexTemplate :: Text -> Template
subfigureRefIndexTemplate :: Template
secHeaderTemplate :: Template
chapDelim :: [Inline]
rangeDelim :: [Inline]
pairDelim :: [Inline]
lastDelim :: [Inline]
refDelim :: [Inline]
lofTitle :: [Block]
lotTitle :: [Block]
lolTitle :: [Block]
outFormat :: Maybe Format
figureTemplate :: Template
subfigureTemplate :: Template
subfigureChildTemplate :: Template
ccsTemplate :: Template
tableTemplate :: Template
listingTemplate :: Template
customLabel :: Text -> Int -> Maybe Text
customHeadingLabel :: Int -> Int -> Maybe Text
ccsDelim :: [Inline]
ccsLabelSep :: [Inline]
tableEqns :: Bool
autoEqnLabels :: Bool
subfigGrid :: Bool
linkReferences :: Bool
nameInLink :: Bool
setLabelAttribute :: Bool
equationNumberTeX :: Text
cref :: Options -> Bool
chaptersDepth :: Options -> Int
listings :: Options -> Bool
codeBlockCaptions :: Options -> Bool
autoSectionLabels :: Options -> Bool
numberSections :: Options -> Bool
sectionsDepth :: Options -> Int
figPrefix :: Options -> Bool -> Int -> [Inline]
eqnPrefix :: Options -> Bool -> Int -> [Inline]
tblPrefix :: Options -> Bool -> Int -> [Inline]
lstPrefix :: Options -> Bool -> Int -> [Inline]
secPrefix :: Options -> Bool -> Int -> [Inline]
figPrefixTemplate :: Options -> Template
eqnPrefixTemplate :: Options -> Template
tblPrefixTemplate :: Options -> Template
lstPrefixTemplate :: Options -> Template
secPrefixTemplate :: Options -> Template
eqnBlockTemplate :: Options -> BlockTemplate
eqnBlockInlineMath :: Options -> Bool
eqnIndexTemplate :: Options -> Template
eqnInlineTemplate :: Options -> Template
refIndexTemplate :: Options -> Text -> Template
subfigureRefIndexTemplate :: Options -> Template
secHeaderTemplate :: Options -> Template
chapDelim :: Options -> [Inline]
rangeDelim :: Options -> [Inline]
pairDelim :: Options -> [Inline]
lastDelim :: Options -> [Inline]
refDelim :: Options -> [Inline]
outFormat :: Options -> Maybe Format
figureTemplate :: Options -> Template
subfigureTemplate :: Options -> Template
subfigureChildTemplate :: Options -> Template
ccsTemplate :: Options -> Template
tableTemplate :: Options -> Template
listingTemplate :: Options -> Template
customLabel :: Options -> Text -> Int -> Maybe Text
customHeadingLabel :: Options -> Int -> Int -> Maybe Text
ccsDelim :: Options -> [Inline]
ccsLabelSep :: Options -> [Inline]
tableEqns :: Options -> Bool
autoEqnLabels :: Options -> Bool
subfigGrid :: Options -> Bool
linkReferences :: Options -> Bool
nameInLink :: Options -> Bool
setLabelAttribute :: Options -> Bool
equationNumberTeX :: Options -> Text
..} RefRec{[Inline]
Maybe Index
Index
refIndex :: RefRec -> Index
refSubfigure :: RefRec -> Maybe Index
refTitle :: RefRec -> [Inline]
refIndex :: Index
refTitle :: [Inline]
refSubfigure :: Maybe Index
..} = case Maybe Index
refSubfigure of
Maybe Index
Nothing ->
let vars :: Map Text [Inline]
vars = [(Text, [Inline])] -> Map Text [Inline]
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
[ (Text
"i", [Inline] -> Index -> [Inline]
chapPrefix [Inline]
chapDelim Index
refIndex)
, (Text
"suf", [Inline]
forall a. Monoid a => a
mempty)
, (Text
"t", [Inline]
refTitle)
]
in Map Text [Inline] -> Template -> [Inline]
forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' Map Text [Inline]
vars (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Text -> Template
refIndexTemplate Text
pfx
Just Index
s ->
let vars :: Map Text [Inline]
vars = [(Text, [Inline])] -> Map Text [Inline]
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
[ (Text
"i", [Inline] -> Index -> [Inline]
chapPrefix [Inline]
chapDelim Index
refIndex)
, (Text
"s", [Inline] -> Index -> [Inline]
chapPrefix [Inline]
chapDelim Index
s)
, (Text
"suf", [Inline]
forall a. Monoid a => a
mempty)
, (Text
"t", [Inline]
refTitle)
]
in Map Text [Inline] -> Template -> [Inline]
forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' Map Text [Inline]
vars Template
subfigureRefIndexTemplate