Gitbook 超過兩層目錄無法創建檔案/目錄的解套

因為gitbook作者認為一本書的章節目錄不應該過於複雜
所以原本預設只有兩層目錄
在去年一堆相關討論串結束後雖然已讓目錄可以超過兩層
但是gitbook (github源)卻無法建立相對應的目錄及檔案

在爬了許多code後 (/usr/lib/node_modules/gitbook)決定還是自己寫一個parse code來處理

這邊在讀完SUMMARY.md後會自動偵測檔案目錄是否不存在
不存在就直接創建
預設只抓三層檔案目錄的結構

# !/usr/bin/python
# -*- coding: utf-8 -*-

import re
import os

for line in open("SUMMARY.md"):
    try:
        src1 = re.search('((.+?))', line)
        src = src1.group(1)
    except:
        print line
    else:
        cnt = src.count('/')
        if cnt == 3:
            str = re.search('(.+?)/(.+?)/(.+?)/(.*)', src)
            path = str.group(1) + "/" + str.group(2) + "/" + str.group(3)
            file = str.group(1) + "/" + str.group(2) + "/" + str.group(3) + "/" + str.group(4)
            if not os.path.isdir(path):
                print "create folder: " + path
                os.mkdir(path)
            if not os.path.isfile(file):
                print "create file: " + file
                open(file, "a").close()
            print str.group(1) + ".." + str.group(2) + ".." + str.group(3) + ".." + str.group(4)

        elif cnt == 2:
            str = re.search('(.+?)/(.+?)/(.*)', src)
            path = str.group(1) + "/" + str.group(2)
            file = str.group(1) + "/" + str.group(2) + "/" + str.group(3)
            if not os.path.isdir(path):
                print "create folder: " + path
                os.mkdir(path)
            if not os.path.isfile(file):
                print "create file: " + file
                open(file, "a").close()
            print str.group(1) + ".." + str.group(2) + ".." + str.group(3)
This entry was posted in Gitbook, Python. Bookmark the permalink.