Jekyll 非你莫属

30 Nov 2011

Jekyll is a simple, blog aware, static site generator. It takes a template directory (representing the raw form of a website), runs it through Textile or Markdown and Liquid converters, and spits out a complete, static website suitable for serving with Apache or your favorite web server. This is also the engine behind GitHub Pages, which you can use to host your project’s page or blog right here from GitHub.

安装 Jekyll

下面详细说明一下在Windows环境下安装Jekyll~~Linux系统下的直接看教程就好了

Step1

首先你需要有一个Windows下的Linux模拟环境,可以安装Cygwin,或者使用MsysGit。

Jekyll是用Ruby编写的。最好的安装方式是通过RubyGems:

1  
2 $ gem install jekyll

在Windows无法直接安装RubyGems,因为Cygwin等客户端不能使用make, gcc, sh来进行编译。教程上给出If you encounter errors like Failed to build gem native extension on Windows you may need to install RubyInstaller DevKit

DevKit 是windows平台下编译和使用本地C/C++扩展包的工具。它就是用来模拟Linux平台下的make, gcc, sh来进行编译。但是这个方法目前仅支持通过RubyInstaller安装的Ruby。

Step2

下载最新的RubyInstaller(我使用的是rubyinstaller-1.9.3-p0.exe),安装

下载最新的DevKit(我使用的是DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe),安装

Step3

在模拟环境Cygwin或者MsysGit中:

1  
2 $ gem install jekyll

安装会附带自动安装所需要的 directory_watcher, liquid, open4, maruku and classifier

Step4

测试一下是否安装成功,在Cygwin或者MsysGit中输入:

1   
2 $ jekyll --version
3 Jekyll 0.11.0

若要在本地测试完再上传,可以在文件根目录下执行:jekyll —server —auto,会在根目录下生成_site文件夹,然后访问 http://localhost:4000 即可~..~

更多请参考博文:

>>Jekyll in Windows 终极奥义
>>Jekyll 本地调试之若干问题


Github 让你拥有属于自己的博客

26 Nov 2011

git

git 是一个分布式版本控制工具(DVCS),不需要服务端软件支持(即使在地铁里也可以正常 commit),Linux 内核开发用的版本控制工具就是它。

常用的 SVN 属于集中式版本控制工具(CVCS),需要在服务端开启SVN服务,然后客户端 checkout, commit, update。

github

github 的标语是:"secure source code hosting and collaborative development"。一个基于 git 的类似 google code 的代码仓库,付费版的用户可以创建私有仓库,支持多人开发。很多项目都选择了 github 来保存代码,如 “jQuery/reddit/RoR/CakePHP/Redis” 等等。

github pages

blog 是在 pages 的基础上搭建的,创建一个用户的页面很简单,假设你的 Github 用户名为 foo

1. 新建一个仓库(repository),名称填 “foo.github.com”

2. 按照 guide 完成

 1 # 在本地新建一个文件夹 foo.github.com
 2 $ mkdir foo.github.com
 3 # 进入文件夹根目录
 4 $ cd foo.github.com
 5 # 初始化
 6 $ git init
 7 $ touch README
 8 $ git add README
 9 # 在根目录下新建一个 index.html 页面,随便写点什么
10 # 把index.html加入到仓库中
11 $ git add .
12 # 提交修改
13 $ git commit -m 'first commit'
14 # 添加 github 的分支
15 $ git remote add origin git@github.com:foo/foo.github.com.git
16 # 提交到 github 分支
17 $ git push origin master

过1分钟左右,浏览 http://foo.github.com,就可以看到刚刚创建的 index.html 文件了。

除了创建用户页面,还可以针对每个项目单独创建项目的主页

创建 blog

到这里其实已经可以写博客了,创建一个 index.html 页面,在里面列出写过的文章,点击标题进去后又是一个手动创建的 html 页。就是太麻烦了,一点都不酷。

github 当然知道这个问题,所以他们创建了jekyll模板引擎 。简单来说,就是你可以用textile 或者markdown语法来写博客,提交到 github 后,会自动转换成 html。

这里有很多网站/博客,都是基于 github 的 jekyll 模板开发的,如果觉得哪个不错,可以查看 source。

先来看看这个仓库,里面有一些特殊的文件/文件夹:

_config.yml

存储了一些设置,大部分的设置都可以通过命令行指定,但放到配置文件里更方便些

_layouts

_layouts 文件夹存放的是模板文件,文章会被渲染到这些模板里,变量指代的是文章内容

_posts

这里就是真正存放博客文章的地方了,文件命名要遵守这种格式:year-month-day-title.textile 或者 year-month-day-title.markdown

_site

这个文件夹是程序生成的,如果本地没有安装 jekyll 的话,是不会有这个文件夹的,如果想要先本地预览一下,再提交到 github,最好通过 .gitignore 把这个文件夹排除。

index.html

这个文件也有一个yaml前缀 ,可以指定使用哪个模板,标题等等,所有根文件夹下的 .html/.htm/.textile/.markdown 都会被解析。

other files/folders

上面没有列出的文件/文件夹都会被 jekyll 放到 _site 文件夹下,如 css/image/script 等等。

jekyll的安装

参考博文Jekyll 非你莫属

绑定域名

很简单,新建一个 CNAME 文本文件,在里面输入域名,如 “chxt6896.com”,然后在域名提供商里,指定该域名的 CNAME 为 “chxt6896.github.com”,搞定

添加评论功能

参考博文Disqus 我的评论我做主

参考文章

>>>jekyll wiki
>>>github blog
>>>publishing a blog with github and jekyll

后记

最后再次感谢 github 提供了这么好的服务,如果对 github 的创业历程感兴趣,可以参考这篇文章

PS

文章是转来的,第一篇引导我的文章,很有纪念意义~

后面我会详细讲一下几个关键功能实现,Category,Archive,Highlight,分页,Disqus,欢呼吧,独此一家~~~


Readability

18 Aug 2011

Readability是用 JavaScript 技术写的程序,能够自动识别 HTML 页面中的正文内容,将导航、菜单、广告、页脚等非正文内容剔除掉,从而留下一个 HTML 页面最有用的信息。

Readability 有 Python、php、ruby 等语言的实现,这里就给大家介绍一下 Readability 的 Python 实现。

Python Readability 是基于 BeautifulSoup 的,所以可移植性很好,可以在 GAE 上面直接使用,但是速度会稍微慢一些。

参考:

Python Readability

Readability

decruft


Beautiful Soup-Quick Start

16 Aug 2011

Beautiful Soup 是一个处理 Python HTML/XML 的模块
Beautiful Soup的官方页面

(一)Beautiful Soup 下载与安装
下载地址
安装其实很简单,Beautiful Soup只有一个文件BeautifulSoup.py,只要把这个文件拷到你的工作目录,就可以了。
from BeautifulSoup import BeautifulSoup # For processing HTML
from BeautifulSoup import BeautifulStoneSoup # For processing XML
import BeautifulSoup # To get everything

(二)创建 Beautiful Soup 对象
BeautifulSoup对象需要一段html文本就可以创建了。
下面的代码就创建了一个BeautifulSoup对象:

 1  
 2 from BeautifulSoup import BeautifulSoup
 3 doc = ['<html><head><title>PythonClub.org</title></head>',
 4        '<body><p id="firstpara" align="center">This is paragraph one',
 5        '<p id="secondpara" align="blah">This is paragraph two',
 6        '</html>']
 7 soup = BeautifulSoup(''.join(doc))
 8 print soup.prettify()
 9 # <title>PythonClub.org</title>
10 # <html>
11 #  <head>
12 #   <title>
13 #    PythonClub.org
14 #   </title>
15 #  </head>
16 #  <body>
17 #   <p id="firstpara" align="center">
18 #    This is paragraph
19 #    <b>
20 #     one
21 #    </b>
22 #    of ptyhonclub.org.
23 #   </p>
24 #   <p id="secondpara" align="blah">
25 #    This is paragraph
26 #    <b>
27 #     two
28 #    </b>
29 #    of pythonclub.org.
30 #   </p>
31 #  </body>
32 # </html>

navigate soup的一些方法:

 1  
 2 >>> soup.contents[0].name
 3 u'html'
 4 >>> soup.contents[0].contents[0].name
 5 u'head'
 6 >>> head = soup.contents[0].contents[0]
 7 >>> head.parent.name
 8 u'html'
 9 >>> head.next
10 <title>Page title</title>
11 >>> head.nextSibling.name
12 u'body'
13 >>> head.nextSibling.contents[0]
14 <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>
15 >>> head.nextSibling.contents[0].nextSibling
16 <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>

下面是一些方法搜索soup,获得特定标签或有着特定属性的标签:

 1  
 2 >>> titleTag = soup.html.head.titletitleTag
 3 <title>Page title</title>
 4 >>> titleTag.string
 5 u'Page title'
 6 >>> len(soup('p'))
 7 2
 8 >>> soup.findAll('p', align="center")
 9 [<p id="firstpara" align="center">This is paragraph <b>one</b>. </p>]
10 >>> soup.find('p', align="center")
11 <p id="firstpara" align="center">This is paragraph <b>one</b>. </p>
12 >>> soup('p', align="center")[0]['id']
13 u'firstpara'
14 >>> soup.find('p', align=re.compile('^b.*'))['id']
15 u'secondpara'
16 >>> soup.find('p').b.string
17 u'one'
18 >>> soup('p')[1].b.string
19 u'two'

修改soup也很简单:

 1  
 2 >>> titleTag['id'] = 'theTitle'
 3 >>> titleTag.contents[0].replaceWith("New title")
 4 >>> soup.html.head
 5 <head><title id="theTitle">New title</title></head>
 6 >>> soup.p.extract()
 7 >>> soup.prettify()
 8 # <html>
 9 #  <head>
10 #   <title id="theTitle">
11 #    New title
12 #   </title>
13 #  </head>
14 #  <body>
15 #   <p id="secondpara" align="blah">
16 #    This is paragraph
17 #    <b>
18 #     two
19 #    </b>
20 #     .
21 #   </p>
22 #  </body>
23 # </html>
24 >>> soup.p.replaceWith(soup.b)
25 # <html>
26 #  <head>
27 #   <title id="theTitle">
28 #    New title
29 #   </title>
30 #  </head>
31 #  <body>
32 #   <b>
33 #    two
34 #   </b>
35 #  </body>
36 # </html>
37 >>> soup.body.insert(0, "This page used to have ")
38 >>> soup.body.insert(2, " &lt;p&gt; tags!")
39 >>> soup.body# <body>This page used to have <b>two</b> &lt;p&gt; tags!</body>

一个实际例子,用于抓取 ICC Commercial Crime Services weekly piracy report页面, 使用Beautiful Soup剖析并获得发生的盗版事件:

1 import urllib2
2 from BeautifulSoup import BeautifulSoup
3 page = urllib2.urlopen("http://www.icc-ccs.org/prc/piracyreport.php")
4 soup = BeautifulSoup(page)
5 for incident in soup('td', width="90%"):
6     where, linebreak, what = incident.contents[:3]
7     print where.strip()
8     print what.strip()
9     print

更多可查看官方中文教程:)


Python-fnmatch 模块

28 Jun 2011

fnmatch 模块实现了 shell patterns 表匹配字符串文件名

最吸引我的是它有一个 translate(pat) 函数:可以将 pat 转换成正则表达式:)

1  
2 >>> import fnmatch
3 >>> fnmatch.translate('I Love You!')
4 'I\\ Love\\ You\\!\\Z(?ms)'

还有一些其他函数:
fnmatch(name, pattern):测试 name 是否匹配 pattern,匹配返回 true,否则返回 false

filter(names, pat):实现列表特殊字符的过滤或筛选,返回符合匹配模式的字符列表

fnmatchcase(name, pat):和 fnmatch() 功能类似,但强制区分大小写匹配,不管文件系是否区分


<< Previous Page Next Page >>
Fork me on GitHub