How do I add carriage return to possible whitespace values in nearley? - nearley

I'm using nearley but it's failing when running on some CRLF files. Right now I've solved it by commenting the whitespace builtin import and instead redoing it with \r like this:
# Whitespace: `_` is optional, `__` is mandatory.
_ -> wschar:* {% function(d) {return null;} %}
__ -> wschar:+ {% function(d) {return null;} %}
wschar -> [ \t\n\r\v\f] {% id %}
It would be great to be able to do something like wschar -> [ \t\n\r\v\f] {% id %} and base it from the builtin file but doing so creates an infinite loop.
Thanks, any help is appreciated.

Well it was easier than expected. You can always add more definitions to already existing names so adding this line fixes it.
wschar -> [\r] {% id %}

Related

translation strings in twig inside an object

I'm trying to put translation strings inside an object within Twig. So far I haven't got it right and can't figure out how to properly do it. I didn't think this would work, but it was my best effort so far.
{% set options = {
0 : {{ 'user.first_name'|trans }},
1 : {{ 'user.surname'|trans }},
2 : {{ 'user.initials'|trans }}
} %}
I get the error:
A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{".
Any one any ideas? Thanks in advance.
The syntax {{ ... }} is used to output content. You don't need to interpolate the variable in order to add it to the object
{% set options = {
0 : user.first_name|trans,
1 : user.surname|trans,
2 : user.initials|trans,
} %}
Some extra notes. As you are using numeric indices you could use the following snippet
{% set options = [
user.first_name|trans,
user.surname|trans,
user.initials|trans,
] %}
demo

how to only show number with truncate or css

I want to display S1 only instead of season 1. Thus I need to truncate season and put only 1 and add "S" at the front.
<a href="{% url 'season_detail' slug=all_episode.season.slug %}">
{{ all_episode.season}}
</a>
How do I truncate the word "season"?
Edit:
Here's what I did again
I created templatetag folder inside my app
then added init.py and seasonify.py
and inside seasonify.py I added
from django import template
register = template.Library()
#register.filter
def seasonify(value):
return value.replace('season', 'S')
then inside my template
I added
{% load seasonify %}
and {% episode.season|seasonify %}
Your best bet is to write a custom template filter. The logic is simple:
#register.filter
def seasonify(value):
return value.replace('season', 'S')
then simply use it in your templates:
{{ all_episode.season | seasonify }}
See Django docs for details on where to put this code.

Remove the '\' from a string in twig

I have this text :
Ch d\`Arnb
Now I want to remove all \ from string if exist. I try a lot of solutions, one of this : {{ item|replace({'\/': ''}) }}. But without success. What can I try next?
There is an error in the replace parameter. You are presently trying to remove the / char. Try this:
{% set item = 'Foo\Bar' %}
{{ item|replace({'\\': ''}) }}
Output: FooBar

How to highlight active item in custom taglist on shopify collection page

I've added custom tags to my collection pages with help from this support forum: https://ecommerce.shopify.com/c/ecommerce-design/t/change-order-in-which-tags-are-displayed-on-collection-page-179468
The tags that appear on the collection pages are defined by linklists in the navigation. There is more about this in the discussion forum I posted above.
The snippet of code I inserted into collection-template.liquid just below the header is:
{% capture collection_taglist %}{{ collection.handle | append: "-tags" }}{% endcapture %}
{% if linklists[collection_taglist] %}
<ul class="tags tags--collection inline-list">
{% for link in linklists[collection_taglist].links %}
<li>{{ link.title | link_to: link.url }}</li>
{% endfor %}
</ul>
{% endif %}
Everything is working as intended, however I cannot get the active tag to be highlighted using CSS styling. I also tried some Javascript (see below) but that does not seem to be working either.
jQuery(".tags").find("a[href='"+window .location .href+"']").each(function() {
jQuery(this).addClass("active");
});
See here: http://shopsexologyinstitute.com/collections/women (you can enter using the password 'eaclim')
Any help would be greatly appreciated!
Many thanks :)
I run into this in all sorts of circumstances. Running the following in the console outlines the issue:
jQuery(".tags").find('a').each(function(){ console.log(this.href);});
The host in the urls is not the host of the site.
You'll need to extract the pathname for comparison. There are various ways to do that but run the following in Chrome or Firefox to get the idea. URL is not widely supported so for production you'd need a different way to parse the link urls:
(function(){
var path = location.pathname;
return jQuery(".tags").find('a').filter(function(){
console.log('checking: '+ this.getAttribute('href'));
var href = new URL(this.getAttribute('href'), location.href);
console.log(href.pathname +' vs '+ path);
return href.pathname == path;
});
})();

Defining states depending on existance of a file/directory

How is it possible to get something like the following running:
{% if not exist('/tmp/dummy/') then %}
dummy:
file.touch:
- name: /tmp/dummy/tmp.txt
...
{% endif %}
I need it for installing software from a ZIP-file. I want to unzip on the minions, but there I don't want to have any remnants of licence files, which I only need for installation, left.
You can use unless for this.
dummy:
file.touch:
- name: /tmp/dummy/tmp.txt
- unless: test -d /tmp/dummy/
{% if 1 == salt['cmd.retcode']('test -f /tmp/woo.test') %}
ack:
file.touch:
- name: /tmp/woo.test
{% endif %}
You could use the pure python renderer for salt. Here is how it would look like.
#!py
import os
def run():
# defines the hash config
config = {}
if (not os.path.isfile("/tmp/dummy")):
config["dummy"] = {
"file.touch": [
{'name': '/tmp/dummy'},
],
}
return config

Resources