Nikola 安裝 Nikola 建網站 寫文章

寫一些不爲人知的文章,用 Nikola 很好。

Nikola 是俄羅斯人用 Python 語言寫的程序,其輸入可以是 Markdown 格式的文本文檔,而其輸出則爲 HTML 文件——網頁。

如果有了很多網頁,通常就意味着有了網站,所以像 Nikola 這樣的程序,通常稱爲靜態網站生成器。爲什麼要有「靜態」這個定語呢?因爲 Nikola 生成的網頁,其內容不會發生變化。有內容發生變化的網頁嗎?有,鋪天蓋地的,例如那些可發表評論的網頁。

除了 Nikola,還有很多靜態網站生成器。Nikola 可能不僅不是其中最好的,而且名聲不顯。我覺得 Nikola 很好,是因爲我已用它多年。當初之所以選它,是因爲對於我以及我使用的計算機操作系統而言,它不會帶來多少負擔。

知道自己需要的究竟是什麼,對於我來說一直都是很難的事。好在我有幸略知一二。

安裝 Nikola

Nikola 的運行,需要 Python 環境的支持。在我的計算機操作系統裏,Python 環境是自然就有的,甚至有氾濫趨向。倘若沒有,但是想體驗一下 Nikola,無論是 Windows,還是 Linux,還是 Mac OS X,安裝最新版本的 Python 環境都不會難,難的是要克服一下內心深處瀰漫的對新事物不由自主的抗力。

習題:在日常使用的計算機操作系統裏安裝 Python 官方發佈的最新版本。

如果已經有了 Python 環境,可以用其中一個模塊 venv 提供的功能,將融入操作系統裏的 Python 環境萃取出來,放到一個專門的目錄(文件夾)裏。這個目錄就是一個虛擬的 Python 環境。

爲什麼系統裏有了一個可視爲實體的 Python 環境,還要構造一個虛擬的 Python 環境呢?爲了便於做一些依賴 Python 環境且自己沒有把握的事情,例如安裝並運行 Nikola。虛擬的 Python 環境就像沙盤,弄壞了,可以毀掉,再重新制作一個。

現在,我要構造一個名爲 my_nikola 的目錄,讓它作爲虛擬的 Python 環境,專用於擺弄 Nikola:

$ python -m venv my_nikola

然後進入這個目錄,運行該目錄內的一個特殊腳本(程序)便可將該目錄對應的虛擬 Python 環境激活:

$ cd my_nikola
$ source bin/active

注:上述命令適合 Linux 用戶。對於 Windows 用戶,可將第二條命令改爲:

$ Scripts\activate

Python 虛擬環境被激活後,會在當前命令窗口裏取代 Python 實體環境,其效用可持續至當前命令窗口關閉或執行以下命令:

$ deactivate

在爲安裝和運行 Nikola 激活 Python 虛擬環境後,還需要使用 Python 的 pip 模塊安裝或更新幾個重要模塊(包括 pip 自身):

$ python -m pip install -U pip setuptools wheel

然後便可安裝 Nikola 及其相關組件:

$ python -m pip install -U "Nikola[extras]"

上述命令會自動下載並安裝 Nikola 依賴的其他 Python 模塊。若該過程未出現任何錯誤信息,則說明 Nikola 安裝成功。

日後,若 Nikola 有新版本發佈,只需激活 my_nikola 目錄裏的 Python 虛擬環境,更新 pip、setuptools 以及 wheel 模塊,然後執行上述的 Nikola 安裝命令。

建網站

在激活了 my_nikola 目錄內的 Python 虛擬環境的命令窗口中,執行以下命令

$ nikola init --demo my_site

然後只需要根據自己的情況,回答 nikola 程序幾個問題(有些問題若不知如何回答,就不回答,摁回車鍵,跳過去):

This is Nikola v8.1.2.  We will now ask you a few easy questions about your new site.
If you do not want to answer and want to go with the defaults instead, simply restart with the `-q` parameter.
--- Questions about the site ---
Site title [My Nikola Site]: AGN 項目
Site author [Nikola Tesla]: garfileo
Site author's e-mail [[email protected]]: [email protected]
Site description [This is a demo site for Nikola.]: 重建的 AGN 項目。
Site URL [https://example.com/]: 
Enable pretty URLs (/page/ instead of /page.html) that don't need web server configuration? [Y/n] n
--- Questions about languages and locales ---
We will now ask you to provide the list of languages you want to use.
Please list all the desired languages, comma-separated, using ISO 639-1 codes.  The first language will be used as the default.
Type '?' (a question mark, sans quotes) to list available languages.
Language(s) to use [en]: en

Please choose the correct time zone for your blog. Nikola uses the tz database.
You can find your time zone here:
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Time zone [Asia/Shanghai]: 
    Current time in Asia/Shanghai: 15:33:47
Use this time zone? [Y/n] Y
--- Questions about comments ---
You can configure comments now.  Type '?' (a question mark, sans quotes) to list available comment systems.  If you do not want any comments, just leave the field blank.
Comment system: 

That's it, Nikola is now configured.  Make sure to edit conf.py to your liking.
If you are looking for themes and addons, check out https://themes.getnikola.com/ and https://plugins.getnikola.com/.
Have fun!
[2021-01-20 15:33:55] INFO: init: A new site with example data has been created at my_site.
[2021-01-20 15:33:55] INFO: init: See README.txt in that folder for more information.

然後,nikola 會在當前目錄裏構建一個名爲 my_site 的目錄,進入該目錄:

$ cd my_site

然後執行

$ nikola build

便得到了一個初建的網站。不過,若想看看看它,就必須明白,網站需要先在服務器上運行起來,然後由客戶端訪問。服務器,是一臺計算機。客戶端,也是一臺計算機。由於主流的操作系統可以在一臺計算機上同時模擬服務器和客戶端,所以,省了好多錢。

在 my_site 目錄內繼續執行

$ nikola serve

運行 Nikola 實現的網站服務器程序。然後,在網頁瀏覽器裏輸入 127.0.0.1:8000,便可打開在上述命令的驅動下由 Nikola 生成的網站的首頁。

運行了網站服務器程序後,命令行窗口就無法再輸入命令。若想繼續輸入其他命令,可使用 Ctrl + C 鍵關閉服務器程序。

寫文章

先不理睬 Nikola 在網站首頁如何彰顯自己的存在,更緊迫的是要在首頁上彰顯自己。

現在,我要寫一篇題目爲「我寫故我在」的文章。首先需要在激活 my_nikola 目錄內的 Python 虛擬環境的命令窗口中,進入 my_site 目錄,然後執行創建新文章的命令:

$ nikola new_post -f markdown

其中,new_post 選項驅動 Nikola 創建新文檔,-f markdown 選項告訴 Nikola,新文檔的格式是 Markdown。對於寫一般性的文檔,Markdown 格式夠用。

Nikola 在執行上述命令後,會問文章的標題是什麼:

Creating New Post
-----------------

Title: 我寫故我在
Scanning posts........done!
[2021-01-20 16:36:47] INFO: new_post: Your post's text is at: posts/wo-xie-gu-wo-zai.md

告訴它標題,然後回車,Nikola 便會在 my_site 的子目錄 posts 內創建 wo-xie-gu-wo-zai.md 文件。然後使用自己熟悉的文本編輯器,在對 Markdown 格式有所熟悉的前提下,打開這份文件,剩下的就是如何寫出好文章的事了。

假設文章撰寫完畢,在 my_site 目錄執行

$ nikola build

然後重新運行服務器:

$ nikola serve

再在網頁瀏覽器裏打開 127.0.0.1:8000,便可看到自己的存在感。

習題:設法讓 Nikola 服務器一直保持運行,讓文章的撰寫、修改、發佈和閱讀等過程的銜接更緊密。

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