使用 Pelican 和 Github Pages 建立博客网站
Posted on 2019-06-09 22:20 in Python
安装 pelican
在 Python 3.5 以上,官方已经集成了虚拟环境功能,不需要再使用 virtualenv
python3 -m venv pelican_env
cd pelican_env
source bin/active
pip install pelican markdown ghp-import
新建 pelican 项目
- 先在 github 上新建一个 repo
yourname.github.io
, 然后 clone 到本地, - 在项目文件夹中运行
pelican-quickstart
在选择的过程中注意选择 default language, time zone, Using Github Pages
Welcome to pelican-quickstart v4.0.1.
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? Martin's Blog
> Who will be the author of this web site? Martin
> What will be the default language of this web site? [zh] zh
> Do you want to specify a URL prefix? e.g., https://example.com (Y/n) n
> Do you want to enable article pagination? (Y/n) Y
> How many articles per page do you want? [10] 10
> What is your time zone? [Europe/Paris] Asia/Shanghai
> Do you want to generate a tasks.py/Makefile to automate generation and publishing? (Y/n) Y
> Do you want to upload your website using FTP? (y/N) N
> Do you want to upload your website using SSH? (y/N) N
> Do you want to upload your website using Dropbox? (y/N) N
> Do you want to upload your website using S3? (y/N) N
> Do you want to upload your website using Rackspace Cloud Files? (y/N) 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/demo/pelican_env/yourname.github.io
pelican config
quick-start 生成的只是最简单的配置,如果想要更好看一点需要更多的自定义配置
下面有一份配置的示例,使用 Flex 主题,sitemap 插件,配置了 disqus 和 Google Adsense
#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals
import os
BASE_DIR = os.getcwd()
ENV_DIR = os.path.dirname(BASE_DIR)
AUTHOR = 'Martin'
#SITEURL = 'https://xingzuoshe.cn'
SITENAME = "Martin's Blog"
SITETITLE = "Martin's Blog"
SITESUBTITLE = "Martin's Blog"
SITEDESCRIPTION = 'Martin\'s Thoughts and Writings'
SITELOGO = '/images/default_profile_200x200.png'
FAVICON = '/images/default_profile_200x200.png'
BROWSER_COLOR = '#333333'
PYGMENTS_STYLE = 'default'
ROBOTS = 'index, follow'
PATH = 'content'
DEFAULT_DATE_FORMAT = '%Y-%m-%d %H:%M'
TIMEZONE = 'Asia/Shanghai'
# Feed generation is usually not desired when developing
FEED_ALL_ATOM = 'feeds/all.atom.xml'
CATEGORY_FEED_ATOM = 'feeds/{slug}.atom.xml'
TRANSLATION_FEED_ATOM = None
AUTHOR_FEED_ATOM = None
AUTHOR_FEED_RSS = None
# Blogroll
LINKS = (
('astrobook', 'https://astrobook.cn/'),
('astrohub', 'https://astrohub.cn'),)
# Social widget
SOCIAL = (('github', 'https://github.com/openmartin'),
('rss', '/blog/feeds/all.atom.xml'),)
# Uncomment following line if you want document-relative URLs when developing
RELATIVE_URLS = True
# Theme
THEME = os.path.join(ENV_DIR, "pelican-themes", "Flex")
PLUGIN_PATHS = [os.path.join(ENV_DIR, "pelican-plugins")]
PLUGINS = ['sitemap', 'i18n_subsites', 'liquid_tags.img', 'liquid_tags.include_code', 'liquid_tags.youtube']
JINJA_ENVIRONMENT = {
'extensions': ['jinja2.ext.i18n'],
}
# Sitemap
SITEMAP = {
'format': 'xml',
'changefreqs': {
'articles': 'daily',
'pages': 'daily',
'indexes': 'daily',
}
}
# Default theme language
I18N_TEMPLATE_LANG = 'zh_CN'
DEFAULT_LANG = 'zh_CN'
OG_LOCALE = 'zh_CN'
LOCALE = 'zh_CN'
# MENU
DEFAULT_PAGINATION = 10
DISPLAY_PAGES_ON_MENU = False
MAIN_MENU = True
HOME_HIDE_TAGS = True
USE_FOLDER_AS_CATEGORY = False
MENUITEMS = (('Archives', '/archives.html'),
('Categories', '/categories.html'),
('Tags', '/tags.html'),)
COPYRIGHT_YEAR = '2020'
# STATIC
STATIC_PATHS = ['images', 'code', 'extra']
CUSTOM_CSS = 'static/custom.css'
EXTRA_PATH_METADATA = {
'extra/CNAME': {'path': 'CNAME'},
'extra/custom.css': {'path': 'static/custom.css'},
}
CODE_DIR = 'code'
USE_LESS = True
# Third page service
DISQUS_SITENAME = "astro-2"
# GOOGLE_ANALYTICS = 'UA-1234-5678'
GOOGLE_ADSENSE = {
'ca_id': 'ca-pub-8640171181637141',
'page_level_ads': False,
'ads': {
'aside': '',
'main_menu': '',
'index_top': '',
'index_bottom': '2770021113',
'article_top': '',
'article_bottom': '4393383534',
}
}
新建一篇 markdown
在 content
文件夹下可以放 markdown 文件和一些图片,或者自定义的 css,js 文件
和普通的 markdown 不同的是在文件头上需要写一些 metadata,参考官方文档
Title: My super title
Date: 2010-12-03 10:20
Modified: 2010-12-05 19:30
Category: Python
Tags: pelican, publishing
Slug: my-super-post
Authors: Alexis Metaireau, Conan Doyle
Summary: Short version for index and feeds
Github Pages 提交
- .gitignore 添加 output 文件夹 参考 官方博客
- 新建
source
分支,把原始的 markdown 文件和 配置文件都放到source
分支上
git checkout -b source
git add .
git commit -a -m "Initial commit"
git push -u origin source
- 把生成的 html 放到 master 分支上,然后 push 到 Github 上
ghp-import output -r origin -b master
git push origin master
- 这个时候就可以通过
yourname.github.io
来访问你的博客了
Github Pages CNAME 设置
参考 Github Help
这里是设置
- 在 dns 服务商 设置域名 @泛解析 A 记录到 Github 的地址
- 然后在 Github Pages 的设置里添加域名
daily workflow
- 编辑 markdown
- make html
- make github
Editor
推荐使用 vscode 来编辑 markdown