Pelican介绍

Pelican是一个用Python语言编写的静态网站生成器,支持使用restructuredText和Markdown写文章,配置灵活,扩展性强,有许多优秀的主题和插件可供使用。

Pelican 的Github地址是:https://github.com/getpelican/pelican

安装Pelican

安装Pelican需要python环境和git,mac和linux一般都自带python,windows下需自行安装。为方便,再顺手装上distributepipvirtualenv

安装 Pelican

pip install pelican

安装Markdown

pip install Markdown

Markdown 免费编辑器

Windows 平台

Linux 平台

Mac 平台

在线编辑器

浏览器插件

创建 Blog

创建一个 Blog 目录(注意文件夹名最好不为空,因为后续make html命令会出错)

mkdir myblog
cd myblog

快速创建 Blog

pelican-quickstart

根据提示一步步输入相应的配置项,不知道如何设置的接受默认即可,后续可以通过编辑pelicanconf.py文件更改配置。

以下是生成的目录结构:

myblog/
├── content              # 存放输入的源文件
   └── (pages)          # 存放手工创建的静态页面
├── output               # 生成的输出文件
├── develop_server.sh    # 方便开启测试服务器
├── Makefile             # 方便管理博客的Makefile
├── pelicanconf.py       # 主配置文件
└── publishconf.py       # 发布时使用的配置文件

撰写文章

在 content 目录下用 Markdown 语法来写一篇文章

Title: My super title
Date: 2010-12-03 10:20
Category: Python
Tags: pelican, publishing
Slug: my-super-post
Author: Alexis Metaireau
Summary: Short version for index and feeds

This is the content of my super blog post.

生成页面

make html

我比较喜欢下面的这个命令,它是实时生成你的站点,就是说你修改你的博客什么的它会实时的生成!很棒吧。

make regenerate

现在就可以在output目录查看生成的html文件了。

生成之后,我们要看下显示的效果,用下面的命令吧

make serve

下面这个我比较喜欢,理由同上面的那个regerate,哈哈

make devserver

至此,我们可以在本地浏览刚才建好的博客了,地址就是http://localhost:8000 停止服务器则是下面的命令:

./develop_server.sh stop

主题选择

按如下步骤下载pelican官方主题,从里面挑选出自己喜欢的主题吧,大多数主题预览界面你可以打开这个网页进行查看。不过如今pelican又新出了很多主题,所以你需看看pelican主题开源库

git clone https://github.com/getpelican/pelican-themes.git

打开pelicanconf.py配置文件,更改或添加THEME为自己喜欢的主题,例如本博客所挑选的tuxlite_tbs,更多的配置含义请关注官方文档

THEME = 'pelican-themes/tuxlite_tbs'

添加评论系统

开启个人博客的原因在于分享知识,分享就需要交流,评论模块当然少不了。在Disqus上申请帐号,按照流程Disqus会分配给你站点的Shortname,记牢Shortname,如果忘了请进入admin/settings中查看。然后同理,在pelicanconf.py添加

DISQUS_SITENAME = Shortname

此处配置的过程中,始终显示

我们没有成功加载Disqus。如果您是管理员,请查看 故障排除指南.

最后发现是受主题影响,一开始用的pelican-bootstrap3主题,后来改用目前使用的tuxlite_tbs主题后就一切正常了,问题在于pelican-bootstrap3主题中嵌入的Disqus的js代码有问题。

使用插件

Pelican 一开始是将插件内置的, 但是新版本 Pelican将插件隔离了出来, 所以我们要到github上 克隆一份新的插件, 在博客目录执行

git clone git://github.com/getpelican/pelican-plugins.git

现在我们博客目录就新添了一个 pelican-plugins目录, 我们以配置sitemap插件为例,sitemap插件可以生成 sitemap.xml 供搜索引擎使用

在pelicanconf.py配置文件里加上如下项:

PLUGIN_PATH = u"pelican-plugins"
PLUGINS = ["sitemap"]

配置sitemap 插件

SITEMAP = { "format": "xml", "priorities": { "articles": 0.7, "indexes": 0.5, "pages": 0.3, }, "changefreqs": { "articles": "monthly", "indexes": "daily", "pages": "monthly", }}

拷贝静态文件

如果我们定义静态的文件, 该如何将它在每次生成的时候拷贝到 output 目录呢, 我们以robots.txt 为例, 在我们的 content/extra 下面我们放了一个定义好的 robots.txt文件, 在pelicanconf.py更改或添加FILES_TO_COPY项:

FILES_TO_COPY = ( ("extra/robots.txt", "robots.txt"),)

这样在每次生成html的时候都会把 content/extra下的 robots.txt 拷贝到 output目录下。

拷贝静态目录

如果是一个静态目录呢?比如有个名为img的目录用来放文章所使用的图片,我们可以在pelicanconf.py里添加或修改STATIC_PATHS项:

STATIC_PATHS = [u"img"]

在Github上显示自己的博客

接下来就是上传博客的时候了,我们要将生成的站点内容上传到刚才建好的example.github.com的工程master主干中,

github官方说明

Create a new repository on the command line

  touch README.md
  git init
  git add README.md
  git commit -m "first commit"
  git remote add origin https://github.com/x-dev-cn/x-dev->cn.github.com.git
  git push -u origin master

Push an existing repository from the command line

  git remote add origin https://github.com/x-dev-cn/x-dev->cn.github.com.git
  git push -u origin master

进入到output目录下,

git init
git remote origin https://github.com/username/  username.github.com.git

生成 .git的目录,此时我们上传到github上面,

git pull
git add .
git commit -m "update"
git push origin master

此时可能需要输入账户和密码,稍后说明如何不需要每次push都输入账户和密码。

上传之后,我们打开github的这个项目,点击右侧的setting项,在下面可以看到github正在生成你的站点,稍等10多分钟,在浏览器中输入example.github.com,哈哈见证奇迹的时刻到了!

域名绑定

在repo的根目录下面,新建一个名为CNAME的文本文件,里面写入你要绑定的域名,比如顶级域名 example.com 或者二级域名 xxx.example.com。

如果绑定的是顶级域名,则DNS要新建一条A记录,指向 204.232.175.78。

如果绑定的是二级域名,则DNS要新建一条CNAME记录,指向 xxx.github.io 或者 xxx.github.com 。

参考资料

1 Coding Our World:使用Pelican和GitHub Pages搭建个人博客 —— 基础篇

2 博客诞生记:基于GitHub+Pelican创建博客的整个过程


Comments

comments powered by Disqus