How to force line break in Symfony2 translation file - symfony

In my messages.en.yml I have an entry that is used for an email body which is in plaintext, so I cannot use any HTML tags. Now when I need a line break and I add a blank line after, the line break appears in my email body.
But when I try to do a line break with a non empty line following, it does not break the lines.
email:
subject: My Subject
message: >
Hello %name%,
This is my second line
Best regards,
John Smith
Customer Care
Arrives like this in my mailbox
Hello Tom,
This is my second line
Best regards, John Smith Customer Care
I've tried to play around with HTML tags or with the \n which I know from PHP. But if I use <br> or <br /> in the translation file and apply the |raw filter in my twig file, the HTML tags still appear in my email body. And also if I use \n to force a line break, no line break is given and instead, the \n appear in my email as well.
Now how can I force the line break if a non empty line is following?

Use |
email:
subject: My Subject
message: |
Hello %name%,
This is my second line
Best regards,
John Smith
Customer Care

In your translations file (e.g. messages.en.yml):
your
translation
key:
here: |
Lorem ipsum.
CRMPICCO.
www.crmpicco.co.uk.
Accessing it in your Twig template:
{{ 'your.translation.key.here'|trans|nl2br }}

Related

symfony yaml: keep new lines (literal)

I am trying to keep newlines in a yaml file.
In a messages.en.yml file, I have.
test:|
Here is line 1
Here is line 2
And in form.html.twig:
{{'test'|trans}}
Both lines are rendered on the same line.
Here is line 1 Here is line 2
In this link, the literal style for Yaml is explained.
and in this other one, it says "Notable lacking features are: document directives, multi-line quoted messages ..."
So is it possible in Symfony Yaml to use multilines? and how?
You could use |nl2br filter
{{ 'test'|trans|nl2br }}
Or, if you are sure your translation can't get altered by any user, you could use some few html in your translation.
test:|
Here is line 1<br/>
Here is line 2<br/>
<br/><br/>
Here is line 5
Then
{{ 'test'|trans|raw }}

Prepend text to file without creating a line break with SaltStack

I am using saltstack to managed configuration on my servers. I have a use case where I need to prepend text to the beginning of the file WITHOUT creating a line break.
Currently:
/path/to/file:
file.prepend:
- text:
here is some text to add
This works great ACCEPT for the fact that "here is some text to add" is added on a new line at the top of the file. I need this text to be prepended at the beginning of the file without creating a new line break. I would love to do this with saltstack but I cannot find a solution to this problem anywhere in their documentation.
The text being added on a new line at the beginning of the file creates things to not work as expected. I have found a few examples of how to do this using sed or a bash script but would love to be able to use saltstack to accomplish this.
Has anyone experiences this same problem that can point me in the right direction?
I was able to come up with a solution for this using pattern matching and replacing.
add_text_to_start:
file.replace:
- name: /path/to/the/file
- pattern: text that will always be at the beginning
- repl: here is some text to add text that will always be at the beginning
The text that is at the beginning of the file will always be at the beginning of the file. So by matching the pattern I am able to add text to the beginning of the file without having to add a line break. All the rest of the text that follows the "text that will always be at the beginning of" is untouched and will be in the same place that is was in before the pattern find and replace occurred.

How to represent markdown properly with escaping and line breaks?

I'm currently trying to build a chat app, using the official markdown package as well as underscore's escape function, and my template contains something like this:
<span class="message-content">
{{#markdown}}{{text}}{{/markdown}}
</span>
When I grab the text from the chat input box, I try to escape any HTML and then add in line breaks. safeText is then inserted into the database and displayed in the above template.
rawText = $("#chat-input-textbox").val();
safeText = _.escape(rawText).replace(/(?:\r\n|\r|\n)/g, '\n');
The normal stuff like headings, italics, and bold looks okay. However, there are two major problems:
Code escape issue - With the following input:
<script>alert("test")</script>
```
alert('hello');
```
This is _italics_!
Everything looks fine, except the alert('hello'); has become alert('hello'); instead. The <pre> blocks aren't rendering the escaped characters, which makes sense. But the problem is, the underscore JS escape function escapes everything.
SOLVED: Line break Issue - With the following input:
first
second
third
I get first second third being displayed with no line breaks. I understand this could be a markdown thing. Since I believe you need an empty line between paragraphs to get linebreaks in markdown. But having the above behaviour would be the most ideal, anyone know how to do this?
UPDATE Line break issue has been solved by adding an extra \n to my regex. So now I'm making sure that any line break will be represented with at least two \n characters (i.e. \n\n).
You should check the showdown docs and the wiki article they have on the topic.
The marked npm package, which is used by Telescope removes disallowed-tags. These include <script> of course. As the article I linked to above explains, there's still another problem with this:
<a href='javascript:alert("kidding! Im more the world domination kinda guy. Muhahahah")'>
click me for world peace!
</a>
Which isn't prevented by marked. I'd follow the advice of the author and use a HTML sanitation library. Like OWASP's ESAPI or Caja's html-sanitizer. Both of these project's seem outdated dough. I also found a showdown extension for it called showdown-xss-filter. So my advice is to write your own helper, and use showdown-xss-filter.

How to break line in web page?

I want to format the content before showing to web page. I am storing '\r\n\' as enter in Database and trying to replace it before show content on the web page. server side my code is:
lblComments.Text = Server.HtmlEncode(((DataRowView)e.Item.DataItem).Row["Comment"].ToString().Replace("\r\n", "<br>"));
I have use Server.HtmlEncode.
My Out put should be:
Comment Type: Public
Comment: Commented on the Data by user A
But, its shows me everything in single line.
You need to use
.Replace("\r\n", "<br/>")
And only AFTER Server.HtmlEncode, because you don't want the <br/> itself to get encoded to <br/> (raw), displaying as <br/> literally.
You could wrap the output in a <pre>...</pre> element instead of replacing the line breaks with <br>. That way, the line breaks should be conserved.
E.g. put a <pre> element around your Label:
<pre><asp:Label id="lblComments" runat="server" /></pre>
Try
Replace("\n", "<br>")
This test worked without encode
string comments = #"Comment Type: Public
Comment: Commented on the Data by user A";
lblComments.Text = comments.Replace("\n","<br>");

Pipe a certain field of an input stream in a command, and paste back the results

Say I have an input stream consisting of lines separated into a certain number of fields. I would like to cut on the various fields, pipe a certain field (or fields) to a program (which is assumed to return one line for each input line) and leave the other fields as is, and paste the results back together. I can probably imagine convoluted solutions, but there ought to be a clean and natural way to do that.
As a specific example, say I have a program producing lines of the form:
$ inputprog
<a> hello world!
<b> hi everyone!
<a> hi!
Say I would like to put the message in uppercase while leaving the first field unchanged. Here is how I would imagine things:
$ inputprog | program -d' ' -f2- "tr a-z A-Z"
<a> HELLO WORLD!
<b> HI EVERYONE!
<a> HI!
I am looking for a reasonably clean way to approximate program. (I am not interested in solutions which are specific to this example.)
Thanks in advance for your help!
awk can do what you want. For example:
$ echo "field1 field2" | awk '{$2 = toupper($2); print;}'
field1 FIELD2
Comes pretty close to what you want to do. $2 = toupper($2); changes the second field, while print prints out the whole (modified) line.
However, you got a problem in how you define a 'field'. In the example above fields are separated by spaces (you can change the field separator to an arbitrary regexp with like so: -F'<[a-zA-Z]+>' - this would consider as a field separator).
But in your example you seem to view <a> as one field and hello world! as another one. Any program could only come to your desired behaviour by wild guessing that way. Why wouldn't world! be considered a third field?
So, if you can get input with a clear policy of separating fields, awk is exactly what you want.
Check out pages like http://people.cs.uu.nl/piet/docs/nawk/nawk_92.html (awk string functions) and http://www.pement.org/awk/awk1line.txt (awk 1 liners) for more information.
BTW, one could also make your specific example above work by looping over all the fields except the first one (NF == Number of Fields):
$ echo "<a> hello world!
<b> hi everyone!
<a> hi" |
awk '{for(i=2;i<=NF;++i) { $i=toupper($i); }; print;}'
<a> HELLO WORLD!
<b> HI EVERYONE!
<a> HI
Even though you are not interested in the solution to this example. ;-)
P.S.: sed should also be able to do the job (http://en.wikipedia.org/wiki/Sed)

Resources