Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nl2br function, like in php #346

Merged
merged 2 commits into from
Aug 21, 2020
Merged
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
15 changes: 14 additions & 1 deletion IHP/ViewSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module IHP.ViewSupport
, query
, isActiveController
, renderFlashMessages
, nl2br
) where

import IHP.Prelude
Expand All @@ -47,6 +48,8 @@ import Text.Blaze.Html5.Attributes as A
import qualified IHP.ControllerSupport as ControllerSupport
import qualified IHP.Controller.Session as Session
import IHP.HtmlSupport.QQ (hsx)
import IHP.HtmlSupport.ToHtml
import qualified Data.Sequences as Sequences

type HtmlWithContext context = (?viewContext :: context) => Html5.Html

Expand Down Expand Up @@ -310,4 +313,14 @@ renderFlashMessages =
renderFlashMessage (Session.SuccessFlashMessage message) = [hsx|<div class="alert alert-success">{message}</div>|]
renderFlashMessage (Session.ErrorFlashMessage message) = [hsx|<div class="alert alert-danger">{message}</div>|]
in
forEach flashMessages renderFlashMessage
forEach flashMessages renderFlashMessage

-- | Replaces all newline characters with a @<br>@ tag. Useful for displaying preformatted text.
--
-- >>> nl2br "Hello\nWorld!"
-- [hsx|Hello<br/>World!|]
nl2br :: (Sequences.Textual text, ToHtml text) => text -> Html5.Html
nl2br content = content
|> Sequences.lines
|> map (\line -> [hsx|{line}<br/>|])
|> mconcat
1 change: 1 addition & 0 deletions ihp.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ common shared-properties
build-depends:
base
, classy-prelude
, mono-traversable
, directory
, string-conversions
, warp
Expand Down