short URLs for blog posts using s42.ca

An htaccess file is generated that redirects the
short URL to blog posts.
This commit is contained in:
Sami Samhuri 2011-12-10 23:05:04 -08:00
parent 5a0c212d26
commit 12b1f60f39
3 changed files with 26 additions and 0 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ discussd/discuss.dirty
public/blog
public/proj
node_modules
public/s42/.htaccess

View file

@ -29,6 +29,7 @@ publish_blog: blog publish_assets
./bin/publish.sh --delete public/blog
scp public/blog/posts.json bohodev.net:discussd/posts.json
scp discussd/discussd.js bohodev.net:discussd/discussd.js
scp public/s42/.htaccess samhuri.net:s42.ca/.htaccess
ssh bohodev.net restart-discussd.sh
publish_proj: proj publish_assets

View file

@ -11,6 +11,9 @@ require 'rdiscount'
DefaultKeywords = ['sjs', 'sami samhuri', 'sami', 'samhuri', 'samhuri.net', 'blog']
ShortURLCodeSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ShortURLBase = ShortURLCodeSet.length.to_f
def main
srcdir = ARGV.shift.to_s
destdir = ARGV.shift.to_s
@ -49,6 +52,7 @@ class Blag
generate_rss
generate_posts_json
generate_archive
generate_short_urls
copy_assets
end
@ -101,6 +105,17 @@ class Blag
File.open(rss_file, 'w') { |f| f.puts(rss_for_posts.target!) }
end
def generate_short_urls
htaccess = ['RewriteEngine on', 'RewriteRule ^$ http://samhuri.net [R=301,L]']
posts.reverse.each_with_index do |post, i|
code = shorten(i + 1)
htaccess << "RewriteRule ^#{code}$ #{post[:url]} [R=301,L]"
end
File.open(File.join(@dest, 's4,', '.htaccess'), 'w') do |f|
f.puts(htaccess)
end
end
def copy_assets
Dir[File.join(@src, 'css', '*.css')].each do |stylesheet|
`yui-compressor #{stylesheet} #{File.join(@css_dest, File.basename(stylesheet).sub('.css', '.min.css'))}`
@ -223,6 +238,15 @@ class Blag
xml
end
def shorten(n)
short = ''
while n > 0
short = ShortURLCodeSet[n % ShortURLBase, 1] + short
n = (n / ShortURLBase).floor
end
short
end
end
main if $0 == __FILE__