diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-03-08 16:57:28 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-03-08 16:57:28 -0500 |
commit | 575e4c49131e7699fc23fca78ec14b400680839a (patch) | |
tree | 7f3417c36b05f306f6014f8b9eff55b86c8515a0 /src | |
parent | 46418b2237f86dc7b41c8c2a3dd5725634dceffe (diff) | |
download | old-guff-575e4c49131e7699fc23fca78ec14b400680839a.tar.bz2 old-guff-575e4c49131e7699fc23fca78ec14b400680839a.zip |
clean up blog handler
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/blog-handler.cr | 147 |
1 files changed, 95 insertions, 52 deletions
diff --git a/src/guff/blog-handler.cr b/src/guff/blog-handler.cr index 577ce70..988eab5 100644 --- a/src/guff/blog-handler.cr +++ b/src/guff/blog-handler.cr @@ -2,61 +2,104 @@ require "./handler" module Guff class BlogHandler < Handler - MATCHES = [%r{ - ^/ - - # match YYYY/MM/DD/SLUG.html - (?<year>\d{4}) - / - (?<month>\d{2}) - / - (?<day>\d{2}) - / - (?<slug>[a-z0-9._-]+) - \.html - - $ - }x, %r{ - ^/ - - # match YYYY/MM/DD - (?<year>\d{4}) - / - (?<month>\d{2}) - / - (?<day>\d{2}) - /? - - $ - }x, %r{ - ^/ - - # match YYYY/MM - (?<year>\d{4}) - / - (?<month>\d{2}) - /? - - $ - }x, %r{ - ^/ - - # match YYYY - (?<year>\d{4}) - /? - - $ - }x, %r{ - # match index - ^/$ - }x] + ROUTES = [{ + list: false, + blog: true, + re: %r{ + ^/ + + # match YYYY/MM/DD/SLUG.html + (?<year>\d{4}) + / + (?<month>\d{2}) + / + (?<day>\d{2}) + / + (?<slug>[a-z0-9._-]+) + \.html + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + ^/ + + # match YYYY/MM/DD + (?<year>\d{4}) + / + (?<month>\d{2}) + / + (?<day>\d{2}) + /? + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + ^/ + + # match YYYY/MM + (?<year>\d{4}) + / + (?<month>\d{2}) + /? + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + ^/ + + # match YYYY + (?<year>\d{4}) + /? + + $ + }x, + }, { + list: false, + blog: false, + re: %r{ + ^/ + + # match slug + (?<slug>[a-z0-9._-]+) + \.html + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + # match index + ^/$ + }x, + }] def call(context : HTTP::Server::Context) path = context.request.path || "" - if md = MATCHES.reduce(nil) { |r, m| r || m.match(path) } - context.response.puts "blog match: %s" % [md.to_s] - else - call_next(context) + + call_next(context) unless ROUTES.reduce(false) do |matched, route| + unless matched + if md = (route[:re] as Regex).match(path) + # matched route + matched = true + + context.response.puts "blog: route = %s, md = %s" % [ + route.to_s, + md.to_s + ] + end + end + + matched end end end |