diff --git a/src/release/calibre_html.py b/src/release/calibre_html.py index 6fea4a1..7655e0e 100755 --- a/src/release/calibre_html.py +++ b/src/release/calibre_html.py @@ -4,8 +4,7 @@ import sys from common import loadData from common import saveFile - -htmlFile = 'xxrs.html' +from common import releaseInfo def formatMetadata(metadata: dict) -> str: @@ -25,4 +24,7 @@ def htmlRelease(metadata: dict, content: dict) -> str: if __name__ == '__main__': data = loadData(sys.argv[1]) - saveFile(htmlFile, htmlRelease(data['metadata'], data['content'])) + saveFile( + releaseInfo['htmlFile'], + htmlRelease(data['metadata'], data['content']) + ) diff --git a/src/release/common.py b/src/release/common.py index 316879e..0b66f5f 100644 --- a/src/release/common.py +++ b/src/release/common.py @@ -9,8 +9,17 @@ rootPath = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../../' ) dataPath = os.path.join(rootPath, './release/') +releasePath = os.path.join(dataPath, './output/') metadataFile = os.path.join(rootPath, './assets/metadata.json') +releaseInfo = { + 'txtFile': 'xxrs.txt', + 'htmlFile': 'xxrs.html', + 'jsonFile': 'xxrs.json', + 'gitbookDir': 'xxrs-online', +} +releaseInfo = {x: os.path.join(releasePath, y) for (x, y) in releaseInfo.items()} + def loadData(jsonName: str) -> dict: # load book data from json file content = json.loads(open( @@ -31,3 +40,6 @@ def saveFile(fileName: str, content: str) -> None: # save into file def createFolder(folderName: str) -> None: # create folder if not os.path.exists(folderName): os.mkdir(folderName) + + +createFolder(releasePath) diff --git a/src/release/gitbook.py b/src/release/gitbook.py index 4ae96e7..9f4b577 100755 --- a/src/release/gitbook.py +++ b/src/release/gitbook.py @@ -8,15 +8,14 @@ import shutil from common import loadData from common import rootPath from common import saveFile +from common import releaseInfo from common import createFolder -workDir = './xxrs-online/' - def initFolder() -> None: - createFolder(workDir) - createFolder(os.path.join(workDir, './assets/')) - createFolder(os.path.join(workDir, './content/')) + createFolder(releaseInfo['gitbookDir']) + createFolder(os.path.join(releaseInfo['gitbookDir'], './assets/')) + createFolder(os.path.join(releaseInfo['gitbookDir'], './content/')) def loadChapter(caption: str, content: list) -> str: @@ -24,7 +23,7 @@ def loadChapter(caption: str, content: list) -> str: chapterNum = '0' * (3 - len(chapterNum)) + chapterNum # add `0` prefix fileName = 'chapter-%s.md' % chapterNum saveFile( - os.path.join(workDir, './content/', fileName), + os.path.join(releaseInfo['gitbookDir'], './content/', fileName), '# %s\n\n%s\n' % (caption, '\n\n'.join(content)) ) return fileName @@ -42,7 +41,7 @@ def loadCover(metadata: dict) -> None: cover += '

栩栩若生

\n\n' cover += '\n>\n'.join(['> %s' % x for x in metadata['desc']]) + '\n\n' cover += '{% embed url="https://github.com/dnomd343/xxrs-crawler.git" %}\n项目地址\n{% endembed %}\n' - saveFile(os.path.join(workDir, 'README.md'), cover + '\n') + saveFile(os.path.join(releaseInfo['gitbookDir'], 'README.md'), cover + '\n') def loadSummary(catalog: dict) -> None: @@ -51,7 +50,7 @@ def loadSummary(catalog: dict) -> None: summary += '## 内容 \n\n' for (title, mdFile) in catalog.items(): summary += '* [%s](content/%s)\n' % (title, mdFile) - saveFile(os.path.join(workDir, 'SUMMARY.md'), summary) + saveFile(os.path.join(releaseInfo['gitbookDir'], 'SUMMARY.md'), summary) def loadGitbook(jsonName: str) -> None: @@ -61,7 +60,7 @@ def loadGitbook(jsonName: str) -> None: loadSummary(catalog) shutil.copy( os.path.join(rootPath, './assets/cover.jpg'), - os.path.join(workDir, './assets/cover.jpg') + os.path.join(releaseInfo['gitbookDir'], './assets/cover.jpg') ) diff --git a/src/release/json_dumps.py b/src/release/json_dumps.py index 5adefa6..c316be6 100755 --- a/src/release/json_dumps.py +++ b/src/release/json_dumps.py @@ -5,8 +5,7 @@ import sys import json from common import loadData from common import saveFile - -jsonFile = 'xxrs.json' +from common import releaseInfo def jsonRelease(metadata: dict, content: dict) -> str: @@ -18,4 +17,7 @@ def jsonRelease(metadata: dict, content: dict) -> str: if __name__ == '__main__': data = loadData(sys.argv[1]) - saveFile(jsonFile, jsonRelease(data['metadata'], data['content'])) + saveFile( + releaseInfo['jsonFile'], + jsonRelease(data['metadata'], data['content']) + ) diff --git a/src/release/release_txt.py b/src/release/txt_release.py similarity index 81% rename from src/release/release_txt.py rename to src/release/txt_release.py index d148554..0b1c67c 100755 --- a/src/release/release_txt.py +++ b/src/release/txt_release.py @@ -4,8 +4,7 @@ import sys from common import loadData from common import saveFile - -txtFile = 'xxrs.txt' +from common import releaseInfo def formatMetadata(metadata: dict) -> str: @@ -25,4 +24,7 @@ def txtRelease(metadata: dict, content: dict) -> str: if __name__ == '__main__': data = loadData(sys.argv[1]) - saveFile(txtFile, txtRelease(data['metadata'], data['content'])) + saveFile( + releaseInfo['txtFile'], + txtRelease(data['metadata'], data['content']) + )