Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Custom permalinks #34

Closed
nogweii opened this issue Mar 17, 2010 · 27 comments
Closed

Request: Custom permalinks #34

nogweii opened this issue Mar 17, 2010 · 27 comments

Comments

@nogweii
Copy link
Contributor

nogweii commented Mar 17, 2010

It seems that sant0sk1 already got it working. You (cloudhead) commented on it already, and you mentioned that go() needs to be patched. He did - sant0sk1/toto@ae4d7a0db960ac33df1200e94d3629ae3247f606 -, so I think it's ready, though, there probably will be a conflict with the newer code base.

@nogweii
Copy link
Contributor Author

nogweii commented Mar 17, 2010

It would also be nice if it could be extended to support any & all metadata found in the YAML head matter, though go would need a serious redux if it were to support that.

(Quick idea: Add an else clause to sant0sk1's go() that matches (.*)?)

@jerodsanto
Copy link

My solution wasn't the DRYest, and I'm pretty sure it won't work any longer since a recent commit added another hardcoded regex that requires year/month/day in the load method.

I'd love to see this feature make it in though, so if somebody else wants to take a crack at it please do!

@cloudhead
Copy link
Owner

Yea I need to look into this.

@nogweii
Copy link
Contributor Author

nogweii commented Apr 2, 2010

Not trying to pressure you or whatever, but my brother did say that the only reason why he doesn't want to use toto is because it uses the /yyyy/mm/dd/xyzq format.

So instead, he chose Posterous, which uses just the post's name in the URL. Bah.

@cloudhead
Copy link
Owner

: |

I could just give the option between /article/ vs /2010/12/12/article - that should mostly work.

But the date is there for a reason: so you can have two posts with the same name, at different dates.

@nogweii
Copy link
Contributor Author

nogweii commented Apr 2, 2010

Exactly! That was my reasoning to him. He still didn't like it. He & I are both stubborn, so meh. Not easy to change his mind. :-)

Regardless, I would like some way to map YAML headers to the path matching expression. (e.g. "/:foo/" relates to "foo: xyz" in 2010-3-30-where-ive-been.txt)

@cloudhead
Copy link
Owner

that's an interesting idea, but it gets hairy when dealing with dates..

@nogweii
Copy link
Contributor Author

nogweii commented Apr 3, 2010

hm, true. Here's an idea:

  • :date matches the generated date (from the config). e.g. "31 March 2010"
  • :year matches the machine year (e.g. 2010)
  • :month, :day - the same as :year

So the default path would be /:year/:month/:day/:title, but it would be also possible to have "/31March2010/where-ive-been/" as the permalink (/:date/:title, with set :date {|date| date.strftime(..)} in config.ru)

@cloudhead
Copy link
Owner

ah so :year :month :day are 'generated', and the rest is from the metadata

@nogweii
Copy link
Contributor Author

nogweii commented Apr 3, 2010

Yes, exactly.

If we were to do this, one issue does arise, what about collisions? What would happen if year were present in the article's YAML?

@cloudhead
Copy link
Owner

I guess it'd overwrite the generated one

@nogweii
Copy link
Contributor Author

nogweii commented Apr 3, 2010

More implicit goodness:

Like with :prefix, should the leading slash be required? This case is a little more fuzzy.

Also, a thought occured to me. If custom permalinks work, could they overlap with :prefix's functionality? What's different between:
set :prefix, "blog"
set :permalink, ":year/:title"
and
set :permalink, "blog/:year/:title"
?

@nogweii
Copy link
Contributor Author

nogweii commented Apr 3, 2010

Getting close now!

http://github.com/evaryont/toto/tree/custom_permalinks

Tests are failing, however. I'm working on it! :-)

@cloudhead
Copy link
Owner

cool. The permalinks are specifically for articles, while the prefix is site-wide.. You could theoretically use both, together, such as:

set :prefix, "blog"
set :permalink, "articles/:title"

resulting in /blog/articles/my-title for an article, and /blog/ for the index

@cloudhead
Copy link
Owner

I'm wondering if :format is not a better name for the permalink option

@nogweii
Copy link
Contributor Author

nogweii commented Apr 4, 2010

Alright, just about done. One last issue.

Do article paths end in /? Should they not end in anything at all? Right now, they do end in / (e.g. "/foo/bar/" vs "/foo/bar")

@benjholla
Copy link

Has this been worked into the main branch yet?

@ixti
Copy link

ixti commented Sep 2, 2011

It's not in the upstream. But it's in @evaryont's master branch.

@nogweii
Copy link
Contributor Author

nogweii commented Sep 2, 2011

Sorry guys, I haven't worked on this in forever. I'll leave my code up for someone else to take a look at, but I don't think it's exactly of the highest quality. :-)

@benjholla
Copy link

@evaryont, did you move the changes into your master branch? When I pull your code it doesn't seem to respond to the :permalink flag. In your blog update you mentioned you had left the custom permalink changes in this branch https://github.com/evaryont/toto/tree/custom_permalinks, but that seems to be removed.

@ixti
Copy link

ixti commented Sep 2, 2011

There's a small problem with the idea of this feature... you have to make sure permalink has all the parts needed to recreate filename. And IMHO results does not worth anything - I mean this is useless overkill. At least that seems to me (Ibelieve somebody believes that categories and tags are also overkill - but I was mssing them ;))

@ixti
Copy link

ixti commented Sep 2, 2011

@benjholla can you describe what exactly do you want to achieve, because I believe tht can be easily done without messing with toto's routings... :)) And do you really need this? :))

@benjholla
Copy link

Sure, if its possible I'd like to just take the date out of the filename itself so that the link results in something like

/blog/article-name
not
/blog/2011/09/01/article-name

I'm fine with the idea that I can't have duplicate article names, and the date information is already included in the YAML of the article itself. In all honesty, I don't "really" need it, but I'd like to be able to update articles and their dates without changing the filename and something about having the date in the url just really bugs me.

@ixti
Copy link

ixti commented Sep 2, 2011

Well, probably you're right (but e.g. I need it and it's constant for me :))

Basically it can be done - the easiest way is to override few things. I don't have free time to prepare a full patch that will simply add required feature without any restrictions (you can see my categories and tags branches in order to get idea of how to implement this feature). Basically the most hard thing for me is to prepare tests. If somebody can prepare toto's tests that will cover this feature - I'll prepare an implementation patch without any problems :))

But if you simply need this feature up and running - you can use this snippet to override some of toto's internals and get what you need (but you will loose /archives/2011 possibilities):

module Toto
  class Article
    def path
      "/#{@config[:prefix]}/articles/#{slug}/}".squeeze('/')
    end
  end

  class Site
    def go route, env = {}, type = :html
      route << self./ if route.empty?
      type, path = type =~ /html|xml|json/ ? type.to_sym : :html, route.join('/')
      context = lambda do |data, page|
        Context.new(data, @config, path, env).render(page, type)
      end

      body, status = if Context.new.respond_to?(:"to_#{type}")
        if route.first == 'articles'
          context[article(route.slice 1..-1), :article]
        elsif respond_to?(path)
          context[send(path, type), path.to_sym]
        elsif (repo = @config[:github][:repos].grep(/#{path}/).first) &&
              !@config[:github][:user].empty?
          context[Repo.new(repo, @config), :repo]
        else
          context[{}, path.to_sym]
        end
      else
        http 400
      end

    rescue Errno::ENOENT => e
      return :body => http(404).first, :type => :html, :status => 404
    else
      return :body => body || "", :type => type, :status => status || 200
    end
  end
end

That's a quick and dirty idea. Even can't guarantee it will work :)) But to use it simply put it into your config.ru right before toto initialization (you can see how I extend my own toto branch in my blog).

@benjholla
Copy link

Thanks for the ideas, it didn't seem to work off the bat. I suppose I will actually need to sit down and figure this one out on my own :) Thanks again.

@ixti
Copy link

ixti commented Sep 2, 2011

Yeah! I assumed it won't work from the first attempt :)) It was some kind of just an idea. To make it real working I believe there should be more than one dummy change ;)) But at least today I realize how I could implement this feature - unfortunately I simply have no time for it :((

@nogweii
Copy link
Contributor Author

nogweii commented Sep 2, 2011

Well it should work, it's all just Ruby code. :-) However I stopped coding for awhile, then I went to college so I don't have time at the moment to pick this up.

@benjholla, feel free to use my code if you want. I'll close this issue for now, if you get something running, please make a new ticket. (This one is old enough. :-) )

@nogweii nogweii closed this as completed Sep 2, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants