From c43641eaac67341e269319f60902c4fb25f38f6c Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sun, 2 Feb 2020 16:07:51 -0500 Subject: [PATCH] Add `src` type variable to Target type And use it from Rib.Shake --- src/Rib/Shake.hs | 22 +++++++++++----------- src/Rib/Target.hs | 13 +++++++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Rib/Shake.hs b/src/Rib/Shake.hs index fe99c04..ed72c7d 100644 --- a/src/Rib/Shake.hs +++ b/src/Rib/Shake.hs @@ -119,10 +119,10 @@ buildHtml :: -- | Path to the source file (relative to `ribInputDir`) Path Rel File -> -- | How to parse the source file - SourceReader repr -> + SourceReader a -> -- | How to render the given source to HTML - (Source repr -> Html ()) -> - Action (Source repr) + (Target (Path Rel File) a -> Html ()) -> + Action (Target (Path Rel File) a) buildHtml k parser r = buildHtml' k parser replaceExtHtml r where replaceExtHtml _ = liftIO $ replaceExtension ".html" k @@ -132,21 +132,21 @@ buildHtml' :: -- | Path to the source file (relative to `ribInputDir`) Path Rel File -> -- | How to parse the source file - SourceReader repr -> + SourceReader a -> -- | Output file name to use (relative to `ribOutputDir`) - (repr -> Action (Path Rel File)) -> + (a -> Action (Path Rel File)) -> -- | How to render the given source to HTML - (Source repr -> Html ()) -> - Action (Source repr) + (Target (Path Rel File) a -> Html ()) -> + Action (Target (Path Rel File) a) buildHtml' k parser outfileFn r = do v <- readSource parser k outfile <- outfileFn v - let src = Source k outfile v - writeHtml outfile $ r src - pure src + let t = Target outfile k v + writeTarget t r + pure t -- | Write the target file -writeTarget :: Target a -> (Target a -> Html ()) -> Action () +writeTarget :: Target src a -> (Target src a -> Html ()) -> Action () writeTarget t r = writeHtml (targetPath t) (r t) -- | Write a single HTML file with the given HTML value diff --git a/src/Rib/Target.hs b/src/Rib/Target.hs index 8faac8d..3902801 100644 --- a/src/Rib/Target.hs +++ b/src/Rib/Target.hs @@ -14,6 +14,7 @@ module Rib.Target targetPath, targetUrl, targetVal, + targetSrc, ) where @@ -22,24 +23,28 @@ import Path import Relude -- | A generated file on disk -data Target a +data Target src a = Target { _target_path :: Path Rel File, -- ^ Path to the generated HTML file (relative to `Rib.Shake.ribOutputDir`) + _target_src :: src, _target_val :: a } deriving (Generic, Functor, Show) -- | Path to the source file (relative to `Rib.Shake.ribInputDir`) -targetPath :: Target repr -> Path Rel File +targetPath :: Target src a -> Path Rel File targetPath = _target_path +targetSrc :: Target src a -> src +targetSrc = _target_src + -- | Parsed representation of the source. -targetVal :: Target repr -> repr +targetVal :: Target src a -> a targetVal = _target_val -- | Relative URL to the generated source HTML. -targetUrl :: Target repr -> Text +targetUrl :: Target src a -> Text targetUrl = urlForPath . _target_path -- | Given a path to a HTML file, return its relative URL