packages feed

casa-reoc-0.1.0.0: src/ReOC/Render/TrainingManual/Embed.hs

{-# OPTIONS_GHC -Wall #-}

{- | Reads the marked-up text of the RePL training manual when the package is
compiled, and checks it: a marker that is not closed, a hole with no value,
or a condition with an unknown key or value fails to compile.
-}
module ReOC.Render.TrainingManual.Embed (
  embedMarkup,
) where

import qualified Data.ByteString as ByteString
import Data.Text.Encoding (decodeUtf8)
import Language.Haskell.TH (Exp, Q, runIO)
import Language.Haskell.TH.Syntax (addDependentFile, lift)
import ReOC.Render.TrainingManual.Holes (courseHoleNames, trainingHoleNames)
import ReOC.Render.TrainingManual.Markup (checkMarkup, parseMarkup)

-- | The marked-up text at a path, relative to the package, as an expression of type @[Chunk]@.
embedMarkup :: FilePath -> Q Exp
embedMarkup path = do
  addDependentFile path
  t <- runIO (decodeUtf8 <$> ByteString.readFile path)
  case parseMarkup t of
    Left e -> fail (path <> ": " <> e)
    Right chunks ->
      case checkMarkup trainingHoleNames courseHoleNames chunks of
        [] -> lift chunks
        es -> fail (path <> ":\n" <> unlines es)