html pre wont change my new lines - html

Im using mongodb to store some details. in this case, a testmess string.
testmess = "Angrep: /n beskyttelse: /n Intelligens";
and saving that do db.
im also retrieving it with <pre>{{ testmess }}</pre>
but what i get is:
Angrep: /n beskyttelse: /n Intelligens
why wont the <pre>work with it? same applies to <br>

It's exactly working as expected:
Whitespace inside this element [pre] is displayed as typed.
So it prints out what it get and does not interprets anything inside testmess.
To have a line break on your site, you have to have it in your testmess:
testmess = "Angrep:
beskyttelse:
Intelligens";

Related

Creating new line within a String in HTML

I have string like as follow, which is generated in mvc controller and display in a view within a table cell. S020050A,S020050B,S020050C,S020050D,S020050E,S020050F
,S020050G,S020050H,S020050I,S020050J,S020050K,S020050L
As I want to wrap the text on every 5 words, I have pushed
tag on every 5 words, so that string look like as follow
S020050A,S020050B,S020050C,S020050D,S020050E,S020050F
,S020050G,S020050H,S020050I,S020050J,S020050K,S020050L
I am intended to break the line from there, but in a view it just shows as it is, tag not interpreted as new line but simply as a part of string
I tried putting Environment.Newline and /n as well, when I did this in simple html page, it worked, so I believe it asp.net own mechanism which suppress tag, how I can make tag interpreted as new line?
I could solve it, from controller code (back end) add -> \n instead where you want line break and then in view use this style ->
style="white-space: pre-line"

Text to HTML conversion in Node Js

I am using nodemailer to send mail from node server. I am getting the content for this mail from MSSQL SQL server which is formatted in plain text format, which meansa there are new line characters in it, however when I send it using nodemailer the newline characters are missing and the whole text looks messed up. The other way is to insert html tags for line break in the plain text and send this works fine. But there is too much mannual work involved what I am looking is for a library or utility which can convert the plain text into the html which I can send using mail.
Is there any liberary for this requirement or a way to do this automatically?
The following will wrap all parts that are separated by more than one newline in paragraphs (<p>...</p>) and insert breaks (<br>) where there is just one newline. A text block without any newlines will simply be wrapped in a paragraph.
template = '<p>' + template.replace(/\n{2,}/g, '</p><p>').replace(/\n/g, '<br>') + '</p>';
So for example, it will take this:
Title
First line.
Second line.
Footer
And convert it to this:
<p>Title</p><p>First line.<br>Second line.</p><p>Footer</p>
The simplest solution is you can replace the new line characters with <br>.
Try
text.split('\n').join('\n<br>\n')
then you are done.
Ok finally this code snippet worked for me -
template = template.replace(/\n/gi, "</p></br/>")
template = template.replace(/<\/p>/gi, "</p><p></br/>")
This was a lot of hit and trial but eventually it worked.

Using ruby variables as html code

I would expect that the following:
<div style="padding-top:90px;"><%= u.one_line %></div>
simply pulls whatever is in u.one_line (which in my case is text from database), and puts it in the html file. The problem I'm having is that sometimes, u.one_line has text with formatted html in it (just line breaks). For example sometimes:
u.one_line is "This is < / b r > awesome"
and I would like the page to process the fact that there's a line break in there... I had to put it with spaces up ^^^ here because the browser would not display it otherwise on stackoverflow. But on my server it's typed correctly, unfortunately instead of the browser processing the line break, it prints out the "< / b r>" part...
I hope you guys understand what I mean :(?
always remember to use raw or html_safe for html output in rails because rails by default auto-escapes html content for protecting against XSS attacks.
for more see
When to use raw() and when to use .html_safe

How can i convert/replace every newline to '<br/>'?

set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on
I tried this, content.gsub("\r\n","<br/>") but when I click the view/show button to see the contents of these line, I get the output/result=>
set tabstop=4<br/> set shiftwidth=4<br/> set nu<br/> set ai<br/> syntax on<br/> filetype plugin indent on
But I tried to get those lines as a seperate lines. But all become as a single line. Why?
How can I make all those lines with a html break (<br/>) ?
I tried this, that didn't work.
#addpost = Post.new params[:data]
#temptest = #addpost.content.html_safe
#addpost.content = #temptest
#logger.debug(#addpost)
#addpost.save
Also tried without saving into database. Tried only in view layer,<%= t.content.html_safe %> That didn't work too.
Got this from page source
vimrc file <br/>
2011-12-06<br/><br/>
set tabstop=4<br/><br/>set shiftwidth=4<br/><br/>set nu<br/><br/>set ai<br/><br/>syntax on<br/><br/>filetype plugin indent on<br/>
Edit
Delete
<br/><br/>
An alternative to convert every new lines to html tags <br> would be to use css to display the content as it was given :
.wrapped-text {
white-space: pre-wrap;
}
This will wrap the content on a new line, without altering its current form.
You need to use html_safe if you want to render embedded HTML:
<%= #the_string.html_safe %>
If it might be nil, raw(#the_string) won't throw an exception. I'm a bit ambivalent about raw; I almost never try to display a string that might be nil.
With Ruby On Rails 4.0.1 comes the simple_format from TextHelper. It will handle more tags than the OP requested, but will filter malicious tags from the content (sanitize).
simple_format(t.content)
Reference : http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html
http://www.ruby-doc.org/core-1.9.3/String.html
as it says there gsub expects regex and replacement
since "\n\r" is a string you can see in the docs:
if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by ā€˜dā€™, instead of a digit.
so you are trying to match "\n\r", you probably want a character class containing \n or \r -[\n\r]
a = <<-EOL
set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on
EOL
print a.gsub(/[\n\r]/,"<br/>\n");
I'm not sure I exactly follow the question - are you seeing the output as e.g. preformatted text, or does the source HTML have those tags? If the source HTML has those tags, they should appear on new lines, even if they aren't on line breaks in the source, right?
Anyway, I'm guessing you're dealing with automatic string escaping. Check out this other Stack Overflow question
Also, this: Katz talking about this feature

preserving linebreaks in values in JSON key-value pairs

I am not sure if line breaks are allowed in JSON values. I certainly am unable to create the following in JSON
{"foo": "I am not sure if line breaks are
allowed in JSON values. I certainly
am unable to create the following in JSON"}
The following certainly does not work
{"foo": "I am not sure if line breaks are\nallowed in JSON values. I certainly\nam unable to create the following in JSON"}
Preamble: I want to send a long message like above either to the browser or to the console app and display it neatly formatted so it is legible to the user.
If you are displaying (or inserting) the json value directly in HTML, you can't use it as it is because in html new lines are ignored and replaced by a space.
For example if you have:
<p>Hello,
I'm in other line.</p>
It will be represented as:
Hello, I'm in other line.
You must convert the new lines to paragraph or <br>, for example:
<p>Hello,<br>
I'm in other line.</p>
That will be show as:
Hello,
I'm in other line
If this is your case, you can simply use String.replace to change \n into <br>\n.
If you're displaying it in HTML, will simple do.
Or in a text area or something, try "\r\n", wrapped in double quotes.
Double backslashes are to escape.