使用 Python 在 GitHub 上運行你的博客

使用 Pelican 創建博客,這是一個基於 Python 的平臺,與 GitHub 配合的不錯。

GitHub 是一個非常流行的用於源代碼控制的 Web 服務,它使用 Git 同步本地文件和 GitHub 服務器上保留的副本,這樣你就可以輕鬆地共享和備份你的工作。

除了爲代碼倉庫提供用戶界面之外,GitHub 還運允許用戶直接從倉庫發佈網頁。GitHub 推薦的網站生成軟件包是 Jekll,是使用 Ruby 編寫的。因爲我是 Python 的忠實粉絲,所以我更喜歡 Pelican,這是一個基於 Python 的博客平臺,可與 GitHub 很好地協同工作。

Pelican 和 Jekll 都可以將 Markdown 或 reStructuredText 中編寫的內容轉換爲 HTML 以生成靜態網站,並且兩個生成器都支持定製的主題。

在本文中,我將介紹如何安裝 Pelican、設置 GitHub 倉庫、運行快速入門幫助、編寫一些 Markdown 文件以及發佈第一篇博客。我假設你有一個 GitHub 賬戶,熟悉基礎的 Git 命令,並且想使用 Pelican 發佈博客。

安裝 Pelican 並創建倉庫
首先,你必須在本地計算機上安裝 Pelican 和 ghp-import。使用 Python 軟件包安裝工具 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 中,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 頁面,我們選擇 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 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.
現在,content 目錄中將包含三個新的 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
OMG,我成功了
現在最激動的時候到了,當你想要看到你發佈給大家的博客內容時,打開瀏覽器輸入:

https://username.github.io
恭喜你可以在 GitHub 上發佈自己的博客了!當你想添加更多頁面或文章時,都可以按照上面的步驟來。希望你可以愉快地發佈博客。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章