If you are using Angular and want something like Rails'
simple_format
Show archive.org snapshot
which HTML-formats a plain-text input into paragraphs and line breaks, this directive is for you.
Any HTML fragments inside that text will still be escaped properly.
Use it like this, where your text
attribute specifies something available in your current scope:
<simple-format text="email.message"></simple-format>
This is the directive, in CoffeeScript syntax:
@app.directive 'simpleFormat', ->
restrict: 'E'
scope: true
template: """
<p ng-repeat='lines in paragraphs track by $index'>
<span ng-repeat='line in lines track by $index'>
<br ng-hide='$first' />
{{ line }}
</span>
</p>
"""
link: (scope, element, attributes) ->
scope.$watch attributes.text, (text) ->
scope.paragraphs = []
if text?
# remove weird newlines
text.replace /\r\n|\r/g, '\n'
# split lines and paragraphs
paragraphs = text.split('\n\n')
scope.paragraphs = _.map paragraphs, (lines) ->
lines.split('\n')
Also see simple_format helper for Javascript.
Posted by Arne Hartherz to makandra dev (2014-04-17 13:13)