标签为 Pelican 的文章

使用Pelican + Markdown + GitHub Pages来撰写Blog

参考资料:
http://www.linuxzen.com/shi-yong-pelicanda-zao-jing-tai-bo-ke.html
http://www.dongxf.com/3_Build_Personal_Blog_With_Pelican_And_GitHub_Pages.html
https://github.com/razius/razius.com/blob/master/pelicanconf.py

背景介绍:
在我还自以为使用Wordpress作为Blog程序很高大上时,身边有不少运维哥们儿却已经采用Pelican作为博客生成器,使用Markdown语法编写文章,并托管在GitHub Pages上,逼格立刻就体现出来了。
为了让自己不至于太落后,我经过一阵子摸索,终于也完成了我的GitHub Page: http://mcsrainbow.github.io

具体步骤:
1. 安装所需软件
sudo yum install python python-devel python-pip
sudo pip install pelican markdown

2. 创建工作目录
cd ~
mkdir pelican

cd pelican
pelican-quickstart

Welcome to pelican-quickstart v3.4.0.

This script will help you create a new Pelican-based website.

Please answer the following questions so this script can generate the files
needed by Pelican.


> Where do you want to create your new web site? [.]
> What will be the title of this web site? HeyDevOps
> Who will be the author of this web site? mcsrainbow
> 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)
> What is your URL prefix? (see above example; no trailing slash) http://mcsrainbow.github.io
> Do you want to enable article pagination? (Y/n)
> How many articles per page do you want? [10]
> Do you want to generate a Fabfile/Makefile to automate generation and publishing? (Y/n)
> Do you want an auto-reload & simpleHTTP script to assist with theme and site development? (Y/n)
> Do you want to upload your website using FTP? (y/N)
> Do you want to upload your website using SSH? (y/N)
> Do you want to upload your website using Dropbox? (y/N)
> Do you want to upload your website using S3? (y/N)
> Do you want to upload your website using Rackspace Cloud Files? (y/N)
> Do you want to upload your website using GitHub Pages? (y/N) Y
> Is this your personal page (username.github.io)? (y/N) Y
Done. Your new project is available at /home/dong/pelican

3. 下载风格包pelican-themes与插件包pelican-plugins
git clone git://github.com/getpelican/pelican-themes.git
git clone git://github.com/getpelican/pelican-plugins.git

4. 配置pelicanconf.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

# Site settings
AUTHOR = u'mcsrainbow'
AUTHOR_EMAIL = u'guosuiyu@gmail.com'
SITENAME = u'HeyDevOps'
TAGLINE = 'Whatever is worth doing is worth doing well.'
SITEURL = 'http://mcsrainbow.github.io'
DEFAULT_DATE_FORMAT = ('%Y-%m-%d')

TIMEZONE = 'Asia/Shanghai'

DEFAULT_LANG = u'zh'
DEFAULT_METADATA = (
)

DELETE_OUTPUT_DIRECTORY = False

# Blogroll
LINKS = (
    ('HeyLinux', 'http://heylinux.com'),
)

# Social widget
SOCIAL = (
    ('Github', 'http://github.com/mcsrainbow/heydevops'),
    ('Twitter', 'http://twitter.com/heydevops'),
)

MENUITEMS = (
)

# Disqus
DISQUS_SITENAME = u"mcsrainbow"

# Content path
PATH = 'content'
PAGE_PATHS = ['pages']
ARTICLE_PATHS = ['articles']
STATIC_PATHS = ['images', 'files']
EXTRA_PATH_METADATA = {
    'files/robots.txt': {'path': 'robots.txt'},
    'images/favicon.ico': {'path': 'favicon.ico'},
}

ARTICLE_URL = ('articles/{slug}.html')
ARTICLE_SAVE_AS = ('articles/{slug}.html')
PAGE_LANG_SAVE_AS = False

# Feed
FEED_DOMAIN = SITEURL
FEED_ALL_ATOM = 'feeds/all.atom.xml'
CATEGORY_FEED_ATOM = 'feeds/%s.atom.xml'
TRANSLATION_FEED_ATOM = None

# Theme
THEME = 'pelican-themes/zurb-F5-basic'
DEFAULT_PAGINATION = 10

MD_EXTENSIONS = (['codehilite(css_class=highlight)', 'extra',
                  'fenced_code', 'tables', 'sane_lists'])

# Plugin
PLUGIN_PATHS = ['pelican-plugins']
PLUGINS = [ 'sitemap', 'gravatar' ]

# Sitemap
SITEMAP = {
    'format': 'xml',
    'priorities': {
        'articles': 1,
        'pages': 0.9,
        'indexes': 0.8,
    },
    'changefreqs': {
        'indexes': 'daily',
        'articles': 'daily',
        'pages': 'weekly'
    }
}

# Can be useful in development, but set to False when you're ready to publish
RELATIVE_URLS = False

5. 配置 publishconf.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

# This file is only used if you use `make publish` or
# explicitly specify it as your config file.

import os
import sys
sys.path.append(os.curdir)
from pelicanconf import *

SITEURL = 'http://mcsrainbow.github.io'
RELATIVE_URLS = False

FEED_ALL_ATOM = 'feeds/all.atom.xml'
CATEGORY_FEED_ATOM = 'feeds/%s.atom.xml'

DELETE_OUTPUT_DIRECTORY = True

# Following items are often useful when publishing

DISQUS_SITENAME = u"mcsrainbow"
#GOOGLE_ANALYTICS = ""

6. 撰写第一篇文章
cd content
mkdir articles files images pages
vim articles/hello.md

Title: Hello
Date: 2014-09-24 02:06
Category: Uncategorized
Tags: Pelican, Markdown
Slug: hello
Author: mcsrainbow
Summary: Hello Pelican, Markdown and GitHub Pages.

Hello Pelican, Markdown and GitHub Pages.

7. 生成robots.txt与favicon.ico
vim files/robots.txt

User-agent: *
Sitemap: http://mcsrainbow.github.io/sitemap.xml

scp heylinux.com:/webserver/blog/rainbow/favicon.ico .

8. 配置Disqus
Disqus上注册一个用户并生成一个站点mcsrainbow.disqus.com;
设置mcsrainbow.disqus.com站点使其允许域名mcsrainbow.github.io;
设置以上配置文件为DISQUS_SITENAME = u"mcsrainbow",mcsrainbow 为站点ID

9. 创建GitHub Pages
直接创建一个新的repo,但是其名称必须与ID相同,并加上github.io或github.com后缀。
就我而言,就必须创建一个repo名为mcsrainbow.github.io

10. 创建好GitHub Pages之后,生成Blog静态HTML文件
cd ~
cd pelican
make html

pelican /home/dong/pelican/content -o /home/dong/pelican/output -s /home/dong/pelican/pelicanconf.py
WARNING: AUTHOR_SAVE_AS is set to False
Done: Processed 1 article(s), 0 draft(s) and 0 page(s) in 0.23 seconds.

11. 进入output目录,将生成好的静态HTML文件上传到GitHub Pages站点mcsrainbow.github.io中
cd output
git init
git remote add origin https://github.com/mcsrainbow/mcsrainbow.github.io.git
git add -A
git commit -m "Update Blog"
git push -u origin master

12. 等待15分钟左右,访问mcsrainbow.github.io即可看到生成的网站效果

13. 注意
由于我设置了在重新生成HTML时默认不删除output目录,因此每次更新Blog时都需要手动执行'rm -rf output/*'。
这样做的目的,是为了避免删除output/.git目录,方面在生成之后立刻提交到GitHub Pages。
当然在实际操作当中我是编写了alias和scripts来完成这一系列动作的。
具体可以参考我的Pelican工作环境:https://github.com/mcsrainbow/pelican

, , ,

1 Comment