Hugo - Taking a video title and turning it into a URL - html

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/

Related

Generate external link to Google search result at Jekyll

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.

Referrering to jinja2 variables inside of a string

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...

Escape HTML text

I am writing html files from a stack. This is a bit of a pain because for every line I have to write something like the following if the file contains quotes.
write "<div id=hidden-" & quote & myKanton & quote && "style=" & quote & "display:block;" &quote&&"class=" &quote & "popuptable" &quote& ">" & LF to file tOutputFileCH
Now I have to add a lot of html code again and I'm wondering if there is an easier way to be able to do something like:
write escaped("my html numbers and "txt" with quotes") to file
I do not need variables within the html text.
Often, people use functions like
function q theText
replace "'" with quote in theText
return theText
end q
which can be used as
write q("<div id=hidden-'" & myKanton & "' style='display:block;'" & "class='popuptable'>" & LF) to file tOutputFileCH
You can use a string like in above example but you can also use any container:
get q(myVariable)
put q(it) into field 1
put q(field 1) into field 2
put q(url myUrl) into url myOtherUrl
put q(the cProperty of me) into myVar
-- etc etc etc
You can also use ´ or ` instead of ' if you change the q function.
By the way, I noticed that you don't include hidden- in the quotes. Are you sure that's correct?
HTML allows use of quotes and single quotes, so you can...
put "<div style='border:1px'>" into tHTML
LiveCode's format command allows you to escape double quotes...
put format("my html numbers and \"txt\" with quotes") into tData
It is working now. I put the html lines in a custom stack property and use that as input when writing the file. Works perfectly. It even seems to work without the q function.
write ( the cMapOverlay of stack "AfaConverter" ) & LF to file tOutputFileCH
I also tried that because
onmouseover="nhpup.popup($('#hidden-VS').html(), {'width': 400});" href="./kantone/index_kanton_VS.html"
this is trouble with q without adaptions because ' is replaced with " which is a problem.
There are some good answers here. Let me suggest another approach. You could use a quoting function, but in a slightly different way:
function q pString
return quote & pString & quote
end q
Then use the LiveCode merge() function. Merge evaluates any LiveCode expression or variable enclosed in [[ ]] and incorporates it into the enclosing quoted text:
write merge("my html numbers and [[q("txt")]]") to file

Newlines not being interpreted when getting from sqlite db?

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)

Usage: Escape HTML problem

I ran into an interesting problem.
In our webpage a user can write their own description. We escape all text to make it easy to write (<3 shows up properly and isnt the start of a tag). This also avoids any problems with trying to inject their javascript code or hide something or do anything with html.
A side effect is when a user writes
Hi
My name is
shows up as
Hi My name is
Initially we (really i) wrote var desc = (SafeHtml)obj.desc.HtmlEscape.replace("\n", "\n<br>") however this doesnt replace anything because what really happens is \n is replaced as #&10; since all characters < 0x20 (<--i think) needs an escape to be represented in html.
So my question is, am i doing things right? I changed the replace to ("
", "\n<br/>");. Is this the right way? Escape everything and replace characters you deem 'legal'? ATM i cant think of any other characters to escape.
That's how I'd do it - escape everything, and then replace safe escaped sequences. That said, I don't think you need to replace all characters < 0x20 - I'd leave 0x10 (newline) and 0x13 (carriage return) alone in the escaping step, and then replace them by <br />. Doesn't make much difference though.