{-
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, FlexibleContexts, RecordWildCards, NamedFieldPuns #-}

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.Sequence as S
import Data.Sequence (ViewR(..))
import qualified Data.Text as T
import Text.Pandoc.Definition
import Text.Read (readMaybe)

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)
runHeader :: Int -> Attr -> [Inline] -> WS (ReplacedResult Block)
runHeader Int
n (Text
label, [Text]
cls, [(Text, Text)]
attrs) [Inline]
text'
  | Text
"unnumbered" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cls = do
      Text
label' <- WS Text
mangleLabel
      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]
text'
  | Bool
otherwise = do
      opts :: Options
opts@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
outFormat :: Options -> Maybe Format
lolTitle :: Options -> [Block]
lotTitle :: Options -> [Block]
lofTitle :: Options -> [Block]
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
lolItemTemplate :: Options -> BlockTemplate
lotItemTemplate :: Options -> BlockTemplate
lofItemTemplate :: 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
..} <- forall r (m :: * -> *). MonadReader r m => m r
ask
      Text
label' <- WS Text
mangleLabel
      Prefix -> Lens' References Index
ctrsAt Prefix
PfxSec forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= \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
<|> Int -> Int -> Maybe Text
customHeadingLabel Int
n Int
i forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Int -> Maybe Text
customLabel Text
"sec" Int
i
            inc :: Index -> Index
inc Index
l = case forall a. Seq a -> ViewR a
S.viewr Index
l of
              ViewR (Int, Maybe Text)
EmptyR -> forall a. HasCallStack => [Char] -> a
error [Char]
"impossible"
              Index
init' :> (Int, Maybe Text)
last' ->
                let i :: Int
i = forall a. Enum a => a -> a
succ forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ (Int, Maybe Text)
last'
                in Index
init' forall a. Seq a -> a -> Seq a
S.|> (Int
i, Int -> Maybe Text
cl Int
i)
            cc' :: Index
cc' | Just Int
num <- forall a. Read a => [Char] -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Char]
T.unpack forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"number" [(Text, Text)]
attrs
                = forall a. Int -> Seq a -> Seq a
S.take (Int
n forall a. Num a => a -> a -> a
- Int
1) Index
cc forall a. Seq a -> a -> Seq a
S.|> (Int
num, Int -> Maybe Text
cl Int
num)
                | Int
ln forall a. Ord a => a -> a -> Bool
> Int
n = Index -> Index
inc forall a b. (a -> b) -> a -> b
$ forall a. Int -> Seq a -> Seq a
S.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}. Seq (Int, Maybe a)
implicitChapters forall a. Seq a -> a -> Seq a
S.|> (Int
1,Int -> Maybe Text
cl Int
1)
            implicitChapters :: Seq (Int, Maybe a)
implicitChapters
              | Bool
numberSections = forall a. Int -> a -> Seq a
S.replicate (Int
nforall a. Num a => a -> a -> a
-Int
lnforall a. Num a => a -> a -> a
-Int
1) (Int
1, forall a. Maybe a
Nothing)
              | Bool
otherwise = forall a. Int -> a -> Seq a
S.replicate (Int
nforall a. Num a => a -> a -> a
-Int
lnforall a. Num a => a -> a -> a
-Int
1) (Int
0, forall a. Maybe a
Nothing)
        in Index
cc'
      Index
cc <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use forall a b. (a -> b) -> a -> b
$ Prefix -> Lens' References Index
ctrsAt Prefix
PfxSec
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text
"sec:" Text -> Text -> Bool
`T.isPrefixOf` Text
label') forall a b. (a -> b) -> a -> b
$
        Prefix -> Lens' References RefMap
refsAt Prefix
PfxSec forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
label' RefRec {
          refIndex :: Index
refIndex = Index
cc
        , refTitle :: [Inline]
refTitle = [Inline]
text'
        , refSubfigure :: Maybe Index
refSubfigure = forall a. Maybe a
Nothing
        }
      let textCC :: [Inline]
textCC
            | Bool
numberSections
            , Int
sectionsDepth forall a. Ord a => a -> a -> Bool
< Int
0
            Bool -> Bool -> Bool
|| Int
n forall a. Ord a => a -> a -> Bool
<= if Int
sectionsDepth forall a. Eq a => a -> a -> Bool
== Int
0 then Int
chaptersDepth else Int
sectionsDepth
            = 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
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
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
$ Template
secHeaderTemplate
            | Bool
otherwise = [Inline]
text'
          idxStr :: [Inline]
idxStr = [Inline] -> Index -> [Inline]
chapPrefix [Inline]
chapDelim Index
cc
          attrs' :: [(Text, Text)]
attrs' = Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(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
  where
    mangleLabel :: WS Text
mangleLabel = do
      Options{Bool
autoSectionLabels :: Bool
autoSectionLabels :: Options -> Bool
autoSectionLabels} <- 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
$
        if Bool
autoSectionLabels 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