Title: Python/pelican搭建個人博客
Date: 2015-12-11 09:23
Modified: 2015-12-11 09:23
Category: 腳本
Tags: Mac,python,pelican
Slug: python_pelican_blog
Authors: lizhonghuan
Summary: 本文使用python pelican搭建個人靜態(tài)博客,并上傳到個人git倉庫鞍匾,實現(xiàn)訪問個人博客满葛,本文作者所用就是基于該博客的個人主頁产艾。
PS:提升到系統(tǒng)root權(quán)限:$ sudo -s旷坦;
1.安裝git白热;
2.安裝pip過程:
查看pythion安裝目錄:
$ which python
/usr/local/bin/python
安裝pip:
$ sudo easy_install pip
第一次輸入時出現(xiàn)無法安裝庸诱,則可以選擇進行下面的安裝捻浦;
安裝最新版本的python,pip&setuptools:
$ brew install python
PS:brew 又叫Homebrew,是Mac OSX上的軟件包管理工具偶翅,能在Mac中方便的安裝軟件或者卸載軟件默勾,只需要一個命令,非常方便brew類似ubuntu系統(tǒng)下的apt-get的功能,http://brew.sh/ 安裝方法如下:
$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
詳細請見:http://www.cnblogs.com/TankXiao/p/3247113.html
3.安裝virtualenv:
$ pip install virtualenv
4.創(chuàng)建虛擬環(huán)境:
$ virtualenv ~/virtualenv/pelican
$ cd ~/virtualenvs/pelican
$ source bin/activate
5.安裝pelican:
$ pip install pelican
6.安裝markdown,typogrify:
$ pip install markdown
$ pip install typogrify
7.創(chuàng)建博客站點:
$ mkdir blog
$ cd blog
$ pelican-quickstart
pelican-quickstart執(zhí)行命令后,會提示輸入博客的配置項聚谁,除了少數(shù)幾個必填以外母剥,其它都可以選擇默認,而且都可以在pelicanconf.py文件中進行更改形导,所以你可以隨意選擇环疼。 命令成功執(zhí)行后,會出現(xiàn)pelican的框架朵耕,如下所示
blog/
├── content # 存放輸入的markdown或RST源文件
│ └── (pages) # 存放手工創(chuàng)建的靜態(tài)頁面炫隶,可選
│ └── (posts) # 存放手工創(chuàng)建的文章,可選
├── output # 存放最終生成的靜態(tài)博客
├── develop_server.sh # 測試服務(wù)器
├── Makefile # 管理博客的Makefile
├── pelicanconf.py # 配置文件
└── publishconf.py # 發(fā)布文件阎曹,可刪除
8.選擇博客主題:
在blog目錄下伪阶,各頁面主題可以在這個網(wǎng)址查看Pelican themes http://pelicanthemes.com/, 克隆主題開源庫 https://github.com/getpelican/pelican-themes
克隆主題到本地
$ git clone https://github.com/getpelican/pelican-themes.git
打開pelicanconf.py配置文件,更改或添加THEME為自己喜歡的主題处嫌,例如本博客所挑選的elegant栅贴,更多的配置含義請關(guān)注官方文檔。
注意下面的這個主題需要填主題的目錄路徑
THEME = 'pelican-themes/gum'
添加評論系統(tǒng):
開啟個人博客的原因在于分享知識熏迹,分享就需要交流檐薯,評論模塊當(dāng)然少不了。在Disqus上申請帳號注暗,按照流程Disqus會分配給你站點的Shortname坛缕,記牢Shortname,如果忘了請進入admin/settings中查看捆昏。然后同理赚楚,在pelicanconf.py添加
DISQUS_SITENAME = Shortname
生成博客站點:
$ Site generation
$ pelican /path/to/your/content/ [-s path/to/your/settings.py]
PS:上面的這個路徑是你自己博客的content路徑;
預(yù)覽生成的站點
For Python 2, run:
$ cd output
$ python -m SimpleHTTPServer
For Python 3, run:
$ cd output
$ python -m http.server
9.環(huán)境建好之后的事情:
OK,到這里骗卜,pelican的環(huán)境部分我們已經(jīng)配置完了直晨,不過博主好奇剛才pelican的安裝些什么了搀军?也就是說pelican的依賴項:
- feedgenerator, to generate the Atom feeds
- jinja2, for templating support
- pygments, for syntax highlighting
- docutils, for supporting reStructuredText as an input format
- pytz, for timezone definitions
- blinker, an object-to-object and broadcast signaling system
- unidecode, for ASCII transliterations of Unicode text
- six, for Python 2 and 3 compatibility utilities
- MarkupSafe, for a markup safe string implementation
- markdown, for supporting Markdown as an input format
好家伙,這么多啊勇皇,都看看 發(fā)現(xiàn)都不錯罩句!之后用到了在展開慢慢來說。
現(xiàn)在敛摘,讓我們看看pelican是多么神奇吧门烂,
$ pelican-quickstart
運行命令之后,在當(dāng)前目錄下有以下的文件
yourproject/
├── content
│ └── (pages)
├── output
├── develop_server.sh
├── fabfile.py
├── Makefile
├── pelicanconf.py # Main settings file
└── publishconf.py # Settings to use when ready to publish
我來說說都是些什么吧兄淫,
content這里是放置你的博文的屯远,例如我的hello_python.md文章;pages是讓永和可以自己定制些頁面捕虽,比如aboutme.md等等頁面慨丐;
output這個目錄下放置的就是一會利用pelican生成的靜態(tài)博客內(nèi)容,當(dāng)然是html的泄私;
pelicanconf.py房揭,是博客的配置文件,后面慢慢講晌端;
Makefile捅暴,make命令的配置文件,如果你懂linux這個就so easy咧纠!不過不懂也沒事蓬痒。
develop_server.sh 本地服務(wù)的腳本;
大致看完這個之后,我們可以先寫一篇自己的文章瞅瞅啊漆羔,文章模板如下:
Title: My super title
Date: 2010-12-03 10:20
Category: Python
Tags: pelican, publishing
Slug: my-super-post
Author: Alexis Metaireau
Summary: Short version for index and feeds
This is the content of my super blog post.
寫完保存后梧奢,要有以下的幾個命令來生成博客內(nèi)容啦;
10.git上創(chuàng)建個人主頁
首先在自己的git帳號下新建一個組織演痒,填寫組織名粹断;添加一個repository:usiege/usiege.github.com(必須用自己用戶名,且唯一)嫡霞;這里需要解決git多用戶的問題;將生成的靜態(tài)頁面push到repository中希柿;此時可以訪問username.github.com的靜態(tài)頁面诊沪;
利用下面的命令來生成你的博客site:
$ make html
我比較喜歡下面的這個命令,它是實時生成你的站點曾撤,就是說你修改你的博客什么的它會實時的生成端姚!很棒吧。
$ make regenerate
ok挤悉,生成之后渐裸,我們要看下顯示的效果,用下面的命令吧
$ make serve
下面這個我比較喜歡,理由同上面的那個regerate昏鹃,哈哈
$ make devserver
至此尚氛,我們可以在本地瀏覽剛才建好的博客了,地址就是http://localhost:8000
停止服務(wù)器則是下面的命令:
$ ./develop_server.sh>
創(chuàng)建成功以后洞渤,便可以把生成的頁面push到github阅嘶。
$ cd output
$ git init
$ git add .
$ git commit -m "first commit"
$ git remote add origin https://github.com/xxx/xxx.github.io.git
$ git push -u origin master
關(guān)于上傳的一些注意點:
$ git push origin master
git push命令會將本地倉庫推送到遠程服務(wù)器。
git pull命令則相反载迄。
修改完代碼后讯柔,使用git status可以查看文件的差別,使用git add 添加要commit的文件护昧,也可以用git add -i來智能添加文件魂迄。之后git commit提交本次修改,git push上傳到github惋耙。