{-
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 #-}

module Text.Pandoc.CrossRef.References.Blocks.CodeBlock where

import Control.Monad.Reader.Class
import qualified Data.Text as T
import Text.Pandoc.Definition
import Text.Pandoc.Shared (stringify)
import qualified Text.Pandoc.Builder as B
import Data.Function ((&))

import Text.Pandoc.CrossRef.References.Monad
import Text.Pandoc.CrossRef.References.Blocks.Util
import Text.Pandoc.CrossRef.Util.Options
import Text.Pandoc.CrossRef.Util.Template
import Text.Pandoc.CrossRef.Util.Util

runCodeBlock :: Attr -> T.Text -> Either T.Text [Inline] -> WS (ReplacedResult Block)
runCodeBlock :: Attr -> Text -> Either Text [Inline] -> WS (ReplacedResult Block)
runCodeBlock (Text
label, [Text]
classes, [(Text, Text)]
attrs) Text
code Either Text [Inline]
eCaption = do
  Options
opts <- forall r (m :: * -> *). MonadReader r m => m r
ask
  case Options -> Maybe Format
outFormat Options
opts of
    Maybe Format
f --if used with listings package,nothing should be done
      | Maybe Format -> Bool
isLatexFormat Maybe Format
f, Options -> Bool
listings Options
opts -> Either Text [Inline]
eCaption forall a b. a -> (a -> b) -> b
&
        forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
          (forall a b. a -> b -> a
const forall (m :: * -> *) a. Monad m => m (ReplacedResult a)
noReplaceNoRecurse)
          (\[Inline]
caption -> forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$
            Attr -> Text -> Block
CodeBlock (Text
label,[Text]
classes,(Text
"caption",Text -> Text
escapeLaTeX forall a b. (a -> b) -> a -> b
$ forall a. Walkable Inline a => a -> Text
stringify [Inline]
caption)forall a. a -> [a] -> [a]
:[(Text, Text)]
attrs) Text
code)
      --if not using listings, however, wrap it in a codelisting environment
      | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
        forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div Attr
nullAttr [
            Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\begin{codelisting}"
          , [Inline] -> Block
Plain [
              Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") Text
"\\caption"
            , Attr -> [Inline] -> Inline
Span Attr
nullAttr forall a b. (a -> b) -> a -> b
$ forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inline
Str) forall a. a -> a
id Either Text [Inline]
eCaption
            ]
          , Attr -> Text -> Block
CodeBlock (Text
label, [Text]
classes, [(Text, Text)]
attrs) Text
code
          , Format -> Text -> Block
RawBlock (Text -> Format
Format Text
"latex") Text
"\\end{codelisting}"
          ]
    Maybe Format
_ -> do
      let cap :: [Inline]
cap = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. Many a -> [a]
B.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inlines
B.text) forall a. a -> a
id Either Text [Inline]
eCaption
      [Inline]
idxStr <- Either Text Text
-> [(Text, Text)] -> [Inline] -> SPrefix -> WS [Inline]
replaceAttr (forall a b. b -> Either a b
Right Text
label) [(Text, Text)]
attrs [Inline]
cap SPrefix
SPfxLst
      let caption' :: [Inline]
caption' = forall a b. MkTemplate a b => [Inline] -> [Inline] -> b -> [a]
applyTemplate [Inline]
idxStr [Inline]
cap forall a b. (a -> b) -> a -> b
$ Options -> Template
listingTemplate Options
opts
      forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div (Text
label, Text
"listing"forall a. a -> [a] -> [a]
:[Text]
classes, []) [
          Options -> Text -> [Inline] -> Block
mkCaption Options
opts Text
"Caption" [Inline]
caption'
        , Attr -> Text -> Block
CodeBlock (Text
"", [Text]
classes, forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Eq a => a -> a -> Bool
/=Text
"caption") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$ Options -> [Inline] -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts [Inline]
idxStr [(Text, Text)]
attrs) Text
code
        ]