{-
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 TemplateHaskell, GeneralizedNewtypeDeriving, RankNTypes, DataKinds #-}
module Text.Pandoc.CrossRef.References.Types where

import Data.Default
import qualified Data.Map as M
import Data.Text (Text)
import Lens.Micro.GHC
import Lens.Micro.TH
import Text.Pandoc.Definition
import qualified Data.Sequence as S

type Index = S.Seq (Int, Maybe Text)

data RefRec = RefRec { RefRec -> Index
refIndex :: Index
                     , RefRec -> [Inline]
refTitle :: [Inline]
                     , RefRec -> Maybe Index
refSubfigure :: Maybe Index
                     } deriving (Int -> RefRec -> ShowS
[RefRec] -> ShowS
RefRec -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RefRec] -> ShowS
$cshowList :: [RefRec] -> ShowS
show :: RefRec -> String
$cshow :: RefRec -> String
showsPrec :: Int -> RefRec -> ShowS
$cshowsPrec :: Int -> RefRec -> ShowS
Show, RefRec -> RefRec -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RefRec -> RefRec -> Bool
$c/= :: RefRec -> RefRec -> Bool
== :: RefRec -> RefRec -> Bool
$c== :: RefRec -> RefRec -> Bool
Eq)

type RefMap = M.Map Text RefRec

data Prefix
  = PfxImg
  | PfxEqn
  | PfxTbl
  | PfxLst
  | PfxSec
  deriving (Prefix -> Prefix -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Prefix -> Prefix -> Bool
$c/= :: Prefix -> Prefix -> Bool
== :: Prefix -> Prefix -> Bool
$c== :: Prefix -> Prefix -> Bool
Eq, Eq Prefix
Prefix -> Prefix -> Bool
Prefix -> Prefix -> Ordering
Prefix -> Prefix -> Prefix
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Prefix -> Prefix -> Prefix
$cmin :: Prefix -> Prefix -> Prefix
max :: Prefix -> Prefix -> Prefix
$cmax :: Prefix -> Prefix -> Prefix
>= :: Prefix -> Prefix -> Bool
$c>= :: Prefix -> Prefix -> Bool
> :: Prefix -> Prefix -> Bool
$c> :: Prefix -> Prefix -> Bool
<= :: Prefix -> Prefix -> Bool
$c<= :: Prefix -> Prefix -> Bool
< :: Prefix -> Prefix -> Bool
$c< :: Prefix -> Prefix -> Bool
compare :: Prefix -> Prefix -> Ordering
$ccompare :: Prefix -> Prefix -> Ordering
Ord, Int -> Prefix
Prefix -> Int
Prefix -> [Prefix]
Prefix -> Prefix
Prefix -> Prefix -> [Prefix]
Prefix -> Prefix -> Prefix -> [Prefix]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Prefix -> Prefix -> Prefix -> [Prefix]
$cenumFromThenTo :: Prefix -> Prefix -> Prefix -> [Prefix]
enumFromTo :: Prefix -> Prefix -> [Prefix]
$cenumFromTo :: Prefix -> Prefix -> [Prefix]
enumFromThen :: Prefix -> Prefix -> [Prefix]
$cenumFromThen :: Prefix -> Prefix -> [Prefix]
enumFrom :: Prefix -> [Prefix]
$cenumFrom :: Prefix -> [Prefix]
fromEnum :: Prefix -> Int
$cfromEnum :: Prefix -> Int
toEnum :: Int -> Prefix
$ctoEnum :: Int -> Prefix
pred :: Prefix -> Prefix
$cpred :: Prefix -> Prefix
succ :: Prefix -> Prefix
$csucc :: Prefix -> Prefix
Enum, Prefix
forall a. a -> a -> Bounded a
maxBound :: Prefix
$cmaxBound :: Prefix
minBound :: Prefix
$cminBound :: Prefix
Bounded, Int -> Prefix -> ShowS
[Prefix] -> ShowS
Prefix -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Prefix] -> ShowS
$cshowList :: [Prefix] -> ShowS
show :: Prefix -> String
$cshow :: Prefix -> String
showsPrec :: Int -> Prefix -> ShowS
$cshowsPrec :: Int -> Prefix -> ShowS
Show)

-- state data type
data References = References { References -> Map Prefix RefMap
_stRefs :: M.Map Prefix RefMap
                             , References -> Map Prefix Index
_stCtrs :: M.Map Prefix Index
                             } deriving (Int -> References -> ShowS
[References] -> ShowS
References -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [References] -> ShowS
$cshowList :: [References] -> ShowS
show :: References -> String
$cshow :: References -> String
showsPrec :: Int -> References -> ShowS
$cshowsPrec :: Int -> References -> ShowS
Show, References -> References -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: References -> References -> Bool
$c/= :: References -> References -> Bool
== :: References -> References -> Bool
$c== :: References -> References -> Bool
Eq)

instance Default References where
  def :: References
def = Map Prefix RefMap -> Map Prefix Index -> References
References forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty

makeLenses ''References

refsAt :: Prefix -> Lens' References RefMap
refsAt :: Prefix -> Lens' References RefMap
refsAt Prefix
pfx = Lens' References (Map Prefix RefMap)
stRefs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Prefix
pfx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Lens' (Maybe a) a
non forall a. Monoid a => a
mempty

ctrsAt :: Prefix -> Lens' References Index
ctrsAt :: Prefix -> Lens' References Index
ctrsAt Prefix
pfx = Lens' References (Map Prefix Index)
stCtrs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Prefix
pfx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Lens' (Maybe a) a
non forall a. Monoid a => a
mempty