jgm/doctemplates

Add a variable pipe transform that removes whitespace

Closed this issue · 4 comments

This is a proposal to add a new pipe transform that will remove whitespace from a variable's value. (recommended name: strip)

Example usage:

---
var: My Variable
---

...

$var/strip$ -> MyVariable

My usage for this would be for generating CSS class/id names from variables in an html template (which hold chapter/section names). There are probably many other use cases.

jgm commented

Moving to doctemplates repository, since that's where these pipes are implemented.

Notes:

  • strip usually denotes a function that removes whitespace from the left and right sides (but not the interior).
  • Removing whitespace may not be enough to give you a valid identifier or class name.

I guess we can break this down into two features:

  1. A pipe to remove whitespace from a variable's value
  2. A pipe to transform a variable's value into a valid class or id name

Either would be fine for my very specific use case, but the first is probably more useful in general. The name could be something like nospace instead of strip. As for the second, it's probably too specific to be worth it.

Side note: I thought about ways of making a string be a valid id or class name, and hashing the variable came up. Of course, the resulting string would lose any resemblance of the original string, but hashing could be a more generally useful feature. I'm not sure if that falls within the scope of doctemplates though.

jgm commented

As for the second, it's probably too specific to be worth it.

On the contrary. I can't really think of any use except this for a pipe that removes all spaces. But generally there are requirements for ids that go beyond just removing all spaces.

Stale