{- HLINT ignore "Use newtype instead of data" -}
-- | Defines output formats for the different documents we can generate.
module Drasil.Generator.Formats (
  -- * Types (Printing Options)
  DocSpec(DocSpec), DocChoices(..),
  DocClass(..), UsePackages(..), ExDoc(..), Filename,
  Format(..),
  -- * Constructors
  docChoices
) where

import Build.Drasil ((+:+), Command, makeS, mkCheckedCommand, mkCommand, mkFreeVar,
  mkFile, mkRule, RuleTransformer(makeRule))
import Drasil.Metadata (watermark)

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

-- | Possible formats for printer output.
data Format = TeX | Plain | HTML | Jupyter | MDBook

instance Show Format where
  show :: Format -> String
show Format
TeX     = String
"PDF"
  show Format
Plain   = String
"Plain"
  show Format
HTML    = String
"HTML"
  show Format
Jupyter = String
"Jupyter"
  show Format
MDBook  = String
"mdBook"

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

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

-- | Constructor for users to choose their document options
docChoices :: [Format] -> DocChoices
docChoices :: [Format] -> DocChoices
docChoices = [Format] -> DocChoices
DC

-- | Allows the creation of Makefiles for documents that use LaTeX.
instance RuleTransformer DocSpec where
  makeRule :: DocSpec -> [Rule]
makeRule (DocSpec (DC [Format
TeX]) String
fn) = [
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkRule [String
watermark] (String -> Target
makeS String
"srs") [Target
pdfName] [],
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkFile [] Target
pdfName [String -> Target
makeS (String -> Target) -> String -> Target
forall a b. (a -> b) -> a -> b
$ String
fn String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
".tex"] ([Command] -> Rule) -> [Command] -> Rule
forall a b. (a -> b) -> a -> b
$
      ((String -> Command) -> Command)
-> [String -> Command] -> [Command]
forall a b. (a -> b) -> [a] -> [b]
map ((String -> Command) -> String -> Command
forall a b. (a -> b) -> a -> b
$ String
fn) [String -> Command
lualatex, String -> Command
bibtex, String -> Command
lualatex, String -> Command
lualatex]] where
        lualatex, bibtex :: String -> Command
        lualatex :: String -> Command
lualatex = Target -> Command
mkCheckedCommand (Target -> Command) -> (String -> Target) -> String -> Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Target -> Target -> Target
(+:+) (String -> Target
makeS String
"lualatex" Target -> Target -> Target
+:+ String -> Target
mkFreeVar String
"TEXFLAGS") (Target -> Target) -> (String -> Target) -> String -> Target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Target
makeS
        bibtex :: String -> Command
bibtex = Target -> Command
mkCommand (Target -> Command) -> (String -> Target) -> String -> Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Target -> Target -> Target
(+:+) (String -> Target
makeS String
"bibtex" Target -> Target -> Target
+:+ String -> Target
mkFreeVar String
"BIBTEXFLAGS") (Target -> Target) -> (String -> Target) -> String -> Target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Target
makeS
        pdfName :: Target
pdfName = String -> Target
makeS (String -> Target) -> String -> Target
forall a b. (a -> b) -> a -> b
$ String
fn String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
".pdf"
  makeRule (DocSpec (DC [Format
MDBook]) String
_) = [
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkRule [String
watermark] (String -> Target
makeS String
"build")  [] [Command
build],
    Annotation -> Target -> Dependencies -> [Command] -> Rule
mkRule [] (String -> Target
makeS String
"server") [] [Command
server]]
    where
      build :: Command
build = Target -> Command
mkCheckedCommand (Target -> Command) -> Target -> Command
forall a b. (a -> b) -> a -> b
$ String -> Target
makeS String
"mdbook build"
      server :: Command
server = Target -> Command
mkCheckedCommand (Target -> Command) -> Target -> Command
forall a b. (a -> b) -> a -> b
$ String -> Target
makeS String
"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