使用 Python 在 GitHub 上運(yùn)行你的博客
使用 Pelican 創(chuàng)建博客,這是一個基于 Python 的平臺,與 GitHub 配合的不錯。
GitHub 是一個非常流行的用于源代碼控制的 Web 服務(wù),它使用 Git 同步本地文件和 GitHub 服務(wù)器上保留的副本,這樣你就可以輕松地共享和備份你的工作。
除了為代碼倉庫提供用戶界面之外,GitHub 還運(yùn)允許用戶直接從倉庫發(fā)布網(wǎng)頁。GitHub 推薦的網(wǎng)站生成軟件包是 Jekll,是使用 Ruby 編寫的。因?yàn)槲沂?Python 的忠實(shí)粉絲,所以我更喜歡 Pelican,這是一個基于 Python 的博客平臺,可與 GitHub 很好地協(xié)同工作。
Pelican 和 Jekll 都可以將 Markdown 或 reStructuredText 中編寫的內(nèi)容轉(zhuǎn)換為 HTML 以生成靜態(tài)網(wǎng)站,并且兩個生成器都支持定制的主題。
在本文中,我將介紹如何安裝 Pelican、設(shè)置 GitHub 倉庫、運(yùn)行快速入門幫助、編寫一些 Markdown 文件以及發(fā)布第一篇博客。我假設(shè)你有一個 GitHub 賬戶,熟悉基礎(chǔ)的 Git 命令,并且想使用 Pelican 發(fā)布博客。
安裝 Pelican 并創(chuàng)建倉庫
首先,你必須在本地計(jì)算機(jī)上安裝 Pelican 和 ghp-import。使用 Python 軟件包安裝工具 pip(你有,對吧?),這非常容易:
- $ pip install pelican ghp-import Markdown
然后,打開瀏覽器并在 GitHub 上為你新鮮出爐的博客創(chuàng)建一個新倉庫,命名如下(在此處以及整個教程中,用 GitHub 用戶名替換 username):
- https://GitHub.com/username/username.github.io
讓它保持為空,稍后我們用引人注目的博客內(nèi)容來填充它。
使用命令行(確保正確),將這個空 Git 倉庫克隆到本地計(jì)算機(jī):
- $ git clone <https://GitHub.com/username/username.github.io> blog
- $ cd blog
奇怪的把戲…
在 GitHub 上發(fā)布 Web 內(nèi)容有一個不太引入注意的技巧,對于托管在名為 username.github.io 的倉庫的用戶頁面,其內(nèi)容由 master 分支提供服務(wù)。
我強(qiáng)烈建議所有的 Pelican 配置文件和原始的 Markdown 文件都不要保留在 master 中,master 中只保留 Web 內(nèi)容。因此,我將 Pelican 配置和原始內(nèi)容保留在一個我喜歡稱為 content 的單獨(dú)分支中。(你可以隨意創(chuàng)建一個分支,但以下內(nèi)容沿用 content。)我喜歡這種結(jié)構(gòu),因?yàn)槲铱梢苑艞壍?master 中的所有文件,然后用 content 分支重新填充它。
- $ git checkout -b content
- Switched to a new branch 'content'
配置 Pelican
現(xiàn)在該進(jìn)行內(nèi)容配置了。Pelican 提供了一個很棒的初始化工具 pelican-quickstart,它會詢問你有關(guān)博客的一系列問題。
- $ 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
你可以對每個問題都采用默認(rèn)值,但除了以下這些問題:
- 網(wǎng)站標(biāo)題,應(yīng)該唯一且特殊
- 網(wǎng)站作者,可以是個人用戶名或你的全名
- 時區(qū),可能你不在巴黎
- 上傳到 GitHub 頁面,我們選擇 y
回答完所有問題后,Pelican 會在當(dāng)前目錄中留下以下內(nèi)容:
- $ ls
- Makefile content/ develop_server.sh*
- fabfile.py output/ pelicanconf.py
- publishconf.py
你可以查看 Pelican 文檔來了解如何使用這些文件,但現(xiàn)在我們要做的是完成手頭的工作。說實(shí)話,我也沒有閱讀文檔。
繼續(xù)
將所有 Pelican 生成的文件添加到本地 Git 倉庫的 content 分支,提交更改,然后將本地更改推送到 Github 上托管的遠(yuǎn)程倉庫:
- $ git add .
- $ git commit -m 'initial pelican commit to content'
- $ git push origin content
這件事情并不是特別令人興奮,但是如果我們需要撤銷這些文件之一的修改時,這將非常方便。
終于
終于,現(xiàn)在你得到一個博客了!你所有的博客文章、照片、圖像、PDF 等都將位于 content 目錄中,它最初是空的。要開始創(chuàng)建第一篇博客和關(guān)于頁面,輸入:
- $ cd content
- $ mkdir pages images
- $ cp /Users/username/SecretStash/HotPhotoOfMe.jpg images
- $ touch first-post.md
- $ touch pages/about.md
接下來,在你喜歡的文本編輯器中打開 first-post.md,并添加以下內(nèi)容:
- 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 用于組織內(nèi)容的元數(shù)據(jù)。有很多不同的元數(shù)據(jù)可供你選擇。再說一次,文檔是你了解更多選項(xiàng)的最佳選擇。
現(xiàn)在,打開空白文件 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.
現(xiàn)在,content 目錄中將包含三個新的 Web 內(nèi)容,在 content 分支中還有很多內(nèi)容。
發(fā)布
不要急,馬上要見到成果了!
剩下要做的就是:
運(yùn)行 Pelican 以在 output 中生成靜態(tài) HTML 文件:
- $ pelican content -o output -s publishconf.py
使用 ghp-import 將 output 目錄的內(nèi)容添加到 master 分支中:
- $ ghp-import -m "Generate Pelican site" --no-jekyll -b master output
將本地 master 分支推送到遠(yuǎn)程倉庫:
- $ git push origin master
提交新內(nèi)容并將其推送到 content 分支
- $ git add content
- $ git commit -m 'added a first post, a photo and an about page'
- $ git push origin content
OMG,我成功了
現(xiàn)在最激動的時候到了,當(dāng)你想要看到你發(fā)布給大家的博客內(nèi)容時,打開瀏覽器輸入:
- https://username.github.io
恭喜你可以在 GitHub 上發(fā)布自己的博客了!當(dāng)你想添加更多頁面或文章時,都可以按照上面的步驟來。希望你可以愉快地發(fā)布博客。