Skip to content

Commit

Permalink
Merge pull request #346 from digitallyinduced/nl2br
Browse files Browse the repository at this point in the history
Added nl2br function, like in php
  • Loading branch information
nezbite committed Aug 21, 2020
2 parents 6e8203f + 8017fe1 commit 0a5b2b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit 0a5b2b8

Please sign in to comment.