Keats / kickstart

A scaffolding tool to get new projects up and running quickly
MIT License
354 stars 24 forks source link

Providing a "slugify" functionality #31

Closed Arteneko closed 4 years ago

Arteneko commented 4 years ago

I have a template where I request two related info.

This slug is closely related to the name, and I'd like to be able to have something to slugify the name the user entered, and use that resulting value as template value.

This would introduce a change, with a concept of transformations.

I see a few ways this could be done.

transforms inside the template.toml file

One way would be, for a given variable (one [[variables]]), to be able to provide a new key, whose format would be expected as a key-value object.

[[variables]]
name="project"
default="My super project"
prompt="Name your project!"

[variables.transforms]
project_slug = slugify

The resulting object would look like this.

{
    "variables": [
        {
            "name": "project",
            "default": "My super project",
            "prompt": "Name your project",
            "transforms": {
                "project_slug": "slugify"
            }
        }
    ]
}

Template variable usage filter application

The second proposal would be to allow a pipe syntax in the placeholder keys, which would then be ran, and the resulting string would be used.

$ ls template/
{{project_name|slugify}}

The algorithm here would be pretty simple.

transform(fullkey):
    [key, ...methods] = split(fullkey, |)
    result = value_for(key) # original value
    for method in methods do
        result = method(result)
        assert result.length, "${method} returned an empty value, cannot continue"
    end
    return result

End

Of course, every method is built-in, and documented.

What do you think?

Keats commented 4 years ago

| is not allowed on Windows filenames so the second proposal is not a good solution. We could replace the | by something else for the filenames only but that would be a weird gotcha maybe? I need to think a bit about the first one.

Maybe 1.0 is premature if there are more feature request on the schema, I'll release a <1 version in the meantime and we can discuss improvements.

Keats commented 4 years ago

I'm kind of warming up to allowing both | and something else like $$ if you want it to work on Windows for filenames/directories only.

Arteneko commented 4 years ago

I guess a "less-common" but more compatible option, such as $$ (if compatible with every supported OS) would be better than to allow both, it provides less confusion around what to use, and will allow for maximum compatibility, as to follow the tool's base goal.

Keats commented 4 years ago

The issue is that | is the official token for filters in Tera so unless we strip them somehow they would be supported, even if not officially/mentioned in the docs. All the code handling would do is render(path.replace("$$", "|"))

Keats commented 4 years ago

I think adding $$ (or anything else that works on every OS) as a solution is good in the end. It's pretty niche to need that anyway, cookiecutter still doesn't have it and they do ok. Are you interested in making a patch for that?

Arteneko commented 4 years ago

Sure enough, making a fork for that, right now!