使用 Python 在 GitHub Pages 上运行你的博客

使用 Pelican 创建博客,这是一个基于 Python 的博客平台,与 GitHub 配合良好。
199 位读者喜欢这篇文章。
Raspberry Pi and Python

Raspberry Pi 基金会。 CC BY-SA 4.0。

GitHub 是一个非常流行的源代码控制网络服务,它使用 Git 来同步本地文件与保存在 GitHub 服务器上的副本,以便你可以轻松共享和备份你的工作。

除了为代码仓库提供用户界面外,GitHub 还允许用户直接从仓库发布网页。GitHub 推荐的网站生成包是 Jekyll,它是用 Ruby 编写的。由于我更喜欢 Python,所以我更喜欢 Pelican,这是一个基于 Python 的博客平台,与 GitHub 配合良好。

Pelican 和 Jekyll 都将以 MarkdownreStructuredText 编写的内容转换为 HTML 以生成静态网站,并且这两个生成器都支持允许无限自定义的主题。

在本文中,我将介绍如何安装 Pelican,设置你的 GitHub 仓库,运行快速入门助手,编写一些 Markdown 文件,并发布你的第一个页面。我假设你有一个 GitHub 账户,熟悉基本的 Git 命令,并且想要使用 Pelican 发布博客。

安装 Pelican 并创建仓库

首先,Pelican(和 ghp-import)必须安装在你的本地机器上。使用 Python 包安装工具 pip 非常容易(你有 pip 吗?)

$ 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 内容的不是很明显的技巧。对于用户页面(托管在名为 username.github.io 的仓库中的页面),内容从 master 分支提供。

我强烈建议不要将所有 Pelican 配置文件和原始 Markdown 文件保存在 master 分支中,而只保留 Web 内容。所以我将 Pelican 配置和原始内容保存在一个单独的分支中,我喜欢称之为 content。(你可以随意命名,但以下说明将称之为 content。)我喜欢这种结构,因为我可以删除 master 分支中的所有文件,并用 content 分支重新填充它。

$ git checkout -b content
Switched to a new branch '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 Pages,在我们的例子中是 “y”

在回答完所有问题后,Pelican 在当前目录中留下以下内容

$ ls
Makefile		content/	develop_server.sh*
fabfile.py		output/		pelicanconf.py
publishconf.py

你可以查看 Pelican 文档以了解如何使用这些文件,但我们现在只想快速完成。不,我也还没读过文档。

继续前进

将所有 Pelican 生成的文件添加到本地 Git 仓库的 content 分支,提交更改,并通过输入以下内容将本地更改推送到托管在 GitHub 上的远程仓库

$ git add .
$ git commit -m 'initial pelican commit to content'
$ git push origin content

这不是很令人兴奋,但如果我们需要还原对其中一个文件的编辑,它将非常方便。

终于有点进展了

好了,现在你可以开始写博客了!你的所有博客文章、照片、图像、PDF 等都将位于 content 目录中,该目录最初是空的。要开始创建第一篇文章和一个带有照片的“关于”页面,请输入

$ cd content
$ mkdir pages images
$ cp /Users/username/SecretStash/HotPhotoOfMe.jpg images
$ touch first-post.md
$ touch pages/about.md

接下来,在你最喜欢的文本编辑器中打开空文件 first-post.md 并添加以下内容

title: First Post on My Sweet New Blog
date: <today's date>
author: Your Name Here

# I am On My Way To Internet Fame and Fortune!

This is my first post on my new blog. While not super informative it
should convey my sense of excitement and eagerness to engage with you,
the reader!

前三行包含 Pelican 用于组织事物的元数据。你可以放置很多不同的元数据;再说一次,文档是了解更多选项的最佳选择。

现在,打开空文件 pages/about.md 并添加此文本

title: About
date: <today's date>

![So Schmexy][my_sweet_photo]

Hi, I am <username> and I wrote this epic collection of Interweb
wisdom. In days of yore, much of this would have been deemed sorcery
and I would probably have been burned at the stake.

?

[my_sweet_photo]: {static}/images/HotPhotoOfMe.jpg

你现在在 content 目录中有了三份新的 Web 内容。在 content 分支中。内容真不少。

发布

别担心;回报就要来了!

剩下要做的就是

  • 运行 Pelican 以在 output 目录中生成静态 HTML 文件
    $ pelican content -o output -s publishconf.py
  • 使用 ghp-importoutput 目录的内容添加到 master 分支
    $ ghp-import -m "Generate Pelican site" --no-jekyll -b master output
  • 将本地 master 分支推送到远程仓库
    $ git push origin master
  • 提交并将新内容推送到 content 分支
    $ 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 上自助发布!无论何时你想添加更多页面或文章,你都可以遵循这种模式。祝你博客愉快。

标签
XENON coated avatar will glow red in the presence of aliens.
Erik O'Shaughnessy 是一位有主见但友好的 UNIX 系统程序员,在德克萨斯州过着美好的生活。在过去的二十年 (或更长时间!) 里,他曾在 IBM、Sun Microsystems、Oracle 以及最近的 Intel 工作,从事计算机系统性能相关的工作。

6 条评论

Pelican 要求你显式安装 Markdown 包才能处理 markdown,你应该将 Markdown 添加到 pip install 命令中

`pip install Markdown`

回复 Pelican requires you to have,作者是 jasper-zanjani

对于内联图像,你忘记了感叹号,而且看起来文档中更推荐使用 {static}。`![my_sweet_photo]({static}/images/image.jpg)` 会更清晰,因为 Pelican 文档说 {filename} 语法将来可能会被删除。

我不是想吹毛求疵,但我一直在努力实现你的教程,并且遇到了困难。

是的,你是在吹毛求疵,但公平地说,那里确实有一些小问题需要解决。我会用你的建议更新文本,以避免其他人遇到麻烦。感谢你花时间告诉我文章的问题!

回复 For the inline image you,作者是 jasper-zanjani

这实际上是一个非常有趣的教程,我做了笔记并通读了全文,如果 Pelican 有一些更漂亮的主题,我认为会有更多人选择这条路线,特别是对于他们项目的快速网页

回复 Yes you are nitpicking, but,作者是 JnyJny

将尝试通过 termux 安装!

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 国际许可协议获得许可。
© . All rights reserved.