多行匹配也很常见,例如截取html源码的时候很有用,实现多行匹配只需要在正则后面加m即可 例如
/http:(.*?)\s?/m
ruby demo:
1
p "ab\r\n334cd".match(/ab(.*)cd/) # nil p "ab\r\n334cd".match(/ab(.*)cd/m) # #<MatchData "ab\r\n334cd" 1:"\r\n334">
2,
str = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><pre>\n<code class=\"ruby\">\nputs \"hello world\"\nputs \"hello world\"\n</code>\n</pre></body></html>\n" p str.match(/<body>(.*)<\/body>/m)[0] # "<body><pre>\n<code class=\"ruby\">\nputs \"hello world\"\nputs \"hello world\"\n</code>\n</pre></body>" p str.match(/<body>(.*)<\/body>/)[0] # nil
环境:centos 5.5 + ruby 1.8.7 + pdfkit 0.4.6
最近给我的blog生成了pdf文档,每半年生成一个pdf文档,尝试不少开源组件,发现好用的不多,其中比较好的两个事prawn 和 pdfkit ,
最后实验下来pdfkit 很好用,可以把html + css 转化为pdf文档,底层使用了wkhtmltopdf , 而且wkhtmltopdf可以为shell直接调用,非常之方便
1,安装pdfkit
>gem install pdfkit >sudo pdfkit --install-wkhtmltopdf
报 lzcat 找不到 , 安装之
>yum update >yum install lzma
2,生成pdf文档的脚本
#注意 http://wxianfeng.com 必须存在 a 链接 , 因为 wkhtmltopdf 可以直接对 url 抓取生成 pdf require 'rubygems' require 'pdfkit/source' # require "pdfkit" 报错,提示找不到PDFKit require 'pdfkit/pdfkit' require 'pdfkit/middleware' require 'pdfkit/configuration' PDFKit.configure do |config| config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' end range_t = [ ["2009-06-30","2009-12-12 23:59:59"], ["2009-12-12","2010-06-31 23:59:59"], ["2010-06-31","2010-12-12 23:59:59"] ] path = "/usr/local/system/src/wxianfeng_com_pdf/" exist_files = Dir.open(path).to_a.select{|x| x != '.' && x!= '..' && x != '.svn' && x != 'Thumbs.db'} range_t.each do |ele| next if exist_files.include?("wxianfeng.com_#{ele.first}~#{ele.last.slice(/\d+-\d+-\d+/)}.pdf") posts = Content.all(:conditions=>["published_at BETWEEN ? AND ?",ele.first,ele.last]) kit , html = nil , '' posts.each do |i| p i.published_at.to_s(:db) + " " +i.title html << "<strong>" + i.title + "</strong><br/><br/>" + i.html(:all).gsub(/[\s\n<br\/>]([a-zA-z]+:\/\/[^\s<>"]*)/,'<a href="\1">\1</a>') + "<br/><br/><br/><br/>" end kit = PDFKit.new(html) # kit.stylesheets << "#{RAILS_ROOT}/themes/lindholmen/stylesheets/main.css" # kit.stylesheets << "#{RAILS_ROOT}/themes/lindholmen/stylesheets/print.css" # kit.stylesheets << "#{RAILS_ROOT}/themes/lindholmen/stylesheets/local.css" kit.to_pdf kit.to_file "/usr/local/system/src/wxianfeng_com_pdf/wxianfeng.com_#{ele.first}~#{ele.last.slice(/\d+-\d+-\d+/)}.pdf" end
3,运行
ruby script/runner script/tools/generate_pdf.rb
注意script/runner 调用的事development指定的db
最近blog换了皮肤,以前是最大宽度,现在宽度限制死了,有的图片超过了宽度,很丑陋,于是用minimagick对所有的图片统一缩放了下,这下图片的大小刚刚好,缩放比resize的效果要好,图片不会扭曲难看,
minimagick和rmagick都是调用imagemagick的ruby接口,使用起来很方便。。。。
1,缩放 (也就是我用来处理我blog里图片的脚本)
require 'rubygems' require 'mini_magick' # path = "E:/Rubyproject/wxianfeng_com/public/files/" # windows路径 path = "/usr/local/system/www/wxianfeng_com/shared/public/files/" files = Dir.open(path).to_a.select{|x| x != '.' && x!= '..' && x != '.svn' && x != 'Thumbs.db'} imgs = files.select { |f| f !~ /^(thumb_|middle_)/ } imgs.each do |ele| p ele img_path = path + ele img = MiniMagick::Image.from_file(img_path) w,h = img[:width],img[:height] percent = ((480/w.to_f) * 100).to_i img.combine_options do |c| c.sample "#{percent}%" # 缩放 end img.write(img_path) end
2,resize
image = MiniMagick::Image.from_file("input.jpg") # or MiniMagick::Image.new("input.jpg") image.resize "100x100" # or image.thumbnail "100x100" image.write("output.jpg")
3,裁剪
require ‘mini_magick’ img = MiniMagick::Image.from_file “1.jpg” #取得宽度和高度 w,h = img[:width],img[:height] #=> [2048, 1536] shaved_off = ((w-h)/2).round #=> 256 img.shave “#{shaved_off}x0″ #此处表示宽度上左右各截取256个像素,高度上截取0像素 img.write “2.jpg”
4,旋转
image = MiniMagick::Image.from_file("input.jpg") image.combine_options do |c| c.rotate "-90>" # 旋转 90 度 end image.write("input.jpg") # 同名 替换掉原来的
SEE:
http://github.com/probablycorey/mini_magick
http://www.blogkid.net/archives/2154.html
相对于插件,我更喜欢gem的形式,今天在windows 上装will_paginate 和 mysql 两个gem包, 可是都失败了 , 尝试了
>gem install mysql (没添加任何source,默认从 "http://rubyforge.org/" 下) >gem install mysql --source "http://gems.github.com" >gem install mysql --source 'http://gemcutter.org'
最后都失败了 , 继续搜了一下 , 发现还有一个管理gem包的地方 http://rubygems.org,最后尝试
>gem install mysql --source 'http://rubygems.org'
成功了,这过程中遇到了不少gem命令 , 再次正好总结一下:
升级ruby gem
>gem update --system
查看gem版本
>gem -v
查看gem版本,gems安装目录,remote sources等
>gem env
查看已经添加的remote sources
>gem sources
添加一个source
>gem sources -a 'htt://rubygems.org'
安装一个gem包
>gem install will_paginate
指定源位置
>gem install will_paginate --source 'http://rubygems.org' >gem install will_paginate -s 'http://rubygems.org'
指定版本
>gem install rails -v=2.3.5
查看已经安装的全部gem包
>gem list
或者 gem list -d 查看具体的信息,例如project的author,homepage,安装在系统中的路径 等信息,建议加上该参数…
查看d开头的gem包
>gem list d
更详细的查看
>gem list will_paginate -d
卸载gem包
>gem uninstall rails
卸载指定版本
>gem uninstall rails -v=2.1.0
查找gem包
>gem search will_paginate --both (在local和remote源中search含有will_paginate关键字的) >gem list -r will_paginate >gem list -dr will_paginate (我常用这个)
查看gem包依赖其他的gem:
>gem dependency rails -v 2.3.5 Gem rails-2.3.5 actionmailer (= 2.3.5, runtime) actionpack (= 2.3.5, runtime) activerecord (= 2.3.5, runtime) activeresource (= 2.3.5, runtime) activesupport (= 2.3.5, runtime) rake (>= 0.8.3, runtime)
查看gem包的rdoc帮助
>gem server (然后http://localhost:8808查看)
windows 平台上安装
>gem install hpricot --platform=mswin32 # 不指定在windows安装会报错
先不指定platform安装,如果不行的话,再指定platform
不安装ri 和 rdoc
>gem install hpricot --no-ri --no-rdoc
上了正式服务器上安装是不需要rdoc的,这样安装会省下很多时间
如果这些方法还是不行 , 可以到gem的网站上 , 例如rubygems.org 上搜gem包,然后下载到本地进行local安装
查看更多的gem帮助,可以 >gem -h
项目里常用gem命令
wxianfeng@wxianfeng-ubuntu:/usr/local/system/entos/netposa_redmine$ rake -T gems: (in /usr/local/system/entos/netposa_redmine) rake gems:build # Build any native extensions for unpacked gems rake gems:build:force # Force the build of all gems rake gems:install # Installs all required gems. rake gems:refresh_specs # Regenerate gem specifications in correct format. rake gems:unpack # Unpacks all required gems into vendor/gems. rake gems:unpack:dependencies # Unpacks all required gems and their dependencies into vendor/gems.
例如你enviroment.rb 中配置了paperclip gem
config.gem 'paperclip', '2.3.5'
那么 rake gems:install 将会安装paperclip 2.3.5 到你的ruby目录里, rake gems:unpack:dependencies 将会把依赖的gem包unpack 到vendor/gems 目录下,如果你是直接从gem包copy 到vendor/gems 目录下的话,需要执行 rake gems:refresh_specs 来生成 .specifications 文件
UPDATE:
默认安装已经不会再安装ri 和rdoc
here

环境:
tor + windows
toonel.jar + linux
最近做了个自已用的 浏览器的 bookmarklet,可以一键 同步更新自己的 status(twitter,douban,xiaonei,kaixin001,meme等),这里以twitter为例,通过twitter api + 代理 更新自己的 tweets
1,windows 平台
(1)打开自己的tor 代理
(2)核心代码
def twitter email = '你注册的twitter email' password = '你的twitter密码' update_text = 'test status from twitter api by proxy' proxy_host = '127.0.0.1' # tor proxy_port = 8118 # tor 的http 端口是 8118 # toonel # proxy_port = 8080 proxy = Net::HTTP::Proxy(proxy_host, proxy_port) url = URI.parse('http://twitter.com/statuses/update.xml') proxy.start(url.host,url.port) do |h| # Create the POST request req = Net::HTTP::Post.new(url.path) req.basic_auth email, password req.set_form_data({'status' => update_text}) response = h.request(req) # Check the request's response if response.message == 'OK' puts response.body else puts 'failure' end end end
2,linux平台,下面这种方法windows同样适用,java是跨平台的
(1)打开 toonel.jar 代理
(2)把上面 的 port 改为 proxy_port = 8080
为了 blog的 安全 , 这里没有介绍 代理的方法,请自行google,当然代理的办法有很多,更新twitter的tweets 也有很多办法,可以根据自己的代理,自行修改代码~!