-- | 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)

-- | 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
Website [Format]
_) Filename
_) = []
  makeRule (DocSpec (DC DocType
dt [Format]
_) Filename
fn) = [
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkRule [] (Filename -> Target
makeS forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> Filename
show DocType
dt) [Target
pdfName] [],
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkFile [] Target
pdfName [Filename -> Target
makeS forall a b. (a -> b) -> a -> b
$ Filename
fn forall a. [a] -> [a] -> [a]
++ Filename
".tex"] forall a b. (a -> b) -> a -> b
$
      forall a b. (a -> b) -> [a] -> [b]
map (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 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") forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filename -> Target
makeS
        bibtex :: Filename -> Command
bibtex = Target -> Command
mkCommand 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") forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filename -> Target
makeS
        pdfName :: Target
pdfName = Filename -> Target
makeS forall a b. (a -> b) -> a -> b
$ Filename
fn forall a. [a] -> [a] -> [a]
++ Filename
".pdf"

-- | 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