{-
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 #-}
module Text.Pandoc.CrossRef.Util.CustomLabels (customLabel, customHeadingLabel) where

import qualified Data.Text as T
import Text.Numeral.Roman
import Text.Pandoc.CrossRef.Util.Meta
import Text.Pandoc.Definition

customLabel :: Meta -> T.Text -> Int -> Maybe T.Text
customLabel :: Meta -> Text -> Int -> Maybe Text
customLabel Meta
meta Text
ref Int
i
  | Text
refLabel <- (Char -> Bool) -> Text -> Text
T.takeWhile (forall a. Eq a => a -> a -> Bool
/=Char
':') Text
ref
  , Just MetaValue
cl <- Text -> Meta -> Maybe MetaValue
lookupMeta (Text
refLabel forall a. Semigroup a => a -> a -> a
<> Text
"Labels") Meta
meta
  = Int -> Text -> MetaValue -> Maybe Text
mkLabel Int
i (Text
refLabel forall a. Semigroup a => a -> a -> a
<> Text
"Labels") MetaValue
cl
  | Bool
otherwise = forall a. Maybe a
Nothing

customHeadingLabel :: Meta -> Int -> Int -> Maybe T.Text
customHeadingLabel :: Meta -> Int -> Int -> Maybe Text
customHeadingLabel Meta
meta Int
lvl Int
i
  | Just MetaValue
cl <- forall a. Default a => (MetaValue -> a) -> Text -> Meta -> Int -> a
getMetaList forall a. a -> Maybe a
Just Text
"secLevelLabels" Meta
meta (Int
lvlforall a. Num a => a -> a -> a
-Int
1)
  = Int -> Text -> MetaValue -> Maybe Text
mkLabel Int
i Text
"secLevelLabels" MetaValue
cl
  | Bool
otherwise = forall a. Maybe a
Nothing

mkLabel :: Int -> T.Text -> MetaValue -> Maybe T.Text
mkLabel :: Int -> Text -> MetaValue -> Maybe Text
mkLabel Int
i Text
n MetaValue
lt
  | MetaList [MetaValue]
_ <- MetaValue
lt
  , Just Text
val <- Text -> MetaValue -> Text
toString Text
n forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> MetaValue -> Maybe MetaValue
getList (Int
iforall a. Num a => a -> a -> a
-Int
1) MetaValue
lt
  = forall a. a -> Maybe a
Just Text
val
  | Text -> MetaValue -> Text
toString Text
n MetaValue
lt forall a. Eq a => a -> a -> Bool
== Text
"arabic"
  = forall a. Maybe a
Nothing
  | Text -> MetaValue -> Text
toString Text
n MetaValue
lt forall a. Eq a => a -> a -> Bool
== Text
"roman"
  = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall n. (Ord n, Num n) => n -> Text
toRoman Int
i
  | Text -> MetaValue -> Text
toString Text
n MetaValue
lt forall a. Eq a => a -> a -> Bool
== Text
"lowercase roman"
  = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> Text
T.toLower forall a b. (a -> b) -> a -> b
$ forall n. (Ord n, Num n) => n -> Text
toRoman Int
i
  | Just (Char
startWith, Text
_) <- Text -> Maybe (Char, Text)
T.uncons forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> Text -> Maybe Text
T.stripPrefix Text
"alpha " (Text -> MetaValue -> Text
toString Text
n MetaValue
lt)
  = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Text
T.singleton forall a b. (a -> b) -> a -> b
$ [Char
startWith..] forall a. [a] -> Int -> a
!! (Int
iforall a. Num a => a -> a -> a
-Int
1)
  | Bool
otherwise = forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Unknown numeration type: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show MetaValue
lt