利用git+hugo+markdown 搭建一個靜態網站

利用git+hugo+markdown 搭建一個靜態網站

一直想要有一個本身的文檔管理系統:html

  1. 能夠很方便書寫,並且相應的文檔很容易被分享
  2. 很方便的存儲、管理、歷史記錄
  3. 比較方面的瀏覽和查詢

第一點用Makrdown來寫文檔是一個很是好的選擇,第二點天然想到了git,
第三點用一個靜態的網站來瀏覽和管理是一個不錯的選擇,這裏選擇了hugo。python

Hugo是由Go語言實現的靜態網站生成器。 注意是生成器。他雖然自帶webserver,可是沒有Nigix強大了。
他能很是方便的把markdown文件轉換爲html。git

如何搭建

首先必須有一臺服務器,我選擇了阿里雲。而後:github

第一步: 安裝hugo

  1. 首先檢查系統的版本:cat /proc/version
  2. 直接用 sudo yum install hugo 發現不行,只能選擇本機安裝了
  3. 固然發現少了一個能上傳的客戶端: yum install lrzsz
  4. 由於是radhat,用hugo-0.16-2.el6.x86_64.rpm 包,而後sudo yum install hugo-0.16-2.el6.x86_64.rpm

第二步: 開始建立站點

  1. hugo new site hub_site
  2. cd hub_sit
  3. 安裝皮膚: cd themes; git clone https://github.com/key-amb/hugo-theme-bootie-docs.git themes/bootie-docs
    也能夠本地上傳
  4. 編輯: config.toml
  5. 加一個主頁: _index.md
  6. 加一個 about頁面: hugo new post/about.md
  7. 啓動:hugo server --buildDrafts -p 8080 --bind ip -b http://ip:8080/

這時候咱們就能夠看到第一個頁面了。web

第三步:和github結合起來

利用git來管理文檔是一個很是好的方式。這裏直接想到用github來存儲文檔。gitbhu支持收到push請求的時候調用固定的地址http。
因此咱們能夠用這個來實現完美的功能。服務器

首先在github上配置請求:settings->Webhooksmarkdown

而後咱們要在外面的服務器上搭建一個Http服務器來接受這個請求, 這裏選擇用python的import http.server來搭建,簡單方便:socket

class EntranceHttpRequestHandler(http.server.CGIHTTPRequestHandler):

    def do_POST(self):
        print('begin')

  if __name__=='__main__':
      handler = EntranceHttpRequestHandler.EntranceHttpRequestHandler
      httpd = socketserver.TCPServer(("", 8001), handler)
      httpd.serve_forever()

第四步 用python把整個鏈路鏈接起來

import http.server
import Convertor
import os
import _thread

TargetPath = "/root/root/site/content/post/blog"
GitSrcPath = "/root/root/site/blog"
HugeSitePath = "/root/root/site/"
HugeStatCommond = r'hugo server --buildDrafts -p 80 --bind 115.28.83.94 -b http://115.28.83.94/'
class EntranceHttpRequestHandler(http.server.CGIHTTPRequestHandler):

    def do_POST(self):
        print('begin')

        self.gitpull(GitSrcPath)

        self.stopHugo()

        convert = Convertor.Convertor()
        convert.excute(GitSrcPath,TargetPath)
        self.startHugo()
        print("finished")
        self.wfile.write(b"msg finished")


    def gitpull(self,  filePath):
        os.chdir(filePath)
        command = "git pull "
        os.system(command)

    def startHugo(self):
        _thread.start_new_thread(self.doStartHugo, ())

    def doStartHugo(self):
        os.chdir(HugeSitePath)
        output = os.system(HugeStatCommond)
        print(output)
        print('sartHugo finished')

    def stopHugo(self):
        command = 'kill -9 $(pidof hugo)'
        os.system(command)
        print('stopHugo finished')

其中Convert 是對文檔作一些分類和tag的轉換不詳細介紹。
到如今一個完整的網站就搭建完成了,每一次只要在本地push文檔,就能在網站上自動更新。post

完整代碼看這裏網站

相關文章
相關標籤/搜索