Another bookshelf question: tidying up titles

Some of the book titles include metadata — a real bugbear of mine. For example, The Ashes of London (James Marwood & Cat Lovett, Book 1) or Faithful Place (Dublin Murder Squad, Book 3). In each case the words in brackets are not part of the title and I’d like to be able to omit them. Is there a function in Hugo or Go that will tell me the position of a particular character in a string — in this case “(“? Then I could use substr to include only the characters before that position.

Thanks, I’m grateful for the help.

Are you familiar with regex? If so, I’d use this around the title in your template code.

So, as Jamie Zawinski once notoriously put it, now I have two problems. Just kidding! I think this is a brilliant idea. Thanks for suggesting it.

1 Like

I gave up on the idea of using regular expressions: the implementation in Hugo seems odd, to say the least. Instead, I tried using split to turn my book title string into an array of substrings, delimited by the open round bracket character, “(”. I thought this might work, but it turns every title (whether it contains “(” or not) into "s%!”. I have no idea why it’s doing that, or what it means. Here is the relevant snippet from my template:

{{ range .Site.Data.bookshelves.wanttoread }}
{{ $booktitle := printf "s" .title }}
{{ $bktitleslice := split $booktitle "(" }}
   <p class="bookshelf_generated"><a href="https://micro.blog/books/{{ .isbn }}">
      <img src="{{ .cover_url }}" alt="{{ .title }} cover" class="bshelf" width="100" />
      {{ .author}}, <cite>
        {{ index $bktitleslice 0 }}
</cite></a></p>
{{ end }}

Any suggestions gratefully received. Incidentally, the poetry critic John Leonard calls round brackets “lunulae”, “little moons”. I’d call them that but I’m afraid people wouldn’t know what I mean.

OK, this is utterly bizarre. Instead of using 0 as the index, I tried 3 (blank result), then 2 (I got the words in brackets that I was trying to remove), then 1 (I got the title but preceded by “EXTRA string=” and followed by a closing lunula). I have absolutely zero idea what’s going on or why, but it should be straightforward to replace the extra text with nothing, so I think I’m nearly there.

So, this is the final version of the relevant snippet:

{{ range .Site.Data.bookshelves.wanttoread }}
{{ $booktitle := printf "s" .title }}
{{ $bktitleslice := split $booktitle "(" }}
   <p class="bookshelf_generated"><a href="https://micro.blog/books/{{ .isbn }}">
      <img src="{{ .cover_url }}" alt="{{ .title }} cover" class="bshelf" width="100" />
      {{ .author}}, <cite>
        {{ $cleantitle := index $bktitleslice 1 }}
        {{ $cleantitle := replace $cleantitle "EXTRA string=" "" }}
        {{ replace $cleantitle ")" "" }}
    </cite></a></p>
{{ end }}

I used two replaces in a row rather than substr because not all the titles had a final closing lunula — only those which didn’t have anything between lunulae to start with, oddly enough. I’m sure there’s a more elegant way to do it but this works, which is all I wanted. Thanks to all who read and to Jason for responding.

Interesting. I think you could remove one replace by using the | — might be easier or harder to read, depending on taste, e.g.

{{ $cleantitle := index $bktitleslice 1 | replace “EXTRA string=“ “” | replace “)” “” }}

Thanks, Jason. I’ll try it. I’ve been a little bit wary of using the pipe, so I’m grateful for the example of how it’s supposed to work.

As a side note, on the web in Micro.blog you can edit a book’s title too. It will only edit your version of the book.

1 Like

Ah, I didn’t notice that Edit button! I’ve mainly been using the Epilogue app since I got it. Thanks, Manton.

yup … that’s what I do - KISS

1 Like