diff --git a/src/release/demo.py b/src/release/demo.py index d62388a..92add75 100644 --- a/src/release/demo.py +++ b/src/release/demo.py @@ -2,22 +2,15 @@ # -*- coding: utf-8 -*- from utils import loadBook -from utils import saveFile from utils import txtRelease from utils import jsonRelease from utils import htmlRelease -from utils import releaseInfo +from utils import gitbookRelease -metadata, content = loadBook('rc-5') - -saveFile( - releaseInfo['txt'], txtRelease(metadata, content) -) -saveFile( - releaseInfo['json'], jsonRelease(metadata, content) -) +metadata, content = loadBook('rc-5') -saveFile( - releaseInfo['calibre'], htmlRelease(metadata, content) -) +txtRelease(metadata, content) +jsonRelease(metadata, content) +htmlRelease(metadata, content) +gitbookRelease(metadata, content) diff --git a/src/release/gitbook.py b/src/release/gitbook.py deleted file mode 100755 index c0d6dd8..0000000 --- a/src/release/gitbook.py +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import os -import re -import sys -import shutil -from common import loadData -from common import rootPath -from common import saveFile -from common import releaseInfo -from common import createFolder - - -def initFolder() -> None: - createFolder(releaseInfo['gitbookDir']) - createFolder(os.path.join(releaseInfo['gitbookDir'], './assets/')) - createFolder(os.path.join(releaseInfo['gitbookDir'], './content/')) - - -def markdownTransfer(content: str) -> str: - symbols = [ - '\\', '`', '*', '_', '~', - '{', '}', '[', ']', '(', ')', - '#', '+', '-', '.', '!', '|', - ] - for symbol in symbols: - content = content.replace(symbol, '\\' + symbol) # add `\` before symbol - return '  ' + content - - -def loadChapter(caption: str, content: list) -> str: - chapterNum = re.search(r'^第(\d+)章', caption)[1] - chapterNum = '0' * (3 - len(chapterNum)) + chapterNum # add `0` prefix - fileName = 'chapter-%s.md' % chapterNum - content = [markdownTransfer(x) for x in content] - saveFile( - os.path.join(releaseInfo['gitbookDir'], './content/', fileName), - '# %s\n\n%s\n' % (caption, '\n\n'.join(content)) - ) - return fileName - - -def loadChapters(chapters: dict) -> dict: - catalog = {} - for (title, chapter) in chapters.items(): - catalog[title] = loadChapter(title, chapter) - return catalog - - -def loadCover(metadata: dict) -> None: - resDownload = { - 'TXT 下载': 'https://res.343.re/Share/XXRS/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.txt', - 'MOBI 下载': 'https://res.343.re/Share/XXRS/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.mobi', - } - cover = '---\ndescription: 作者:%s\n---\n\n# %s\n\n' % (metadata['author'], metadata['name']) - cover += '

栩栩若生

\n\n' - cover += '\n>\n'.join(['> %s' % x for x in metadata['desc']]) + '\n\n' - for (resName, resUrl) in resDownload.items(): - cover += '{%% hint style="success" %%}\n### >>> [%s](%s) <<<\n{%% endhint %%}\n\n' % (resName, resUrl) - cover += '{% embed url="https://github.com/dnomd343/xxrs-crawler" %}\n项目地址\n{% endembed %}\n' - saveFile(os.path.join(releaseInfo['gitbookDir'], 'README.md'), cover) - - -def loadSummary(catalog: dict) -> None: - summary = '# XXRS\n\n' - summary += '* [序言](README.md)\n\n' - summary += '## 内容 \n\n' - for (title, mdFile) in catalog.items(): - summary += '* [%s](content/%s)\n' % (title, mdFile) - saveFile(os.path.join(releaseInfo['gitbookDir'], 'SUMMARY.md'), summary) - - -def loadGitbook(jsonName: str) -> None: - data = loadData(jsonName) - loadCover(data['metadata']) - catalog = loadChapters(data['content']) - loadSummary(catalog) - shutil.copy( - os.path.join(rootPath, './assets/cover.jpg'), - os.path.join(releaseInfo['gitbookDir'], './assets/cover.jpg') - ) - - -if __name__ == '__main__': - initFolder() - loadGitbook(sys.argv[1]) diff --git a/src/release/utils/__init__.py b/src/release/utils/__init__.py index c3016ba..a379427 100644 --- a/src/release/utils/__init__.py +++ b/src/release/utils/__init__.py @@ -2,9 +2,8 @@ # -*- coding: utf-8 -*- from .common import loadBook -from .common import saveFile -from .common import releaseInfo -from .sequence import txtRelease -from .sequence import jsonRelease -from .sequence import htmlRelease +from .release import txtRelease +from .release import jsonRelease +from .release import htmlRelease +from .release import gitbookRelease diff --git a/src/release/utils/common.py b/src/release/utils/common.py index a8aafbe..e122f1b 100644 --- a/src/release/utils/common.py +++ b/src/release/utils/common.py @@ -4,6 +4,8 @@ import os import json +projectUrl = 'https://github.com/dnomd343/xxrs-crawler' + rootPath = os.path.join( # project root directory os.path.dirname(os.path.realpath(__file__)), '../../../' ) @@ -24,6 +26,19 @@ releaseInfo = { # convert to absolute path key: os.path.join(releasePath, file) for (key, file) in releaseInfo.items() } +resourceInfo = { # resource download links + 'TXT 下载': [ + 'https://res.343.re/Share/XXRS/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.txt', + 'https://cdn.dnomd343.top/xxrs/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.txt', + 'https://github.com/dnomd343/xxrs-crawler/releases/latest/download/XXRS.txt', + ], + 'MOBI 下载': [ + 'https://res.343.re/Share/XXRS/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.mobi', + 'https://cdn.dnomd343.top/xxrs/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.mobi', + 'https://github.com/dnomd343/xxrs-crawler/releases/latest/download/XXRS.mobi', + ], +} + def createFolder(folderName: str) -> None: # create folder if not os.path.exists(folderName): diff --git a/src/release/utils/release.py b/src/release/utils/release.py new file mode 100644 index 0000000..6c58e2a --- /dev/null +++ b/src/release/utils/release.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import shutil + +from .common import rootPath +from .common import saveFile +from .common import projectUrl +from .common import releaseInfo +from .common import createFolder +from .common import resourceInfo + +from .sequence import txtSerialize +from .sequence import jsonSerialize +from .sequence import htmlSerialize + +from .sequence import gitbookSummary +from .sequence import gitbookChapters +from .sequence import gitbookMetadata + + +def txtRelease(metadata: dict, content: dict) -> None: + saveFile( + releaseInfo['txt'], txtSerialize(metadata, content) + ) + + +def jsonRelease(metadata: dict, content: dict) -> None: + saveFile( + releaseInfo['json'], jsonSerialize(metadata, content) + ) + + +def htmlRelease(metadata: dict, content: dict) -> None: + saveFile( + releaseInfo['calibre'], htmlSerialize(metadata, content) + ) + + +def gitbookRelease(metadata: dict, content: dict) -> None: + createFolder(releaseInfo['gitbook']) + createFolder(os.path.join(releaseInfo['gitbook'], './assets/')) + createFolder(os.path.join(releaseInfo['gitbook'], './content/')) + + cover = gitbookMetadata(metadata) + for (resName, resUrls) in resourceInfo.items(): + cover += '{% hint style="success" %}\n' \ + + '### >>> [%s](%s) <<<\n' % (resName, resUrls[0]) \ + + '{% endhint %}\n\n' + cover += '{%% embed url="%s" %%}\n项目地址\n{%% endembed %%}\n' % projectUrl + + saveFile( + os.path.join(releaseInfo['gitbook'], 'README.md'), cover + ) + saveFile( + os.path.join(releaseInfo['gitbook'], 'SUMMARY.md'), gitbookSummary(content) + ) + for (chapterPath, chapterContent) in gitbookChapters(content).items(): + saveFile( + os.path.join(releaseInfo['gitbook'], chapterPath), chapterContent + ) + shutil.copy( # gitbook cover + os.path.join(rootPath, './assets/cover.jpg'), + os.path.join(releaseInfo['gitbook'], './assets/cover.jpg') + ) diff --git a/src/release/utils/sequence.py b/src/release/utils/sequence.py index 087ddea..ba9bddc 100644 --- a/src/release/utils/sequence.py +++ b/src/release/utils/sequence.py @@ -1,10 +1,24 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import os +import re import json +MarkdownSymbols = [ + '\\', '`', '*', '_', '~', + '{', '}', '[', ']', '(', ')', + '#', '+', '-', '.', '!', '|', +] -def jsonRelease(metadata: dict, content: dict) -> str: + +def markdownTransfer(content: str) -> str: + for symbol in MarkdownSymbols: + content = content.replace(symbol, '\\' + symbol) # add `\` before symbol + return '  ' + content # add chinese indentation + + +def jsonSerialize(metadata: dict, content: dict) -> str: return json.dumps({ 'metadata': metadata, 'content': content, @@ -19,7 +33,7 @@ def txtMetadata(metadata: dict) -> str: # txt metadata ) -def txtRelease(metadata: dict, content: dict) -> str: +def txtSerialize(metadata: dict, content: dict) -> str: result = [txtMetadata(metadata)] for (title, chapter) in content.items(): result.append('\n\n'.join([title] + chapter)) # combine txt content @@ -32,7 +46,7 @@ def htmlMetadata(metadata: dict) -> str: # html metadata ) -def htmlRelease(metadata: dict, content: dict) -> str: +def htmlSerialize(metadata: dict, content: dict) -> str: result = [htmlMetadata(metadata)] for (title, chapter) in content.items(): result.append( @@ -41,3 +55,36 @@ def htmlRelease(metadata: dict, content: dict) -> str: ) ) return '\n\n'.join(result) + '\n' + + +def gitbookMetadata(metadata: dict) -> str: + return '---\ndescription: 作者:%s\n---\n\n# %s\n\n' % ( + metadata['author'], metadata['name'] + ) + '

%s

\n\n' % ( + 'assets/cover.jpg', metadata['name'] + ) + '\n>\n'.join(['> %s' % x for x in metadata['desc']]) + '\n\n' + + +def gitbookChapterPath(caption: str) -> str: + chapterNum = re.search(r'^第(\d+)章', caption)[1] # match chapter number + chapterNum = '0' * (3 - len(chapterNum)) + chapterNum # add `0` prefix + return os.path.join('content', 'chapter-%s.md' % chapterNum) + + +def gitbookSummary(chapters: dict) -> str: + summary = '# XXRS\n\n' + summary += '* [序言](README.md)\n\n' + summary += '## 内容 \n\n' + for caption in chapters: + summary += '* [%s](%s)\n' % (caption, gitbookChapterPath(caption)) + return summary + + +def gitbookChapters(chapters: dict) -> dict: + result = {} + for (caption, content) in chapters.items(): + content = [markdownTransfer(x) for x in content] + result[gitbookChapterPath(caption)] = '# %s\n\n%s\n' % ( + caption, '\n\n'.join(content) + ) + return result