How to preserve leading whitespace in Jinja variable? - jinja2

I have a variable source containing multiple lines containing indentation (i.e. leading whitespaces):
.. admonition:: Lorem ipsum dolor sit amet
Consectetur adipiscing elit. Aenean in dolor
id massa convallis eleifend sed eu mauris.
However, when printed with {{ source }}, Jinja actually prints
.. admonition:: Lorem ipsum dolor sit amet
Consectetur adipiscing elit. Aenean in dolor
id massa convallis eleifend sed eu mauris.
i.e. it strips leading whitespaces! How to preserve them?

Try:
{{ source|indent }}
See jinja documentation indent (default indentation is four spaces, the first and blank lines are not indented - which is exactly what you need)

When reproducing your example this way:
import jinja2
source = """
.. admonition:: Lorem ipsum dolor sit amet
Consectetur adipiscing elit. Aenean in dolor
id massa convallis eleifend sed eu mauris.
"""
template = jinja2.Template("""Content of 'source': {{ source }}
jinja2 version {{ version }} was used.""")
result = template.render(source=source, version=jinja2.__version__)
print(result)
the output was
Content of 'source':
.. admonition:: Lorem ipsum dolor sit amet
Consectetur adipiscing elit. Aenean in dolor
id massa convallis eleifend sed eu mauris.
jinja2 version 2.11.3 was used.
(so the indentation of source was not modified by jinja2).

Related

Replacing values from JSON result with HTML and CSS

I'll try to explain this the best I can. There is a JSON result which separates every paragraph of the text and marks them with 0 or 1 (or even 0.3 etc). The Stringified results on the frontend shows those numbers too. Is there a way to replace these and apply CSS styles based on the value (0, 0.1, or 1)?
the JSON:
RAW:
"content": [
[
0.0,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
[]
],
[
0.0,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
[]
],
[
1,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
[]
]
],
...
What I get in the front end is:
0Lorem ipsum dolor sit amet, consectetur adipiscing elit.0Lorem ipsum dolor sit amet, consectetur adipiscing elit.1Lorem ipsum dolor sit amet, consectetur adipiscing elit.
I should get:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
With 0s and 1s replacedby html (and classes)
You should just be able to map over that content and pull the values out of each item, then render them.
data.content.map((item, i) =>
<p key={i} className={ item[0] < 0.5 ? 'low-value' : 'high-value' }>{ item[1] }</p>
)
Then you can use deconstructing assignment to make this cleaner and give each value a named local variable.
data.content.map(([value, text, array], i) =>
<p key={i} className={ value < 0.5 ? 'low-value' : 'high-value' }>{ text }</p>
)

Kendo Grid does not accept

My Kendo Grid accepts the following json format
{"GetAllStratagyResult":[{"Id":14,"Region":"xxx","Terminal":"NGAY-HG6","Type":1,"VerisonNumber":"5476","VersionDate":"\/Date(1427782421023+0400)\/","VersionName":"Second Version1","content":"<p><span>Lorem ipsum dolor sit amet, consectetuer adipiscing elit<\/span><\/p><p><span>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.<\/span><\/p><p><span>Proin pharetra novnummy pede.<\/span><\/p><p>cccccccccc<\/p>"},{"Id":15,"Region":"yyy","Terminal":"NGAY-HG6","Type":1,"VerisonNumber":"456","VersionDate":"\/Date(1427722608457+0400)\/","VersionName":"Second Version1","content":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."},{"Id":16,"Region":"zzz","Terminal":"NGAY-HG6","Type":1,"VerisonNumber":"456","VersionDate":"\/Date(1427722395997+0400)\/","VersionName":"Second Version1","content":"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas."}]
but it won't show column value for following json:
{"readexcelbyteResult":"[{\"IFRS\":\"Volume\",\"2001\":\"645645\",\"2002\":\"875\",\"2003\":\"8598\",\"2004\":\"6986\",\"2005\":\"98698\",\"2006\":\"698698\",\"2007\":\"6989698\",\"2008\":\"57487\",\"2009\":\"487487\",\"2010\":\"4875\",\"2011\":\"458\"},{\"IFRS\":\"ipsum \",\"2001\":\"456456\",\"2002\":\"6986\",\"2003\":\"98698\",\"2004\":\"6986\",\"2005\":\"986\",\"2006\":\"98698\",\"2007\":\"698698\",\"2008\":\"6986\",\"2009\":\"98698\",\"2010\":\"69860606\",\"2011\":\"9860\"},{\"IFRS\":\"dolor \",\"2001\":\"366397\",\"2002\":\"4364\",\"2003\":\"98308\",\"2004\":\"467\",\"2005\":\"4086\",\"2006\":\"40874\",\"2007\":\"6487\",\"2008\":\"4984\",\"2009\":\"8749\",\"2010\":\"87598\",\"2011\":\"4874\"},{\"IFRS\":\"amet\",\"2001\":\"646456\",\"2002\":\"875\",\"2003\":\"8598\",\"2004\":\"6986\",\"2005\":\"98698\",\"2006\":\"698698\",\"2007\":\"6989698\",\"2008\":\"57487\",\"2009\":\"487487\",\"2010\":\"4875\",\"2011\":\"458\"},{\"IFRS\":\"consectetuer \",\"2001\":\"645645\",\"2002\":\"6986\",\"2003\":\"98698\",\"2004\":\"6986\",\"2005\":\"986\",\"2006\":\"98698\",\"2007\":\"698698\",\"2008\":\"6986\",\"2009\":\"98698\",\"2010\":\"69860606\",\"2011\":\"9860\"},{\"IFRS\":\"adipiscing \",\"2001\":\"536397\",\"2002\":\"4364\",\"2003\":\"98308\",\"2004\":\"467\",\"2005\":\"4086\",\"2006\":\"40874\",\"2007\":\"6487\",\"2008\":\"4984\",\"2009\":\"8749\",\"2010\":\"87598\",\"2011\":\"4874\"},{\"IFRS\":\"Maecenas \",\"2001\":\"456456\",\"2002\":\"875\",\"2003\":\"8598\",\"2004\":\"6986\",\"2005\":\"98698\",\"2006\":\"698698\",\"2007\":\"6989698\",\"2008\":\"57487\",\"2009\":\"487487\",\"2010\":\"4875\",\"2011\":\"458\"},{\"IFRS\":\"porttitor \",\"2001\":\"564564\",\"2002\":\"6986\",\"2003\":\"98698\",\"2004\":\"6986\",\"2005\":\"986\",\"2006\":\"98698\",\"2007\":\"698698\",\"2008\":\"6986\",\"2009\":\"98698\",\"2010\":\"69860606\",\"2011\":\"9860\"},{\"IFRS\":\"congue \",\"2001\":\"366397\",\"2002\":\"4364\",\"2003\":\"98308\",\"2004\":\"467\",\"2005\":\"4086\",\"2006\":\"40874\",\"2007\":\"6487\",\"2008\":\"4984\",\"2009\":\"8749\",\"2010\":\"87598\",\"2011\":\"4874\"}]"}
All my schemas, model and structure of json response are the same.
Please someone help me
In the second case you have quotes before the array of element { "readexcelbyteResult":"[ , that makes the system understand it as string and not as an array of data. Also, all the elements seems to have an extra escape character before the quotes \"IFRS\":\"Volume\" should be: "IFRS":"Volume"
So, it should looks like:
{"readexcelbyteResult":[{"IFRS":"Volume","2001":"645645","2002":"875","2003":"8598","2004":"6986","2005":"98698","2006":"698698","2007":"6989698","2008":"57487","2009":"487487","2010":"4875","2011":"458"},{"IFRS":"ipsum","2001":"456456","2002":"6986","2003":"98698","2004":"6986","2005":"986","2006":"98698","2007":"698698","2008":"6986","2009":"98698","2010":"69860606","2011":"9860"},{"IFRS":"dolor ","2001":"366397","2002":"4364","2003":"98308","2004":"467","2005":"4086","2006":"40874","2007":"6487","2008":"4984","2009":"8749","2010":"87598","2011":"4874"}, {"IFRS":"amet","2001":"646456","2002":"875","2003":"8598","2004":"6986","2005":"98698","2006":"698698","2007":"6989698","2008":"57487","2009":"487487","2010":"4875","2011":"458"},{"IFRS":"consectetuer ","2001":"645645","2002":"6986","2003":"98698","2004":"6986","2005":"986","2006":"98698","2007":"698698","2008":"6986","2009":"98698","2010":"69860606","2011":"9860"},{"IFRS":"adipiscing ","2001":"536397","2002":"4364","2003":"98308","2004":"467","2005":"4086","2006":"40874","2007":"6487","2008":"4984","2009":"8749","2010":"87598","2011":"4874"},{"IFRS":"Maecenas ","2001":"456456","2002":"875","2003":"8598","2004":"6986","2005":"98698","2006":"698698","2007":"6989698","2008":"57487","2009":"487487","2010":"4875","2011":"458"},{"IFRS":"porttitor ","2001":"564564","2002":"6986","2003":"98698","2004":"6986","2005":"986","2006":"98698","2007":"698698","2008":"6986","2009":"98698","2010":"69860606","2011":"9860"},{"IFRS":"congue ","2001":"366397","2002":"4364","2003":"98308","2004":"467","2005":"4086","2006":"40874","2007":"6487","2008":"4984","2009":"8749","2010":"87598","2011":"4874"}]}

Replace custom tags with html using preg_replace_callback

Am trying to do a replacement of customized tags with html tags in my website as follows:
[block-2] "Donec volutpat nibh sit amet libero ornare non laoreet arcu luctus. Donec id arcu quis mauris". [/block-2]
If the above [block] tag is encountered, it should be replaced by some html tags (using regexp), specifically:
<blockquote class="tm-style2"><span>
"Donec volutpat nibh sit amet libero ornare non laoreet arcu luctus. Donec id arcu quis mauris".
</span></blockquote>
I have tried the following but is not working. Please help:
$article_text = preg_replace_callback(
"(\[block-([0-9]+)\](.+)\[\/block-([0-9]+)\])",
create_function('$p','return "<blockquote class=\"tm-style".$p[1]."\"><span>".$p[2]."</span></blockquote>";'), $article_text);
You don't actually need to use preg_replace_callback function, just preg_replace would be fine.
\[(block-([0-9]+))\](.+?)\[\/\1\]
Then replace the matched characters with
<blockquote class="tm-style\2"><span>\n\3\n</span></blockquote>
DEMO
$re = "~\\[(block-([0-9]+))\\](.+?)\\[\\/\\1\\]~m";
$str = "[block-2] \"Donec volutpat nibh sit amet libero ornare non laoreet arcu luctus. Donec id arcu quis mauris\". [/block-2]";
$subst = '<blockquote class="tm-style\2"><span>'."\n".'\3'."\n".'</span></blockquote>';
$result = preg_replace($re, $subst, $str);
echo $result;
Output:
<blockquote class="tm-style2"><span>
"Donec volutpat nibh sit amet libero ornare non laoreet arcu luctus. Donec id arcu quis mauris".
</span></blockquote>
You need to escape closing tag's backslash in your pattern ...[\/block....
So full pattern is (\[block-([0-9]+)\](.+)\[\/block-([0-9]+)\])

Markdown Lorem Ipsum generator?

There are dozens of html/text Lorem ipsum generators around; However something I haven't found which I find strange with the prevalence of Markdown online is a Markdown Lorem Ipsum generator.
Does anyone know of one online somewhere or of any reason one wouldn't exist?
I recently came across this awesome markdown lorem ipsum generator called Lorem Markdownum.
It's configurable and includes random generation of all markdown elements using Markov chains and other cool stuff.
Here's a sample output:
Amoris Pindusque spreta Denique et positus quoniam
Inquit Tyriis cervos circuit Hyrie ocius sitientes
Renuente feruntur fontibus pares, ait tertia lacunabant animo, est. Limina
pulvere victima fores Aeacidae. Cum sua muneris spargit albis
glomerabat novercae puniceo. Vel quae
hac valuisse et artus intexuit ramosam Herculeos umbras. Ferarum niger.
Vestigia votorum turbatusque putes haeremusque matrem fateor
Iam natam sic fataliter uterque exiliis una
Proceres aethera
Amando littera solantia iam Paridis priorem dixisse
Illis altior bracchia lapides decus increpat ignota. Lucis suis eheu nec, sive
dicta villosa deos flexi me parvo tendere, talibus semper Polydegmona ait.
Refert versato vultus, percussit pavor, primum matrem care grande, iam adeundi
est arenti! Dracones hinnitus soleo sequenti, Arethusae me morte sanguine;
inscripsere summa: ego.
logicPim = imapOptical(19 - laserIconSpreadsheet, text_technology(
yobibyte_regular_hyperlink, icio_gbps_compiler),
swipeOdbcInterface.gnutella_copyright(backside_netmask,
abend_camelcase, mbrCycleTag) + smmFileMemory);
waveform_server(mini_degauss_paper(ccMegapixel + default_minisite_ieee,
script_system));
rtf_restore_crossplatform(barBlu, css);
Quod res fulmen in, hunc, atque vivum. Quid aequor captivo illum, clausit in
dubitas mixta. Est penates est celas, modo perque reticere
orbem timore neptis refero. Sperneret viridis iaculis avis Phrygiisque alto!
I did this little thing while learning emberjs. Hope it helps!
http://juanda.me/tests/ember-lipsum-md/
This http://www.lorem-ipsum-html.com/ it is simple and clean, This provides code snippets with html like this:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam tincidunt mauris eu risus.
Vestibulum auctor dapibus neque.

Rails simple_format and display of 'Read More' link inline with text

Should have been very easy but somehow cannot get it to work. I want to display truncated text on my blog home page with a 'read more' link that shows up inline with the text i.e.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ac tortor et felis aliquet vestibulum sed in sem. Aliquam pharetra ultricies nunc, non pellentesque ... Read More
Since I use simple_format for my text, all breaks in the text get wrapped in tags and I get Read More in a separate line as below
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ac tortor et felis aliquet vestibulum sed in sem. Aliquam pharetra ultricies nunc, non pellentesque ...
Read More
How do I place it inline after a < p > tag. Guess it's more of a CSS question rather than a Rails one. Again, I feel this should be super simple. What am I forgetting?
Here's the code snippet in the view
<%= simple_format truncate(h(feed.description), length: 400, separator: ' ') %><%= link_to "Read More", feed_path(feed)%>
I suspect that you can just put your link inside the simple_format content-parameter link this:
<%= simple_format truncate((h(feed.description) + link_to("Read More", feed_path(feed))), length: 400, separator: ' ') %>
(Note: not tested)