casa-reoc-0.1.0.0: src/ReOC/Render/Write.hs
{-# OPTIONS_GHC -Wall #-}
-- | Writes the documents of a 'ReOC' into a directory.
module ReOC.Render.Write (
applicationFormFile,
operationsManualFile,
trainingManualFile,
writtenSignatureImageFile,
textDocuments,
formFiles,
writeDocuments,
writeFilledDocuments,
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import ReOC.Model
import ReOC.Render.ApplicationForm (renderApplicationForm)
import ReOC.Render.BlankForm (blankForm, blankFormFile)
import ReOC.Render.BuildInfo (BuildInfo, fillPlaceholders)
import ReOC.Render.OperationsManual (renderOperationsManual)
import ReOC.Render.Text (get)
import ReOC.Render.TrainingManual (renderTrainingManual)
import System.Directory (createDirectoryIfMissing)
import System.FilePath ((</>))
-- | The file name of the filled application form. The document build names the signed form after it.
applicationFormFile :: FilePath
applicationFormFile =
"rpa-operators-certificate-initial-issue.xfdf"
-- | The file name of the operations manual.
operationsManualFile :: FilePath
operationsManualFile =
"rpas-operations-manual.fodt"
-- | The file name of the RePL training manual.
trainingManualFile :: FilePath
trainingManualFile =
"repl-training-manual.fodt"
-- | The file name of the written signature of the declarant, which is written beside the application form.
writtenSignatureImageFile :: FilePath
writtenSignatureImageFile =
"written-signature.png"
{- | The text documents of a ReOC, with their file names: the application
form, the operations manual and, with RePL training, the RePL training
manual.
-}
textDocuments :: ReOC -> [(FilePath, Text)]
textDocuments r =
[ (applicationFormFile, renderApplicationForm r)
, (operationsManualFile, renderOperationsManual r)
]
<> maybe [] (\t -> [(trainingManualFile, t)]) (renderTrainingManual r)
{- | The other files that the application form needs, with their file names:
the blank form that it fills and, if it is to be signed, the written
signature, read from its file if it is given by a path.
-}
formFiles :: ReOC -> IO [(FilePath, ByteString)]
formFiles r = do
signatureFiles <-
case get (applicationForm . signature) r of
Nothing -> pure []
Just (WrittenSignatureImage b) -> pure [(writtenSignatureImageFile, b)]
Just (WrittenSignatureFile p) -> (\b -> [(writtenSignatureImageFile, b)]) <$> ByteString.readFile p
pure ((blankFormFile, blankForm) : signatureFiles)
-- Writes text documents, and the files of the application form, into a directory.
writeAll :: (Text -> Text) -> FilePath -> ReOC -> IO ()
writeAll f dir r = do
files <- formFiles r
createDirectoryIfMissing True dir
mapM_ (\(n, t) -> ByteString.writeFile (dir </> n) (encodeUtf8 (f t))) (textDocuments r)
mapM_ (\(n, b) -> ByteString.writeFile (dir </> n) b) files
{- | Writes the documents into a directory, in UTF-8, with the placeholders of
the document build: the application form, the operations manual and, with
RePL training, the RePL training manual; and beside the application form,
the blank form that it fills and the written signature, if there is one. A
written signature given by a path is read from that path.
-}
writeDocuments :: FilePath -> ReOC -> IO ()
writeDocuments =
writeAll id
-- | Writes the documents as 'writeDocuments' does, with the placeholders of the document build filled.
writeFilledDocuments :: BuildInfo -> FilePath -> ReOC -> IO ()
writeFilledDocuments b =
writeAll (fillPlaceholders b)