{-
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>
Copyright (C) 2017  Masamichi Hosoda <trueroad@trueroad.jp>

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.Settings (getSettings, defaultMeta) where

import Control.Exception (IOException, handle)
import Text.Pandoc
import Text.Pandoc.Builder

import qualified Data.Text as T
import System.Directory
import System.FilePath
import System.IO
import Text.Pandoc.CrossRef.Util.Meta
import Text.Pandoc.CrossRef.Util.Settings.Gen

getSettings :: Maybe Format -> Meta -> IO Meta
getSettings :: Maybe Format -> Meta -> IO Meta
getSettings Maybe Format
fmt Meta
meta = do
  Meta
dirConfig <- FilePath -> IO Meta
readConfig (Text -> FilePath
T.unpack forall a b. (a -> b) -> a -> b
$ Text -> Meta -> Text
getMetaString Text
"crossrefYaml" (Meta
defaultMeta forall a. Semigroup a => a -> a -> a
<> Meta
meta))
  FilePath
home <- IO FilePath
getHomeDirectory
  Meta
globalConfig <- FilePath -> IO Meta
readConfig (FilePath
home FilePath -> FilePath -> FilePath
</> FilePath
".pandoc-crossref" FilePath -> FilePath -> FilePath
</> FilePath
"config.yaml")
  Meta
formatConfig <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. Monad m => a -> m a
return Meta
nullMeta) (FilePath -> Format -> IO Meta
readFmtConfig FilePath
home) Maybe Format
fmt
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Meta
defaultMeta forall a. Semigroup a => a -> a -> a
<> Meta
globalConfig forall a. Semigroup a => a -> a -> a
<> Meta
formatConfig forall a. Semigroup a => a -> a -> a
<> Meta
dirConfig forall a. Semigroup a => a -> a -> a
<> Meta
meta
  where
    readConfig :: FilePath -> IO Meta
readConfig FilePath
path =
      forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle IOException -> IO Meta
handler forall a b. (a -> b) -> a -> b
$ do
        Handle
h <- FilePath -> IOMode -> IO Handle
openFile FilePath
path IOMode
ReadMode
        Handle -> TextEncoding -> IO ()
hSetEncoding Handle
h TextEncoding
utf8
        FilePath
yaml <- Handle -> IO FilePath
hGetContents Handle
h
        Pandoc Meta
meta' [Block]
_ <- Text -> IO Pandoc
readMd forall a b. (a -> b) -> a -> b
$ FilePath -> Text
T.pack forall a b. (a -> b) -> a -> b
$ [FilePath] -> FilePath
unlines [FilePath
"---", FilePath
yaml, FilePath
"---"]
        forall (m :: * -> *) a. Monad m => a -> m a
return Meta
meta'
    readMd :: Text -> IO Pandoc
readMd = forall a. Either PandocError a -> IO a
handleError 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 :: * -> *) a.
(PandocMonad m, ToSources a) =>
ReaderOptions -> a -> m Pandoc
readMarkdown forall a. Default a => a
def{readerExtensions :: Extensions
readerExtensions=Extensions
pandocExtensions}
    readFmtConfig :: FilePath -> Format -> IO Meta
readFmtConfig FilePath
home Format
fmt' = FilePath -> IO Meta
readConfig (FilePath
home FilePath -> FilePath -> FilePath
</> FilePath
".pandoc-crossref" FilePath -> FilePath -> FilePath
</> FilePath
"config-" forall a. [a] -> [a] -> [a]
++ Format -> FilePath
fmtStr Format
fmt' forall a. [a] -> [a] -> [a]
++ FilePath
".yaml")
    handler :: IOException -> IO Meta
    handler :: IOException -> IO Meta
handler IOException
_ = forall (m :: * -> *) a. Monad m => a -> m a
return Meta
nullMeta
    fmtStr :: Format -> FilePath
fmtStr (Format Text
fmtstr) = Text -> FilePath
T.unpack Text
fmtstr


defaultMeta :: Meta
defaultMeta :: Meta
defaultMeta =
     forall a. ToMetaValue a => a -> Meta
cref Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
chapters Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
chaptersDepth (Text -> MetaValue
MetaString Text
"1")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
listings Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
codeBlockCaptions Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
autoSectionLabels Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
numberSections Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
sectionsDepth (Text -> MetaValue
MetaString Text
"0")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
figLabels (Text -> MetaValue
MetaString Text
"arabic")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
eqLabels (Text -> MetaValue
MetaString Text
"arabic")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
tblLabels (Text -> MetaValue
MetaString Text
"arabic")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lstLabels (Text -> MetaValue
MetaString Text
"arabic")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
secLabels (Text -> MetaValue
MetaString Text
"arabic")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
figureTitle (Text -> Inlines
str Text
"Figure")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
tableTitle (Text -> Inlines
str Text
"Table")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
listingTitle (Text -> Inlines
str Text
"Listing")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
titleDelim (Text -> Inlines
str Text
":")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
chapDelim (Text -> Inlines
str Text
".")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
rangeDelim (Text -> Inlines
str Text
"-")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
pairDelim (Text -> Inlines
str Text
"," forall a. Semigroup a => a -> a -> a
<> Inlines
space)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lastDelim (Text -> Inlines
str Text
"," forall a. Semigroup a => a -> a -> a
<> Inlines
space)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
refDelim (Text -> Inlines
str Text
"," forall a. Semigroup a => a -> a -> a
<> Inlines
space)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
figPrefix [Text -> Inlines
str Text
"fig.", Text -> Inlines
str Text
"figs."]
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
eqnPrefix [Text -> Inlines
str Text
"eq." , Text -> Inlines
str Text
"eqns."]
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
tblPrefix [Text -> Inlines
str Text
"tbl.", Text -> Inlines
str Text
"tbls."]
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lstPrefix [Text -> Inlines
str Text
"lst.", Text -> Inlines
str Text
"lsts."]
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
secPrefix [Text -> Inlines
str Text
"sec.", Text -> Inlines
str Text
"secs."]
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
figPrefixTemplate (Text -> Inlines
var Text
"p" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"\160" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
eqnPrefixTemplate (Text -> Inlines
var Text
"p" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"\160" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
tblPrefixTemplate (Text -> Inlines
var Text
"p" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"\160" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lstPrefixTemplate (Text -> Inlines
var Text
"p" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"\160" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
secPrefixTemplate (Text -> Inlines
var Text
"p" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"\160" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
eqnBlockTemplate (
      Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Blocks
table
        Caption
emptyCaption
        [(Alignment
AlignCenter, Double -> ColWidth
ColWidth Double
0.9), (Alignment
AlignRight, Double -> ColWidth
ColWidth Double
0.1)]
        (Attr -> [Row] -> TableHead
TableHead Attr
nullAttr [])
        [Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr (Int -> RowHeadColumns
RowHeadColumns Int
0) [] [
          Attr -> [Cell] -> Row
Row Attr
nullAttr
            [ Blocks -> Cell
simpleCell forall a b. (a -> b) -> a -> b
$ Inlines -> Blocks
plain (Text -> Inlines
var Text
"t")
            , Blocks -> Cell
simpleCell forall a b. (a -> b) -> a -> b
$ Blocks
wordVerticalAlign forall a. Semigroup a => a -> a -> a
<> Inlines -> Blocks
plain (Text -> Inlines
var Text
"i")
            ]
          ]]
        (Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr [])
      )
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
eqnIndexTemplate (Text -> Inlines
str Text
"(" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Inlines
")")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
eqnInlineTemplate (Text -> Inlines
var Text
"e" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"equationNumberTeX" forall a. Semigroup a => a -> a -> a
<> Inlines
"{" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Inlines
"}")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
eqnBlockInlineMath Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
refIndexTemplate (Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"suf")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
subfigureRefIndexTemplate (Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"suf" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"(" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"s" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
")")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
secHeaderTemplate (Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"secHeaderDelim[n]" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
secHeaderDelim Inlines
space
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lofTitle (Int -> Inlines -> Blocks
header Int
1 forall a b. (a -> b) -> a -> b
$ Text -> Inlines
text Text
"List of Figures")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lotTitle (Int -> Inlines -> Blocks
header Int
1 forall a b. (a -> b) -> a -> b
$ Text -> Inlines
text Text
"List of Tables")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lolTitle (Int -> Inlines -> Blocks
header Int
1 forall a b. (a -> b) -> a -> b
$ Text -> Inlines
text Text
"List of Listings")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
figureTemplate (Text -> Inlines
var Text
"figureTitle" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"titleDelim" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
tableTemplate (Text -> Inlines
var Text
"tableTitle" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"titleDelim" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
listingTemplate (Text -> Inlines
var Text
"listingTitle" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"titleDelim" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lofItemTemplate (Inlines -> Blocks
plain forall a b. (a -> b) -> a -> b
$ Text -> Inlines
var Text
"lofItemTitle" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"listItemTitleDelim" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t" forall a. Semigroup a => a -> a -> a
<> Inlines
linebreak)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lotItemTemplate (Inlines -> Blocks
plain forall a b. (a -> b) -> a -> b
$ Text -> Inlines
var Text
"lotItemTitle" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"listItemTitleDelim" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t" forall a. Semigroup a => a -> a -> a
<> Inlines
linebreak)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lolItemTemplate (Inlines -> Blocks
plain forall a b. (a -> b) -> a -> b
$ Text -> Inlines
var Text
"lolItemTitle" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"listItemTitleDelim" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t" forall a. Semigroup a => a -> a -> a
<> Inlines
linebreak)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lofItemTitle (forall a. Monoid a => a
mempty :: Inlines)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lotItemTitle (forall a. Monoid a => a
mempty :: Inlines)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
lolItemTitle (forall a. Monoid a => a
mempty :: Inlines)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
listItemTitleDelim (Text -> Inlines
str Text
".")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
crossrefYaml (Text -> MetaValue
MetaString Text
"pandoc-crossref.yaml")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
subfigureChildTemplate (Text -> Inlines
var Text
"i")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
subfigureTemplate (Text -> Inlines
var Text
"figureTitle" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"titleDelim" forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"." forall a. Semigroup a => a -> a -> a
<> Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"ccs")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
subfigLabels (Text -> MetaValue
MetaString Text
"alpha a")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
ccsDelim (Text -> Inlines
str Text
"," forall a. Semigroup a => a -> a -> a
<> Inlines
space)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
ccsLabelSep (Inlines
space forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
str Text
"—" forall a. Semigroup a => a -> a -> a
<> Inlines
space)
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
ccsTemplate (Text -> Inlines
var Text
"i" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"ccsLabelSep" forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
var Text
"t")
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
tableEqns Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
autoEqnLabels Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
subfigGrid Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
linkReferences Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
nameInLink Bool
False
  forall a. Semigroup a => a -> a -> a
<> forall a. ToMetaValue a => a -> Meta
equationNumberTeX (Text
"\\qquad" :: T.Text)
  where
    var :: Text -> Inlines
var = Text -> Inlines
displayMath
    wordVerticalAlign :: Blocks
wordVerticalAlign = Text -> Text -> Blocks
rawBlock Text
"openxml" Text
"<w:tcPr><w:vAlign w:val=\"center\"/></w:tcPr>"