Prevent HAML from rendering newline in produced HTML file - html

I have this HAML code:
%p
This page is for our staff. If you came here by mistake,
%a(href="index.html") you can go back
\.
The isolated \. is there because I don't want the full-stop (.) to be part of the link.
This almost works but there is a space between back and .; naturally, HAML is inserting the newline in the HAML source code in the HTML rendered file.
In other words, this is the HTML produced:
<p>
This page is for our staff. If you came here by mistake,
you can go back
. <!-- I want the period to be on the previous line -->
</p>
Because words inside the <p> tag are separated by a space, there is a space between back and .. How can I remove this space?
I found one way to do this, but it is ugly (or I won't have asked this question):
%p
This page is for our staff. If you came here by mistake,
%a(href="index.html") you can go back
%span>\.
Is there a better way to do this?

HAML accepts plain html, so you could write:
%p
This page is for our staff. If you came here by mistake,
you can go back.
Which will give you the output you need.

You can also use the succeed helper for this, although it reads a little funny:
= succeed '.' do
%a(href="index.html") you can go back
Will produce:
you can go back.\n
So full example would be like:
%p
This page is for our staff. If you came here by mistake,
= succeed "." do
%a(href="index.html") you can go back
Rendered Output:
<p>
This page is for our staff. If you came here by mistake,
<a href='index.html'>you can go back</a>.
</p>

Related

Replacing stuff of HTML using regex

I am editing a couple of hundred HTML files and I have to replace all the stuff manually, so I was wondering whether it could be done using regex.I don't think it is possible, but it might be, so please help me out.
Okay, so for example, I have many <p> tags in a file, each with a different class. eg:
<p class="class1">stuff here</p>
<p class="class2">more stuff here</p>
I wanted to replace the "stuff here" and "more stuff here" with something, for example
<p class="class1">[content]</p>
<p class="class2">[content]</p> .
I wanted to know if that is possible.
I'm using notepad++.
P.S. I'm new to regex.
I think notepad++ is great for stuff like this. Open up Find/Replace, and check the regular expressions box in the dialog's Search Mode section.
In the "Find what" field, try this:
\<p\ class\=(.*)\>(.*)\<\/p\>
and in "Replace with":
\<p\ class\=\1\>[content]\<\/p\>
the \1 here will take whatever (found by (.*)) between the class= and the angle bracket > which ends the tag, and replace it with itself, which essentially results in ignoring the class name, rather than having to specify. the second (.*) catches the current content inside the paragraph tag, which is what you want to replace. So where I wrote [content] in the "Replace with" block, that's where you'd put your new content. This does limit you to content that you can paste into the notepad++ find/replace dialog, but I think it has a pretty huge limit.
If I'm remembering that text field's limitations incorrectly, another thing you could do is just adjust my "Replace with" text to just replace the old text with some newlines:
\<p\ class\=\1\>\n\n\<\/p\>
This will delete the old text and leave a clear line where it once was, making it easy to paste whatever you want into the normal editor pane.
The first way is probably better, if your new content will fit the Replace With field, because this regex works once per line. And you can click "Replace" a couple times, and if it's working, clicking "Replace all" will iterate through every <p> element in the file.
Note: this solution assumes that your <p> tags open and close within one line, as you typed them your question description. If they break lines, you're going to want to enable . matches newline in the Replace dialog, and... you need trickier (more precise) syntax than (.*) to catch your class name and content-to-be-replaced. Let me know if this is the the case, and I'll fiddle with it and see if I can help more. The (.*) needs to change to (.*?) or something; the search needs to get more greedy, because if . matches newline, then .* matches any and every possible character infinite times, i.e., the whole document.

How to keep trailing whitespace with gulp-jade

My client has scripts to build templates with Jade. We use gulp-jade. I run into an issue where trailing whitespace is removed but should be kept. Here is an example of what the template is:
p
span This is the first part of the sentence,
strong this part makes a strong point,
span and this part concludes the sentence.
Since inline HTML elements are whitespace sensitive, after compiling the template, this is what I would expect:
This is the first part of the sentence, this part makes a strong point, and this part concludes the sentence.
However, this is what is actually compiled:
This is the first part of the sentence,this part makes a strong point,and this part concludes the sentence.
Notice the spaces missing?
Looking through the Jade documentation, it seems this shouldn't happen. Would this be related to gulp-jade? I tried to look for options about this, I didn't find it.
Please do not suggest CSS solutions, as they do not qualify as a valid solution. HTML already handles this normally, it's a template compilation issue.
NOTE: I can't use markdown in my example, as removing the spaces break the bold/strong part.
Thanks in advance!
I've experienced this problem before, solved using gulp-prettify. Here's my jade task:
gulp.task('html', function() {
return gulp.src(config.src)
.pipe(jade())
.pipe(prettify({ indent_size: 2, unformatted: ['pre', 'code'] }))
.pipe(gulp.dest(config.dest));
});

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

Multiline Edit Box value into HTML to be sent in email in xpages

I found some great javascript code(xpHTMLMail file) to be able to create an HTML e-mail that the users create on the fly from an xpage document that they write a review on a salesperson. However, there are some Multiline edit boxes on there and they have carriage returns, spaces, etc in them. These do not come over when they are added to the HTML. Anything I can do to keep the formatting for the e-mail that is created? Thanks in advance.
Here's the code that deals with this part of my question(inputClosing is a Multiline Edit Box):
mail.addHTML("<br /><br /><b>Closing</b><br />"+getComponent('inputClosing').getValue())
If inputClosing has...
"Dear Joe,
Great work. Keep it up!
Thanks,
Bill"
It comes into the email as...
Dear Joe, Great Work. Keep it up! Thanks, Bill
I wrote that library so thanks!
Since you're creating an HTML mail, you need to replace the line breaks in the value of the Multiline Edit Box by <br /> tags. Since you're dealing with Java in XPages, the line breaks are stored in the value using the \r\n sequence.
You can replace them using the the replaceAll() or (SSJS) #ReplaceSubstring() function.
Your code might then look like this:
var content:string = getComponent("inputClosing").getValue();
mail.addHTML("<b>Closing</b><br />" + content.replaceAll("\r\n", "<br />") );
Mark's suggestion definitely works, but it might be easier just to wrap the text fields with <pre></pre> then it will treat them as text instead of html, no matter what kind of formatting character is in it.

display mysql newline in HTML

Certain fields in our mysql db appear to contain newline characters so that if I SELECT on them something like the following will be returned for a single SQL call:
Life to be sure is nothing much to lose
But young men think it is and we were young
If I want to preserve the line breaks when displaying this field on a webpage, is the standard solution to write a script to replace '\n\r' with a br HTML tag or is there a better way?
Thanks!
Assuming PHP here...
nl2br() adds in <br /> for every \n. Don't forget to escape the content first, to prevent XSS attacks. See below:
<?php echo nl2br(htmlspecialchars($content)); ?>
HTML is a markup language. Regardless of how many linebreaks you put in the source code, you won't see anything from it back in the presentation (of course assuming you aren't using <pre> or white-space:pre). HTML uses the <br> element to represent a linebreak. So you basically indeed need to convert the real and invisible linebreaks denoted by the characters xA (newline, linefeed, LF, \n) and/or xD (carriage return, CR, \r) by a HTML <br> element.
In most programming languages you can just do this by a string replace of "\n" by "<br>".
You can wrap it in <pre> .. </pre>.