-- | Defines output formats for the different documents we can generate.
module Language.Drasil.Output.Formats where

import Data.Char (toLower)
import Build.Drasil ((+:+), Command, makeS, mkCheckedCommand, mkCommand, mkFreeVar,
  mkFile, mkRule, RuleTransformer(makeRule))
import Language.Drasil.Printers (DocType(..), Format(TeX, MDBook))
import Metadata.Drasil.DrasilMetaCall (watermark)

-- | When choosing your document, you must specify the filename for
-- the generated output (specified /without/ a file extension).
type Filename = String

-- | Document choices include the type of document as well as the file formats we want to generate as.
data DocChoices = DC {
  DocChoices -> DocType
doctype :: DocType,
  DocChoices -> [Format]
format :: [Format]
}

-- | Document specifications. Holds the type of document ('DocType') and its name ('Filename').
data DocSpec = DocSpec DocChoices Filename

-- | Allows the creation of Makefiles for documents that use LaTeX.
instance RuleTransformer DocSpec where
  makeRule :: DocSpec -> [Rule]
makeRule (DocSpec (DC DocType
dt [Format
TeX]) Filename
fn) = [
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkRule [Filename
watermark] (Filename -> Target
makeS (Filename -> Target) -> Filename -> Target
forall a b. (a -> b) -> a -> b
$ (Char -> Char) -> Filename -> Filename
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (Filename -> Filename) -> Filename -> Filename
forall a b. (a -> b) -> a -> b
$ DocType -> Filename
forall a. Show a => a -> Filename
show DocType
dt) [Target
pdfName] [],
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkFile [] Target
pdfName [Filename -> Target
makeS (Filename -> Target) -> Filename -> Target
forall a b. (a -> b) -> a -> b
$ Filename
fn Filename -> Filename -> Filename
forall a. [a] -> [a] -> [a]
++ Filename
".tex"] ([Command] -> Rule) -> [Command] -> Rule
forall a b. (a -> b) -> a -> b
$
      ((Filename -> Command) -> Command)
-> [Filename -> Command] -> [Command]
forall a b. (a -> b) -> [a] -> [b]
map ((Filename -> Command) -> Filename -> Command
forall a b. (a -> b) -> a -> b
$ Filename
fn) [Filename -> Command
lualatex, Filename -> Command
bibtex, Filename -> Command
lualatex, Filename -> Command
lualatex]] where
        lualatex, bibtex :: String -> Command
        lualatex :: Filename -> Command
lualatex = Target -> Command
mkCheckedCommand (Target -> Command) -> (Filename -> Target) -> Filename -> Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Target -> Target -> Target
(+:+) (Filename -> Target
makeS Filename
"lualatex" Target -> Target -> Target
+:+ Filename -> Target
mkFreeVar Filename
"TEXFLAGS") (Target -> Target) -> (Filename -> Target) -> Filename -> Target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filename -> Target
makeS
        bibtex :: Filename -> Command
bibtex = Target -> Command
mkCommand (Target -> Command) -> (Filename -> Target) -> Filename -> Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Target -> Target -> Target
(+:+) (Filename -> Target
makeS Filename
"bibtex" Target -> Target -> Target
+:+ Filename -> Target
mkFreeVar Filename
"BIBTEXFLAGS") (Target -> Target) -> (Filename -> Target) -> Filename -> Target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filename -> Target
makeS
        pdfName :: Target
pdfName = Filename -> Target
makeS (Filename -> Target) -> Filename -> Target
forall a b. (a -> b) -> a -> b
$ Filename
fn Filename -> Filename -> Filename
forall a. [a] -> [a] -> [a]
++ Filename
".pdf"
  makeRule (DocSpec (DC DocType
_ [Format
MDBook]) Filename
_) = [
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkRule [Filename
watermark] (Filename -> Target
makeS Filename
"build")  [] [Command
build],
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkRule [] (Filename -> Target
makeS Filename
"server") [] [Command
server]]
    where
      build :: Command
build = Target -> Command
mkCheckedCommand (Target -> Command) -> Target -> Command
forall a b. (a -> b) -> a -> b
$ Filename -> Target
makeS Filename
"mdbook build"
      server :: Command
server = Target -> Command
mkCheckedCommand (Target -> Command) -> Target -> Command
forall a b. (a -> b) -> a -> b
$ Filename -> Target
makeS Filename
"mdbook serve --open"
  makeRule DocSpec
_ = []

-- | LaTeX helper.
data DocClass = DocClass (Maybe String) String
-- | LaTeX helper for adding packages. Wraps a list of package names.
newtype UsePackages = UsePackages [String] -- Package name list
-- | LaTeX helper.
data ExDoc = ExDoc (Maybe String) String