emacs-love / weblorg

Static Site Generator for Emacs
https://emacs.love/weblorg
GNU General Public License v3.0
278 stars 20 forks source link

Option to use the filename as slug instead #+TITLE #27

Open semente opened 3 years ago

semente commented 3 years ago

I like the possibility of using a post title as the slug but it would be great to support the filename as source for the "slugify" function as well. Maybe a variable weblog-filename-slugs and :filename-slugs option in routes to switch to filename based slugs works great.

I would to be able to rename a title without worrying about it changing the URL that might have been bookmarked or indexed already, or add a #+SLUG on every file. Also I would to be able to use [[file:path/to/file.org]] to link internal pages so I could browse my Org files in Emacs the same way in a web browser.

clarete commented 3 years ago

I like the idea of adding it as a configuration switch; and the direct translation of the file name in an existing archive makes the point extremely compelling!

clarete commented 3 years ago

Hmm, I think I may have overthought this issue; what if instead of adding a switch, we added just another property to the post? something like file_slug? Which would then contain the output of feeding basename of the file path into slugify. Do you think that would work for you?

semente commented 3 years ago

Yea! this is much better solution!

On Tue, 2 Mar 2021, at 23:54, Lincoln de Sousa wrote:

Hmm, I think I may have overthought this issue; what if instead of adding a switch, we added just another property to the post? something like file_slug? Which would then contain the output of feeding basename of the file path into slugify. Do you think that would work for you?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/emacs-love/weblorg/issues/27#issuecomment-789387894, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAHJJVC3NVN7XACDGZNFK3TBWQFBANCNFSM4X7FBAGQ.

clarete commented 3 years ago

Just added it, try it out! 😄

semente commented 3 years ago

I think this deserve a mention in https://emacs.love/weblorg/api.html#symbol-weblorg-route :)

semente commented 3 years ago

And thank you very much by the way! I'll be testing all the recent changes asap!

semente commented 3 years ago

Hey @clarete, I'm using this now. It works and, with the help of #39, I now can do what I would but I felt it a bit confusing. Look what I got:

(weblorg-route
 ...
 :output ".build/{{ file_slug }}.html"
 :url "/{{ slug }}.html")

I can't use {{ file_slug }} in :url, and in templates {{ post.slug }} should be rewritten to {{ post.file_slug }}. So I'm wondering if we could change this to a different implementation, a slug-format option per route:

(weblorg-route
 ...
 :slug-format FORMAT
 :output ".build/{{ slug }}.html"
 :url "/{{ slug }}.html")

Where FORMAT could be a callback function, which defaults to slugified title, what allow me have a function (lambda (post) (weblorg--slugify (file-name-sans-extension (file-name-nondirectory post.file))). The default value for slug-format could be (weblorg-site :slug-format) or the slugified title if none is set.

With this in mind, every reference to slug, either in templates or (weblorg-route :url), would refer to the return value of the slug format function.

Would be great to support this kind of strings in routes as well: "{{ post.date | slugify }}". A full example:

(weblorg-route
...
:slug-format "{{ post.title | slugify }}"  ;; default
:slug-format "{{ post.date | slugify }}"
:slug-format '(lambda (post) (weblorg--slugify (file-name-sans-extension (file-name-nondirectory post.file)))
 :output ".build/{{ slug }}.html"
 :url "/{{ slug }}.html")

Well, that is what I have in mind but I'm not sure what is the best solution here or if this is too complex to implement.

clarete commented 3 years ago

hi @semente, thank you for the PR removing the file extension; I don't know why I left it in there 😄 hmm, why can't you use file_slug in :url? On the idea around slug-format, I wonder if you have a use case for these other variants. I think the file_slug thing would help with the use case you have with the tree of org files that you want to just publish with the same name but .html. I'm just asking because there's some refactoring I'm planning to do on the way we're piping parsed data from org files to the aggregation function and I'm trying to see how flexible this new thing I'm planning would need to be.

semente commented 3 years ago

hmm, why can't you use file_slug in :url?

Hey @clarete! It doesn't work:

Template Error: parameter :url of route pages: Variable `file_slug' not declared

I wonder if you have a use case for these other variants

I was thinking about allowing URLs like /2021-03-16-slug.html or any other popular format but that could be the case of just having the filename in the format you wish when using file_slug...

Anyway, file_slug works but it is currently confusing/inconsistent. Look how it looks like:

(weblorg-route
 ...
 :output ".build/{{ file_slug }}.html"
 :url "/{{ slug }}.html")

And in templates

{{ post.slug }}

As you can see only :output property needs to have file_slug. And it won't work if you use it in other places, which is actually right for templates once if the site is reusing a theme it will work without the need of modifications everywhere it uses post.slug. But because of that this makes it confusing for the developer because it is not clear how it works.

That is how it works currently: if you use file_slug in :output, it makes all references to slug return the value of file_slug. But that is hard to interpret. In this case an option would be more clear:

(weblorg-route
 ...
 :filename-based-slug t
 :output ".build/{{ slug }}.html"
 :url "/{{ slug }}.html")

But my proposal is to make it more flexible to fit in different use cases:

(weblorg-route
 ...
 :slug-format  my-function
 :output ".build/{{ slug }}.html"
 :url "/{{ slug }}.html")

Also would be great to have a way to set a default slug format for all routes:


(setq weblorg-default-slug-format my-function)
(weblorg-site
 ...
 :slug-format my-function)
clarete commented 3 years ago

yeah, I think adopting a more flexible way of creating the slug might be a better solution. The one thing I still have in mind is the possible connection of this issue with #32. I wanted to try to find a good design for supporting the use case you mentioned, where you're trying to mimic the structure of a file system directory with org mode files that just translate into .html files.

Do you think it makes sense to try to find something in common in these two issues or do you think by implementing these two separate things (this issue and #32) we'd give you that experience? Sorry for all the back and forth and thanks for all the help and feedback 😄

semente commented 3 years ago

hey @clarete, sorry for the delay!

You mean having a function similar to (weblorg-route) that could export a directory, converting orgs to htmls and copying the other files? It works too and looks interesting but not sure.

In any case, #32 still might be interesting for some use cases but I'm actually using rsync to copy other files than ".org" and that is enough for me.

semente commented 3 years ago

This came in my mind:

(weblog-directory
  :name "my-directory"
  :directory "path/to/dir/"
  :recursive nil
  :template "page.html"  ;; the default template, set a different one per file using `#+TEMPLATE:`
  :output ".build/my-directory/"
  :url "/my-directory/"
  :copy-other-files t)
clarete commented 2 years ago

I think the proposal that you have here for a field for configuring the slug is the nicest one, where we can provide default functions for "replacing extension", "slugifying", "slugifying with title" etc. I think it'd be nice to implement this in terms of #32 though, just so we can do this on a per "handler" basis. (Definition of handling is described in #32)

semente commented 2 years ago

I think it'd be nice to implement this in terms of #32 though, just so we can do this on a per "handler" basis. (Definition of handling is described in #32)

I agree