日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python实现GitBook工具

發布時間:2023/12/20 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python实现GitBook工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面

本工具是通過Python腳本實現 GitBook 自動 生成 執行 編譯 發布的功能

你可以在這里下載exe

使用

1. exe下載,并移動位置

將exe文件放在你的gitbook文件夾中,或者放在空文件夾中

2. file.md

創建 名為file.md的文件,在你要寫book的目錄下

注意: 這里file.md文件名不可更改

3. 編輯文件內容

類似這樣

01_JVM內存與垃概述.md 02_如何看術與JVM.md 03_為什學習JVM.md 04_面課程特點.md

4. 運行

gitbook-tools-21.4.18.exe

gitbook-tools-21.4.23.exe

5. 執行

1:生成md

運行這條選項會根據file.md每行的文本生成對應文件

并且在每個文件中自動加入 一級標題

現在就可以編寫主要內容了

2: 轉換SUMMARY

執行這條命令會根據file.md每行的文本生成

目錄格式的 SUMMARY.md

生成后的文件如下

如果你可以自己寫SUMMARY,這步可以忽略

3. 編譯build

這步相當于

在終端直接敲

gitbook build

不同的是,如果你沒有README文件,會自動創建

編譯后,會在當前目錄生成_book
文件夾,里面為編譯后的HTML,可供發布或部署

4. git 指令

git add _book git commit -m\"Commit by gitbook tool!!!\" git push

這里 add 只是add了 _book 文件夾

commit的信息的固定的

push時,如果是已經clone下來自己的庫,能夠直接push

否則要先登錄

5. gitee pages

gitee pages 部署,這個update

只有gitee pro 會員才能夠 支持自動 更新

但是這里可以通過py提供了一些代碼參考

先 tag一個 TODO

環境

Python: 3.7

GitBook CLI version: 2.3.2

GitBook version: 3.2.3

Node.js v15.8.0

npm@7.10.0

Pycharm 2021.3

Pyinstaller

Gitee Pages

Gitbook 介紹

GitBook 是一個基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 來制作精美的電子書,GitBook 并非關于 Git 的教程。

Gitbook教程

安裝遇到的問題

實現功能

  • 生成md文件列表,通過讀取文件,創建md文件

  • 生成SUMMARY.md 替換文件名 為 Gitbook的SUMMARY格式

  • build 編譯gitbook ,html格式以便發布

  • git 自動 push _book文件夾

  • gitee pages 自動update(dev)

  • 代碼

    ''' #!/usr/bin/env python # -*- coding:utf-8 -*- # Created by victor # Created Time: '2021/4/17 0:42' '''''' version: 21.4.18 TODO : gitee pages auto sync!!! ''' import os # import sys # import re# dir 是全路徑 # 比如: E:\Projects\PycharmProjects\untitled\test\database\redis\2019-09-25-deepin-install-redis.md # :param dir: # :return: class GitbookTool:def __init__(self):self.sum_file_name = "file.md"# 當前腳本目錄# self.pypath = sys.path[0]# self.pypath = os.getcwd()self.pypath = input("please input the root path(windows split symbol is \\)\n:")# self.root_path = self.pypath# sour_path source 源 路徑self.sour_path = self.pypath + "\\" + self.sum_file_name# SUMMARY.md 路徑self.summary_path = self.pypath + "\\" + "SUMMARY.md"print("| self.pypath >>> ",self.pypath)def repSpilt(self, path):"""替換路徑分隔符:param path::return:"""return path.replace("\\", "\\\\")def newFile(self, line, dirname):'''創建文件:param line::param dirname::return:'''print("| gen file >>>")# 新建文件的文件名,最后的\n去掉newName = dirname + "\\" + line[:-1]print("| \t", line[:-1])with open(newName, "w", encoding="utf-8") as f2:f2.write("# " + line[:-4])def for_line(self, file):'''讀取 sm文件,并遍歷行:param file::return:'''# 獲取目錄dirname = os.path.dirname(file)with open(file, "r", encoding="utf-8") as f1:i = 0for line in f1:if line == "\n":# line 是空行passelse:# 判斷line是不是最后一行if line[-1] != "\n":# 加上換行line += "\n"i += 1self.newFile(line, dirname)print("| gen ", i, "file success!!!")print("| path:", dirname)def gen_md(self):"""生成md文件:return:"""# 獲取輸入print("| -----------------------------------------------------")print("| md文件生成器")print("| 通過讀取file.md文件中的行數來創建文件")print("| 生成的文件會和源文件同目錄")print("| 注意:原有文件會被替換")# print("| 請輸入源目錄文件路徑,window用 \ 來分隔文件夾")# sourceFile = input(":")# E:\Projects\PycharmProjects\untitled\newFile# sourceFile = self.pypath + "\\" + "SUMMARY.md"self.for_line(self.repSpilt(self.sour_path))os.system('pause')def gitbook_build(self):'''編譯gitbook:return:'''# fname = self.pypath + +"\\"+"SUMMARY.md"# os.path.isfile(fname)rname = self.pypath + "\\" + "README.md"# print("rname >>> ",rname)if os.path.isfile(rname):# 文件存在passelse:with open(rname, "w", encoding="utf-8") as f2:f2.write("This file is generated by py script!!!\n")f2.write("Please write the contents of the README.md")print("| building...")os.chdir(self.pypath)os.system("gitbook build")os.system('pause')def replace_sum(self):with open(self.sour_path, "r", encoding="utf-8") as f1, open(self.summary_path, "w", encoding="utf-8") as f2:i = 0for line in f1:if line == "\n":# line 是空行passelse:# 判斷line是不是最后一行i += 1if line[-1] != "\n":# 加上換行line += "\n"f2.write("- [")f2.write(line[:-4])f2.write("](")f2.write(line[:-1])f2.write(")")f2.write("\n")print("| gen summary success!!!")print("| total effect line:",i)os.system('pause')def qucik_git(self):os.chdir(self.pypath)os.system("git add _book")os.system("git commit -m\"Commit by gitbook tool!!!\"")os.system("git push")os.system('pause')def menu(self):# 獲取輸入# print("| =========================================")# print("| ================ gitbook tools ================")print("| --------------------------- gitbook tools ---------------------------")print("| 1:生成md")print("| 2: 轉換SUMMARY")print("| 3: 編譯>HTML")print("| 4: 發布Git")print("| 0: exit()")print("| --------------------------- gitbook tools ---------------------------")return input("| choose operation you need:")if __name__ == '__main__':yt = '''┌─┐ ┌─┐ + +┌──┘ ┴───────┘ ┴──┐++│ ││ ─── │++ + + +███████───███████ │+│ │+│ ─┴─ ││ │└───┐ ┌───┘│ ││ │ + +│ ││ └──────────────┐│ ││ ├─┐│ ┌─┘│ │└─┐ ┐ ┌───────┬──┐ ┌──┘ + + + +│ ─┤ ─┤ │ ─┤ ─┤└──┴──┘ └──┴──┘ + + + +神獸保佑代碼無BUG!'''print(yt)print("| --------------------------- gitbook tools ---------------------------")print("| @version: 21.4.18")print("| @description: gitbook tools auto gen file & build & sync to git")print("| @author: victor")print("| @site: https://victorfengming.gitee.io/")print("| @introduce: https://victorfengming.gitee.io/comic/python-gitbook-tools/")print("| @readme: https://victorfengming.gitee.io/file/exe/gitbook-tools/readme.md")print("| @download: https://victorfengming.gitee.io/file/exe/gitbook-tools/gitbook-tools-21.4.18.exe")print("| --------------------------- gitbook tools ---------------------------")print("| 注意:使用前請將exe文件放到file.md同級目錄下")# print("| ========================================")# print(os.path.isfile("E:\\Projects\\PycharmProjects\\untitled\\newFiletest\\12.md"))gt = GitbookTool()while True:cho = gt.menu()if cho == "1":print(1)gt.gen_md()elif cho == "2":print(2)gt.replace_sum()elif cho == "3":print(3)gt.gitbook_build()elif cho == "4":print(4)gt.qucik_git()elif cho == "0":# print("| bye~")exit(0)

    gitee page 代碼(dev)

    from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait as Waitprint("start refresh gitee pages...")repo_user_name = "victorfengming" repo_name = "shell" login_user = "victorfengming" login_pwd = "xxxx"url = "https://gitee.com/"+repo_user_name+"/"+repo_name+"/pages"driver = "E:\\chrome\\chromedriver.exe" chrome_options = Options() chrome_options.add_argument("--window-size=1920,1080") chrome_options.add_argument("--start-maximized") chrome_options.add_argument("--headless") browser=webdriver.Chrome(executable_path=driver, options=chrome_options)browser.get(url)Wait(browser, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "item.git-nav-user__login-item"))) print("load finish. url=" + url) login_btn = browser.find_element_by_class_name("item.git-nav-user__login-item") login_btn.click()Wait(browser, 10).until(EC.presence_of_element_located((By.ID, "user_login"))) Wait(browser, 10).until(EC.presence_of_element_located((By.ID, "user_password"))) print("login page load finish.") user_input = browser.find_element_by_id("user_login") pwd_input = browser.find_element_by_id("user_password") login_btn = browser.find_element_by_name("commit") user_input.send_keys(login_user) pwd_input.send_keys(login_pwd) login_btn.click()Wait(browser, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "button.orange.redeploy-button.ui.update_deploy"))) print("login finish.") deploy_btn = browser.find_element_by_class_name('button.orange.redeploy-button.ui.update_deploy')browser.execute_script("window.scrollTo(100, document.body.scrollHeight);") deploy_btn.click() dialog = browser.switch_to.alert dialog.accept() print("refresh gitee pages finish.") browser.close()

    參考: https://www.jianshu.com/p/6460df84a099

    https://blog.csdn.net/weixin_29981095/article/details/113987875

    Pyinstaller

    設置 運行參數

    -F $FileNameWithoutExtension$.py

    TODO

    gitee pages 自動update(dev)

    tkinter界面

    附錄源碼

    cmd 版本 (fix bug)

    ''' #!/usr/bin/env python # -*- coding:utf-8 -*- # Created by victor # Created Time: '2021/4/17 0:42' '''''' version: 21.4.18 TODO : gitee pages auto sync!!! ''' import os # import sys # import re# dir 是全路徑 # 比如: E:\Projects\PycharmProjects\untitled\test\database\redis\2019-09-25-deepin-install-redis.md # :param dir: # :return: class GitbookTool:def __init__(self):self.sum_file_name = "file.md"# 當前腳本目錄# self.pypath = sys.path[0]# self.pypath = os.getcwd()self.pypath = input("please input the root path(windows split symbol is \\)\n:")# self.root_path = self.pypath# sour_path source 源 路徑self.sour_path = self.pypath + "\\" + self.sum_file_name# SUMMARY.md 路徑self.summary_path = self.pypath + "\\" + "SUMMARY.md"print("| self.pypath >>> ",self.pypath)def repSpilt(self, path):"""替換路徑分隔符:param path::return:"""return path.replace("\\", "\\\\")def newFile(self, line, dirname):'''創建文件:param line::param dirname::return:'''print("| gen file >>>")# 新建文件的文件名,最后的\n去掉newName = dirname + "\\" + line[:-1]print("| \t", line[:-1])with open(newName, "w", encoding="utf-8") as f2:f2.write("# " + line[:-4])def for_line(self, file):'''讀取 sm文件,并遍歷行:param file::return:'''# 獲取目錄dirname = os.path.dirname(file)with open(file, "r", encoding="utf-8") as f1:i = 0for line in f1:if line == "\n":# line 是空行passelse:# 判斷line是不是最后一行if line[-1] != "\n":# 加上換行line += "\n"i += 1self.newFile(line, dirname)print("| gen ", i, "file success!!!")print("| path:", dirname)def gen_md(self):"""生成md文件:return:"""# 獲取輸入print("| -----------------------------------------------------")print("| md文件生成器")print("| 通過讀取file.md文件中的行數來創建文件")print("| 生成的文件會和源文件同目錄")print("| 注意:原有文件會被替換")# print("| 請輸入源目錄文件路徑,window用 \ 來分隔文件夾")# sourceFile = input(":")# E:\Projects\PycharmProjects\untitled\newFile# sourceFile = self.pypath + "\\" + "SUMMARY.md"self.for_line(self.repSpilt(self.sour_path))os.system('pause')def gitbook_build(self):'''編譯gitbook:return:'''# fname = self.pypath + +"\\"+"SUMMARY.md"# os.path.isfile(fname)rname = self.pypath + "\\" + "README.md"print("rname >>> ",rname)if os.path.isfile(rname):# 文件存在passelse:with open(rname, "w", encoding="utf-8") as f2:f2.write("This file is generated by py script!!!\n")f2.write("Please write the contents of the README.md")# bookignore = self.pypath + +"\\"+"SUMMARY.md"# os.path.isfile(fname)bookignore = self.pypath + "\\" + ".bookignore"if os.path.isfile(bookignore):# 文件存在 啥也不干passelse:with open(bookignore, "w", encoding="utf-8") as f2:f2.write("file.md")print("| building...")os.chdir(self.pypath)os.system("gitbook build")os.system('pause')def replace_sum(self):with open(self.sour_path, "r", encoding="utf-8") as f1, open(self.summary_path, "w", encoding="utf-8") as f2:i = 0for line in f1:if line == "\n":# line 是空行passelse:# 判斷line是不是最后一行i += 1if line[-1] != "\n":# 加上換行line += "\n"f2.write("- [")f2.write(line[:-4])f2.write("](")f2.write(line[:-1])f2.write(")")f2.write("\n")print("| gen summary success!!!")print("| total effect line:",i)os.system('pause')def qucik_git(self):os.chdir(self.pypath)os.system("git add _book")os.system("git commit -m\"Commit by gitbook tool!!!\"")os.system("git push")os.system('pause')def menu(self):# 獲取輸入# print("| =========================================")# print("| ================ gitbook tools ================")print("| --------------------------- gitbook tools ---------------------------")print("| 1:生成md")print("| 2: 轉換SUMMARY")print("| 3: 編譯>HTML")print("| 4: 發布Git")print("| 0: exit()")print("| --------------------------- gitbook tools ---------------------------")return input("| choose operation you need:")if __name__ == '__main__':yt = '''┌─┐ ┌─┐ + +┌──┘ ┴───────┘ ┴──┐++│ ││ ─── │++ + + +███████───███████ │+│ │+│ ─┴─ ││ │└───┐ ┌───┘│ ││ │ + +│ ││ └──────────────┐│ ││ ├─┐│ ┌─┘│ │└─┐ ┐ ┌───────┬──┐ ┌──┘ + + + +│ ─┤ ─┤ │ ─┤ ─┤└──┴──┘ └──┴──┘ + + + +神獸保佑代碼無BUG!'''print(yt)print("| --------------------------- gitbook tools ---------------------------")print("| @version: 21.4.22")print("| @description: gitbook tools auto gen file & build & sync to git")print("| @author: victor")print("| @site: https://victorfengming.gitee.io/")print("| @introduce: https://victorfengming.gitee.io/comic/python-gitbook-tools/")print("| @readme: https://victorfengming.gitee.io/file/exe/gitbook-tools/readme.md")print("| @download: https://victorfengming.gitee.io/file/exe/gitbook-tools/gitbook-tools-21.4.22.exe")print("| --------------------------- gitbook tools ---------------------------")print("| 注意:使用前請將exe文件放到file.md同級目錄下")# print("| ========================================")# print(os.path.isfile("E:\\Projects\\PycharmProjects\\untitled\\newFiletest\\12.md"))gt = GitbookTool()while True:cho = gt.menu()if cho == "1":print(1)gt.gen_md()elif cho == "2":print(2)gt.replace_sum()elif cho == "3":print(3)gt.gitbook_build()elif cho == "4":print(4)gt.qucik_git()elif cho == "0":# print("| bye~")exit(0)

    圖形Tk版本

    main.py

    # 導包 from tkinter import * from tkinter import filedialog, messagebox from gitbook_tools import GitbookTool'''| --------------------------- gitbook tools --------------------------- | @version: 21.4.23 | @description: gitbook tools auto gen file & build & sync to git | @author: victor | @site: https://victorfengming.gitee.io/ | @introduce: https://victorfengming.gitee.io/comic/python-gitbook-tools/ | @readme: https://victorfengming.gitee.io/file/exe/gitbook-tools/readme.md | @download: https://victorfengming.gitee.io/file/exe/gitbook-tools/gitbook-tools-21.4.23.exe | --------------------------- gitbook tools --------------------------- | TODO : | 1. 遞歸掃描md文件,根據相對路徑 生成`SUMMARY.md` | 2. cmd 日志 放入 tk頁面 | 5. gitee pages auto update by chrome tools | --------------------------- gitbook tools ---------------------------''' class Tk_gui():def __init__(self, gt):'''初始化魔術方法用于設置界面的初始狀態'''# 創建tkinter窗口self.root = Tk()# 設置窗口的標題self.root.title('Gitbook Tools')# 設置窗口的長和寬,最大值和最小值設置相同,用戶不可調整窗口大小self.root.minsize(90, 180)self.root.maxsize(780, 180)self.gt = gtself.root_path = ""# 初始化# 初始化主要url# 調用主要邏輯執行函數self.main_logic()def main_logic(self):'''主業務邏輯:return:'''# 頂部信息欄topp = Frame()topp.grid(row=0, column=0)# 內容欄self.cont = Frame()self.cont.grid(row=1, column=0)# 輸入選項操作self.indo = Frame()self.indo.grid(row=0, column=1, rowspan=2)# 狀態欄self.stat = Frame()self.stat.grid(row=2, column=0)# self.get_path()self.put_button()# 加入主消息循環self.root.mainloop()# ## self.myStdout() # 實例化重定向類# self.restoreStd() # 恢復標準輸出def put_button(self):'''用于繪制頂部菜單:param topp::return:'''# 菜單欄# print("| 1:生成md")# print("| 2: 轉換SUMMARY")# print("| 3: 編譯>HTML")# print("| 4: 發布Git")# print("| 0: exit()")self.gen_button("設置工作路徑", self.get_path).grid(row=0, column=0)self.gen_button("生成md", lambda: self.button_run_before(gt.gen_md)()).grid(row=1, column=0)self.gen_button("轉換SUMMARY", lambda: self.button_run_before(gt.replace_sum)()).grid(row=2, column=0)self.gen_button("編譯>HTML", lambda: self.button_run_before(gt.gitbook_build)()).grid(row=3, column=0)self.gen_button("發布Git", lambda: self.button_run_before(gt.qucik_git)()).grid(row=4, column=0)def gen_button(self, text, method):'''生成 button:param text::param method::return:'''return Button(self.cont, text=text, command=method, width=22)def get_path(self):self.root_path = filedialog.askdirectory()print("getpath>>>",self.root_path)# 將路徑 從 圖形類 傳入 工具類self.gt.pypath = self.root_path# cmd 消息 放入 tk頁面 (dev)# def myStdout(self): # 重定向類# # 將其備份# self.stdoutbak = sys.stdout# self.stderrbak = sys.stderr# # 重定向# sys.stdout = self# sys.stderr = self## def write(self, info):# t = Text(self.cont) # 創建多行文本控件# t.pack() # 布局在窗體上# # info信息即標準輸出sys.stdout和sys.stderr接收到的輸出信息# t.insert('end', info) # 在多行文本控件最后一行插入print信息# t.update() # 更新顯示的文本,不加這句插入的信息無法顯示# t.see(END) # 始終顯示最后一行,不加這句,當文本溢出控件最后一行時,不會自動顯示最后一行## def restoreStd(self):# # 恢復標準輸出# sys.stdout = self.stdoutbak# sys.stderr = self.stderrbakdef button_run_before(self,func):# 判斷# if func != self.get_path and self.root_path == "":if self.root_path == "":messagebox.showinfo('錯誤','請先設置工作路徑')self.get_path()else:return func# print("| ========================================") # print(os.path.isfile("E:\\Projects\\PycharmProjects\\untitled\\newFiletest\\12.md"))# gt = GitbookTool(input("please input the root path(windows split symbol is \\)\n:"))gt = GitbookTool()t = Tk_gui(gt)

    gitbook_tools.py

    ''' #!/usr/bin/env python # -*- coding:utf-8 -*- # Created by victor # Created Time: '2021/4/17 0:42' '''''' version: 21.4.18 TODO : gitee pages auto sync!!! ''' import os # import sys # import re# dir 是全路徑 # 比如: E:\Projects\PycharmProjects\untitled\test\database\redis\2019-09-25-deepin-install-redis.md # :param dir: # :return: class GitbookTool:def __init__(self):self.sum_file_name = "file.md"self.summary_file_name = "SUMMARY.md"self.readme_name = "README.md"# 當前腳本目錄# self.pypath = sys.path[0]# self.pypath = os.getcwd()# self.pypath = input("please input the root path(windows split symbol is \\)\n:")self.pypath = ""# self.root_path = self.pypath# sour_path source 源 路徑self.sour_path = self.pypath + "\\" + self.sum_file_name# SUMMARY.md 路徑self.summary_path = self.pypath + "\\" + self.summary_file_nameprint("| self.pypath >>> ",self.pypath)def repSpilt(self, path):"""替換路徑分隔符:param path::return:"""return path.replace("\\", "\\\\")def newFile(self, line, dirname):'''創建文件:param line::param dirname::return:'''print("| gen file >>>")# 新建文件的文件名,最后的\n去掉newName = dirname + "\\" + line[:-1]print("| \t", line[:-1])with open(newName, "w", encoding="utf-8") as f2:f2.write("# " + line[:-4])def for_line(self, file):'''讀取 sm文件,并遍歷行:param file::return:'''# 獲取目錄dirname = os.path.dirname(file)with open(file, "r", encoding="utf-8") as f1:i = 0for line in f1:if line == "\n":# line 是空行passelse:# 判斷line是不是最后一行if line[-1] != "\n":# 加上換行line += "\n"i += 1self.newFile(line, dirname)print("| gen ", i, "file success!!!")print("| path:", dirname)def gen_md(self):"""生成md文件:return:"""# 獲取輸入print("| -----------------------------------------------------")print("| md文件生成器")print("| 通過讀取file.md文件中的行數來創建文件")print("| 生成的文件會和源文件同目錄")print("| 注意:原有文件會被替換")# print("| 請輸入源目錄文件路徑,window用 \ 來分隔文件夾")# sourceFile = input(":")# E:\Projects\PycharmProjects\untitled\newFile# sourceFile = self.pypath + "\\" + "SUMMARY.md"sour_path = self.pypath + "\\" + self.sum_file_nameself.for_line(self.repSpilt(sour_path))# os.system('pause')def gitbook_build(self):'''編譯gitbook:return:'''# fname = self.pypath + +"\\"+"SUMMARY.md"# os.path.isfile(fname)rname = self.pypath + "\\" + self.readme_nameprint("rname >>> ",rname)if os.path.isfile(rname):# 文件存在passelse:with open(rname, "w", encoding="utf-8") as f2:f2.write("This file is generated by py script!!!\n")f2.write("Please write the contents of the README.md")# bookignore = self.pypath + +"\\"+"SUMMARY.md"# os.path.isfile(fname)bookignore = self.pypath + "\\" + ".bookignore"if os.path.isfile(bookignore):# 文件存在 啥也不干passelse:with open(bookignore, "w", encoding="utf-8") as f2:f2.write("file.md\n")print("| building...")os.chdir(self.pypath)os.system("gitbook build")# os.system('pause')def replace_sum(self):# SUMMARY.md 路徑# 更新路徑summary_path = self.pypath + "\\" + self.summary_file_namesour_path = self.pypath + "\\" + self.sum_file_name# summary_path = self.pypath + "\\" + "SUMMARY.md"with open(sour_path, "r", encoding="utf-8") as f1, open(summary_path, "w", encoding="utf-8") as f2:i = 0for line in f1:if line == "\n":# line 是空行passelse:# 判斷line是不是最后一行i += 1if line[-1] != "\n":# 加上換行line += "\n"f2.write("- [")f2.write(line[:-4])f2.write("](")f2.write(line[:-1])f2.write(")")f2.write("\n")print("| gen summary success!!!")print("| total effect line:",i)# os.system('pause')def qucik_git(self):os.chdir(self.pypath)os.system("git add _book")os.system("git commit -m\"Commit by gitbook tool!!!\"")os.system("git push")# os.system('pause')def menu(self):# 獲取輸入# print("| =========================================")# print("| ================ gitbook tools ================")print("| --------------------------- gitbook tools ---------------------------")print("| 1:生成md")print("| 2: 轉換SUMMARY")print("| 3: 編譯>HTML")print("| 4: 發布Git")print("| 0: exit()")print("| --------------------------- gitbook tools ---------------------------")return input("| choose operation you need:")if __name__ == '__main__':yt = '''┌─┐ ┌─┐ + +┌──┘ ┴───────┘ ┴──┐++│ ││ ─── │++ + + +███████───███████ │+│ │+│ ─┴─ ││ │└───┐ ┌───┘│ ││ │ + +│ ││ └──────────────┐│ ││ ├─┐│ ┌─┘│ │└─┐ ┐ ┌───────┬──┐ ┌──┘ + + + +│ ─┤ ─┤ │ ─┤ ─┤└──┴──┘ └──┴──┘ + + + +神獸保佑代碼無BUG!'''print(yt)print("| --------------------------- gitbook tools ---------------------------")print("| @version: 21.4.23")print("| @description: gitbook tools auto gen file & build & sync to git")print("| @author: victor")print("| @site: https://victorfengming.gitee.io/")print("| @introduce: https://victorfengming.gitee.io/comic/python-gitbook-tools/")print("| @readme: https://victorfengming.gitee.io/file/exe/gitbook-tools/readme.md")print("| @download: https://victorfengming.gitee.io/file/exe/gitbook-tools/gitbook-tools-21.4.23.exe")print("| --------------------------- gitbook tools ---------------------------")# print("| 注意:使用前請將exe文件放到file.md同級目錄下")# print("| ========================================")# print(os.path.isfile("E:\\Projects\\PycharmProjects\\untitled\\newFiletest\\12.md"))gt = GitbookTool(input("please input the root path(windows split symbol is \\)\n:"))while True:cho = gt.menu()if cho == "1":print(1)gt.gen_md()elif cho == "2":print(2)gt.replace_sum()elif cho == "3":print(3)gt.gitbook_build()elif cho == "4":print(4)gt.qucik_git()elif cho == "0":# print("| bye~")exit(0)

    總結

    以上是生活随笔為你收集整理的Python实现GitBook工具的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。