Is there any way to get a substring from a string in Nunjucks templating language? - nunjucks

If we have value 'cats', I want the substring 'at' (need to delete first and last letters). How can we accomplish that in a Nunjucks template?

You can do this using the JavaScript slice method, like so:
{{cats.slice(1, -1)}}

Related

Escape single quote in Xtend template expression

I have a very simple question, but could not figure it out by Google search, please help.
I want to produce this string '\u0000' (note the simple quote marks surrounding it!) using the following simple Xtend method containing a template expression:
def String makeDefaultChar()
{
''''\u0000''''
}
However, this is not accepted as proper syntax (probably because of the four ''''. Is there an escape character for this use case or what is the right syntax?
Thank you in advance!
P.S.
Of course I could use plain Java string like this "'\\u0000'" to achieve the same, but I want to use an Xtend template expression.
My Xtend version is: 2.9.1.v201512180746
There is no "escaping" in template expressions, so you have to use the workaround you mentioned:
'''«"'\\u0000'"»'''
or
'''«"'"»\u0000«"'"»'''
Related discussion: https://groups.google.com/forum/#!topic/xtend-lang/bVZ0nKmQGAI
Single quotes are allowed within Xtend templates as long as they do not occur at the beginning or the end of the template. So a simple workaround is to add an empty expression before/after the single quote:
'''«»'\u0000'«»'''

Handlebars triple stache inside of a double stache

I have registered a handlebars helper by the name of "t" that will translate a string. I have a Handlebars template which uses the value of a variable that will be a word:
{{{word}}}
I would like to translate the value of that variable using my helper, but cannot do
{{t "{{{word}}}" }} or {{t {{{word}}} }}
I am inexperienced with Handlebars and am wondering - What is the simplest way to achieve this?
{{t word}}
Simply pass the reference into your helper without brackets.
You only need brackets for outputting the value of the reference to the page:
Handlebars HTML-escapes values returned by a {{expression}}. If you
don't want Handlebars to escape a value, use the "triple-stash", {{{.
UPDATE:
If you need to perform some kind of operation (such as escaping HTML) on the value of word, you can make a second helper and pass that into your first helper using ( parentheses ):
{{t (myOtherHelper word)}}

How to handle word boundary in FreeMarker when replacing string?

When using FreeMarker, I want to replace some words in the template, but replace function does not handle word boundary, so my output is messed. Is it possible handle word boundary in FreeMarker? Thanks!
Edit:
The word boundary question is solved, but I have another question about backreference.
I just found I should use the third optional flag 'r' to tell FreeMarker that I am using regular expression. For my purpose, I use something like this:
block?replace("\\b${arg}\\b", "__${arg}", "r")
Note we must use \\b for the word boundary matching.

Ruby Regular Expressions: Matching if substring doesn't exist

I'm having an issue trying to capture a group on a string:
"type=gist\nYou need to gist this though\nbecause its awesome\nright now\n</code></p>\n\n<script src=\"https://gist.github.com/3931634.js\"> </script>\n\n\n<p><code>Not code</code></p>\n"
My regex currently looks like this:
/<code>([\s\S]*)<\/code>/
My goal is to get everything in between the code brackets. Unfortunately, it's matching up to the 2nd closing code bracket Is there a way to match everything inside the code brackets up until the first occurrence of ending code bracket?
All repetition quantifiers in regular expressions are greedy by default (matching as many characters as possible). Make the * ungreedy, like this:
/<code>([\s\S]*?)<\/code>/
But please consider using a DOM parser instead. Regex is just not the right tool to parse HTML.
And I just learned that for going through multiple parts, the
String.scan( /<code>(.*?)<\/code>/ ){
puts $1
}
is a very nice way of going through all occurences of code - but yes, getting a proper parser is better...

Ruby list of tags to a fluent regex

I want to clean an HTML page of its tags, using Ruby.
I have the raw HTML, and would like to define a list of tags, e.g. ['span', 'li', 'div'],
and create an array of regular expressions that I could run sequentially, so that I have
clean_text = raw.gsub(first_regex,' ').gsub(second_regex,' ')...
with two regular expressions per tag (start and end).
Do I have a way to do this programmatically (i.e. pre-build the regex array from a tag array and then run them in a fluent pattern)?
EDIT: I realize I actually asked two questions at once - The first about transforming a list of tags to a list of regular expressions, and the second about calling a list of regular expressions as a fluent. Thanks for answering both questions. I will try to make my next questions single-themed.
This should produce a single regexp to remove all your tags.
clean_text = raw.gsub(/<\/?(#{tags.join("|")})>/, '')
However, you have to improve it to support tags with attributes (e.g. <a href="...">), currently only simple tags are removed (e.g. <a>)
Assuming you have a build_regex method to turn a tag into a regex, this should do it:
tags = %w(span div li)
clean_text = tags.inject(raw) {|text, tag| text.gsub build_regex(tag), ' ' }
The inject call passes the result of each substitution into the next iteration of the block, giving the effect of running each gsub on the string one by one.

Resources