让Octopress支持短链接

增加gem到Gemfile中

echo "gem 'dljbz'">>Gemfile

让dlj缓存不提交到git中

echo ".dlj-cache">>.gitignore

新增plugins/dlj_tag.rb文件,内容如下:

```ruby plugins/dlj_tag.rb

-- encoding : utf-8 --

require 'fileutils' require 'digest/md5' require 'dljbz'

DLJ_CACHE_DIR = File.expand_path('../../.dlj-cache', FILE) FileUtils.mkdir_p(DLJ_CACHE_DIR)

module Jekyll class DljTag < Liquid::Tag

def initialize(tag_name, markup, tokens) super end

def render(context) output = super config = context.registers[:site].config url =config['url'] page_url = context.environments.first["page"]["url"] current_url = "#{url}#{page_url}" if defined?(DLJ_CACHE_DIR) path = File.join(DLJ_CACHE_DIR, "dlj-#{Digest::MD5.hexdigest(current_url)}") if File.exist?(path) dlj_url = File.read(path) else dlj_url = Dljbz.shorten(current_url).short_url File.open(path, 'w') {|f| f.print(dlj_url) } end else dlj_url = Dljbz.shorten(current_url).short_url end dlj = "

" dlj += "原文地址:#{dlj_url}" dlj += "
" rescue Exception => e puts "......dlj error: #{e.inspect}" end end

end

Liquid::Template.register_tag('dlj_tag', Jekyll::DljTag)


在source/_includes/article.html最后一行添加标签

{ % dlj_tag % }


bundle install rake preview


底部就会看到

![](/images/uploads/octopress-plugins-short-link-dlj/8AA0332B-EA8F-4D86-8C20-F9405CB3F435.jpg)




* [http://jekyllrb.com/docs/templates/](http://jekyllrb.com/docs/templates/)
* [短链接](http://blog.growcn.com/blog/2014/04/01/short-url-server-for-dlj-dot-bz/)

Comments