String Slugs - Rib

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Joel McCracken

I figure it could be useful in general though

Joel McCracken

i can submit a PR if you can give any guidance to where you think it should go, etc

Joel McCracken

I wasnt going to bother with adding a newtype Slug = Slug Text but if you think I should then thats fine

Sridhar Ratnakumar

Where would you use the slug type in rib?

Joel McCracken

so, on my glossary page, I am using it to convert a name e.g. "Category Theory" into "category-theory". Its really just a generally useful thing to have. It might also be handy if you had some common source from which you wanted to generate a bunch of individual html pages. Like lets say you had a source with a bunch of e.g. "Diet and Exercise 12-12-2019" this helps you convert it to "diet-and-exercise-12-12-2019"

Sridhar Ratnakumar

I don't think I really understand how this would fit in with existing Rib API. Perhaps if you can show me code example ....

Presently, Path Rel File is used in Source to denote any slug. I think that's reasonable given that these slugs map to file on the disk anyway.

Sridhar Ratnakumar

Also, more generally speaking, a good approach to figure out how to add functionality to RIb would be to add the said functionality to your own static site first, and then see if it can be extracted in general fashion to be included in Rib for use with other static sites.

Joel McCracken

sure; i implemented this last night, it wasn't too hard

Sridhar Ratnakumar

Turns out I actually found some use for this Slug site in my static site today. Here's another point of integration: https://github.com/srid/rib/issues/77#issuecomment-571402658

I was hoping that the directory structure of the source area would be replicated in the target, that is, a/some/path/index.md would end up at b/some/path/index.html. Is there a way to get this to w...
Sridhar Ratnakumar

@Joel McCracken I think a newtype Slug = Slug { unSlug :: Path Rel File } would definitely be a good thing. Feel free to open a PR for it. Then we extend rib's buildHtmlMulti (for Jonathan's case) to take in a Path Rel File -> Slug function (default being foo.md -> foo.html) that allows the user to customize it to their needs.

Sridhar Ratnakumar

The Slug type would basically be used by the _source_url field of the Source record.

Sridhar Ratnakumar

And we can also have,

data SlugBuilder
  = SlugBuilder_Simple --- Just replaces extension
   | SlugBuilder_Clean -- No .html suffix
   | SlugBuilder_Custom (FilePath -> FilePath)

mkSlug :: FilePath -> SlugBuilder -> Slug
mkSlug path = Slug . \case
  SlugBuilder_Simple -> replaceExtension ".html" path
  SlugBuilder_Clean -> T.replace ".md" "/index.html" path
  SlugBuilder_Custom f -> f path

And then have buildHtmlMulti take in the SlugBuilder as an argument.

That's one approach I can think of. Wonder if it can be simplified ....

Sridhar Ratnakumar

One thing I don't like about the API is that these functions tend to take too many arguments.

Sridhar Ratnakumar

actually, just do: type SlugBuilder = FilePath -> FilePath and be done with it. Then we only need the defaults:

buildSimpleSlug :: SlugBuilder
buildSimpleSlug = replaceExtension ".html"
-- etc...