还在花钱建网站吗?教你用Python把github变身免费个人网站


概要:
Pelican是创建和撰写博客的,基于Python的博客平台,可以很好地与GitHub配合使用。本文从安装到使用,介绍了如何用Pelican在github上撰写和维护博客的生动例子。最后还给出了如何更好使用Pelican给出了诀窍。
GitHub 是一个非常受欢迎的源代码控制Web服务,它使用Git将本地文件与GitHub服务器上保存的副本同步,这样您就可以轻松共享和备份您的工作。除了为代码存储库提供用户界面外,GitHub还使用户能够直接从存储库发布网页。GitHub推荐的网站生成包是用Ruby编写的Jekyll。对于Python的忠实粉丝, Pelican是不二之选。

这是一个基于Python的博客平台,可以很好地与GitHub配合使用。Pelican和Jekyll都将用Markdown或reStructuredText编写的内容转换为HTML以生成静态网站,两个生成器都支持允许无限制自定义的主题。
在本文中,将介绍如何安装Pelican,设置GitHub存储库,运行快速入门帮助程序,编写一些Markdown文件以及发布第一页。要学习本文,需要注册一个GitHub帐户,对基本的Git命令感到满意,并希望使用Pelican发布博客。

安装Pelican
首先,必须在本地计算机上安装Pelican(和 ghp-import )。pip来安装是非常容易的,Python包安装工具:
$ pip install pelican ghp-import Markdown
接下来,打开浏览器并在GitHub上为您的新博客创建一个新的存储库。将其命名如下(在此处和本教程中,将您的GitHub用户名替换为<username>):
https://GitHub.com/username/username.github.io
使用命令行(命令行权限?),将空的Git存储库克隆到本地计算机:
$ git clone https:// GitHub.com / username / username.github.io blog
$ cd blog
这是关于在GitHub上发布Web内容的一个不太明显的技巧。对于用户页面(在名为 http:// username.github.io 的 repos中托管的页面),内容由 主 分支提供。
强烈建议不要将所有Pelican配置文件和原始Markdown文件保留在 master中 ,而不仅仅是web内容。所以将Pelican配置和原始内容保存在一个单独的分支中,称之为 内容 。
我喜欢这种结构,因为我可以丢弃 master中的 所有文件并使用 内容 分支重新填充它。
$ git checkout -b content
切换到新分支'content'
配置 Pelican
现在是内容配置的时候了。Pelican提供了一个很棒的初始化工具,名为 pelican-quickstart ,它将向您提示设置博客的一系列选项。

$ pelican-quickstart
Welcome to pelican-quickstart v3.7.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? Super blog
> Who will be the author of this web site? username
> What will be the default language of this web site? [en]
> Do you want to specify a URL prefix? e.g., http://example.com (Y/n) n
> Do you want to enable article pagination? (Y/n)
> How many articles per page do you want? [10]
> What is your time zone? [Europe/Paris] US/Central
> Do you want to generate a Fabfile/Makefile to automate generation and publishing? (Y/n) y
> Do you want an auto-reload & simpleHTTP script to assist with theme and site development? (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 /Users/username/blog
可以在每个问题上采用默认值,除了:
- 网站标题,应该是独特和特殊的
- 网站作者,可以是个人用户名或您的全名
- 时区,按照你当地时区设置
- 上传到GitHub页面,在我们的案例中是“y”
在回答完所有问题后,Pelican将以下内容留在当前目录中:
$ ls
Makefile content / develop_server.sh *
fabfile.py output / pelicanconf.py
publishconf.py
推送博客
将所有Pelican生成的文件添加到本地Git仓库的 内容 分支,提交更改,并通过输入以下命令将本地更改推送到GitHub上托管的远程仓库:
$ git add。
$ git commit -m'initial pelican commit to content'
$ git push origin content
这不是非常令人兴奋,但如果我们需要将编辑内容还原到其中一个文件,它将会很方便。
撰写博客
好的,现在你可以搞博客了!您的所有博文,照片,图片,PDF等都将位于 内容 目录中,该目录最初为空。要开始创建第一个帖子和带有照片的“关于”页面,请输入:
$ cd content
$ mkdir pages images
$ cp / Users / username / SecretStash / HotPhotoOfMe.jpg images
$ touch first-post.md
$ touch pages / about.md
接下来,在您喜欢的文本编辑器中打开空文件 first-post.md 并添加以下内容:
标题:我的新博客
日期上的第一篇文章:<今天的日期>
作者:你的名字在这里
#我正在走向互联网名望和财富!
这是我在新博客上发表的第一篇文章!
前三行包含Pelican用于组织事物的元数据。你可以在那里放置许多不同的元数据; 再次,文档是您了解更多选项的最佳选择。
现在,打开空文件 页面/ about.md 并添加此文本:
标题:关于
日期:<今天的日期>
![So Schmexy] [my_sweet_photo]
嗨,我是<用户名>,我写了博客。
[my_sweet_photo]:{static} /images/HotPhotoOfMe.jpg
您现在在内容目录中有三个新的Web内容。内容分支。这是很多内容。
发布博客
别担心; 收益即将到来!
剩下要做的就是:
- 运行Pelican以在 输出中 生成静态HTML文件:
$ pelican content -o output -s publishconf.py
- 使用 ghp-import 将 输出 目录的内容添加到 主 分支:
$ ghp-import -m "Generate Pelican site" --no-jekyll -b master output
- 将本地主分支推送到远程仓库:
$ git push origin master
- 提交并将新内容推送到 内容 分支:
$ git add content
$ git commit -m 'added a first post, a photo and an about page'
$ git push origin content
天哪,博客发布成功!
现在,当你看到你发布的内容供大家看时,令人兴奋的部分就在这里!打开浏览器并输入:
https://username.github.io
恭喜您的新博客,在GitHub上自行发布!只要您想添加更多页面或文章,就可以遵循此模式。
当然,良好的Pelican使用,绝对需要听取长期使用高手的诀窍,下面就给出几个能更好使用Pelican的诀窍。
Pelican的诀窍

自定义404页面
当浏览器请求Web服务器找不到的资源时,Web服务器通常显示通用的“未找到文件”(404)错误页面,该页面可能是鲜明且难看的。提供与您网站主题相匹配的错误页面的一种方法是创建自定义404页面,例如此Markdown格式示例:
标题:未找到
状态:隐藏
Save_as:404.html
找不到请求的项目。也许你可能想检查一下
[档案](/ archives.html)?
下一步是配置Web服务器以显示此自定义页面而不是其默认的404页面。对于Nginx,将以下内容添加到配置文件的location块中:
error_page 404 /404.html;
对于Apache:
ErrorDocument 404 /404.html
发布到Github
GitHub Pages提供了一种简单方便的方式来发布Pelican站点。GitHub页面有两种类型: 项目页面 和 用户页面 。Pelican站点可以作为项目页面和用户页面发布。
项目页面
要将Pelican站点作为项目页面发布,您需要 将 output Pelican生成的目录内容 推 送到gh-pagesGitHub上的存储库分支。
可以安装的优秀ghp-importpip使这个过程非常简单。
例如,如果Pelican站点的源包含在GitHub存储库中,并且您希望以项目页面的形式将该Pelican站点发布到此存储库,则可以使用以下命令:
$ pelican content -o output -s pelicanconf.py
$ ghp-import output
$ git push origin gh-pages
该命令使用目录的内容更新本地分支(如果该分支尚不存在,则创建分支)。该命令更新远程 分支,有效地发布Pelican站点。ghp-import outputgh-pagesoutputgit push origin gh-pagesgh-pages
注意
如上所述,该命令创建github的Makefile(以及gh_pagesFabfile 的任务)的目标pelican-quickstart将Pelican站点发布为项目页面。
Windows上的ghp-import
在接受ghp-import Pull Request#25之前 ,您需要安装ghp-import的自定义构建:pip install https://github.com/chevah/ghp-import/archive/win-support.zip
用户页面
要以用户页面的形式发布Pelican站点,您需要 将 output Pelican生成的目录内容 推 送到GitHub上master的<username>.github.io存储库分支。
再次,你可以利用ghp-import:
$ pelican content -o output -s pelicanconf.py