{-# LANGUAGE Rank2Types, OverloadedStrings, FlexibleContexts #-}
module Text.Pandoc.CrossRef.References.Blocks.Header where
import Control.Monad.Reader.Class
import Control.Monad.State hiding (get, modify)
import qualified Data.Map as M
import qualified Data.Text as T
import Text.Pandoc.Definition
import Control.Applicative
import Lens.Micro.Mtl
import Text.Pandoc.CrossRef.References.Types
import Text.Pandoc.CrossRef.References.Monad
import Text.Pandoc.CrossRef.References.Blocks.Util (setLabel)
import Text.Pandoc.CrossRef.Util.Options
import Text.Pandoc.CrossRef.Util.Template
import Text.Pandoc.CrossRef.Util.Util
runHeader :: Int -> Attr -> [Inline] -> WS (ReplacedResult Block)
Int
n (Text
label, [Text]
cls, [(Text, Text)]
attrs) [Inline]
text' = do
Options
opts <- forall r (m :: * -> *). MonadReader r m => m r
ask
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 Index
curChap forall a b. (a -> b) -> a -> b
$ \Index
cc ->
let ln :: Int
ln = forall (t :: * -> *) a. Foldable t => t a -> Int
length Index
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 :: Index -> Index
inc Index
l = let i :: Int
i = forall a b. (a, b) -> a
fst (forall a. [a] -> a
last Index
l) forall a. Num a => a -> a -> a
+ Int
1 in forall a. [a] -> [a]
init Index
l forall a. Semigroup a => a -> a -> a
<> [(Int
i, Int -> Maybe Text
cl Int
i)]
cc' :: Index
cc' | Int
ln forall a. Ord a => a -> a -> Bool
> Int
n = Index -> Index
inc forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
take Int
n Index
cc
| Int
ln forall a. Eq a => a -> a -> Bool
== Int
n = Index -> Index
inc Index
cc
| Bool
otherwise = Index
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 Index
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
Index
index <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' References Index
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 :: Index
refIndex=Index
index
, refTitle :: [Inline]
refTitle= [Inline]
text'
, refSubfigure :: Maybe Index
refSubfigure = forall a. Maybe a
Nothing
}
Index
cc <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' References Index
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] -> Index -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
opts) Index
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