I am sending a text to a web service. The web service reads the text and makes a html report. The text is multiline. The web service connects all the lines and makes a single line and then wraps it in a quoted string. I want the text lines come separately in the html report. I have no access to change or view the web service's code.
I tried to add <br/> at the end of each message line before sending, but it didn't work.The browser handles <br/> like a normal text and it comes exactly in the report: line1<br/>line2.
I look for a trick to get rid of the quotes and allow the browser to interpret the html tags like <br/>.
Instead of sending
text = '************ <br/> SomeText <br/>**********';
Use
html='************ <br/> SomeText <br/> **********';
I don't know whether it satisfies your requirement
Related
I'm using Google Cloud Translator API to translate some HTML texts. I set the format to HTML, and the translation qualities are pretty good (it keeps all the tags untranslated and only translated the text between the tags). However, it often removes all the line breaks in the HTML text. For example, I selected the English-German option, and
<p><a class="selfLink" id="notes" href="#notes" rel="help"><strong>Notes</strong></a>
<ul>
<li><a class="selfLink" id="disclaimer" href="#disclaimer" rel="help">DISCLAIMER OF LIABILITY</a>
...
becomes
<p><a class="selfLink" id="notes" href="#notes" rel="help"><strong>Anmerkungen</strong></a><ul><li> <a class="selfLink" id="disclaimer" href="#disclaimer" rel="help">...
It's very difficult to read the translated text since it's all in one line. I know that I can set the translator mode to treat the input text as "text" to preserve the line breaks, but in text mode, the translator is not able to identify HTML entities and determine whether a piece of text should be translated or not. Manually adding the line breaks is not a desirable approach. What can I do to improve the readability of the HTML translation?
Disappearing newlines is one of the features of the HTML mode, another is that some of the Unicode characters will turn into HTML entities. You will run into it sooner or later :-)
The solution is to replace all newlines with <br/> before sending the text to Google Translate API, and after getting the translation replace <br/> with newlines + making HTML decode.
I'm using the Microsoft Translator Text API to translate parts of a webpage. The platform we use, inserts in the HTML to render empty lines. So a part of the webpage can be:
<p>
<span>This is a dummy text</span>
</p>
<p>
<span> </span>
</p>
When I send this to the Microsoft Translator Text API, it returns the following HTML:
<p>
<span>Il s’agit d’un texte factice</span>
</p>
<p>
<span></span>
</p>
I've set the content type to text/html, and escape the HTML characters to be able to send it to the API (so will be replaced with ). But the text that is returned by the API has completely lost the .
How can I prevent the API from removing the instances in the HTML? Or is this a bug in the API?
A notranslate span may help to prevent translation. You would have to try it to see if it does indeed preserve the nbsp tag.
See the answer to Microsoft Translator API - notranslate trimming leading space? from Chis Wendt (Microsoft):
Translator trims leading and trailing space, and compresses any other white space to a single space. This is by design. Translator needs to move the words around freely to form the newly composed sentence, and wouldn't know what to do with the extra white space. A workaround would be to trim in your code before translation, and then restore the trimmed off pieces afterwards, depending on the context.
Line breaks and non-breaking spaces tend to be used for specific line layout based on the particular source text that would need to be laid out differently in another language in any case because of different word lengths and arrangements of the significant words.
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.
I am having problem displaying a text with multiple lines. For example, the user can type their text in a textarea in a registration form and the text can be of more than one line i.e. he can hit the Enter (return) key to insert line breaks.
On one page, if I want to show the text that he typed and I use a textarea to display (with EL), it displays the way user had entered initially.
But on another page I need this text be shown in paragraph format (using <p> tag). On this page, when I display the value that the user entered while registering, it does not have the line breaks i.e. it displays in a single line rather than with multiple lines as was entered by the user.
I already tried displaying the text through EL within a <p> tag and using a <c:out> tag of the JSTL within a <p> tag.
Some code that I tried:
Trial-1:
<p>${product.description}</p> //Doesn't show line breaks
Trial-2:
<p><c:out value="${product.description}" /></p> //Doesn't show line breaks too
How can I fix this?
Did you view the source sent to the browser ? Please try
<p><pre>${product.description}</pre></p>
Right now I can think of something as to replace the \r\n sequence in the product.description string with <br /> with help of scriptlets or fn JSTL (function) tag
Idea Courtesy: SO Answer.
Is there a way to get an <input />-field in HTML to wrap lines if the text is longer than the field using CSS? I don't want to use <textarea /> as I want to avoid users entering hard line-breaks by pressing enter.
No, sorry. <input type=text> is single line by definition. See the W3C document Forms in HTML Documents:
text
Creates a single-line text input control.
Using Dojo's Dijit TextArea form control, based off TextArea, you can have an input field which begins as a single line and expands as the user adds to it.
See its documentation.
You can't do what you want with CSS alone, but you could use JavaScript to prevent the user from entering line breaks in a <textarea> field.
Look at this,
http://www.echoecho.com/htmlforms08.htm
The wrap options are the most tricky part of text areas.
If you turn wrap off the text is handled as one long sequence of text without linebreaks.
If you set it to virtual the text appears on your page as if it recognized linebreaks - but when the form is submitted the linebreaks are turned off.
If you set it to physical the text is submitted exactly as it appears on the screen - linebreaks included.
Your best bet is use a textarea (with autogrow capabilities if you like), and then strip out the new lines when the form is submitted. Using php it would be something like this:
$text = str_replace(array("\n","\r"),'',$_POST['text_field']);
This would have the desired effect of blocking newline characters. As others have pointed out it's not really possible to get multi-line input in an input field.