{-
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.ModifyMeta
    (
    modifyMeta
    ) where

import Control.Monad.Writer
import qualified Data.Text as T
import Text.Pandoc
import Text.Pandoc.Builder hiding ((<>))
import Text.Pandoc.CrossRef.Util.Meta
import Text.Pandoc.CrossRef.Util.Options
import Text.Pandoc.CrossRef.Util.Util

modifyMeta :: Options -> Meta -> Meta
modifyMeta :: Options -> Meta -> Meta
modifyMeta Options
opts Meta
meta
  | Maybe Format -> Bool
isLatexFormat (Options -> Maybe Format
outFormat Options
opts)
  = forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
setMeta Text
"header-includes"
      (Maybe MetaValue -> MetaValue
headerInc forall a b. (a -> b) -> a -> b
$ Text -> Meta -> Maybe MetaValue
lookupMeta Text
"header-includes" Meta
meta)
      Meta
meta
  | Bool
otherwise = Meta
meta
  where
    headerInc :: Maybe MetaValue -> MetaValue
    headerInc :: Maybe MetaValue -> MetaValue
headerInc Maybe MetaValue
Nothing = MetaValue
incList
    headerInc (Just (MetaList [MetaValue]
x)) = [MetaValue] -> MetaValue
MetaList forall a b. (a -> b) -> a -> b
$ [MetaValue]
x forall a. Semigroup a => a -> a -> a
<> [MetaValue
incList]
    headerInc (Just MetaValue
x) = [MetaValue] -> MetaValue
MetaList [MetaValue
x, MetaValue
incList]
    incList :: MetaValue
incList = [Block] -> MetaValue
MetaBlocks forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ forall w a. Writer w a -> w
execWriter forall a b. (a -> b) -> a -> b
$ do
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [ Text
"\\makeatletter" ]
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
subfig
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
floatnames
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
listnames
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
subfigures
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Options -> Bool
listings Options
opts) forall a b. (a -> b) -> a -> b
$
          forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
codelisting
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
lolcommand
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Options -> Bool
cref Options
opts) forall a b. (a -> b) -> a -> b
$ forall {a}. WriterT [Text] Identity a -> WriterT [Text] Identity a
atEndPreamble forall a b. (a -> b) -> a -> b
$ do
          forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
cleveref
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Options -> Bool
listings Options
opts) forall a b. (a -> b) -> a -> b
$
            forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text]
cleverefCodelisting
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [ Text
"\\makeatother" ]
      where
        atEndPreamble :: WriterT [Text] Identity a -> WriterT [Text] Identity a
atEndPreamble = forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor (\[Text]
c -> Text
"\\AtEndPreamble{%"forall a. a -> [a] -> [a]
:[Text]
c forall a. Semigroup a => a -> a -> a
<> [Text
"}"])
        subfig :: [Text]
subfig = [
            [Text] -> Text -> Text
usepackage [] Text
"subfig"
          , [Text] -> Text -> Text
usepackage [] Text
"caption"
          , Text
"\\captionsetup[subfloat]{margin=0.5em}"
          ]
        floatnames :: [Text]
floatnames = [
            Text
"\\AtBeginDocument{%"
          , Text
"\\renewcommand*\\figurename{" forall a. Semigroup a => a -> a -> a
<> Text -> Text
metaString Text
"figureTitle" forall a. Semigroup a => a -> a -> a
<> Text
"}"
          , Text
"\\renewcommand*\\tablename{" forall a. Semigroup a => a -> a -> a
<> Text -> Text
metaString Text
"tableTitle" forall a. Semigroup a => a -> a -> a
<> Text
"}"
          , Text
"}"
          ]
        listnames :: [Text]
listnames = [
            Text
"\\AtBeginDocument{%"
          , Text
"\\renewcommand*\\listfigurename{" forall a. Semigroup a => a -> a -> a
<> Text -> Text
metaString' Text
"lofTitle" forall a. Semigroup a => a -> a -> a
<> Text
"}"
          , Text
"\\renewcommand*\\listtablename{" forall a. Semigroup a => a -> a -> a
<> Text -> Text
metaString' Text
"lotTitle" forall a. Semigroup a => a -> a -> a
<> Text
"}"
          , Text
"}"
          ]
        subfigures :: [Text]
subfigures = [
            Text
"\\newcounter{pandoccrossref@subfigures@footnote@counter}"
          , Text
"\\newenvironment{pandoccrossrefsubfigures}{%"
          , Text
"\\setcounter{pandoccrossref@subfigures@footnote@counter}{0}"
          , Text
"\\begin{figure}\\centering%"
          , Text
"\\gdef\\global@pandoccrossref@subfigures@footnotes{}%"
          , Text
"\\DeclareRobustCommand{\\footnote}[1]{\\footnotemark%"
          , Text
"\\stepcounter{pandoccrossref@subfigures@footnote@counter}%"
          , Text
"\\ifx\\global@pandoccrossref@subfigures@footnotes\\empty%"
          , Text
"\\gdef\\global@pandoccrossref@subfigures@footnotes{{##1}}%"
          , Text
"\\else%"
          , Text
"\\g@addto@macro\\global@pandoccrossref@subfigures@footnotes{, {##1}}%"
          , Text
"\\fi}}%"
          , Text
"{\\end{figure}%"
          , Text
"\\addtocounter{footnote}{-\\value{pandoccrossref@subfigures@footnote@counter}}"
          , Text
"\\@for\\f:=\\global@pandoccrossref@subfigures@footnotes\\do{\\stepcounter{footnote}\\footnotetext{\\f}}%"
          , Text
"\\gdef\\global@pandoccrossref@subfigures@footnotes{}}"
          ]
        codelisting :: [Text]
codelisting = [
            [Text] -> Text -> Text
usepackage [] Text
"float"
          , Text
"\\floatstyle{ruled}"
          , Text
"\\@ifundefined{c@chapter}{\\newfloat{codelisting}{h}{lop}}{\\newfloat{codelisting}{h}{lop}[chapter]}"
          , Text
"\\floatname{codelisting}{" forall a. Semigroup a => a -> a -> a
<> Text -> Text
metaString Text
"listingTitle" forall a. Semigroup a => a -> a -> a
<> Text
"}"
          ]
        lolcommand :: [Text]
lolcommand
          | Options -> Bool
listings Options
opts = [
              Text
"\\newcommand*\\listoflistings\\lstlistoflistings"
            , Text
"\\AtBeginDocument{%"
            , Text
"\\renewcommand*{\\lstlistlistingname}{" forall a. Semigroup a => a -> a -> a
<> Text -> Text
metaString' Text
"lolTitle" forall a. Semigroup a => a -> a -> a
<> Text
"}"
            , Text
"}"
            ]
          | Bool
otherwise = [Text
"\\newcommand*\\listoflistings{\\listof{codelisting}{" forall a. Semigroup a => a -> a -> a
<> Text -> Text
metaString' Text
"lolTitle" forall a. Semigroup a => a -> a -> a
<> Text
"}}"]
        cleveref :: [Text]
cleveref =
          [ [Text] -> Text -> Text
usepackage [Text]
cleverefOpts Text
"cleveref" ]
          forall a. Semigroup a => a -> a -> a
<> forall {t}.
Num t =>
Text -> (Options -> Bool -> t -> [Inline]) -> [Text]
crefname Text
"figure" Options -> Bool -> Int -> [Inline]
figPrefix
          forall a. Semigroup a => a -> a -> a
<> forall {t}.
Num t =>
Text -> (Options -> Bool -> t -> [Inline]) -> [Text]
crefname Text
"table" Options -> Bool -> Int -> [Inline]
tblPrefix
          forall a. Semigroup a => a -> a -> a
<> forall {t}.
Num t =>
Text -> (Options -> Bool -> t -> [Inline]) -> [Text]
crefname Text
"equation" Options -> Bool -> Int -> [Inline]
eqnPrefix
          forall a. Semigroup a => a -> a -> a
<> forall {t}.
Num t =>
Text -> (Options -> Bool -> t -> [Inline]) -> [Text]
crefname Text
"listing" Options -> Bool -> Int -> [Inline]
lstPrefix
          forall a. Semigroup a => a -> a -> a
<> forall {t}.
Num t =>
Text -> (Options -> Bool -> t -> [Inline]) -> [Text]
crefname Text
"section" Options -> Bool -> Int -> [Inline]
secPrefix
        cleverefCodelisting :: [Text]
cleverefCodelisting = [
            Text
"\\crefname{codelisting}{\\cref@listing@name}{\\cref@listing@name@plural}"
          , Text
"\\Crefname{codelisting}{\\Cref@listing@name}{\\Cref@listing@name@plural}"
          ]
        cleverefOpts :: [Text]
cleverefOpts | Options -> Bool
nameInLink Options
opts = [ Text
"nameinlink" ]
                     | Bool
otherwise = []
        crefname :: Text -> (Options -> Bool -> t -> [Inline]) -> [Text]
crefname Text
n Options -> Bool -> t -> [Inline]
f = [
            Text
"\\crefname{" forall a. Semigroup a => a -> a -> a
<> Text
n forall a. Semigroup a => a -> a -> a
<> Text
"}" forall a. Semigroup a => a -> a -> a
<> forall {t} {t}.
Num t =>
(Options -> t -> t -> [Inline]) -> t -> Text
prefix Options -> Bool -> t -> [Inline]
f Bool
False
          , Text
"\\Crefname{" forall a. Semigroup a => a -> a -> a
<> Text
n forall a. Semigroup a => a -> a -> a
<> Text
"}" forall a. Semigroup a => a -> a -> a
<> forall {t} {t}.
Num t =>
(Options -> t -> t -> [Inline]) -> t -> Text
prefix Options -> Bool -> t -> [Inline]
f Bool
True
          ]
        usepackage :: [Text] -> Text -> Text
usepackage [] Text
p = Text
"\\@ifpackageloaded{" forall a. Semigroup a => a -> a -> a
<> Text
p forall a. Semigroup a => a -> a -> a
<> Text
"}{}{\\usepackage{" forall a. Semigroup a => a -> a -> a
<> Text
p forall a. Semigroup a => a -> a -> a
<> Text
"}}"
        usepackage [Text]
xs Text
p = Text
"\\@ifpackageloaded{" forall a. Semigroup a => a -> a -> a
<> Text
p forall a. Semigroup a => a -> a -> a
<> Text
"}{}{\\usepackage" forall a. Semigroup a => a -> a -> a
<> Text
o forall a. Semigroup a => a -> a -> a
<> Text
"{" forall a. Semigroup a => a -> a -> a
<> Text
p forall a. Semigroup a => a -> a -> a
<> Text
"}}"
          where o :: Text
o = Text
"[" forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
"," [Text]
xs forall a. Semigroup a => a -> a -> a
<> Text
"]"
        toLatex :: [Inline] -> Text
toLatex = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. HasCallStack => [Char] -> a
error forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> [Char]
show) forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. PandocPure a -> Either PandocError a
runPure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeLaTeX forall a. Default a => a
def forall b c a. (b -> c) -> (a -> b) -> a -> c
. Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Block
Plain
        metaString :: Text -> Text
metaString Text
s = [Inline] -> Text
toLatex forall a b. (a -> b) -> a -> b
$ Text -> Meta -> [Inline]
getMetaInlines Text
s Meta
meta
        metaString' :: Text -> Text
metaString' Text
s = [Inline] -> Text
toLatex [Text -> Inline
Str forall a b. (a -> b) -> a -> b
$ Text -> Meta -> Text
getMetaString Text
s Meta
meta]
        prefix :: (Options -> t -> t -> [Inline]) -> t -> Text
prefix Options -> t -> t -> [Inline]
f t
uc = Text
"{" forall a. Semigroup a => a -> a -> a
<> [Inline] -> Text
toLatex (Options -> t -> t -> [Inline]
f Options
opts t
uc t
0) forall a. Semigroup a => a -> a -> a
<> Text
"}"  forall a. Semigroup a => a -> a -> a
<>
                      Text
"{" forall a. Semigroup a => a -> a -> a
<> [Inline] -> Text
toLatex (Options -> t -> t -> [Inline]
f Options
opts t
uc t
1) forall a. Semigroup a => a -> a -> a
<> Text
"}"