Jinja2 trim CR when rendering - jinja2

I was trying to figure out why Jinja2 is triming my \r out of my model. I've got something like this :
Something useful\r\n
And again\r\n
Which then is rendered like this :
Something useful\n
And again\n
And this seems to be known as this link shows : https://github.com/ansible/ansible/issues/22676
As drewdogg is demonstrating here :
>>> t = Template("{{ 'lorem\r\nipsum' }}")
>>> t.render()
u'lorem\nipsum'
Why is this happening? Can I do something to preserve my \r that are necessary for me? The only solutions I came accross was to explicitly write :
{{ '\r\n' }}
Or worse replace, either in Jinja2 or Python3...
Has anybody came over this problem and found a solution?
Thanks a lot :)

Related

Wrap integers in quotes from json data

I created this question yesterday
I've since realised there are actually a few other bits of data that cause issues with the solutions I received. Hence, I thought it best to make a new question
Take the following example data;
"87",0000,0767,"078",0785,"0723",23487, "061 904 5284","17\/10\/2016","some.name.789#hotmail.com"
Using the accepted solution form above (?<!")(\b\d+\b)(?!")
The date string ends up having the middle number in between the two \/ wrapped, the number in quotes with spaces breaks as well as the email address.
The issues can be seen here: https://regex101.com/r/qVQYA7/6
My Solution
The following does seem to work for me, however it seems a bit messy. I have a feeling there's a much more succinct way to achieve the same result;
,(?<!("|\/|\\))(\b\d+\b)(?!("|\/|\\|( \d))) Replace with ,"$2"
https://regex101.com/r/qVQYA7/5
EDIT:
#Federico this screenshot shows that spaces before or after commas breaks the replace;
By reading your both questions, what I understand is that you want to wrap in double quots some numbers that aren't, so for this I can come up with a simple regex like this:
(?<=,)(\d+)(?=,)
With the replacement string: "$1"
Working demo
Update: after you updated the question, here I put the update for the answer. You can use this regex instead:
(?<=,)\s*(\d+)\s*(?=,)

HAML Syntax highlighting misalignment

Hoping this is a quick fix,
I have a some HAML I would like to have highlighted with the prism library.
%pre
%code.language-haml
%header.post-header
%h1= data.title
%time{ datetime: data.date }= pretty_date(data.date)
only it's coming out like this
how do I get it to look like this
it's pretty frustrating, if I leave it unescaped it will not be visible.
I eventually just hacked it using the HTML escape HTML code for spaces and then aligned them all using that.

Regex find two characters in order, between others, ignoring punctuation

I'm trying to filter using regex in mySQL.
The field is a text field and I want to find all that match 'MD' or similar ('M.D.', 'M. D.', 'DDS, M.D.' etc.).
I do not want to accept those that contain M and D as a part of another acronym (e.g., 'DMD'). However 'DMD, M.D.' I would want to find.
Apologies if this is a simple task - I read through some regex tutorials and couldn't figure this out! Thanks.
Update:
With help from the suggestions I arrived at the following solution:
(\s|^)M\.?\s*D\.?
which works for all of my cases. The quotes in my questions were to indicate it was a string, they are not a part of the string.
You can use a regex like this:
\b(M\.?\s*D\.?|D\.?\s*D\.?\s*S\.?)
Working demo
If I have understood your requirement:
'([^'.]*[ ,]*M[. ]*D[. ]*)'
this looks for MD preceded by space comma or ' separated by 0 or more dots & spaces, followed by '
it matches all the contents between the '' marks
test: https://regex101.com/r/oV2kV8/2
In the end I found this solution works:
(\s|^)M\.?\s*D\.?(\s|$)
This allows for the 'MD' to be at the start or after another credential and to have spaces or periods or nothing between the letters.

Yahoo Pipes and Regex with an html formatting issue

I am struggling to see how to use the regex to add a non-printable carriage return character into an html string.
Its a WordPress thing in that to auto-embed a video I need to put the URL on its own line in the html.
First I use a regex:
In item.vid_src replace ($) with \\r$1
s is checked.
After which I am using a loop with a string builder in it - I am prefixing vid_src to the start of description thus:
item.vid_src
<br><br>
item.description
assign results to item.description
Before I include the Regex module in the pipe I get this:
http://www.youtube.com/watch?v=THA_5cqAfCQ<br><br><p><h1 class="MsoNormal">Cheetahs on
the edge</h1>
But I need this:
http://www.youtube.com/watch?v=THA_5cqAfCQ
<br><br><p><h1 class="MsoNormal">Cheetahs on the edge</h1>
Adding the regex module I get this:
http://www.youtube.com/watch?v=THA_5cqAfCQ\r<br><br><p><h1 class="MsoNormal">
Cheetahs on the edge</h1>
Clearly its inserting exactly what I have asked for, but It is not what I was expecting, I need to get the html formatted with the newline. Does anybody have an insight as to how to tackle the problem?

How to generate a line break in Django template

I want to give default value to a textarea. The code is something like this:
<textarea>{{userSetting.list | join:"NEWLINE"}}</textarea>
where userSetting.list is a string list, each item of whom is expected to show in one line.
textarea takes the content between the tags as the default value, preserving its line breaks and not interpreting any HTML tags (which means <br>,\n won't work).
I have found a solution: {{userSetting.list | join:" " | wordwrap:0}} (there is no whitespace in the list). But obviously it is NOT a good one. Any help would be appreciated.
Since Chris didn't come and collect the credit, I have to answer my question myself. (But still thanks him for pointing to the right direction)
The HTML entity
stands for a NEWLINE character, and won't be interpreted in Django template. So this will work:
<textarea>{{userSetting.list | join:"
"}}</textarea>
A textarea in HTML does respect newlines. Does this not escape the \n:
<textarea>{{userSetting.list | join:"\n"}}</textarea>
I'm thinking that it should turn the \n into a newline character, so your source looks like this:
<textarea>Setting 1
Setting 2
Setting 3</textarea>
This would work if it did, but it may not convert \n correctly.
Try using {{userSetting.list | join:"
"}} if and \n won't work. Let me know how you get on...
:P
For some reason the accepted answer did not work for me, but this did:
<div>{{ list|join:'<br>' }}</div>
Docs reference for join: https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#join