]> Git — Sourcephile - haskell/literate-web.git/blob - src/Literate/Web/Live/Asset.hs
correctness(URI): use `Network.HTTP.Types.URI`
[haskell/literate-web.git] / src / Literate / Web / Live / Asset.hs
1 module Literate.Web.Live.Asset where
2
3 import Control.Arrow ((>>>))
4 import Data.Eq (Eq (..))
5 import Data.Function ((&))
6 import Data.Functor (Functor, (<&>))
7 import Data.Ord (Ord)
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)
15
16 -- | The type of assets that can be bundled in a static site.
17 data Asset a
18 = -- | A file that is copied as-is from the source directory.
19 --
20 -- Relative paths are assumed relative to the source directory. Absolute
21 -- paths allow copying static files outside of source directory.
22 AssetStatic FilePath
23 | -- | A file whose contents are generated at runtime by user code.
24 AssetGenerated Format a
25 deriving stock (Eq, Show, Ord, Functor, Generic)
26
27 -- | The format of a generated asset.
28 data Format
29 = -- | Html assets are served by the live server with hot-reload
30 Html
31 | -- | Other assets are served by the live server as static files.
32 Other
33 deriving stock (Eq, Show, Ord, Generic)
34
35 decodeOutputPath :: Text -> Web.OutputPath
36 decodeOutputPath p =
37 Web.OutputPath
38 { Web.outputPathSegs =
39 segs
40 & File.splitDirectories
41 <&> (Text.pack >>> Web.textToPathSegment)
42 , Web.outputPathExts =
43 Text.pack (case exts of '.' : e -> e; _ -> exts)
44 & Text.split (== '.')
45 <&> Web.textToPathSegment
46 }
47 where
48 (segs, exts) = File.splitExtensions (Text.unpack p)