Error calling replaceRE: unable to cast []string(nil) of type []string to string

Hello Hugo community.

I am trying to configure my site to be a gopherhole through hugo however i am a little confused on the regex used, both regex have been tested through regex101 and worked within the editor with golang as the language but not in hugo. So my question is what changed the datatype?

I have the following layouts/index.gopher.txt:

{{ $content :=	.RawContent -}}
{{ $content := $content | replaceRE `!\[(.+?)]\((.+?.png|.jpg)\)` "\nI$1\t$2\tlocalhost\t70" -}}
{{ $content := $content | replaceRE `!\[(\(.+?\))]\((.+?.png|.jpg)\)` "\nI$1\t$2\tlocalhost\t70" -}}
{{ $content := $content | replaceRE `!\[(.+?)]\((.+?.gif)\)` "g$1\t$2\tlocalhost\t70" -}}
{{ $content := $content | replaceRE `\[(.+?)]\((.+?.asc)\)` "\n9$1\t$2\tlocalhost\t70\n" -}}
{{ $content := $content | replaceRE `\[(.+?)\]\((.+?)\)` "\nh$1\tURL:$2\tlocalhost\t70\n" -}}
{{ $content := $content | replaceRE `(?m)\x60|[[:space:]]\{.*\}` "" -}}
{{ $content := $content | replaceRE `\\\]` "]" -}}
{{ $content := $content | replaceRE `\\\[` "[" -}}
{{ // other regexes above work fine, just the one below is problematic }}
{{ $content := $content | findRE `^[^0-9Iigh!](.+)([^0]$|[^7]0$|[^[[:blank:]]]70$|[^t]\t70$|[^s]t\t70$|[^o]st\t70$|[^h]ost\t70$|[^l]host\t70$|[^a]lhost\t70$|[^c]alhost	70$|[^o]calhost\t70$|[^l]ocalhost\t70$|[^[[:blank:]]]localhost\t70$)` | replaceRE `(.+?.{68}\b)` "i$1\t/\tlocalhost\t70\n" -}}
{{- range (split $content "\n") }}
{{- . }}
{{ end }}

And the following page content/_index.md:

#about
this is some text about me
[shortened..]

[My ASCII Art](ascii-art.txt)

More text

![javascript free button](images/javascriptfree.gif)
![made with CSS buton](images/css.gif)

my expected result is for the layout to match everything but lines with localhost[TAB]70 at the end (ie anything not currently formatted for gopher which should just be text) and then to insert a hard break at the last space before the 68th charter, and then format it as a information item type (iSome Text here[TAB] \ [TAB] localhost [TAB] 70).

Actual result: executing "index.gopher.txt" at <replaceRE \(.+?.{68}\b)\ "i$1\t/\tlocalhost\t70\n">: error calling replaceRE: unable to cast []string(nil) of type []string to string. It had worked well prior with earlier regexes, i had checked the public/gophermap.txt as i was making each change, and it shows as expected until the final regex

Hugo version: hugo v0.109.0+extended

Additionally, am i doing this completely wrong? Writing a shell script to do the same functionality is also an option if what i’m attempting isn’t possible, is this a bug, or expected behavior?

If I understand what you are trying to do, it seems like it would be a lot easier to use Link and image render hooks specifically for your gopher output format.

All links and images work as expected which i’m surprised about, but it does work, though Link and Image render hooks could make it easier i’d think, i’ll have to look into how to do that.

What i want to do is have all text that isn’t an image or link be put into the right format, limited to the space closet to a 68 charter limit, and hard break the line there. I’m unsure if there are hooks for such a thing though. I’ll take a look at the docs.

OK, I understand now. Your output is plain text, so if the content contains markdown you don’t want it rendered to HTML.

In that case, link and image render hooks won’t do what you want.

That’s correct, yes. Though if there is support for gopher officially that would probably make things easier for me since i could just print the page content like normal.

Ah right, that makes sense.

replaceRE returns a string.

findRE returns an array.

Ah that explains it, so i’d have to pipe from findRE | delimit | replaceRE i would presume?

I haven’t taken the time to understand exactly what you’re doing here, but I would think you’d do something like:

{{ $s := findRE `needle` $haystack }}
{{ range $s }}
  DO SOMETHING WITH EACH ELEMENT OF THE SLICE
{{ end }}

So far i haven’t been able to make it work as expected. though hugo doesn’t give an error now so it’s likely my regex or some other logic error but the text still remains the same. with the following layouts/index.gopher.txt:

// everything else the same as before
{{ $match := $content | findRE `^[^0-9Iigh!](.+)([^0]$|[^7]0$|[^[[:blank:]]]70$|[^t]\t70$|[^s]t\t70$|[^o]st\t70$|[^h]ost\t70$|[^l]host\t70$|[^a]lhost\t70$|[^c]alhost	70$|[^o]calhost\t70$|[^l]ocalhost\t70$|[^[[:blank:]]]localhost\t70$)` -}}

{{- range $match }}
{{- $content := $match | replaceRE `(.+?.{0,68}\b)` "i$1\t/\tlocalhost\t70\n" -}}
{{ end }}
{{ range (split $content "\n") }}
{{- . }}
{{ end }}
{{ range $match }}
  {{ $content := $match | replaceRE ...
{{ end }}

Not sure what you’re trying to do here, but…

  1. You are not using the current element of $match within each iteration
  2. You are initializing $content within each iteration

If you fix both of those it’s still broken, because you will throw away the previous value of $content.

I suggest you come up with a VERY simple example string and VERY simple regex values, prototype the logic until it works, and then try it on something real.

1 Like

Apologies for the delay in reply.

I’m trying to implement inverse lookahead, which go’s regex doesn’t support. So i’m trying to match every line that isn’t a gopher link already (which should be all other text) and then insert a line break at the space closest to 68 charters and format each non link text line (once line broken) in the form i[some text here][TAB]\[TAB]localhost[TAB]70, though i think this would best be left up to the gopher server to handle instead of hugo after thinking about it.