Jade - Print many variables in one line - node.js

I am trying to print several variables that i have passed from the controller to view in one line.
The content of the variables is like "xyz", where tag is a valid html tag.
What i am doing now is -
div
!= (notification.content + notification._.publishedDate.format('MMMM Do, YYYY'))
However this prints the div on two lines.
The html content generated is -
<div>
<p>School is closed tomorrow link</p>
March 7th, 2016
</div>
Additionally i cannot do -
p= (notification.content + notification._.publishedDate.format('MMMM Do, YYYY'))
Because the output is this then in HTML -
<p><p>School is closed tomorrow&nbsp;<a href="http://www.booking.com">link</a></p>March 7th, 2016</p>

Hey!
See some ways to display your message using Jade/HTML.
Please, test these ways and use the best on your project:
Declaring Variables
// Variable with `<p></p>`
- var notification = {content: '<p>School is closed tomorrow link</p>', publishedDate: '2016-03-07'}
// Variable withOUT `<p></p>`
- var test = {content: 'The message link', publishedDate: '2016-03-07'}
1) Return Escaped Text
div #{notification.content} #{notification.publishedDate}
// Return | escaped string:
// <p>School is closed tomorrow link</p> 2016-03-07
2) Return HTML Code (unescaped | real tags)
div !{notification.content} !{notification.publishedDate}
// Return | unescaped (2 lines)
// School is closed tomorrow link
// 2016-03-07
3) Return HTML Code Inline (unescaped too)
You need modify the array entries. See the var "test" above (on Declaring Variables):
div: p !{test.content} !{test.publishedDate}
// Return | unescaped (1 line)
// The message link 2016-03-07
If works for you, try apply your own variables and rules.
I'm learning about Jade. I hope to help you.
- Any doubts comment here or talk with me #devromulobastos

Related

JScript Escape an ampersand in payload data for URL

I am attempting to launch a view using the following JS function:
$('#filterTop').click(function () {
var filterValue = $('#filterValueTop').val();
refreshView(`#Url.Action(Model.Action, Model.Controller)?pageSize=#Model.PageSize&pageNumber=#Model.PageNumber&sortDesc=#Model.SortDescending&filterType=#Model.FilterType&filterValue=${filterValue}&showAll=#Model.ShowAll` + `#Model.Payload`, '#Model.ResultView');
});
It worked great until I needed to append a static payload to the end of the URL. The relevant part is line 3 at the end:
&showAll=#Model.ShowAll` + `#Model.Payload`
I am assigning #Model.Payload a value of:
opts.Payload = "&batchID=" + batchID;
or "&batchID=25". The resulting URL is:
https://localhost:44303/Employee/Repaginate?pageSize=20&pageNumber=1&sortDesc=True&filterType=Name&filterValue=Jes&showAll=False&batchID=25
For some reason, it's translating the "&" to "&a.m.p;" (with no periods) which isn't a valid URL. I've tried various methods of escaping the character like using "%26", "/&", and several other garden varieties but alas, my attempts have been in vain. Any suggestions on what I am doing wrong?

How to read the value of an span element with Puppeteer

I am trying to do some web scraping reading some lines inside a html page. I need to look for a text which is repeated through the page inside some <span> elements. In the following example I would like to end with an array of strings with ['Text number 1','Text number 2','Text number 3']
<html>
...
<span>Text number 1</span>
...
<span>Text number 2</span>
...
<span>Text number 3</span>
...
</html>
I have the following code
sElements = ' ... span'; // I declare the selector.
cs = await page.$$(sElements); // I get an array of ElementHandle
The selector is working as in Google Chrome developer tools it captures exactly the 3 elements I am looking for. Also the cs variable is filled with an array of three elements. But then I am trying
for(c in cs)
console.log(c.innerText);
But undefined is logged. I have tried with .text .value .innerText .innerHTML .textContent ... I do not know what I am missing as I think this is really simple
I have also tried this with the same undefined result.
cs = await page.$$eval(sElements, e => e.innerHTML);
Here is an example that would get the innerText of the last span element.
let spanElement;
spanElement = await this.page.$$('span');
spanElement = spanElement.pop();
spanElement = await spanElement.getProperty('innerText');
spanElement = await spanElement.jsonValue();
If you still are unable to get any text then ensure the selector is correct and that the span elements have an innerText defined (not outerText). You can run $(selector) in Chrome console to check.

Generate hyperlink in Sharepoint

I have to generate an hyperlink in sharepoint based on sql table like this:
+----+----------------------------+
| ID | path |
+----+----------------------------+
| 1 | file://test/9932323.pdf |
+----+----------------------------+
| 2 | file://test/1653156423.pdf |
+----+----------------------------+
Actually there is this code to generate html link:
<asp:Label runat="server" id="ff1{$ID}" text="{$thisNode/#PATH}" />
I cannot modify SQL table (dinamically generated) but I have to substitute:
/test/ with /test.abc.local/
and
displayed text with filename only ("path" field substring after last '/')
How can I to that without creating new view or calculated fields?
I tried with:
<a href="{REPLACE($thisNode/#PATH),12,1,'.abc.local/')}"><asp:L ...
but with no success. (I'm really newbie in Sharepoint)
thanks
I'm not going to remove the previous answer because it still valid and it is pretty handy if somebody comes across the same issue, so the RegEx() expression you will have something like this:
/(file\:\/\/)/g
Using the expression above you can find the string you want, so the example below gives you all the LABELS on a page, and from there you can use the following:
.replace( new RegExp("/(file\:\/\/test)","gm"),"/test.abc.local")
Using the expression above you can find the string you want, so the example below gives you all the LABELS on a page, and from there you can use the following:
$('input[type=text]').each(
function(index)
{
console.log(' text: ' + $(this).val() + ' replace: ' + $(this).val().replace( new RegExp("(file\:\/\/test)","gm"),"/test.abc.local") );
}
);
If I understood your question correctly, I would strongly suggest you to use RegEx(), it is possibly the most handy thing ever created to handle string find/replacement.
You can put a JavaScript function to perform the RegEx() substitution on the page inside of a <script language="javascript"></script> , then on your <asp:label> you will replace the output for calling this function by passing the string #thisNode/#PATH to the JavaScript function you wrote, which will find and replace the substring for the desired output

Groovy XmlParser / XmlSlurper: node.localText() position?

I have a follow-up question for this question: Groovy XmlSlurper get value of the node without children.
It explains that in order to get the local inner text of a (HTML) node without recursively get the nested text of potential inner child nodes as well, one has to use #localText() instead of #text().
For instance, a slightly enhanced example from the original question:
<html>
<body>
<div>
Text I would like to get1.
extra stuff
Text I would like to get2.
link to example
Text I would like to get3.
</div>
<span>
extra stuff
Text I would like to get2.
link to example
Text I would like to get3.
</span>
</body>
</html>
with the solution applied:
def tagsoupParser = new org.ccil.cowan.tagsoup.Parser()
def slurper = new XmlSlurper(tagsoupParser)
def htmlParsed = slurper.parseText(stringToParse)
println htmlParsed.body.div[0].localText()[0]
would return:
[Text I would like to get1., Text I would like to get2., Text I would like to get3.]
However, when parsing the <span> part in this example
println htmlParsed.body.span[0].localText()
the output is
[Text I would like to get2., Text I would like to get3.]
The problem I am facing now is that it's apparently not possible to pinpoint the location ("between which child nodes") of the texts. I would have expected the second invocation to yield
[, Text I would like to get2., Text I would like to get3.]
This would have made it clear: Position 0 (before child 0) is empty, position 1 (between child 0 and 1) is "Text I would like to get2.", and position 2 (between child 1 and 2) is "Text I would like to get3." But given the API works as it does, there is apparently no way to determine whether the text returned at index 0 is actually positioned at index 0 or at any other index, and the same is true for all the other indices.
I have tried it with both XmlSlurper and XmlParser, yielding the same results.
If I'm not mistaken here, it's as a consequence also impossible to completely recreate an original HTML document using the information from the parser because this "text index" information is lost.
My question is: Is there any way to find out those text positions? An answer requiring me to change the parser would also be acceptable.
UPDATE / SOLUTION:
For further reference, here's Will P's answer, applied to the original code:
def tagsoupParser = new org.ccil.cowan.tagsoup.Parser()
def slurper = new XmlParser(tagsoupParser)
def htmlParsed = slurper.parseText(stringToParse)
println htmlParsed.body.div[0].children().collect {it in String ? it : null}
This yields:
[Text I would like to get1., null, Text I would like to get2., null, Text I would like to get3.]
One has to use XmlParser instead of XmlSlurper with node.children().
I don't know jsoup, and i hope it is not interfering with the solution, but with a pure XmlParser you can get an array of children() which contains the raw string:
html = '''<html>
<body>
<div>
Text I would like to get1.
extra stuff
Text I would like to get2.
link to example
Text I would like to get3.
</div>
<span>
extra stuff
Text I would like to get2.
link to example
Text I would like to get3.
</span>
</body>
</html>'''
def root = new XmlParser().parseText html
root.body.div[0].children().with {
assert get(0).trim() == 'Text I would like to get1.'
assert get(0).getClass() == String
assert get(1).name() == 'a'
assert get(1).getClass() == Node
assert get(2) == '''
Text I would like to get2.
'''
}

Jade variable rendering inside tag specifications

I have a Jade page like so:
table
th Site Name
th Deadline
th Delete Transaction
- if (transactions != null)
each item in transactions
tr
td= item.item_name
td
span(id='countdown' + item.timeout + ')= item.timeout
td
span(style='cursor: pointer;', onclick='deleteTransaction("=item.uniqueId")')= "X"
button(id='confirmButton', onclick='confirm();')Confirm
As you can see in both the span attribute I try to put a local variable in two different ways and it doesn't work. Concerning the first way, I receive a token ILLEGAL error, while the second simply writes in my JavaScript something like deleteTransaction("=item.uniqueId");. I know the answer is something really stupid, but again and again Jade doc (even if it has improved) doesn't help me.
Thanks
To quote the docs:
Suppose we have the user local { id: 12, name: 'tobi' } and we wish to create an anchor tag with href pointing to "/user/12" we could use regular javascript concatenation:
a(href='/user/' + user.id)= user.name
Ergo:
span(id='countdown' + item.timeout)= item.timeout
// ...
span(style='cursor: pointer;', onclick='deleteTransaction("' + item.uniqueId + '")')= "X"
Quoting again:
or we could use jade's interpolation, which I added because everyone using Ruby or CoffeeScript seems to think this is legal js..:
a(href='/user/#{user.id}')= user.name
And so:
span(style='cursor: pointer;', onclick='deleteTransaction("#{item.uniqueId}")')= "X"
As a general tip that you'll use every day of your programming life: balance your quotes. Just like brackets and parentheses, every quotation mark must either open a new quotation or close an already-open quotation (of the same kind, i.e. double-quotes close double-quotes, single-quotes close single-quotes). To borrow your code:
span(id='countdown' + item.timeout + ')= item.timeout
// ^
// |
// What's this guy doing? ---------+
Even though Jade is a templating language and perhaps not a "real" programming language this rule, just as in HTML (also not a programming language), will serve you well.

Resources