GitHub 是一个非常流行的源代码控制 Web 服务,它使用 Git 将本地文件与 GitHub 服务器上保存的副本同步,以便你可以轻松共享和备份你的工作。
除了为代码仓库提供用户界面外,GitHub 还允许用户直接从仓库发布网页。GitHub 推荐的网站生成包是 Jekyll,用 Ruby 编写。由于我更喜欢 Python,所以我更喜欢 Pelican,这是一个基于 Python 的博客平台,与 GitHub 配合良好。
Pelican 和 Jekyll 都将用 Markdown 或 reStructuredText 编写的内容转换为 HTML 以生成静态网站,并且这两个生成器都支持允许无限自定义的主题。
在本文中,我将介绍如何安装 Pelican,设置你的 GitHub 仓库,运行快速入门助手,编写一些 Markdown 文件,以及发布你的第一个页面。我假设你有一个 GitHub 账户,熟悉基本的 Git 命令,并且想要使用 Pelican 发布博客。
安装 Pelican 并创建仓库
首先,必须在你的本地机器上安装 Pelican(和 ghp-import)。使用 Python 包安装工具 pip 可以非常容易地完成此操作(你已经安装了 pip 吧?)
$ pip install pelican ghp-import Markdown
接下来,打开浏览器并在 GitHub 上为你的新博客创建一个新的仓库。按照以下方式命名(在此教程中以及后续内容中,将 <username> 替换为你的 GitHub 用户名)
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
你现在在你的内容目录中有了三个新的 Web 内容。在 content 分支中。内容真多。
发布
别担心;回报马上就来了!
剩下的就是
- 运行 Pelican 以在 output 中生成静态 HTML 文件
$ pelican content -o output -s publishconf.py
- 使用 ghp-import 将 output 目录的内容添加到 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 上自助发布!你可以随时按照此模式添加更多页面或文章。祝你博客愉快。
6 条评论