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