您的位置:首页 > 运维架构

如何使用octopress插件同步博文到OSChina上?

2013-05-06 00:00 302 查看
(转载请注明原文链接:http://fzyz999.github.io/blog/2013/05/06/ru-he-shi-yong-octopresscha-jian-tong-bu-bo-wen-dao-oschinashang/)

#序
使用Octopress写播客固然方便,但每篇博客手工发布到自己所写的各个博客上未免有些太麻烦了。比如我目前就同步更新着OSChina上的博客和我利用Github Pages+Octopress建立的http://fzyz999.github.io/这个博客。虽然均使用markdown,但有一些地方还是不完全一样的。比如代码高亮什么的,原理不太相同。搜寻很久,终于发现了这个同步插件。

#步骤
http://huangbowen.net/blog/2013/04/14/octopress-plugin-to-sync-post/

直接照这个步骤走就可以,相同的部分我就不再赘述了。

#遇到的问题
我使用的是Ubuntu 13.04系统,已经之前配置Octopress时已经使用apt安装了ruby和ruby-dev软件包。
我在执行
bundle install
时遇到错误:

Installing nokogiri (1.5.9) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/usr/bin/ruby1.9.1 extconf.rb
checking for libxml/parser.h... yes
checking for libxslt/xslt.h... no
-----
libxml2 is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/bin/ruby1.9.1
--with-zlib-dir
--without-zlib-dir
--with-zlib-include
--without-zlib-include=${zlib-dir}/include
--with-zlib-lib
--without-zlib-lib=${zlib-dir}/lib
--with-iconv-dir
--without-iconv-dir
--with-iconv-include
--without-iconv-include=${iconv-dir}/include
--with-iconv-lib
--without-iconv-lib=${iconv-dir}/lib
--with-xml2-dir
--without-xml2-dir
--with-xml2-include
--without-xml2-include=${xml2-dir}/include
--with-xml2-lib
--without-xml2-lib=${xml2-dir}/lib
--with-xslt-dir
--without-xslt-dir
--with-xslt-include
--without-xslt-include=${xslt-dir}/include
--with-xslt-lib
--without-xslt-lib=${xslt-dir}/lib
--with-libxslt-config
--without-libxslt-config
--with-pkg-config
--without-pkg-config
--with-libxml-2.0-config
--without-libxml-2.0-config
--with-libiconv-config
--without-libiconv-config

Gem files will remain installed in /home/wlm/.bundler/tmp/8210/gems/nokogiri-1.5.9 for inspection.
Results logged to /home/wlm/.bundler/tmp/8210/gems/nokogiri-1.5.9/ext/nokogiri/gem_make.out
An error occurred while installing nokogiri (1.5.9), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.5.9'` succeeds before bundling.

观察后发现是没有安装libxml2(
libxml2 is missing
)。知道问题就好办了,缺什么装什么呗。于是Ctrl+Alt+t打开终端,直接输入:

sudo apt-get install libxml2 libxml2-dev

然后重新执行
bundle install
后继续报错。怎么回事?难道是我判断错了?再读输出发现
libxml2 is missing
已经变成
libxslt is missing
了。很好,再装个库而已。不是大问题。于是:

sudo apt-get install libxslt-dev

再执行
bundle install
终于显示出令人欣慰的
Your bundle is complete!


很好,接下来试一试能不能同步成功。

在_config.yml里设置好用户名和密码。按照oschina上配置live writer的指示填写xmlrpc地址(http://my.oschina.net/action/xmlrpc)

# MetaWeblog
MetaWeblog_username: wlm199558@126.com
MetaWeblog_password: ××××××××××××××××××××××××××××××××//密码就不展示了:)
MetaWeblog_url:  http://my.oschina.net/action/xmlrpc MetaWeblog_blogid: 19950508  //can be any number

之后
rake new_post["test OSChina blog"]
,然后
rake generate
,最后
rake sync_post
。刷新oschina,失望地发现没有成功。怎么回事?看看命令行输出:

Sync the latest post to MetaWeblog site
_custom/sync_post.rb:35:in \`getLatestBlogPath': undefined method \`[]' for nil:NilClass (NoMethodError)
from _custom/sync_post.rb:18:in `postBlog'
from _custom/sync_post.rb:66:in `<main>'

谷歌了一下undefined method `[]' for nil:NilClass (NoMethodError)错误,发现是这个可能是由于正则表达式匹配失败导致的。打开sync_post.rb,观察出错代码:

<!-- lang: ruby -->
def getLatestBlogPath
indexFile = File.open(File.expand_path(File.dirname(__FILE__) + '/../public/index.html'), 'r')
contents = indexFile.read
html = Nokogiri::HTML(contents)

# get latest post path(错误在这里)
path = html.css('//h1[@class="entry-title"]/a')[0]['href']

File.expand_path(File.dirname(__FILE__) + '/../public' + path) + '/index.html'

end

根据注释我们可以知道出错的代码的功能是获取最后一篇日志的路径。显然,这里用到正则表达式是为了匹配最后一篇日志,并获取路径。它打开了public/index.html文件。我打开了我的octopress/public下的index,发现根本就没有
h1
,更没有
@class="entry-title"
。我这里的结构是h2标签跟着
@class="title"
。怪不得匹配不上,原来是这里出了问题。不知什么原因(主题不是默认主题?octopress版本?)我这里的最后一篇日志的匹配方式和插件作者设想得有些不一样。不过,知道问题了就好办了。把h1改为h2,entry-title改为title。重新运行,发现发生了同样的错误,但错误发生在下一段代码。

<!-- lang: ruby -->
def getBlogHtml(path)
contents = File.open(path).read
Nokogiri::HTML(contents)

end

def getPost html

# get post title(这里这里!错误在这里!)
title = html.css('//h1[@class="entry-title"]')[0].content

# get post content
entryContent = html.css('//div[@class="entry-content"]')[0].to_html

# keep same structure with a article
article = '<div id="main"><div id="content"><div><article class="hentry" role="article">' + entryContent + '</article></div></div></div>'

MetaWeblog::Post.new(title, '', article)
end

果然是一模一样的错误原因,进行同样的修改后就正常了。

#总结
曾经一遇到这类问题就上网谷歌百度,寻求高手们提供的现成的解决方案。今天发现这个问题没有人遇到过(至少我没能搜到),又不忍心放弃这个插件。于是,自己动手,尝试解决。我既没学过ruby,也没学过博客网站相关的任何知识,完全是现查现用。

突然发现,其实,很多东西并没有我们最开始想象的那想复杂,很多问题只要认真思考,然后凭借谷歌到的高人们的文字,还是可以自己找出解决方法的。以前一直疑惑,为何很多小白们的问题会遭到高手指责。或许正是因为,这些问题其实并不需要太多的知识,只需要耐心查一查,耐心理解理解,就可以找到解决问题的关键所在,进而自己找去解决方法吧。

敢看,敢想,敢实践

注:最后同步到oschina上面的效果还需要想些办法改进。主要问题在于代码部分,其他的效果完全没有问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息