{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Rib.Source
(
Source (..),
SourceReader,
sourcePath,
sourceUrl,
sourceVal,
)
where
import qualified Data.Text as T
import Development.Shake (Action)
import Path
import Relude
data Source repr
= Source
{ _source_path :: Path Rel File,
_source_builtPath :: Path Rel File,
_source_val :: repr
}
deriving (Generic, Functor)
sourcePath :: Source repr -> Path Rel File
sourcePath = _source_path
sourceUrl :: Source repr -> Text
sourceUrl = stripIndexHtml . relPathToUrl . _source_builtPath
where
relPathToUrl = toText . toFilePath . ([absdir|/|] </>)
stripIndexHtml s =
if T.isSuffixOf "index.html" s
then T.dropEnd (T.length $ "index.html") s
else s
sourceVal :: Source repr -> repr
sourceVal = _source_val
type SourceReader repr = forall b. Path b File -> Action (Either Text repr)