{-
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 OverloadedStrings, LambdaCase, RankNTypes #-}
module Text.Pandoc.CrossRef.References.Refs (replaceRefs) where

import Control.Arrow as A
import Control.Monad.Reader
import Data.Function
import Data.List
import qualified Data.List.HT as HT
import qualified Data.Map as M
import Data.Maybe
import qualified Data.Text as T
import Text.Pandoc.Builder
import qualified Data.Sequence as S
import Data.Sequence (ViewR(..))
import Control.Monad (liftM2, join)

import Debug.Trace
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.Template
import Text.Pandoc.CrossRef.Util.Util

replaceRefs :: [Inline] -> WS [Inline]
replaceRefs :: [Inline] -> WS [Inline]
replaceRefs (Cite [Citation]
cits [Inline]
_:[Inline]
xs) = do
  Options
opts <- WS Options
forall r (m :: * -> *). MonadReader r m => m r
ask :: WS Options
  Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline])
-> ([[Inline]] -> Inlines) -> [[Inline]] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> [Inline] -> Inlines
forall a. [a] -> Many a
fromList [Inline]
xs) (Inlines -> Inlines)
-> ([[Inline]] -> Inlines) -> [[Inline]] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> [Inlines] -> Inlines
forall a (f :: * -> *).
(Eq a, Monoid a, Foldable f) =>
a -> f a -> a
intercalate' (Text -> Inlines
text Text
", ") ([Inlines] -> Inlines)
-> ([[Inline]] -> [Inlines]) -> [[Inline]] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> Inlines) -> [[Inline]] -> [Inlines]
forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> Inlines
forall a. [a] -> Many a
fromList ([[Inline]] -> [Inline]) -> WS [[Inline]] -> WS [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Citation] -> WS [Inline]) -> [[Citation]] -> WS [[Inline]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Options -> [Citation] -> WS [Inline]
replaceRefs' Options
opts) ((Citation -> Citation -> Bool) -> [Citation] -> [[Citation]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy Citation -> Citation -> Bool
eqPrefix [Citation]
cits)
  where
    eqPrefix :: Citation -> Citation -> Bool
eqPrefix Citation
a Citation
b = (Maybe Text -> Maybe Text -> Bool)
-> (Maybe Text, Maybe Text) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
(==) ((Maybe Text, Maybe Text) -> Bool)
-> (Maybe Text, Maybe Text) -> Bool
forall a b. (a -> b) -> a -> b
$
      ((Text -> Text) -> Maybe Text -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
uncapitalizeFirst (Maybe Text -> Maybe Text)
-> (Citation -> Maybe Text) -> Citation -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe Text
getLabelPrefix (Text -> Maybe Text)
-> (Citation -> Text) -> Citation -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> Text
citationId) (Citation -> Maybe Text)
-> (Citation, Citation) -> (Maybe Text, Maybe Text)
forall {b'} {c'}. (b' -> c') -> (b', b') -> (c', c')
<***> (Citation
a,Citation
b)
    <***> :: (b' -> c') -> (b', b') -> (c', c')
(<***>) = ((b' -> c') -> (b' -> c') -> (b', b') -> (c', c'))
-> (b' -> c') -> (b', b') -> (c', c')
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (b' -> c') -> (b' -> c') -> (b', b') -> (c', c')
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
(***)
    replaceRefs' :: Options -> [Citation] -> WS [Inline]
    replaceRefs' :: Options -> [Citation] -> WS [Inline]
replaceRefs' Options
opts [Citation]
cits'
      | Just Prefix
prefix <- [Citation] -> Maybe Prefix
allCitsPrefix [Citation]
cits'
      = Options -> Prefix -> [Citation] -> WS [Inline]
replaceRefs'' Options
opts Prefix
prefix [Citation]
cits'
      | Bool
otherwise = [Inline] -> WS [Inline]
forall a. a -> WS a
forall (m :: * -> *) a. Monad m => a -> m a
return [[Citation] -> [Inline] -> Inline
Cite [Citation]
cits' [Inline]
il']
        where
          il' :: [Inline]
il' = Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline]) -> Inlines -> [Inline]
forall a b. (a -> b) -> a -> b
$
              Text -> Inlines
str Text
"["
            Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Inlines -> [Inlines] -> Inlines
forall a (f :: * -> *).
(Eq a, Monoid a, Foldable f) =>
a -> f a -> a
intercalate' (Text -> Inlines
text Text
"; ") ((Citation -> Inlines) -> [Citation] -> [Inlines]
forall a b. (a -> b) -> [a] -> [b]
map Citation -> Inlines
citationToInlines [Citation]
cits')
            Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"]"
          citationToInlines :: Citation -> Inlines
citationToInlines Citation
c =
            [Inline] -> Inlines
forall a. [a] -> Many a
fromList (Citation -> [Inline]
citationPrefix Citation
c) Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
text (Text
"@" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Citation -> Text
citationId Citation
c)
              Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> [Inline] -> Inlines
forall a. [a] -> Many a
fromList (Citation -> [Inline]
citationSuffix Citation
c)
    replaceRefs'' :: Options -> Prefix -> [Citation] -> WS [Inline]
    replaceRefs'' :: Options -> Prefix -> [Citation] -> WS [Inline]
replaceRefs'' Options
opts = ((Options -> Prefix -> [Citation] -> WS [Inline])
-> Options -> Prefix -> [Citation] -> WS [Inline]
forall a b. (a -> b) -> a -> b
$ Options
opts) ((Options -> Prefix -> [Citation] -> WS [Inline])
 -> Prefix -> [Citation] -> WS [Inline])
-> ((Prefix -> Options -> [Citation] -> WS [Inline])
    -> Options -> Prefix -> [Citation] -> WS [Inline])
-> (Prefix -> Options -> [Citation] -> WS [Inline])
-> Prefix
-> [Citation]
-> WS [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Prefix -> Options -> [Citation] -> WS [Inline])
-> Options -> Prefix -> [Citation] -> WS [Inline]
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Prefix -> Options -> [Citation] -> WS [Inline])
 -> Prefix -> [Citation] -> WS [Inline])
-> (Prefix -> Options -> [Citation] -> WS [Inline])
-> Prefix
-> [Citation]
-> WS [Inline]
forall a b. (a -> b) -> a -> b
$
      if Options -> Bool
isLatexFormat Options
opts
      then Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsLatex
      else Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsOther
replaceRefs [Inline]
x = [Inline] -> WS [Inline]
forall a. a -> WS a
forall (m :: * -> *) a. Monad m => a -> m a
return [Inline]
x

pfxMap :: T.Text -> Maybe Prefix
pfxMap :: Text -> Maybe Prefix
pfxMap = \case
  Text
"fig:" -> Prefix -> Maybe Prefix
forall a. a -> Maybe a
Just Prefix
PfxImg
  Text
"eq:"  -> Prefix -> Maybe Prefix
forall a. a -> Maybe a
Just Prefix
PfxEqn
  Text
"tbl:" -> Prefix -> Maybe Prefix
forall a. a -> Maybe a
Just Prefix
PfxTbl
  Text
"lst:" -> Prefix -> Maybe Prefix
forall a. a -> Maybe a
Just Prefix
PfxLst
  Text
"sec:" -> Prefix -> Maybe Prefix
forall a. a -> Maybe a
Just Prefix
PfxSec
  Text
_ -> Maybe Prefix
forall a. Maybe a
Nothing

pfxMapR :: Prefix -> T.Text
pfxMapR :: Prefix -> Text
pfxMapR = \case
  Prefix
PfxImg -> Text
"fig:"
  Prefix
PfxEqn -> Text
"eq:"
  Prefix
PfxTbl -> Text
"tbl:"
  Prefix
PfxLst -> Text
"lst:"
  Prefix
PfxSec -> Text
"sec:"

-- accessors to options
prefMap :: Prefix -> (Options -> Bool -> Int -> [Inline], Options -> Template)
prefMap :: Prefix -> (Options -> Bool -> Int -> [Inline], Options -> Template)
prefMap = \case
  Prefix
PfxImg -> (Options -> Bool -> Int -> [Inline]
figPrefix, Options -> Template
figPrefixTemplate)
  Prefix
PfxEqn -> (Options -> Bool -> Int -> [Inline]
eqnPrefix, Options -> Template
eqnPrefixTemplate)
  Prefix
PfxTbl -> (Options -> Bool -> Int -> [Inline]
tblPrefix, Options -> Template
tblPrefixTemplate)
  Prefix
PfxLst -> (Options -> Bool -> Int -> [Inline]
lstPrefix, Options -> Template
lstPrefixTemplate)
  Prefix
PfxSec -> (Options -> Bool -> Int -> [Inline]
secPrefix, Options -> Template
secPrefixTemplate)

prefixes :: [Prefix]
prefixes :: [Prefix]
prefixes = [Prefix
forall a. Bounded a => a
minBound..]

getRefPrefix :: Options -> Prefix -> Bool -> Int -> [Inline] -> [Inline]
getRefPrefix :: Options -> Prefix -> Bool -> Int -> [Inline] -> [Inline]
getRefPrefix Options
opts Prefix
prefix Bool
capitalize Int
num [Inline]
cit =
  Map Text [Inline] -> Template -> [Inline]
forall a b. MkTemplate a b => Map Text [Inline] -> b -> [a]
applyTemplate' ([(Text, [Inline])] -> Map Text [Inline]
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList [(Text
"i", [Inline]
cit), (Text
"p", [Inline]
refprefix)])
        (Template -> [Inline]) -> Template -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Template
reftempl Options
opts
  where (Options -> Bool -> Int -> [Inline]
refprefixf, Options -> Template
reftempl) = Prefix -> (Options -> Bool -> Int -> [Inline], Options -> Template)
prefMap Prefix
prefix
        refprefix :: [Inline]
refprefix = Options -> Bool -> Int -> [Inline]
refprefixf Options
opts Bool
capitalize Int
num

allCitsPrefix :: [Citation] -> Maybe Prefix
allCitsPrefix :: [Citation] -> Maybe Prefix
allCitsPrefix [Citation]
cits = (Prefix -> Bool) -> [Prefix] -> Maybe Prefix
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find Prefix -> Bool
isCitationPrefix [Prefix]
prefixes
  where
  isCitationPrefix :: Prefix -> Bool
isCitationPrefix Prefix
p =
    (Citation -> Bool) -> [Citation] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Prefix -> Text
pfxMapR Prefix
p Text -> Text -> Bool
`T.isPrefixOf`) (Text -> Bool) -> (Citation -> Text) -> Citation -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
uncapitalizeFirst (Text -> Text) -> (Citation -> Text) -> Citation -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> Text
citationId) [Citation]
cits

replaceRefsLatex :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsLatex :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsLatex Prefix
prefix Options
opts [Citation]
cits
  | Options -> Bool
cref Options
opts
  = Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsLatex' Prefix
prefix Options
opts [Citation]
cits
  | Bool
otherwise
  = Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline])
-> ([[Inline]] -> Inlines) -> [[Inline]] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> [Inlines] -> Inlines
forall a (f :: * -> *).
(Eq a, Monoid a, Foldable f) =>
a -> f a -> a
intercalate' (Text -> Inlines
text Text
", ") ([Inlines] -> Inlines)
-> ([[Inline]] -> [Inlines]) -> [[Inline]] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> Inlines) -> [[Inline]] -> [Inlines]
forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> Inlines
forall a. [a] -> Many a
fromList ([[Inline]] -> [Inline]) -> WS [[Inline]] -> WS [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
      ([Citation] -> WS [Inline]) -> [[Citation]] -> WS [[Inline]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsLatex' Prefix
prefix Options
opts) ((Citation -> Citation -> Bool) -> [Citation] -> [[Citation]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy Citation -> Citation -> Bool
citationGroupPred [Citation]
cits)

replaceRefsLatex' :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsLatex' :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsLatex' Prefix
prefix Options
opts [Citation]
cits =
  [Inline] -> WS [Inline]
forall a. a -> WS a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> WS [Inline]) -> [Inline] -> WS [Inline]
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
p [Inline
texcit]
  where
    texcit :: Inline
texcit =
      Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"tex") (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$
      if Options -> Bool
cref Options
opts then
        Text
cref'Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"{"Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Prefix -> Text -> Text -> Text -> [Citation] -> Text
listLabels Prefix
prefix Text
"" Text
"," Text
"" [Citation]
citsText -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"}"
        else
          Prefix -> Text -> Text -> Text -> [Citation] -> Text
listLabels Prefix
prefix Text
"\\ref{" Text
", " Text
"}" [Citation]
cits
    suppressAuthor :: Bool
suppressAuthor = (Citation -> Bool) -> [Citation] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((CitationMode -> CitationMode -> Bool
forall a. Eq a => a -> a -> Bool
==CitationMode
SuppressAuthor) (CitationMode -> Bool)
-> (Citation -> CitationMode) -> Citation -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> CitationMode
citationMode) [Citation]
cits
    noPrefix :: Bool
noPrefix = (Citation -> Bool) -> [Citation] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Inline] -> Bool) -> (Citation -> [Inline]) -> Citation -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> [Inline]
citationPrefix) [Citation]
cits
    p :: [Inline] -> [Inline]
p | Options -> Bool
cref Options
opts = [Inline] -> [Inline]
forall a. a -> a
id
      | Bool
suppressAuthor
      = [Inline] -> [Inline]
forall a. a -> a
id
      | Bool
noPrefix
      = Options -> Prefix -> Bool -> Int -> [Inline] -> [Inline]
getRefPrefix Options
opts Prefix
prefix Bool
cap ([Citation] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Citation]
cits Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
      | Bool
otherwise = ((Citation -> [Inline]
citationPrefix ([Citation] -> Citation
forall a. HasCallStack => [a] -> a
head [Citation]
cits) [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline
Space]) [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<>)
    cap :: Bool
cap = Bool -> (Text -> Bool) -> Maybe Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False Text -> Bool
isFirstUpper (Maybe Text -> Bool) -> Maybe Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
getLabelPrefix (Text -> Maybe Text)
-> ([Citation] -> Text) -> [Citation] -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> Text
citationId (Citation -> Text)
-> ([Citation] -> Citation) -> [Citation] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Citation] -> Citation
forall a. HasCallStack => [a] -> a
head ([Citation] -> Maybe Text) -> [Citation] -> Maybe Text
forall a b. (a -> b) -> a -> b
$ [Citation]
cits
    cref' :: Text
cref' | Bool
suppressAuthor = Text
"\\labelcref"
          | Bool
cap = Text
"\\Cref"
          | Bool
otherwise = Text
"\\cref"

listLabels :: Prefix -> T.Text -> T.Text -> T.Text -> [Citation] -> T.Text
listLabels :: Prefix -> Text -> Text -> Text -> [Citation] -> Text
listLabels Prefix
prefix Text
p Text
sep Text
s =
  Text -> [Text] -> Text
T.intercalate Text
sep ([Text] -> Text) -> ([Citation] -> [Text]) -> [Citation] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Citation -> Text) -> [Citation] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ((Text
p Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (Citation -> Text) -> Citation -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
s) (Text -> Text) -> (Citation -> Text) -> Citation -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
mkLaTeXLabel' (Text -> Text) -> (Citation -> Text) -> Citation -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Prefix -> Text
pfxMapR Prefix
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (Citation -> Text) -> Citation -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
getLabelWithoutPrefix (Text -> Text) -> (Citation -> Text) -> Citation -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> Text
citationId)

getLabelWithoutPrefix :: T.Text -> T.Text
getLabelWithoutPrefix :: Text -> Text
getLabelWithoutPrefix = Int -> Text -> Text
T.drop Int
1 (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> Text
T.dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
':')

getLabelPrefix :: T.Text -> Maybe T.Text
getLabelPrefix :: Text -> Maybe Text
getLabelPrefix Text
lab
  | Just Prefix
pfx <- Text -> Maybe Prefix
pfxMap (Text -> Text
uncapitalizeFirst Text
p)
  , Prefix
pfx Prefix -> [Prefix] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Prefix]
prefixes = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p
  | Bool
otherwise = Maybe Text
forall a. Maybe a
Nothing
  where p :: Text
p = (Text -> Char -> Text) -> Char -> Text -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Char -> Text
T.snoc Char
':' (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
':') (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text
lab

replaceRefsOther :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsOther :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsOther Prefix
prefix Options
opts [Citation]
cits = Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline])
-> ([[Inline]] -> Inlines) -> [[Inline]] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> [Inlines] -> Inlines
forall a (f :: * -> *).
(Eq a, Monoid a, Foldable f) =>
a -> f a -> a
intercalate' (Text -> Inlines
text Text
", ") ([Inlines] -> Inlines)
-> ([[Inline]] -> [Inlines]) -> [[Inline]] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> Inlines) -> [[Inline]] -> [Inlines]
forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> Inlines
forall a. [a] -> Many a
fromList ([[Inline]] -> [Inline]) -> WS [[Inline]] -> WS [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    ([Citation] -> WS [Inline]) -> [[Citation]] -> WS [[Inline]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsOther' Prefix
prefix Options
opts) ((Citation -> Citation -> Bool) -> [Citation] -> [[Citation]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy Citation -> Citation -> Bool
citationGroupPred [Citation]
cits)

citationGroupPred :: Citation -> Citation -> Bool
citationGroupPred :: Citation -> Citation -> Bool
citationGroupPred = ([Inline], CitationMode) -> ([Inline], CitationMode) -> Bool
forall a. Eq a => a -> a -> Bool
(==) (([Inline], CitationMode) -> ([Inline], CitationMode) -> Bool)
-> (Citation -> ([Inline], CitationMode))
-> Citation
-> Citation
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` ([Inline] -> CitationMode -> ([Inline], CitationMode))
-> (Citation -> [Inline])
-> (Citation -> CitationMode)
-> Citation
-> ([Inline], CitationMode)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,) Citation -> [Inline]
citationPrefix Citation -> CitationMode
citationMode

replaceRefsOther' :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsOther' :: Prefix -> Options -> [Citation] -> WS [Inline]
replaceRefsOther' Prefix
prefix Options
opts [Citation]
cits = do
  [RefData]
indices <- (Citation -> WS RefData) -> [Citation] -> WS [RefData]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Prefix -> Options -> Citation -> WS RefData
getRefIndex Prefix
prefix Options
opts) [Citation]
cits
  let
    cap :: Bool
cap = Bool -> (Text -> Bool) -> Maybe Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False Text -> Bool
isFirstUpper (Maybe Text -> Bool) -> Maybe Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
getLabelPrefix (Text -> Maybe Text)
-> ([Citation] -> Text) -> [Citation] -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> Text
citationId (Citation -> Text)
-> ([Citation] -> Citation) -> [Citation] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Citation] -> Citation
forall a. HasCallStack => [a] -> a
head ([Citation] -> Maybe Text) -> [Citation] -> Maybe Text
forall a b. (a -> b) -> a -> b
$ [Citation]
cits
    writePrefix :: [Inline] -> [Inline]
writePrefix | (Citation -> Bool) -> [Citation] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((CitationMode -> CitationMode -> Bool
forall a. Eq a => a -> a -> Bool
==CitationMode
SuppressAuthor) (CitationMode -> Bool)
-> (Citation -> CitationMode) -> Citation -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> CitationMode
citationMode) [Citation]
cits
                = [Inline] -> [Inline]
forall a. a -> a
id
                | (Citation -> Bool) -> [Citation] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Inline] -> Bool) -> (Citation -> [Inline]) -> Citation -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Citation -> [Inline]
citationPrefix) [Citation]
cits
                = ([Inline] -> [Inline]) -> [Inline] -> [Inline]
cmap (([Inline] -> [Inline]) -> [Inline] -> [Inline])
-> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ Options -> Prefix -> Bool -> Int -> [Inline] -> [Inline]
getRefPrefix Options
opts Prefix
prefix Bool
cap ([Citation] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Citation]
cits Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
                | Bool
otherwise
                = ([Inline] -> [Inline]) -> [Inline] -> [Inline]
cmap (([Inline] -> [Inline]) -> [Inline] -> [Inline])
-> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline])
-> ([Inline] -> Inlines) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Inline] -> Inlines
forall a. [a] -> Many a
fromList (Citation -> [Inline]
citationPrefix ([Citation] -> Citation
forall a. HasCallStack => [a] -> a
head [Citation]
cits)) Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Inlines
space) Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<>) (Inlines -> Inlines)
-> ([Inline] -> Inlines) -> [Inline] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inlines
forall a. [a] -> Many a
fromList
    cmap :: ([Inline] -> [Inline]) -> [Inline] -> [Inline]
cmap [Inline] -> [Inline]
f [Link Attr
attr [Inline]
t Target
w]
      | Options -> Bool
nameInLink Options
opts = [Attr -> [Inline] -> Target -> Inline
Link Attr
attr ([Inline] -> [Inline]
f [Inline]
t) Target
w]
    cmap [Inline] -> [Inline]
f [Inline]
x = [Inline] -> [Inline]
f [Inline]
x
  [Inline] -> WS [Inline]
forall a. a -> WS a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> WS [Inline]) -> [Inline] -> WS [Inline]
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
writePrefix (Options -> [RefData] -> [Inline]
makeIndices Options
opts [RefData]
indices)

data RefData = RefData { RefData -> Text
rdLabel :: T.Text
                       , RefData -> Maybe (Seq (Int, Maybe Text))
rdIdx :: Maybe Index
                       , RefData -> Maybe (Seq (Int, Maybe Text))
rdSubfig :: Maybe Index
                       , RefData -> [Inline]
rdSuffix :: [Inline]
                       , RefData -> Maybe [Inline]
rdTitle :: Maybe [Inline]
                       , RefData -> Text
rdPfx :: T.Text
                       } deriving (RefData -> RefData -> Bool
(RefData -> RefData -> Bool)
-> (RefData -> RefData -> Bool) -> Eq RefData
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RefData -> RefData -> Bool
== :: RefData -> RefData -> Bool
$c/= :: RefData -> RefData -> Bool
/= :: RefData -> RefData -> Bool
Eq)

instance Ord RefData where
  <= :: RefData -> RefData -> Bool
(<=) = Maybe (Seq (Int, Maybe Text))
-> Maybe (Seq (Int, Maybe Text)) -> Bool
forall a. Ord a => a -> a -> Bool
(<=) (Maybe (Seq (Int, Maybe Text))
 -> Maybe (Seq (Int, Maybe Text)) -> Bool)
-> (RefData -> Maybe (Seq (Int, Maybe Text)))
-> RefData
-> RefData
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` RefData -> Maybe (Seq (Int, Maybe Text))
rdIdx

getRefIndex :: Prefix -> Options -> Citation -> WS RefData
getRefIndex :: Prefix -> Options -> Citation -> WS RefData
getRefIndex Prefix
prefix Options
_opts Citation{citationId :: Citation -> Text
citationId=Text
cid,citationSuffix :: Citation -> [Inline]
citationSuffix=[Inline]
suf}
  = do
    Maybe RefRec
ref <- Text -> Map Text RefRec -> Maybe RefRec
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
lab (Map Text RefRec -> Maybe RefRec)
-> WS (Map Text RefRec) -> WS (Maybe RefRec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting (Map Text RefRec) References (Map Text RefRec)
-> WS (Map Text RefRec)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting (Map Text RefRec) References (Map Text RefRec)
prop
    let sub :: Maybe (Maybe (Seq (Int, Maybe Text)))
sub = RefRec -> Maybe (Seq (Int, Maybe Text))
refSubfigure (RefRec -> Maybe (Seq (Int, Maybe Text)))
-> Maybe RefRec -> Maybe (Maybe (Seq (Int, Maybe Text)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe RefRec
ref
        idx :: Maybe (Seq (Int, Maybe Text))
idx = RefRec -> Seq (Int, Maybe Text)
refIndex (RefRec -> Seq (Int, Maybe Text))
-> Maybe RefRec -> Maybe (Seq (Int, Maybe Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe RefRec
ref
        tit :: Maybe [Inline]
tit = RefRec -> [Inline]
refTitle (RefRec -> [Inline]) -> Maybe RefRec -> Maybe [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe RefRec
ref
    RefData -> WS RefData
forall a. a -> WS a
forall (m :: * -> *) a. Monad m => a -> m a
return RefData
      { rdLabel :: Text
rdLabel = Text
lab
      , rdIdx :: Maybe (Seq (Int, Maybe Text))
rdIdx = Maybe (Seq (Int, Maybe Text))
idx
      , rdSubfig :: Maybe (Seq (Int, Maybe Text))
rdSubfig = Maybe (Maybe (Seq (Int, Maybe Text)))
-> Maybe (Seq (Int, Maybe Text))
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join Maybe (Maybe (Seq (Int, Maybe Text)))
sub
      , rdSuffix :: [Inline]
rdSuffix = [Inline]
suf
      , rdTitle :: Maybe [Inline]
rdTitle = Maybe [Inline]
tit
      , rdPfx :: Text
rdPfx = Prefix -> Text
pfxMapR Prefix
prefix
      }
  where
  prop :: Getting (Map Text RefRec) References (Map Text RefRec)
prop = Prefix -> Lens' References (Map Text RefRec)
refsAt Prefix
prefix
  lab :: Text
lab = Prefix -> Text
pfxMapR Prefix
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
getLabelWithoutPrefix Text
cid

data RefItem = RefRange RefData RefData | RefSingle RefData

makeIndices :: Options -> [RefData] -> [Inline]
makeIndices :: Options -> [RefData] -> [Inline]
makeIndices Options
o [RefData]
s = [RefItem] -> [Inline]
format ([RefItem] -> [Inline]) -> [RefItem] -> [Inline]
forall a b. (a -> b) -> a -> b
$ ([RefData] -> [RefItem]) -> [[RefData]] -> [RefItem]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [RefData] -> [RefItem]
f ([[RefData]] -> [RefItem]) -> [[RefData]] -> [RefItem]
forall a b. (a -> b) -> a -> b
$ (RefData -> RefData -> Bool) -> [RefData] -> [[RefData]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
HT.groupBy RefData -> RefData -> Bool
g ([RefData] -> [[RefData]]) -> [RefData] -> [[RefData]]
forall a b. (a -> b) -> a -> b
$ [RefData] -> [RefData]
forall a. Ord a => [a] -> [a]
sort ([RefData] -> [RefData]) -> [RefData] -> [RefData]
forall a b. (a -> b) -> a -> b
$ [RefData] -> [RefData]
forall a. Eq a => [a] -> [a]
nub [RefData]
s
  where
  g :: RefData -> RefData -> Bool
  g :: RefData -> RefData -> Bool
g RefData
a RefData
b = (RefData -> Bool) -> [RefData] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Inline] -> Bool) -> (RefData -> [Inline]) -> RefData -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RefData -> [Inline]
rdSuffix) [RefData
a, RefData
b] Bool -> Bool -> Bool
&& (
            (RefData -> Bool) -> [RefData] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Maybe (Seq (Int, Maybe Text)) -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe (Seq (Int, Maybe Text)) -> Bool)
-> (RefData -> Maybe (Seq (Int, Maybe Text))) -> RefData -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RefData -> Maybe (Seq (Int, Maybe Text))
rdSubfig) [RefData
a, RefData
b] Bool -> Bool -> Bool
&&
            Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== ((Seq (Int, Maybe Text) -> Seq (Int, Maybe Text) -> Bool)
-> Maybe (Seq (Int, Maybe Text))
-> Maybe (Seq (Int, Maybe Text))
-> Maybe Bool
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 Seq (Int, Maybe Text) -> Seq (Int, Maybe Text) -> Bool
follows (Maybe (Seq (Int, Maybe Text))
 -> Maybe (Seq (Int, Maybe Text)) -> Maybe Bool)
-> (RefData -> Maybe (Seq (Int, Maybe Text)))
-> RefData
-> RefData
-> Maybe Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` RefData -> Maybe (Seq (Int, Maybe Text))
rdIdx) RefData
b RefData
a Bool -> Bool -> Bool
||
            RefData -> Maybe (Seq (Int, Maybe Text))
rdIdx RefData
a Maybe (Seq (Int, Maybe Text))
-> Maybe (Seq (Int, Maybe Text)) -> Bool
forall a. Eq a => a -> a -> Bool
== RefData -> Maybe (Seq (Int, Maybe Text))
rdIdx RefData
b Bool -> Bool -> Bool
&&
            Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== ((Seq (Int, Maybe Text) -> Seq (Int, Maybe Text) -> Bool)
-> Maybe (Seq (Int, Maybe Text))
-> Maybe (Seq (Int, Maybe Text))
-> Maybe Bool
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 Seq (Int, Maybe Text) -> Seq (Int, Maybe Text) -> Bool
follows (Maybe (Seq (Int, Maybe Text))
 -> Maybe (Seq (Int, Maybe Text)) -> Maybe Bool)
-> (RefData -> Maybe (Seq (Int, Maybe Text)))
-> RefData
-> RefData
-> Maybe Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` RefData -> Maybe (Seq (Int, Maybe Text))
rdSubfig) RefData
b RefData
a
          )
  follows :: Index -> Index -> Bool
  follows :: Seq (Int, Maybe Text) -> Seq (Int, Maybe Text) -> Bool
follows Seq (Int, Maybe Text)
a Seq (Int, Maybe Text)
b
    | Seq (Int, Maybe Text)
ai :> (Int, Maybe Text)
al <- Seq (Int, Maybe Text) -> ViewR (Int, Maybe Text)
forall a. Seq a -> ViewR a
S.viewr Seq (Int, Maybe Text)
a
    , Seq (Int, Maybe Text)
bi :> (Int, Maybe Text)
bl <- Seq (Int, Maybe Text) -> ViewR (Int, Maybe Text)
forall a. Seq a -> ViewR a
S.viewr Seq (Int, Maybe Text)
b
    = Seq (Int, Maybe Text)
ai Seq (Int, Maybe Text) -> Seq (Int, Maybe Text) -> Bool
forall a. Eq a => a -> a -> Bool
== Seq (Int, Maybe Text)
bi Bool -> Bool -> Bool
&& (Int -> Int) -> (Int, Maybe Text) -> (Int, Maybe Text)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
A.first Int -> Int
forall a. Enum a => a -> a
succ (Int, Maybe Text)
bl (Int, Maybe Text) -> (Int, Maybe Text) -> Bool
forall a. Eq a => a -> a -> Bool
== (Int, Maybe Text)
al
    | Bool
otherwise = Bool
False
  f :: [RefData] -> [RefItem]
  f :: [RefData] -> [RefItem]
f []  = []                          -- drop empty lists
  f [RefData
w] = [RefData -> RefItem
RefSingle RefData
w]                   -- single value
  f [RefData
w1,RefData
w2] = [RefData -> RefItem
RefSingle RefData
w1, RefData -> RefItem
RefSingle RefData
w2] -- two values
  f (RefData
x:[RefData]
xs) = [RefData -> RefData -> RefItem
RefRange RefData
x ([RefData] -> RefData
forall a. HasCallStack => [a] -> a
last [RefData]
xs)] -- shorten more than two values
  format :: [RefItem] -> [Inline]
  format :: [RefItem] -> [Inline]
format [] = []
  format [RefItem
x] = Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline]) -> Inlines -> [Inline]
forall a b. (a -> b) -> a -> b
$ RefItem -> Inlines
show'' RefItem
x
  format [RefItem
x, RefItem
y] = Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline]) -> Inlines -> [Inline]
forall a b. (a -> b) -> a -> b
$ RefItem -> Inlines
show'' RefItem
x Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> [Inline] -> Inlines
forall a. [a] -> Many a
fromList (Options -> [Inline]
pairDelim Options
o) Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> RefItem -> Inlines
show'' RefItem
y
  format [RefItem]
xs = Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> [Inline]) -> Inlines -> [Inline]
forall a b. (a -> b) -> a -> b
$ Inlines -> [Inlines] -> Inlines
forall a (f :: * -> *).
(Eq a, Monoid a, Foldable f) =>
a -> f a -> a
intercalate' ([Inline] -> Inlines
forall a. [a] -> Many a
fromList ([Inline] -> Inlines) -> [Inline] -> Inlines
forall a b. (a -> b) -> a -> b
$ Options -> [Inline]
refDelim Options
o) [Inlines]
init' Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> [Inline] -> Inlines
forall a. [a] -> Many a
fromList (Options -> [Inline]
lastDelim Options
o) Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Inlines
last'
    where initlast :: [a] -> ([a], a)
initlast []     = [Char] -> ([a], a)
forall a. HasCallStack => [Char] -> a
error [Char]
"emtpy list in initlast"
          initlast [a
y]    = ([], a
y)
          initlast (a
y:[a]
ys) = ([a] -> [a]) -> ([a], a) -> ([a], a)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], a) -> ([a], a)) -> ([a], a) -> ([a], a)
forall a b. (a -> b) -> a -> b
$ [a] -> ([a], a)
initlast [a]
ys
          ([Inlines]
init', Inlines
last') = [Inlines] -> ([Inlines], Inlines)
forall {a}. [a] -> ([a], a)
initlast ([Inlines] -> ([Inlines], Inlines))
-> [Inlines] -> ([Inlines], Inlines)
forall a b. (a -> b) -> a -> b
$ (RefItem -> Inlines) -> [RefItem] -> [Inlines]
forall a b. (a -> b) -> [a] -> [b]
map RefItem -> Inlines
show'' [RefItem]
xs
  show'' :: RefItem -> Inlines
  show'' :: RefItem -> Inlines
show'' (RefSingle RefData
x) = RefData -> Inlines
show' RefData
x
  show'' (RefRange RefData
x RefData
y) = RefData -> Inlines
show' RefData
x Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> [Inline] -> Inlines
forall a. [a] -> Many a
fromList (Options -> [Inline]
rangeDelim Options
o) Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> RefData -> Inlines
show' RefData
y
  show' :: RefData -> Inlines
  show' :: RefData -> Inlines
show' RefData{rdLabel :: RefData -> Text
rdLabel=Text
l, rdIdx :: RefData -> Maybe (Seq (Int, Maybe Text))
rdIdx=Just Seq (Int, Maybe Text)
i, rdSubfig :: RefData -> Maybe (Seq (Int, Maybe Text))
rdSubfig = Maybe (Seq (Int, Maybe Text))
sub, rdSuffix :: RefData -> [Inline]
rdSuffix = [Inline]
suf, rdTitle :: RefData -> Maybe [Inline]
rdTitle=Maybe [Inline]
tit, rdPfx :: RefData -> Text
rdPfx=Text
pfx}
    | Options -> Bool
linkReferences Options
o = Text -> Text -> Inlines -> Inlines
link (Char
'#' Char -> Text -> Text
`T.cons` Text
l) Text
"" ([Inline] -> Inlines
forall a. [a] -> Many a
fromList [Inline]
txt)
    | Bool
otherwise = [Inline] -> Inlines
forall a. [a] -> Many a
fromList [Inline]
txt
    where
      txt :: [Inline]
txt
        | Just Seq (Int, Maybe Text)
sub' <- Maybe (Seq (Int, Maybe Text))
sub
        = let vars :: Map Text [Inline]
vars = [(Text, [Inline])] -> Map Text [Inline]
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
                      [ (Text
"i", [Inline] -> Seq (Int, Maybe Text) -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
o) Seq (Int, Maybe Text)
i)
                      , (Text
"s", [Inline] -> Seq (Int, Maybe Text) -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
o) Seq (Int, Maybe Text)
sub')
                      , (Text
"suf", [Inline]
suf)
                      , (Text
"t", [Inline] -> Maybe [Inline] -> [Inline]
forall a. a -> Maybe a -> a
fromMaybe [Inline]
forall a. Monoid a => a
mempty Maybe [Inline]
tit)
                      ]
          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
$ Options -> Template
subfigureRefIndexTemplate Options
o
        | Bool
otherwise
        = let vars :: Map Text [Inline]
vars = [(Text, [Inline])] -> Map Text [Inline]
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
                      [ (Text
"i", [Inline] -> Seq (Int, Maybe Text) -> [Inline]
chapPrefix (Options -> [Inline]
chapDelim Options
o) Seq (Int, Maybe Text)
i)
                      , (Text
"suf", [Inline]
suf)
                      , (Text
"t", [Inline] -> Maybe [Inline] -> [Inline]
forall a. a -> Maybe a -> a
fromMaybe [Inline]
forall a. Monoid a => a
mempty Maybe [Inline]
tit)
                      ]
          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
$ Options -> Text -> Template
refIndexTemplate Options
o (Int -> Text -> Text
T.dropEnd Int
1 Text
pfx)
  show' RefData{rdLabel :: RefData -> Text
rdLabel=Text
l, rdIdx :: RefData -> Maybe (Seq (Int, Maybe Text))
rdIdx=Maybe (Seq (Int, Maybe Text))
Nothing, rdSuffix :: RefData -> [Inline]
rdSuffix = [Inline]
suf} =
    [Char] -> Inlines -> Inlines
forall a. [Char] -> a -> a
trace (Text -> [Char]
T.unpack (Text -> [Char]) -> Text -> [Char]
forall a b. (a -> b) -> a -> b
$ Text
"Undefined cross-reference: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
l)
          (Inlines -> Inlines
strong (Text -> Inlines
text (Text -> Inlines) -> Text -> Inlines
forall a b. (a -> b) -> a -> b
$ Text
"¿" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
l Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"?") Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> [Inline] -> Inlines
forall a. [a] -> Many a
fromList [Inline]
suf)