Regexp to avoid ZgotmplZ in cite attributes

Hi,
I use to pass markdown cite attributes to blockquotes, links in particular, in captions.
but then the actual html attribute gets messy (ZgotmplZ).
So I want to extract the url from the cite attribute is there is one, ortherwise strip it from html and markdown.

{{ $author := .Attributes.author }}
{{ $cite := .Attributes.cite }}
{{ $cite_html := .Attributes.cite | findRE (https?://)?(www\.)?[^]]+ | string }}
{{ $class := .Attributes.class }}
<blockquote {{if or $cite $cite_html}}cite=“{{if $cite_html}}{{$cite_html|safeURL}}{{else}}{{$cite|markdownify|plainify|safeURL}}{{end}}”{{end}}>{{ .Inner|$.Page.RenderString }}

But it fails immediately, I can’t get the expression. I thought string would change an array of strings into one big string.
And if there is no html, the string would be empty, and the normal cite attribute preferred, purged with |markdownify|plainify|safeURL

Is this in an image render hook?

no, a codeblock, tweaked to produce blockquotes.

_markup/render-codeblock-quote.html

{{ $author := .Attributes.author }}
{{ $cite := .Attributes.cite }}
{{ $cite_html := “”}}
{{ $class := .Attributes.class }}

<figure class=“non-picture {{with $class}}class={{.}}{{end}}”>
<blockquote {{if or $cite $cite_html}}cite=“{{if $cite_html}}{{$cite_html|safeURL}}{{else}}{{$cite|markdownify|plainify|safeURL}}{{end}}”{{end}}>{{ .Inner|$.Page.RenderString }}
{{ if or $author $cite }}<figcaption class=cite_class>{{with $author}}<strong>{{.|markdownify }}{{end}}{{with $cite }}<cite> in {{.|markdownify}}{{end}}{{end}}

This version works, just filtering html.

Using a regex to “avoid ZgotmplZ” is the wrong approach.

markdown

```bq {.foo #bar cite="https://example.org/something/" author="John Doe"}
This is the **inner** content.
```

layouts/_default/_markup/render-codeblock-bq.html

<figure>
  <blockquote
    {{- range $k, $v := .Attributes }}
      {{- if $v }}
        {{- if eq $k "author"}}
          {{- continue }}
        {{- end }}
        {{- printf " %s=%q" $k $v | safeHTMLAttr }}
      {{- end }}
    {{- end -}}
  >
  {{ .Inner | $.Page.RenderString }}
  </blockquote>
  <figcaption>{{ .Attributes.author }}</figcaption>
</figure>

This adds all but the author attributes to the blockquote element, and you don’t have to mess around with a bunch of conditionals. It produces this HTML:

<figure>
  <blockquote cite="https://www.google.com" class="foo" id="bar">
  This is the <strong>inner</strong> content.
  </blockquote>
  <figcaption>John Doe</figcaption>
</figure>

Except I don’t want just this under figcaption tags.
Exemple:

```quote { author="Tessier, 1776" cite="Rollet: La révolution française aurait-elle contribué à avancer l'âge de la puberté des filles ?, 2015 (translated)"}
blablabla
\```

would produce something like:
<blockquote>blablabl
<figcaption><cite><em>Tessier, 1976<br>
in <strong>Rollet: La révolution française aurait-elle contribué à avancer l’âge de la puberté des filles ?, 2015 (translated)</figcaption>
Except no em strong or
would be present just the effect of css.
A complex formatting, what’s in the .attributes.cite will have links that I want under <\cite> but not in the html attribute. Heck, once I tried having a detail element under <cite> but that became nightmarish… ahahah, funny. I’ll succeed one day.

So to use your exemple, what do you do if you want “John Doe” to be a link people can click on ? Usually, I have the author of my quotes but the source is a book or article with a title, and an URL.

Then change the HTML. The point of the example is to indicate that the attribute values are safe:

safeHTMLAttr

Not in my case. What sequence of safeURL, safeHTMLAttr, markdownify and plainify would make sure to remove that pesky ZgotmplZ ? Because I’m still getting it.
{{if or $cite $cite_html}}cite=“{{if $cite_html}}{{$cite_html|safeURL}}{{else}}{{$cite|markdownify|plainify|safeHTMLAttr}}{{end}}”{{end}} doesn’t do the trick.

{{ printf "href=%q" .URL | safeHTMLAttr }} is long-winded, why doesn’t href={{.URL|safeHTMLAttr}} suffices ?

I have given you an example that works.

Are you sure you don’t want to pay someone to do this for you? You could post a bounty in the services section of this forum.

2 Likes

You provided a code that doesn’t do what I wished for at all. No problem, but I’ll find a way. And I’ll never pay for something I can do myself with a bit of time. Ever.

If {{ printf "href=%q" .URL | safeHTMLAttr }}> is the only way to avoid ZgotmplZ directly, no problem, I’m making a regexp. Or is there a parameter to inhibit this security check ?

For those interested in doing like me, I’m close to finish the expression to extract any url it finds in the cite markdown attribute, to remove markups and tags for the actual html attribute.

{{ $CiteURL := index (findRE (?:(?:https?://(?:www\.)?)|(?:www\.))[^(?:\\|\]|\)]+ (findRE (?s)(\!?\[.*]\((?:(?:https?://(?:www\.)?)|(?:www\.))[^(?:\\|\]|\)]+\)|:\s(?:(?:https?://(?:www\.)?)|(?:www\.)).*) .Attributes.cite 1) 1) 0 }}
The constituents parts all work, it looks for things like:
![A](B) [A](B) and `B: URL’ and extract the URL.
Ah, I haven’t made room for links with .Text in them… But I wouldn’t put that in cite=“” anyway.