Output text file with fixed line width

I want to create plain text files, to be read from a terminal. Having a line width makes me consistent, and I have no idea how to generate that with a Go template. But I’ve seen folks do some really amazing things here, so I am throwing it out there.

The most basic operation I want is to take .Content (or .Plain/.PlainWords), plainify it, and then hard wrap it at 76 or 80 chars, without breaking words.

Possible with just Hugo templates?

You could try printf:

Note that the link above will not solve your problem, but printf has many options.

1 Like

Challenge accepted:

{{ delimit (findRE "[^\\n]{1,76}[ \\n]+" (.Plain | htmlUnescape)) "\n" }}

The htmlUnescape call is to deal with the smart quotes, converting them from escape sequences to literal UTF-8 characters.


1 Like

Thanks @bep and @jgreely!

I had looked at printf in the golang docs before, and must have skimmed completely over the section on width. I’ve seen this done in other languages, so I will take a stab at it.

And that line looks awesome, @jgreely! I have no idea what it means; I kinda get it, but I want to read it fluently, so I will figure it out and ask if I have questions. :slight_smile:

1 Like