diff --git a/src/release/demo.py b/src/release/demo.py index 92add75..3ae4223 100644 --- a/src/release/demo.py +++ b/src/release/demo.py @@ -5,6 +5,7 @@ from utils import loadBook from utils import txtRelease from utils import jsonRelease from utils import htmlRelease +from utils import staticRelease from utils import gitbookRelease @@ -13,4 +14,5 @@ metadata, content = loadBook('rc-5') txtRelease(metadata, content) jsonRelease(metadata, content) htmlRelease(metadata, content) +staticRelease(metadata, content) gitbookRelease(metadata, content) diff --git a/src/release/utils/__init__.py b/src/release/utils/__init__.py index a379427..1f7f49c 100644 --- a/src/release/utils/__init__.py +++ b/src/release/utils/__init__.py @@ -6,4 +6,5 @@ from .common import loadBook from .release import txtRelease from .release import jsonRelease from .release import htmlRelease +from .release import staticRelease from .release import gitbookRelease diff --git a/src/release/utils/common.py b/src/release/utils/common.py index e122f1b..b3a11a2 100644 --- a/src/release/utils/common.py +++ b/src/release/utils/common.py @@ -4,6 +4,7 @@ import os import json +projectDesc = '《栩栩若生》电子书' projectUrl = 'https://github.com/dnomd343/xxrs-crawler' rootPath = os.path.join( # project root directory @@ -21,6 +22,7 @@ releaseInfo = { # release file name 'static': 'XXRS.tar.xz', 'calibre': 'xxrs.html', 'gitbook': 'xxrs-online/', + 'temp': 'xxrs-static/', } releaseInfo = { # convert to absolute path key: os.path.join(releasePath, file) for (key, file) in releaseInfo.items() @@ -30,12 +32,10 @@ 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', ], } diff --git a/src/release/utils/release.py b/src/release/utils/release.py index 6c58e2a..f112282 100644 --- a/src/release/utils/release.py +++ b/src/release/utils/release.py @@ -2,11 +2,13 @@ # -*- coding: utf-8 -*- import os +import json import shutil from .common import rootPath from .common import saveFile from .common import projectUrl +from .common import projectDesc from .common import releaseInfo from .common import createFolder from .common import resourceInfo @@ -14,28 +16,21 @@ 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) - ) + saveFile(releaseInfo['txt'], txtSerialize(metadata, content)) def jsonRelease(metadata: dict, content: dict) -> None: - saveFile( - releaseInfo['json'], jsonSerialize(metadata, content) - ) + saveFile(releaseInfo['json'], jsonSerialize(metadata, content)) def htmlRelease(metadata: dict, content: dict) -> None: - saveFile( - releaseInfo['calibre'], htmlSerialize(metadata, content) - ) + saveFile(releaseInfo['calibre'], htmlSerialize(metadata, content)) def gitbookRelease(metadata: dict, content: dict) -> None: @@ -50,17 +45,52 @@ def gitbookRelease(metadata: dict, content: dict) -> None: + '{% 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) - ) + 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 - ) + 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') ) + + +def staticDepends(workDir: str, metadata: dict, content: dict) -> None: + createFolder(os.path.join(workDir, './assets/')) + createFolder(os.path.join(workDir, './content/')) + + cover = gitbookMetadata(metadata) + '
\n' + for (resName, resUrls) in resourceInfo.items(): + cover += '\n{% hint style="tip" %}\n' \ + + '#### [%s](%s)([备用地址](%s))\n' % (resName, resUrls[0], resUrls[1]) \ + + '{% endhint %}\n' + + bookInfo = json.dumps({ + 'title': metadata['name'], + 'author': metadata['author'], + 'description': projectDesc, + "language": "zh-hans", + 'plugins': [ + '-search', '-sharing', 'hints', 'github', + 'fontsettings', 'image-captions', 'back-to-top-button' + ], + 'pluginsConfig': { + 'github': {'url': projectUrl} + } + }) + saveFile(os.path.join(workDir, 'README.md'), cover) + saveFile(os.path.join(workDir, 'book.json'), bookInfo) + saveFile(os.path.join(workDir, 'SUMMARY.md'), gitbookSummary(content)) + for (chapterPath, chapterContent) in gitbookChapters(content).items(): + saveFile(os.path.join(workDir, chapterPath), chapterContent) + shutil.copy( # gitbook cover + os.path.join(rootPath, './assets/cover.jpg'), + os.path.join(workDir, './assets/cover.jpg') + ) + + +def staticRelease(metadata: dict, content: dict) -> None: + createFolder(releaseInfo['temp']) + # TODO: using temp folder module + staticDepends(releaseInfo['temp'], metadata, content) + # TODO: building static html by docker `node:10-alpine` container diff --git a/src/release/utils/sequence.py b/src/release/utils/sequence.py index ba9bddc..dc2c63f 100644 --- a/src/release/utils/sequence.py +++ b/src/release/utils/sequence.py @@ -60,8 +60,8 @@ def htmlSerialize(metadata: dict, content: dict) -> str: 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'] + ) + '

%s

\n\n' % ( + 'text-align:center', 'assets/cover.jpg', metadata['name'] ) + '\n>\n'.join(['> %s' % x for x in metadata['desc']]) + '\n\n'