Skip to content

Instantly share code, notes, and snippets.

@arganzheng
Last active December 21, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arganzheng/6238926 to your computer and use it in GitHub Desktop.
Save arganzheng/6238926 to your computer and use it in GitHub Desktop.
sinatra arganzheng

使用Sintra开发文档系统

安装

ruby && gem

对于windows环境,最简单的方式是下载一个installer,双击运行就可以了。

sinatra

本来gem install sinatra就可以了。但是由于办公网需要走代理才能出去,所以需要添加代理。

简单的方式是每次执行通过--http-proxy指定代理:

gem install --http-proxy http://proxy.tencent.com:8080 sinatra

Hello world

# myapp.rb
require 'sinatra'

get '/' do
  'Hello world!'
end

在命令行中执行:

ruby myapp.rb

就可以访问http://localhost:4567了。

Sinatra with Markdown

文档系统使用markdown是比较好的选择。不过我们的文档系统既然有静态的网页,也有动态的内容。sinatra的markdown templates引擎不支持动态内容:

It is not possible to call methods from markdown, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:

erb :overview, :locals => { :text => markdown(:introduction) }

Note that you may also call the markdown method from within other templates:

%h1 Hello From Haml!
%p= markdown(:greetings)

Since you cannot call Ruby from Markdown, you cannot use layouts written in Markdown. However, it is possible to use another rendering engine for the template than for the layout by passing the :layout_engine option.

可以使用erb这样的动态模版引擎来实现到。这篇文章有介绍:Using Markdown with Sinatra

Rack it up

gem install --http-proxy http://proxy.tencent.com:8080 rack

rack-cache

gem install --http-proxy http://proxy.tencent.com:8080 rack-cache
  1. Sinatra with rack-cache on Heroku

thin

C:\Users\arganzheng>gem install --http-proxy http://proxy.tencent.com:8080 thin
Fetching: eventmachine-1.0.3-x86-mingw32.gem (100%)
Fetching: daemons-1.1.9.gem (100%)
Fetching: thin-1.5.1.gem (100%)
ERROR:  Error installing thin:
        The 'thin' native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'

根据提示,到http://github.com/oneclick/rubyinstaller/wiki/Development-Kit安装Development Kit。对于1.93的版本要下载4.5.2的DevKit:

For RubyInstaller versions 1.8.7, 1.9.2, and 1.9.3 use the DevKit 4.5.2 from our main downloads page.

其实是个7Zip文件来的,下载解压放在某个目录下(我是放在C:\Ruby193\DevKit 下)然后安装提示安装就可以了。

安装完成之后再执行就么有问题了:

C:\Users\arganzheng>gem install --http-proxy http://proxy.tencent.com:8080 thin
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed thin-1.5.1
1 gem installed
Installing ri documentation for thin-1.5.1...
Installing RDoc documentation for thin-1.5.1...

然后就可以运行了:

D:\code\Sinatra-Tutorial-Up-And-Running-master>rackup -E production  -s thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop

不过请求还是遇到问题了:

D:\code\Sinatra-Tutorial-Up-And-Running-master>rackup -E production  -s thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
LoadError - cannot load such file -- redcarpet:
        C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:144:in `require_template_library'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/markdown.rb:56:in `initialize_engine'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:56:in `initialize'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt.rb:98:in `new'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt.rb:98:in `block in []'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt.rb:96:in `each'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt.rb:96:in `[]'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.4.3/lib/sinatra/base.rb:817:in `block in compile_template'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt.rb:127:in `fetch'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.4.3/lib/sinatra/base.rb:816:in `compile_template'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.4.3/lib/sinatra/base.rb:797:in `render'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.4.3/lib/sinatra/base.rb:700:in `markdown'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/post.erb:11:in `block in singletonclass'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/post.erb:65531:in `instance_eval'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/post.erb:65531:in `singletonclass'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/post.erb:65529:in `__tilt_12598140'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `call'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `evaluate'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.4.3/lib/sinatra/base.rb:798:in `render'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.4.3/lib/sinatra/base.rb:657:in `erb'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/index.erb:2:in `block (2 levels) in singletonclass'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/index.erb:1:in `each'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/index.erb:1:in `block in singletonclass'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/index.erb:65531:in `instance_eval'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/index.erb:65531:in `singletonclass'
        D:/code/Sinatra-Tutorial-Up-And-Running-master/views/index.erb:65529:in `__tilt_12598140'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `call'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `evaluate'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
        C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.4.3/lib/sinatra/base.rb:798:in `render'

我们解析markdown文件,需要一个相应的库,这里是使用了redcarpet。安装一下就可以了。

C:\Ruby193\DevKit>gem install --http-proxy http://proxy.tencent.com:8080 redcarpet
Fetching: redcarpet-3.0.0.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed redcarpet-3.0.0
1 gem installed
Installing ri documentation for redcarpet-3.0.0...
Installing RDoc documentation for redcarpet-3.0.0...

C:\Ruby193\DevKit>

Rack it up!

编写config.ru文件:

##config.ru

$LOAD_PATH.unshift 'lib'
require 'blog'
if ENV['URL'] and ENV['DATABASE_URL']
  # we're on heroku, no cache needed
  # also, it's a read-only file system
  GithubHook.disable :autopull
elsif Blog.production?
  require 'rack/cache'
  use Rack::Cache
end
run Blog

然后就可以运行了:

D:\code\Sinatra-Tutorial-Up-And-Running-master>rackup -E production -s thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop

运行良好,但是发现一个问题,就是如果文件名带有中文,就会出错:

Encoding::CompatibilityError - incompatible character encodings: GBK and UTF-8:
    D:/code/Sinatra-Tutorial-Up-And-Running-master/views/post.erb:4:in `concat'

提示这一行出错:

<a href="<%= url(article.slug) %>"><%= article.title %></a>
@arganzheng
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment