{-# 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 = forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Maybe Format -> Bool
isLatexFormat forall b c a. (b -> c) -> (a -> b) -> a -> c
. Options -> Maybe Format
outFormat) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Bool
True -> 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
-> forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Prefix -> Lens' References RefMap
refsAt Prefix
PfxImg) 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
-> forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Prefix -> Lens' References RefMap
refsAt Prefix
PfxTbl) 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
-> forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Prefix -> Lens' References RefMap
refsAt Prefix
PfxLst) 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]
_ -> 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 <- forall r (m :: * -> *). MonadReader r m => m r
ask
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Options -> [Block]
titlef Options
o forall a. Semigroup a => a -> a -> a
<> (Attr -> [Block] -> Block
Div (Text
"", [Text
"list", Text
"list-of-" forall a. Semigroup a => a -> a -> a
<> Text
pfx], []) (Options -> [Block]
items Options
o) forall a. a -> [a] -> [a]
: [Block]
xs)
where
items :: Options -> [Block]
items Options
o = let is :: [Block]
is = Options -> [Block]
items' Options
o in forall a. a -> Maybe a -> a
fromMaybe [Block]
is forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList forall a. Maybe a
Nothing [] [Block]
is
items' :: Options -> [Block]
items' Options
o = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Options -> RefRec -> [Block]
itemChap Options
o forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
style) ([[Block]]
item 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 (forall a. a -> Maybe a
Just [[Block]] -> Block
BulletList) ([[Block]]
item 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 [] forall a. Eq a => a -> a -> Bool
== ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
style [] =
Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
style) ([[Block]]
item 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 [] forall a. Eq a => a -> a -> Bool
== [[Block]] -> Block
BulletList[] =
Maybe ([[Block]] -> Block) -> [[Block]] -> [Block] -> Maybe Block
mergeList (forall a. a -> Maybe a
Just [[Block]] -> Block
BulletList) ([[Block]]
item forall a. Semigroup a => a -> a -> a
<> [[Block]]
acc) [Block]
ys
mergeList (Just [[Block]] -> Block
cons) [[Block]]
acc [] = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ [[Block]] -> Block
cons forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse [[Block]]
acc
mergeList Maybe ([[Block]] -> Block)
_ [[Block]]
_ [Block]
_ = forall a. Maybe a
Nothing
refsSorted :: [(Text, RefRec)]
refsSorted = forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy forall {a} {a}. (a, RefRec) -> (a, RefRec) -> Ordering
compare' forall a b. (a -> b) -> a -> b
$ 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})
= 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
refTitle :: RefRec -> [Inline]
refSubfigure :: Maybe Index
refTitle :: [Inline]
refIndex :: Index
refSubfigure :: RefRec -> Maybe Index
refIndex :: RefRec -> Index
..} = 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
[Block]
[Inline]
Maybe Format
Text
BlockTemplate
Template
Bool -> Int -> [Inline]
Int -> Int -> Maybe Text
Text -> Template
Text -> Int -> Maybe Text
equationNumberTeX :: Options -> Text
setLabelAttribute :: Options -> Bool
nameInLink :: Options -> Bool
linkReferences :: Options -> Bool
subfigGrid :: Options -> Bool
autoEqnLabels :: Options -> Bool
tableEqns :: Options -> Bool
ccsLabelSep :: Options -> [Inline]
ccsDelim :: Options -> [Inline]
customHeadingLabel :: Options -> Int -> Int -> Maybe Text
customLabel :: Options -> Text -> Int -> Maybe Text
listingTemplate :: Options -> Template
tableTemplate :: Options -> Template
ccsTemplate :: Options -> Template
subfigureChildTemplate :: Options -> Template
subfigureTemplate :: Options -> Template
figureTemplate :: Options -> Template
refDelim :: Options -> [Inline]
lastDelim :: Options -> [Inline]
pairDelim :: Options -> [Inline]
rangeDelim :: Options -> [Inline]
chapDelim :: Options -> [Inline]
secHeaderTemplate :: Options -> Template
subfigureRefIndexTemplate :: Options -> Template
refIndexTemplate :: Options -> Text -> Template
eqnInlineTemplate :: Options -> Template
eqnIndexTemplate :: Options -> Template
eqnBlockInlineMath :: Options -> Bool
eqnBlockTemplate :: Options -> BlockTemplate
secPrefixTemplate :: Options -> Template
lstPrefixTemplate :: Options -> Template
tblPrefixTemplate :: Options -> Template
eqnPrefixTemplate :: Options -> Template
figPrefixTemplate :: Options -> Template
secPrefix :: Options -> Bool -> Int -> [Inline]
lstPrefix :: Options -> Bool -> Int -> [Inline]
tblPrefix :: Options -> Bool -> Int -> [Inline]
eqnPrefix :: Options -> Bool -> Int -> [Inline]
figPrefix :: Options -> Bool -> Int -> [Inline]
sectionsDepth :: Options -> Int
numberSections :: Options -> Bool
autoSectionLabels :: Options -> Bool
codeBlockCaptions :: Options -> Bool
listings :: Options -> Bool
chaptersDepth :: Options -> Int
cref :: Options -> Bool
equationNumberTeX :: Text
setLabelAttribute :: Bool
nameInLink :: Bool
linkReferences :: Bool
subfigGrid :: Bool
autoEqnLabels :: Bool
tableEqns :: Bool
ccsLabelSep :: [Inline]
ccsDelim :: [Inline]
customHeadingLabel :: Int -> Int -> Maybe Text
customLabel :: Text -> Int -> Maybe Text
listingTemplate :: Template
tableTemplate :: Template
ccsTemplate :: Template
subfigureChildTemplate :: Template
subfigureTemplate :: Template
figureTemplate :: Template
outFormat :: Maybe Format
lolTitle :: [Block]
lotTitle :: [Block]
lofTitle :: [Block]
refDelim :: [Inline]
lastDelim :: [Inline]
pairDelim :: [Inline]
rangeDelim :: [Inline]
chapDelim :: [Inline]
secHeaderTemplate :: Template
subfigureRefIndexTemplate :: Template
refIndexTemplate :: Text -> Template
eqnInlineTemplate :: Template
eqnIndexTemplate :: Template
eqnBlockInlineMath :: Bool
eqnBlockTemplate :: BlockTemplate
lolItemTemplate :: BlockTemplate
lotItemTemplate :: BlockTemplate
lofItemTemplate :: BlockTemplate
secPrefixTemplate :: Template
lstPrefixTemplate :: Template
tblPrefixTemplate :: Template
eqnPrefixTemplate :: Template
figPrefixTemplate :: Template
secPrefix :: Bool -> Int -> [Inline]
lstPrefix :: Bool -> Int -> [Inline]
tblPrefix :: Bool -> Int -> [Inline]
eqnPrefix :: Bool -> Int -> [Inline]
figPrefix :: Bool -> Int -> [Inline]
sectionsDepth :: Int
numberSections :: Bool
autoSectionLabels :: Bool
codeBlockCaptions :: Bool
listings :: Bool
chaptersDepth :: Int
cref :: Bool
lolTitle :: Options -> [Block]
lolItemTemplate :: Options -> BlockTemplate
lotTitle :: Options -> [Block]
lotItemTemplate :: Options -> BlockTemplate
lofTitle :: Options -> [Block]
lofItemTemplate :: Options -> BlockTemplate
outFormat :: Options -> Maybe Format
..} RefRec{[Inline]
Maybe Index
Index
refSubfigure :: Maybe Index
refTitle :: [Inline]
refIndex :: Index
refTitle :: RefRec -> [Inline]
refSubfigure :: RefRec -> Maybe Index
refIndex :: RefRec -> Index
..} = case Maybe Index
refSubfigure of
Maybe Index
Nothing ->
let vars :: Map Text [Inline]
vars = forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
[ (Text
"i", [Inline] -> Index -> [Inline]
chapPrefix [Inline]
chapDelim Index
refIndex)
, (Text
"suf", forall a. Monoid a => a
mempty)
, (Text
"t", [Inline]
refTitle)
]
in forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' Map Text [Inline]
vars forall a b. (a -> b) -> a -> b
$ Text -> Template
refIndexTemplate Text
pfx
Just Index
s ->
let vars :: Map Text [Inline]
vars = 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", forall a. Monoid a => a
mempty)
, (Text
"t", [Inline]
refTitle)
]
in forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' Map Text [Inline]
vars Template
subfigureRefIndexTemplate