-- | Defines types and functions to help generate common LaTeX preamble.
module Language.Drasil.TeX.Preamble (genPreamble) where

import Data.List (nub)

import Language.Drasil.Printing.LayoutObj (LayoutObj(..))
import Language.Drasil.TeX.Monad (D, vcat, (%%))
import Language.Drasil.TeX.Helpers (docclass, command, command1o, command2, command3,
  usepackage)

import Language.Drasil.Config (hyperSettings, fontSize, bibFname)

-- FIXME: this really shouldn't be in code, it should be data!
-- | LaTeX packages.
data Package = AMSMath      -- ^ Improves information structure for mathematical formulas.
             | BookTabs     -- ^ Enhances quality of tables in the document.
             | Caption      -- ^ Customize the captions in floating environments.
             | FullPage     -- ^ Sets margins and page style.
             | Graphics     -- ^ Manipulate graphical elements.
             | HyperRef     -- ^ Handles cross-referencing within the document.
             | Listings     -- ^ Source code printer for LaTeX.
             | Tikz         -- ^ Create graphical elements.
             | Dot2Tex      -- ^ Create better graphs.
             | AdjustBox    -- ^ Adjustable boxed content.
             | AMSsymb      -- ^ Displays bold math sets (reals, naturals, etc.).
--           | Breqn --line breaks long equations automatically
             | FileContents -- ^ Creates .bib file within .tex file.
             | BibLaTeX     -- ^ Reimplementation of bibliography elements.
             | Tabularray   -- ^ Adds auto column width feature for tables.
             | TabularX     -- ^ Adds \arraybackslash
             | Mathtools    -- ^ Line breaks for long fractions and cases.
             | URL          -- ^ Allows for hyperlinks.
             | FontSpec     -- ^ For utf-8 encoding in lualatex.
             | Unicode      -- ^ For unicode-math in lualatex.
             | EnumItem     -- ^ Contol basic list environments.
             | SVG          -- ^ For rendering svg diagrams.
             deriving Package -> Package -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Package -> Package -> Bool
$c/= :: Package -> Package -> Bool
== :: Package -> Package -> Bool
$c== :: Package -> Package -> Bool
Eq

-- | Adds a 'Package' to the LaTeX document.
addPackage :: Package -> D
addPackage :: Package -> D
addPackage Package
AMSMath   = String -> D
usepackage String
"amsmath"
addPackage Package
BookTabs  = String -> D
usepackage String
"booktabs"
addPackage Package
Caption   = String -> D
usepackage String
"caption"
addPackage Package
FullPage  = String -> D
usepackage String
"fullpage"
addPackage Package
Graphics  = String -> D
usepackage String
"graphics"
addPackage Package
HyperRef  = String -> D
usepackage String
"hyperref" D -> D -> D
%%
                       String -> String -> D
command String
"hypersetup" String
hyperSettings
addPackage Package
Listings  = String -> D
usepackage String
"listings"
addPackage Package
Tikz      = String -> D
usepackage String
"tikz" D -> D -> D
%%
                       String -> String -> D
command String
"usetikzlibrary" String
"arrows.meta, shapes"
addPackage Package
Dot2Tex   = String -> D
usepackage String
"dot2texi"
addPackage Package
AdjustBox = String -> D
usepackage String
"adjustbox"
addPackage Package
AMSsymb   = String -> D
usepackage String
"amssymb"
--addPackage Breqn     = usepackage "breqn"
addPackage Package
FileContents = String -> D
usepackage String
"filecontents"
addPackage Package
BibLaTeX  = String -> Maybe String -> String -> D
command1o String
"usepackage" (forall a. a -> Maybe a
Just String
"backend=bibtex") String
"biblatex"
addPackage Package
Tabularray = String -> D
usepackage String
"tabularray"
addPackage Package
TabularX  = String -> D
usepackage String
"tabularx"
addPackage Package
Mathtools = String -> D
usepackage String
"mathtools"
addPackage Package
URL       = String -> D
usepackage String
"url"
-- Discussed in issue #1819
-- Because we are using LuaLatex, we use fontspec here instead of fontenc
addPackage Package
FontSpec  = String -> D
usepackage String
"fontspec"
addPackage Package
Unicode   = String -> D
usepackage String
"unicode-math"
addPackage Package
EnumItem  = String -> D
usepackage String
"enumitem"
addPackage Package
SVG       = String -> D
usepackage String
"svg"

-- | Common LaTeX commands.
data Def = Bibliography
         | GreaterThan
         | LessThan
         | SetMathFont
         | SymbDescriptionP1
         | SymbDescriptionP2
         deriving Def -> Def -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Def -> Def -> Bool
$c/= :: Def -> Def -> Bool
== :: Def -> Def -> Bool
$c== :: Def -> Def -> Bool
Eq

-- | Define common LaTeX commands.
addDef :: Def -> D
addDef :: Def -> D
addDef Def
Bibliography  = String -> String -> D
command String
"bibliography" String
bibFname
addDef Def
GreaterThan   = String -> String -> String -> D
command2 String
"newcommand" String
"\\gt" String
"\\ensuremath >"
addDef Def
LessThan      = String -> String -> String -> D
command2 String
"newcommand" String
"\\lt" String
"\\ensuremath <"
addDef Def
SetMathFont   = String -> String -> D
command String
"setmathfont" String
"Latin Modern Math"
addDef Def
SymbDescriptionP1 = String -> String -> String -> String -> D
command3 String
"newlist" String
"symbDescription" String
"description" String
"1"
addDef Def
SymbDescriptionP2 = String -> Maybe String -> String -> D
command1o String
"setlist" (forall a. a -> Maybe a
Just String
"symbDescription") String
"noitemsep, topsep=0pt, parsep=0pt, partopsep=0pt"

-- | Generates LaTeX document preamble.
genPreamble :: [LayoutObj] -> D
genPreamble :: [LayoutObj] -> D
genPreamble [LayoutObj]
los = let ([Package]
pkgs, [Def]
defs) = [LayoutObj] -> ([Package], [Def])
parseDoc [LayoutObj]
los
  in String -> String -> D
docclass (forall a. Show a => a -> String
show Int
fontSize forall a. [a] -> [a] -> [a]
++ String
"pt") String
"article" D -> D -> D
%%
     [D] -> D
vcat (forall a b. (a -> b) -> [a] -> [b]
map Package -> D
addPackage [Package]
pkgs) D -> D -> D
%% [D] -> D
vcat (forall a b. (a -> b) -> [a] -> [b]
map Def -> D
addDef [Def]
defs)

-- | Helper to gather all preamble information.
parseDoc :: [LayoutObj] -> ([Package], [Def])
parseDoc :: [LayoutObj] -> ([Package], [Def])
parseDoc [LayoutObj]
los' = 
  ([Package
FontSpec, Package
FullPage, Package
HyperRef, Package
AMSMath, Package
AMSsymb, Package
Mathtools, Package
Unicode] forall a. [a] -> [a] -> [a]
++ 
   forall a. Eq a => [a] -> [a]
nub (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> a
fst [([Package], [Def])]
res)
  , [Def
SetMathFont, Def
GreaterThan, Def
LessThan] forall a. [a] -> [a] -> [a]
++ forall a. Eq a => [a] -> [a]
nub (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> b
snd [([Package], [Def])]
res))
  where 
    res :: [([Package], [Def])]
res = forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> ([Package], [Def])
parseDoc' [LayoutObj]
los'
    parseDoc' :: LayoutObj -> ([Package], [Def])
    parseDoc' :: LayoutObj -> ([Package], [Def])
parseDoc' Table{} = ([Package
Tabularray,Package
TabularX,Package
BookTabs,Package
Caption], [])
    parseDoc' (HDiv Tags
_ [LayoutObj]
slos Label
_) = 
      let res1 :: [([Package], [Def])]
res1 = forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> ([Package], [Def])
parseDoc' [LayoutObj]
slos in
      let pp :: [Package]
pp = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> a
fst [([Package], [Def])]
res1 in
      let dd :: [Def]
dd = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> b
snd [([Package], [Def])]
res1 in
      ([Package]
pp, [Def]
dd)
    parseDoc' (Definition DType
_ [(String, [LayoutObj])]
ps Label
_) =
      let res1 :: [([Package], [Def])]
res1 = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> ([Package], [Def])
parseDoc' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(String, [LayoutObj])]
ps in
      let pp :: [Package]
pp = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> a
fst [([Package], [Def])]
res1 in
      let dd :: [Def]
dd = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> b
snd [([Package], [Def])]
res1 in
      (Package
Tabularrayforall a. a -> [a] -> [a]
:Package
TabularXforall a. a -> [a] -> [a]
:Package
BookTabsforall a. a -> [a] -> [a]
:[Package]
pp,Def
SymbDescriptionP1forall a. a -> [a] -> [a]
:Def
SymbDescriptionP2forall a. a -> [a] -> [a]
:[Def]
dd)
    parseDoc' Figure{}     = ([Package
Graphics,Package
Caption, Package
SVG],[])
    parseDoc' Graph{}      = ([Package
Caption,Package
Tikz,Package
Dot2Tex,Package
AdjustBox],[])
    parseDoc' Bib{}        = ([Package
FileContents,Package
BibLaTeX,Package
URL],[Def
Bibliography])
    parseDoc' Header{}     = ([], [])
    parseDoc' Paragraph{}  = ([], [])
    parseDoc' List{}       = ([Package
EnumItem], [])
    parseDoc' EqnBlock{}   = ([], [])
    parseDoc' Cell{}       = ([], [])
    parseDoc' CodeBlock{}  = ([], [])