使用Pelican搭建Blog

安装配置Pelican


使用环境:
    系统  :  Linux X86_64
    Pelican : 3.3.0

首先说一下安装: 网上有不少建议说使用虚拟环境 virtualenv,以免污染本地的环境。不过,如果不是去更改Python的全局设置的话,也不至于影响本地环境的。我是直接安装的。根据 Pelican 官方的文档,我是使用 pip 安装的,不过pip又是依赖于distribute 安装的。所以,我使用的命令如下:

    curl -O http://python-distribute.org/distribute_setup.py
    sudo python distribute_setup.py

    curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
    sudo python get-pip.py

    sudo pip install pelican

还有,如果要用 Markdown来写文章,也需要执行如下命令:

    pip install markdown

新建一个目录,用来存放你的博客文件(我直接建立了"blog"文件夹)

    mkdir blog
    cd blog

新建博客:

使用如下命令:

    pelican-quickstart

按照提示一步步即可完成新建, 这些配置之后可以在pelicanconf.py这个文件中修改

    Where do you want to create your new web site?[.]  (你想在哪里创建你的网站,默认为当前目录)
    What will be the title of this web site?(网站的标题是什么)
    Who will be the author of this web site?(网站的作者是谁)
    What will be the default language of this web site? [en]   (网站的默认语言是),这里我们写'zh'中文
    Do you want to specify a URL prefix? e.g., http://example.com(Y/n) (是否指定域名),Y
    What is your URL prefix?(see above example; no trailing slash)(输入域名,不能包含反斜杠'/'),http://moelove.info
    Do you want to enable article pagination?(是否启用文章分页)
    Do you want to generate a Makefile to easily manage your website?(是否生成一个Makefile来管理网站(这里我建议生成,因为会比较方便的))
    Do you want an auto-reload & simpleHTTP script to assist with theme and site development?(是否想有一个自动加载的小型http脚本用来修改主题和站点开发)
    Do you want to upload your website using FTP?
    Do you want to upload your website using SSH?
    Do you want to upload your website using Dropbox?
    Do you want to upload your website using S3?

新建博文:

这个时候就可以写博客了。具体的写作方式可以参考 Pelican的官方文档 我以Markdown 为例说明一下。

在content 目录,新建一个 hello.md文件

    vim content/htllo.md


    Date: 2014-04-18 #日期
    Title: HelloWorld #标题
    Tags: hello #标签
    Category: test #分类
    Hello World !

之后,执行make html 或者 pelican content即可将 md文件转换成html文件。执行 make serve 即可打开本地浏览器,预览

现在一个使用默认主题的博客就建立好了。


后续

接下来说一下修改主题和使用插件的方法

Pelican 默认提供了很多主题可以 github 上下载,当然也可以自己制作。

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

在里面找到你喜欢的主题,使用

    sudo pelican-theme -i /path/to/your/themes

命令进行主题的安装。

随后可以使用pelican-themes -l -v 查看已经安装好的主题。

    /usr/lib/python2.7/site-packages/pelican/themes/simple
    /usr/lib/python2.7/site-packages/pelican/themes/notmyidea
    /usr/lib/python2.7/site-packages/pelican/themes/bootstrap2

想要开始使用主题的话, 只需要在 pelicanconf.py文件中添加 THEME项为主题名,例如:

    THEME = "bootstrap2"

重新 make html 即可, 这时候声成的页面就是所要使用的主题了。

为博客使用插件

Pelican 也提供了不少的插件来方便使用, 可以从github 上面下载到博客所在目录:

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

现在博客目录就新添了一个 pelican-plugins 目录,接下来在 pelicanconf.py 里面添加配置即可(里面是我最开始使用的三个插件):

    PLUGIN_PATH = u"pelican-plugins"
    PLUGINS = ["sitemap", "summary", "neighbors"]

再添加 sitemap 的配置

    SITEMAP = {
    'format': 'xml',
    'priorities': {
        'articles': 0.5,
        'indexes': 0.5,
        'pages': 0.5
        },
    'changefreqs': {
        'articles': 'weekly',
        'indexes': 'daily',
        'pages': 'monthly'
        }
    }

然后执行 *make html* 即可

添加多说评论

一开始搭建静态博客的时候,使用的disqus 并且Pelican 的主题中默认提供了支持,只要在 pelicanconf.py 的里面加入 disqus 的short_name 即可开始使用了。不过,身在天朝,还是尽量本地化一些吧,使用多说评论插件。 在多说注册一个帐号,添加一个站点,获取自己的 short_name ,点击工具一栏-->获取代码

<!-- 多说评论框 start -->
    <div class="ds-thread" data-thread-key="请将此处替换成文章在你的站点中的ID" data-title="请替换成文章的标题" data-url="请替换成文章的网址"></div>
<!-- 多说评论框 end -->
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"替换成你自己的"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0]
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
<!-- 多说公共JS代码 end -->

把这段代码放在你想要显示评论的地方就可以了。建议直接修改放在article.html模板里面。

添加其他功能

这里我还加入了百度分享的功能。直接定制个人想要的代码, 放在你需要出现分享按钮的地方即可,我放在了文章的下面。

<div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_qzone" data-cmd="qzone"></a><a href="#" class="bds_tsina" data-cmd="tsina"></a><a href="#" class="bds_tqq" data-cmd="tqq"></a><a href="#" class="bds_renren" data-cmd="renren"></a><a href="#" class="bds_weixin" data-cmd="weixin"></a></div>

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

还有加入了 Google Analytics 谷歌站内搜索 以及七牛提供的云存储 等。

加载评论