1 module Literate.Web.Live.Asset where
3 import Control.Arrow ((>>>))
4 import Data.Eq (Eq (..))
5 import Data.Function ((&))
6 import Data.Functor (Functor, (<&>))
8 import Data.Text (Text)
9 import Data.Text qualified as Text
10 import GHC.Generics (Generic)
11 import Literate.Web qualified as Web
12 import System.FilePath qualified as File
13 import System.IO (FilePath)
14 import Text.Show (Show)
16 -- | The type of assets that can be bundled in a static site.
18 = -- | A file that is copied as-is from the source directory.
20 -- Relative paths are assumed relative to the source directory. Absolute
21 -- paths allow copying static files outside of source directory.
23 | -- | A file whose contents are generated at runtime by user code.
24 AssetGenerated Format a
25 deriving stock (Eq, Show, Ord, Functor, Generic)
27 -- | The format of a generated asset.
29 = -- | Html assets are served by the live server with hot-reload
31 | -- | Other assets are served by the live server as static files.
33 deriving stock (Eq, Show, Ord, Generic)
35 decodeOutputPath :: Text -> Web.OutputPath
38 { Web.outputPathSegs =
40 & File.splitDirectories
41 <&> (Text.pack >>> Web.textToPathSegment)
42 , Web.outputPathExts =
43 Text.pack (case exts of '.' : e -> e; _ -> exts)
45 <&> Web.textToPathSegment
48 (segs, exts) = File.splitExtensions (Text.unpack p)