1
1
mirror of https://github.com/srid/rib.git synced 2024-11-27 01:12:09 +03:00

readSource: handle errors internally

So the user does not have to.
This commit is contained in:
Sridhar Ratnakumar 2019-12-31 19:40:25 -05:00
parent ece3101f89
commit a1409e4963

View File

@ -67,17 +67,20 @@ buildStaticFiles staticFilePatterns = do
readSource ::
SourceReader repr ->
Path Rel File ->
Action (Either Text (Source repr))
readSource r k = do
input <- ribInputDir
let f = input </> k
Action (Source repr)
readSource sourceReader k = do
f <- (</> k) <$> ribInputDir
-- NOTE: We don't really use cacheActionWith prior to parsing content,
-- because the parsed representation (`repr`) may not always have instances
-- for Typeable/Binary/Generic (for example, MMark does not expose its
-- structure.). Consequently we are forced to cache merely the HTML writing
-- stage (see buildHtml').
need [toFilePath f]
fmap (Source k) <$> r f
sourceReader f >>= \case
Left e ->
fail $ "Error parsing source " <> toFilePath k <> ": " <> show e
Right v ->
pure $ Source k v
-- | Convert the given pattern of source files into their HTML.
buildHtmlMulti ::
@ -92,14 +95,11 @@ buildHtmlMulti ::
buildHtmlMulti pats parser r = do
input <- ribInputDir
fs <- getDirectoryFiles' input pats
forP fs $ \k ->
readSource parser k >>= \case
Left e ->
fail $ "Error parsing source " <> toFilePath k <> ": " <> show e
Right src -> do
outfile <- liftIO $ replaceExtension ".html" k
writeFileCached outfile $ toString $ Lucid.renderText $ r src
pure src
forP fs $ \k -> do
src <- readSource parser k
outfile <- liftIO $ replaceExtension ".html" k
writeFileCached outfile $ toString $ Lucid.renderText $ r src
pure src
-- | Build a single HTML file with the given HTML value
--