From 75cdb8eba2f3d7bfecf2f6829534dbd73a8c46ae Mon Sep 17 00:00:00 2001 From: liuzihua Date: Sun, 10 Apr 2011 04:08:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0=E5=88=97=E8=A1=A8/=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/post.rb | 96 --------------- main.rb | 128 ++++++-------------- public/main.css | 295 --------------------------------------------- views/about.erb | 4 - views/archive.erb | 9 -- views/edit.erb | 11 -- views/index.erb | 70 ++--------- views/layout.erb | 10 +- views/menu.erb | 14 +-- views/post.erb | 15 --- views/projects.erb | 3 - views/tagged.erb | 14 --- 12 files changed, 49 insertions(+), 620 deletions(-) delete mode 100644 lib/post.rb delete mode 100644 views/about.erb delete mode 100644 views/archive.erb delete mode 100644 views/edit.erb delete mode 100644 views/post.erb delete mode 100644 views/projects.erb delete mode 100644 views/tagged.erb diff --git a/lib/post.rb b/lib/post.rb deleted file mode 100644 index b2fd0db..0000000 --- a/lib/post.rb +++ /dev/null @@ -1,96 +0,0 @@ -require File.dirname(__FILE__) + '/../vendor/maruku/maruku' - -$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/syntax' -require 'syntax/convertors/html' - -class Post < Sequel::Model - unless table_exists? - set_schema do - primary_key :id - text :title - text :body - text :slug - text :tags - timestamp :created_at - end - create_table - end - - def url - d = created_at - "/past/#{d.year}/#{d.month}/#{d.day}/#{slug}/" - end - - def full_url - Blog.url_base.gsub(/\/$/, '') + url - end - - def body_html - to_html(body) - end - - def summary - @summary ||= body.match(/(.{200}.*?\n)/m) - @summary || body - end - - def summary_html - to_html(summary.to_s) - end - - def more? - @more ||= body.match(/.{200}.*?\n(.*)/m) - @more - end - - def linked_tags - tags.split.inject([]) do |accum, tag| - accum << "#{tag}" - end.join(" ") - end - - def self.make_slug(title) - #title.downcase.gsub(/ /, '_').gsub(/[^a-z0-9_]/, '').squeeze('_') - title.gsub(/ /, '_').gsub(/[^a-z0-9_]/, '').squeeze('_') - end - - ######## - - def to_html(markdown) - out = [] - noncode = [] - code_block = nil - markdown.split("\n").each do |line| - if !code_block and line.strip.downcase == '' - out << Maruku.new(noncode.join("\n")).to_html - noncode = [] - code_block = [] - elsif code_block and line.strip.downcase == '' - convertor = Syntax::Convertors::HTML.for_syntax "ruby" - highlighted = convertor.convert(code_block.join("\n")) - out << "#{highlighted}" - code_block = nil - elsif code_block - code_block << line - else - noncode << line - end - end - out << Maruku.new(noncode.join("\n")).to_html - out.join("\n") - end - - def split_content(string) - parts = string.gsub(/\r/, '').split("\n\n") - show = [] - hide = [] - parts.each do |part| - if show.join.length < 100 - show << part - else - hide << part - end - end - [ to_html(show.join("\n\n")), hide.size > 0 ] - end -end diff --git a/main.rb b/main.rb index d317fcd..36972a3 100644 --- a/main.rb +++ b/main.rb @@ -4,21 +4,16 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/vendor/sequel' require 'sequel' +$LOAD_PATH.unshift File.dirname(__FILE__) +require 'setting' + configure do - Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://blog.db') - - require 'ostruct' - Blog = OpenStruct.new( - :title => '简单笔记', - :author => 'liuzihua', - :url_base => 'http://localhost:4567/', - :admin_password => 'change me', - :admin_cookie_key => 'scanty_admin', - :admin_cookie_value => '51d6d976913ace58', - :disqus_shortname => 'liuzihua' - ) + Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://note.db') end +$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib') +require 'note' + error do e = request.env['sinatra.error'] puts e.to_s @@ -26,114 +21,63 @@ "Application error" end -$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib') -require 'post' +error 403 do + 'Access forbidden' +end helpers do - def partial(template, locals = {}) erb(template, :layout => false, :locals => locals) end def admin? - request.cookies[Blog.admin_cookie_key] == Blog.admin_cookie_value + request.cookies[Setting.admin_cookie_key] == Setting.admin_cookie_value end def auth - stop [ 401, 'Not authorized' ] unless admin? + unless admin? + status 401 + body 'Not authorized' + end end - end layout 'layout' -### Public - get '/' do - posts = Post.reverse_order(:created_at).limit(10) - erb :index, :locals => { :posts => posts }, :layout => false -end - -get '/past/:year/:month/:day/:slug/' do - post = Post.filter(:slug => params[:slug]).first - stop [ 404, "Page not found" ] unless post - @title = post.title - erb :post, :locals => { :post => post } -end - -get '/past/:year/:month/:day/:slug' do - redirect "/past/#{params[:year]}/#{params[:month]}/#{params[:day]}/#{params[:slug]}/", 301 -end - -get '/past' do - posts = Post.reverse_order(:created_at) - @title = "Archive" - erb :archive, :locals => { :posts => posts } -end - -get '/past/tags/:tag' do - tag = params[:tag] - posts = Post.filter(:tags.like("%#{tag}%")).reverse_order(:created_at).limit(30) - @title = "Posts tagged #{tag}" - erb :tagged, :locals => { :posts => posts, :tag => tag } + redirect '/notes' end -get '/feed' do - @posts = Post.reverse_order(:created_at).limit(20) - content_type 'application/atom+xml', :charset => 'utf-8' - builder :feed -end - -get '/about' do - erb :about -end - -get '/projects' do - erb :projects -end - -get '/rss' do - redirect '/feed', 301 -end - -### Admin - get '/auth' do erb :auth end post '/auth' do - response.set_cookie(Blog.admin_cookie_key, Blog.admin_cookie_value) if params[:password] == Blog.admin_password - redirect '/' -end - -get '/posts/new' do - auth - erb :edit, :locals => { :post => Post.new, :url => '/posts' } -end - -post '/posts' do - auth - post = Post.new :title => params[:title], :tags => params[:tags], :body => params[:body], :created_at => Time.now, :slug => Post.make_slug(params[:title]) - post.save - redirect post.url + if params[:password] == Setting.admin_password + response.set_cookie(Setting.admin_cookie_key, Setting.admin_cookie_value) + redirect '/' + end + redirect '/auth' end -get '/past/:year/:month/:day/:slug/edit' do +get '/notes/new' do auth - post = Post.filter(:slug => params[:slug]).first - stop [ 404, "Page not found" ] unless post - erb :edit, :locals => { :post => post, :url => post.url } + erb :edit, :locals => { :note => Note.new, :url => '/notes' } end -post '/past/:year/:month/:day/:slug/' do +post '/notes' do auth - post = Post.filter(:slug => params[:slug]).first - stop [ 404, "Page not found" ] unless post - post.title = params[:title] - post.tags = params[:tags] - post.body = params[:body] - post.save - redirect post.url + note = Note.new params + note.created_at = Time.now + note.save + redirect "/notes" +end + +get '/notes' do + auth + notes = Note.filter(:body.like("%#{params[:keyword]}%")) + .order(:created_at.desc, :id.desc) + .limit(20) + erb :index, :locals => { :notes => notes }, :layout => false end diff --git a/public/main.css b/public/main.css index 257f5d3..e69de29 100644 --- a/public/main.css +++ b/public/main.css @@ -1,295 +0,0 @@ -body { - margin: 0px; - padding: 20px; - margin-left: 50px; - background: #0A0A0D; - text-align: justify; - font: 14px "Trebuchet MS", Arial, Helvetica, sans-serif; - color: #FFFFDC; -} - -h1, h2, h3 { - font: 1.82em; - font-weight: normal; - font-family: Arial, Helvetica, sans-serif; - color: #FFFFFF; - text-align: left; -} - -p, ol, ul { - line-height: 1.67em; -} - -ul { - padding-left: 1.2em; -} - -a { - color: #828170; -} - -a:hover { - text-decoration: none; -} - -hr { - display: none; -} - -/* Logo */ - -#logo h1 { - margin: 0; - height: 160px; - padding: 40px 0 0 0px; - text-transform: lowercase; - letter-spacing: -2px; - font-size: 4em; - font-weight: normal; - color: #FFFFFF; -} - -#logo h1 a { - display: block; - text-decoration: none; - color: #FFFFFF; -} - -#logo p { - margin: -103px 0 0 2px; - text-transform: uppercase; - font-family: Tahoma, Arial, Helvetica, sans-serif; - font-weight: bold; -} - -#logo a { - display: block; - text-decoration: none; - color: #FFFFFF; -} - -#logo2 h1 { - margin: 0; - height: 130px; - padding: 40px 0 0 0px; - text-transform: lowercase; - letter-spacing: -2px; - font-size: 2em; - font-weight: normal; - color: #FFFFFF; -} - -#logo2 h1 a { - display: block; - text-decoration: none; - color: #FFFFFF; -} - -#logo2 p { - margin: -103px 0 0 2px; - text-transform: uppercase; - font-family: Tahoma, Arial, Helvetica, sans-serif; - font-weight: bold; -} - -#logo2 a { - display: block; - text-decoration: none; - color: #FFFFFF; -} - -#menu { - margin-top: 18px; -} - -/* Content */ - -#content { - margin: 0px auto; - padding: 20px 0 0 0; - float: left; - width: 50em; - margin-bottom: 10px; -} - -/* Post */ - -.post { - margin-top: 20px; -} - -.post .date { - float: left; - width: 29px; - height: 32px; - margin: 0; - margin-top: 3px; - margin-right: 20px; - padding-top: 2px; - text-transform: uppercase; - text-align: center; - font-size: 10px; - font-weight: bold; - color: #999999; -} - -.post .date b { - margin: 0; - padding: 0; - display: block; - margin-top: -5px; - font-size: 14px; - color: #5C5C5C; - -} - -.post .title { - margin: 0; - padding: 0px 0 0 0; - margin-left: 10px; - padding-left: 10px; - font-size: 1.4em; -} - -.post .title a { - color: white; - text-decoration: none; -} - -.post .meta { - margin: 0 0 10px 20px; - padding: 0; - line-height:normal; - text-transform: uppercase; - color: #979680; -} - -.post .meta a { - color: #828170; -} - -.post .entry { - margin: 0; - padding: 0 0 20px 50px; -} - -#older_posts { - font-size: 120%; -} - -#new_post { - position: absolute; - top: 1em; - right: 1em; -} - -/* Post */ - -.postzoom { - margin-top: 20px; - position: relative; -} - -.postzoom .title { - margin: 0; - padding: 0px 0 0 0; - font-size: 3em; -} - -.postzoom .meta { - margin: 0; - padding: 0; - line-height: normal; - text-transform: uppercase; - color: #979680; -} - -.postzoom .entry { - margin: 0; - padding: 0; - padding-top: 10px; - padding-bottom: 20px; -} - -.postzoom .edit { - text-align: right; - position: absolute; - top: 0; - right: 0; -} - -/* Footer */ - -#footer { - margin: 0 auto; - padding-top: 20px; - padding-bottom: 20px; - font-family: Tahoma, Arial, Helvetica, sans-serif; - color: #FFFFFF; -} - -#legal { - clear: both; - margin: 0; - padding: 10px 0; - font-size: 10px; - color: #525252; -} - -/* Archive */ -.archive .date { - font-size: 85%; - color: #828170; -} - -.archive .title a { - font-size: 120%; - text-decoration: none; - color: #FFFFDC; -} - -/* Code blocks */ - -pre { - font-family: Monaco, monospace; - color: #ccc; - background: #000; - border-left: 6px solid #222; - padding: 0.4em; - padding-left: 0.8em; -} - -pre span.attribute { color: #009900; } -pre span.char { color: #F00; } -pre span.class { color: #A020F0; font-weight: bold; } -pre span.comment { color: #0000FF; } -pre span.constant { color: #008B8B; } -pre span.escape { color: #6A5ACD; } -pre span.expr { color: #2222CC; } -pre span.global { color: #11AA44; } -pre span.ident { color: #CCCCCC; } -pre span.keyword { color: #A52A2A; font-weight: bold; } -pre span.method { color: #008B8B; } -pre span.module { color: #A020F0; font-weight: bold; } -pre span.number { color: #DD00DD; } -pre span.punct { color: #6A5ACD; } -pre span.regex { color: #DD00DD; } -pre span.string { color: #DD00DD; } -pre span.symbol { color: #008B8B; } - -/* Edit post */ - -form[name=edit] input[name=title] { - font-size: 100%; - width: 300pt; -} -form[name=edit] input[name=tags] { - font-size: 100%; - width: 300pt; -} -form[name=edit] textarea { - font-size: 100%; - width: 50em; - height: 25em; -} - diff --git a/views/about.erb b/views/about.erb deleted file mode 100644 index 9301064..0000000 --- a/views/about.erb +++ /dev/null @@ -1,4 +0,0 @@ -

about

- -程序员
-喜欢足球,编程 diff --git a/views/archive.erb b/views/archive.erb deleted file mode 100644 index 6d06c2a..0000000 --- a/views/archive.erb +++ /dev/null @@ -1,9 +0,0 @@ -

Archive

- -<% posts.each do |post| %> - - - - -<% end %> -
<%= post.created_at.strftime("%b %d %Y") %><%= post.title %>
diff --git a/views/edit.erb b/views/edit.erb deleted file mode 100644 index b1686c3..0000000 --- a/views/edit.erb +++ /dev/null @@ -1,11 +0,0 @@ -
-
-

-

-

<%= post.created_at %>

-
- -
-
-
-
diff --git a/views/index.erb b/views/index.erb index e9e6d41..f033abf 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,73 +1,19 @@ - - - - <%= Blog.title %> - - - - -