Hello I am generating layout with an external link to Google search result with inserted variables (that look like #SOMEWORDSASTAGS) I am using Jekyll syntax and HTML together, see:
<p class="one">#<u><a href=”https://www.google.com/search?q=#{{ page.shortname }}”>{{ page.shortname }}</a></u> ({{ page.content | number_of_words }})</b></p>
I want Google to search anything with inserted hashtag #SOMEWORDSASTAGS created by #{{ page.shortname }}
As result I am getting the link that includes my_domain in the beginning and therefore don't work correctly.
https://my_domain/%E2%80%9Dhttps://www.google.com/search?q=#SOMEWORDSASTAGS
See sample page here click link on left bottom "GALINSKAYA"
In _includes/footer.html, you're quoting your url with inappropriate characters : ” (charcode 8221), but your are supposed to quote with simple quote ' (charcode 39) or double quote " (charcode 34).
”https://www.google.com/search?q=#{{ page.shortname }}” fail, but
"https://www.google.com/search?q=#{{ page.shortname }}" works.
Related
I'm working with Hugo and have a use case where there are a lot of various video titles that all follow roughly the same considerations:
remove all underscores and replace with dashes
remove all dashes with spaces on either side and replace with a dash with no spaces
remove all special characters (? and !) and replace with nothing
remove all periods and replace with dashes
remove all remaining spaces and replace with dashes
everything lowercase
So far I have the following code written out, and I'm trying to figure out how to streamline it.
{{ $step1_title := replace .title "_" " " }}
{{ $step2_title := replace $step1_title " - " "-" }}
{{ $step3_title := replace $step2_title "?" "" }}
{{ $step4_title := replace $step3_title "." "-" }}
{{ $step5_title := replace $step4_title " " "-" | lower }}
{{ $step5_title }}
It seems like a lot of steps, and doesn't account for both ? and !. I'm assuming replaceRE would be more appropriate for that step, yet it won't recognize those characters when defined with [?]|[!]. Any pointers for making the above more efficient would be much appreciated.
For others looking for an answer - Hugo does actually have a built-in function called urlize that does exactly this!
https://gohugo.io/functions/urlize/
For the curious, there is an opposite function that "humanizes" the string and returns it with separators removed and the first letter capitalized:
https://gohugo.io/functions/humanize/
First of all this is an example of what I'm supposed to do
<p cp-for="x in t;i=index"> {{x}}, {{i}} </p>
I need to parse the mustache syntax separately from HTML text
(Which means the comma must not be parsed as html text).
And this is part of the grammar i wrote:
OPEN_MUSTACHE: '{{' -> pushMode (MUSTACHE_SYNTAX)
mode MUSTACHE_SYNTAX;
//there are other tokens describing the syntax but i didn't write it
CLOSE_MUSTACHE: '}}' -> popMode
So if i want to add another mustache tag like in the example, how can i tell the lexer to look ahead and not pop immediately after it sees MUSTACHE_CLOSE?
P.S
this is my first time asking a question on this website;
I apologize if my question is not clear
Edit: i now understand that i got the grammar syntax wrong,
The comma in {{ x }} , {{ i }} is actually plain html text
So if i want to add another mustache tag like in the example, how can i tell the lexer to look ahead and not pop immediately after it sees MUSTACHE_CLOSE?
For the input {{#check}}, {{/check}}, the lexer should create the following tokens:
{{: open token
#: open tag token
check: name/id token
}}: close token
,: plain HTML token
: plain HTML token
{{: open token
/: close tag token
check: name/id token
}}: close token
So you can just pop back to the default mode when you encounter }} inside your MUSTACHE_SYNTAX mode.
And then in your parser, you do something like this:
parser grammar MustacheParser;
options {
tokenVocab=MustacheLexer;
}
template
: template_contents EOF
;
template_contents
: template_part*
;
template_part
: html
| mustache
;
html
: HTML+
;
mustache
: section
| ...
;
section
: '{{' '#' NAME '}}' template_contents '{{' '/' NAME '}}'
;
(Of course the literal tokens, like '{{', '#' etc., are not allowed inside a parser grammar, it's just pseudo code. Replace them with the tokens from your lexer grammar.)
I am using the jinja2 templating language to create dual language documents. To that end, I have created a macro called select_lang which takes two strings as an argument, the text in the primary language and in the secondary language, and returns it on the format
<text in primary language> / <i><text in secondary language></i>
Sometimes, as input, I want do use a jinja2 variable, and this is where I struggle. Given the following code:
<!DOCTYPE HTML>
{% set bilingual = primary_lang and secondary_lang %}
{% from 'templates/partials/macro_select_lang.j2.html' import select_lang with context %}
<html>
<body>
{{ select_lang('Testo in italiano','Text in English') }}<br>
{{name.upper()}}<br>
{{ select_lang('Ciao, {{name.upper()}}','Hello, {{name.upper()}}') }}
</body>
</html>
I get this output:
Testo in italiano / *Text in English*
JANE DOE
Ciao, {{name.upper()}} / Hello, {{name.upper()}}
but the desired outcome would be that {{name.upper()}} was evaluated before being passed on to the select_lang macro.
I have searched the jinja2 documentation, but I can't find any relevant topic.
Note: one might think that this is a silly macro which could be replaced with some simple html-code. This is true in this example, but in the real application it does a whole lot more, so replacing the macro does not solve the problem; I need to evaluate the expression before passing it on.
In a regular programming language, I would have written something like
{{ select_lang('Ciao, ' + {{name.upper()}},'Hello, ' + {{name.upper()}}) }}
but this does not work and I suppose jinja2 does not offer an operator for string concatenation.
It seems you have too many curly braces!
Try:
{{ select_lang('Ciao, ' + name.upper(),'Hello, ' + name.upper()) }}
As you are already inside a {{...}} statement...
I have an web page which has tooltip set as follows:
title="Tel: {%- recordFields.providerTel || 'N/A' %} Email: {%-recordFields.providerEmail || 'N/A' %}"
The line occupies 142 columns...
Is there a way to break up the title string in the source so that it can span multiple lines?
Something along these lines:
title="Tel: {%- recordFields.providerTel || 'N/A' %} \
Email: {%-recordFields.providerEmail || 'N/A' %}"
In a comment on the question I asked:
You only want it to span multiple lines in the source, right? The actual value shouldn't have newlines (e.g., when used)?
and you said:
Let's say that. In this particular case, I also want a newline in the output, but I'll remove it for clarity.
It's a really fundamental part of the question. :-)
If you do want the newlines, the answer is easy but (to my mind) unsatisfying: Just put them in, literally:
<div title="Tel: {%- recordFields.providerTel || 'N/A' %}
Email: {%-recordFields.providerEmail || 'N/A' %}">...</div>
Live example. Note that it's important not to have leading whitespace on the next line, because that whitespace is part of the attribute value. This is what makes it unsatisfying to me, because having that subsequent line start at column 0 in something that's otherwise indented seems unclean (and some tools will fight with you, trying to indent it).
If you don't want the newlines in the attribute's value, I'm not aware of a way to do it. According to the HTML specification, an attribute's value is "...Attribute values are a mixture of text and character references...", and if we follow that link for "text" it doesn't say anything about putting source-only linebreaks in the value.
Since you seem to be using some kind of templating engine, if it runs server-side then you could of course define a property on the values object to hold the title string:
title="{%- getTitleFor(recordFields) %}"
...but that moves the content out of your HTML source (where content generally belongs) into your server-side language source, so it's not a great alternative.
I'm using Flask and Sqlite.
I take some string, which contains newlines, and store it in the db. At some later point I get it from the db and include it on some page, and the string shows up without newlines. What's with that?
For example if I have
{{ entry.content }}
in my template, and the entry that was stored had content "hello\nhello", it displays "hellohello" on the page.
However if I have
{{ entry.content.replace('\r\n','<br />') }}
or
{{ entry.content.replace('\r\n','
') }}
in my template, it will display "hellohello" or "hello
hello" on the page.
So my impression is that the newline characters just aren't being interpreted and displayed by the browser. What am I doing wrong?
Try {{ entry.content|safe }} so Flask/Jinja doesn't escape your HTML.
(Be careful, though, as any user entered content, including script tags, will be output as-is. If you really want to be cautious and only allow tags you might want to do write your own scrubber: Jinja2 escape all HTML but img, b, etc)