Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions compiler/src/Generate/Html.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ sandwich_ root moduleName javascript =
customHead
else
"<title>" <> name <> "</title>"

htmlTag = Lamdera.unsafe $ Lamdera.Live.lamderaHtmlLang root
in
[r|<!DOCTYPE HTML>
<html>
"<!DOCTYPE HTML>\n"
<> htmlTag
<> [r|
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0">
Expand Down
32 changes: 26 additions & 6 deletions extra/Lamdera/Live.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Lamdera.Live where

import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as B
import qualified Data.Text.Encoding as T
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified System.Directory as Dir
import System.FilePath as FP
import Control.Exception (finally, throw)
Expand Down Expand Up @@ -40,16 +41,16 @@ lamderaLiveSrc =
overrideM <- readUtf8Text overridePathBuilt
case overrideM of
Just override -> do
pure (T.encodeUtf8Builder override)
pure (TE.encodeUtf8Builder override)
Nothing -> do
Lamdera.debug $ "Couldn't load override " ++ overridePath ++ ", using compiled lamderaLive"
pure (T.encodeUtf8Builder (T.decodeUtf8 lamderaLive))
pure (TE.encodeUtf8Builder (TE.decodeUtf8 lamderaLive))
else do
Lamdera.debug $ "Couldn't find override " ++ overridePath ++ ", using compiled lamderaLive"
pure (T.encodeUtf8Builder (T.decodeUtf8 lamderaLive))
pure (TE.encodeUtf8Builder (TE.decodeUtf8 lamderaLive))
else do
Lamdera.debug $ "🗿 Using compiled lamderaLive"
pure (T.encodeUtf8Builder (T.decodeUtf8 lamderaLive))
pure (TE.encodeUtf8Builder (TE.decodeUtf8 lamderaLive))


-- @TODO means we have to restart live for any changes... how to improve that?
Expand All @@ -58,12 +59,31 @@ lamderaLiveHead root = do
headHtmlM <- readUtf8Text $ root </> "head.html"
case headHtmlM of
Just headHtml ->
pure (True, T.encodeUtf8Builder headHtml)
pure (True, TE.encodeUtf8Builder headHtml)

Nothing ->
pure (False, "")


lamderaHtmlLang :: FilePath -> IO B.Builder
lamderaHtmlLang root = do
langM <- readUtf8Text $ root </> "html-lang"
pure $ maybe "<html>" toHtmlTag langM
where
toHtmlTag lang =
let trimmed = T.strip lang in
if T.null trimmed
then "<html>"
else "<html lang=\"" <> TE.encodeUtf8Builder (escapeHtmlAttr trimmed) <> "\">"

escapeHtmlAttr = T.concatMap $ \c -> case c of
'&' -> "&amp;"
'"' -> "&quot;"
'<' -> "&lt;"
'>' -> "&gt;"
_ -> T.singleton c


lamderaLive :: BS.ByteString
lamderaLive =
$(bsToExp =<< runIO (Lamdera.Relative.readByteString "extra/dist/live.js"))