Browse Source

feat: static gitbook project build

master
Dnomd343 2 years ago
parent
commit
d97ef99653
  1. 2
      src/release/demo.py
  2. 1
      src/release/utils/__init__.py
  3. 4
      src/release/utils/common.py
  4. 68
      src/release/utils/release.py
  5. 4
      src/release/utils/sequence.py

2
src/release/demo.py

@ -5,6 +5,7 @@ from utils import loadBook
from utils import txtRelease from utils import txtRelease
from utils import jsonRelease from utils import jsonRelease
from utils import htmlRelease from utils import htmlRelease
from utils import staticRelease
from utils import gitbookRelease from utils import gitbookRelease
@ -13,4 +14,5 @@ metadata, content = loadBook('rc-5')
txtRelease(metadata, content) txtRelease(metadata, content)
jsonRelease(metadata, content) jsonRelease(metadata, content)
htmlRelease(metadata, content) htmlRelease(metadata, content)
staticRelease(metadata, content)
gitbookRelease(metadata, content) gitbookRelease(metadata, content)

1
src/release/utils/__init__.py

@ -6,4 +6,5 @@ from .common import loadBook
from .release import txtRelease from .release import txtRelease
from .release import jsonRelease from .release import jsonRelease
from .release import htmlRelease from .release import htmlRelease
from .release import staticRelease
from .release import gitbookRelease from .release import gitbookRelease

4
src/release/utils/common.py

@ -4,6 +4,7 @@
import os import os
import json import json
projectDesc = '《栩栩若生》电子书'
projectUrl = 'https://github.com/dnomd343/xxrs-crawler' projectUrl = 'https://github.com/dnomd343/xxrs-crawler'
rootPath = os.path.join( # project root directory rootPath = os.path.join( # project root directory
@ -21,6 +22,7 @@ releaseInfo = { # release file name
'static': 'XXRS.tar.xz', 'static': 'XXRS.tar.xz',
'calibre': 'xxrs.html', 'calibre': 'xxrs.html',
'gitbook': 'xxrs-online/', 'gitbook': 'xxrs-online/',
'temp': 'xxrs-static/',
} }
releaseInfo = { # convert to absolute path releaseInfo = { # convert to absolute path
key: os.path.join(releasePath, file) for (key, file) in releaseInfo.items() key: os.path.join(releasePath, file) for (key, file) in releaseInfo.items()
@ -30,12 +32,10 @@ resourceInfo = { # resource download links
'TXT 下载': [ 'TXT 下载': [
'https://res.343.re/Share/XXRS/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.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://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 下载': [ 'MOBI 下载': [
'https://res.343.re/Share/XXRS/%E6%A0%A9%E6%A0%A9%E8%8B%A5%E7%94%9F.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://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',
], ],
} }

68
src/release/utils/release.py

@ -2,11 +2,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import json
import shutil import shutil
from .common import rootPath from .common import rootPath
from .common import saveFile from .common import saveFile
from .common import projectUrl from .common import projectUrl
from .common import projectDesc
from .common import releaseInfo from .common import releaseInfo
from .common import createFolder from .common import createFolder
from .common import resourceInfo from .common import resourceInfo
@ -14,28 +16,21 @@ from .common import resourceInfo
from .sequence import txtSerialize from .sequence import txtSerialize
from .sequence import jsonSerialize from .sequence import jsonSerialize
from .sequence import htmlSerialize from .sequence import htmlSerialize
from .sequence import gitbookSummary from .sequence import gitbookSummary
from .sequence import gitbookChapters from .sequence import gitbookChapters
from .sequence import gitbookMetadata from .sequence import gitbookMetadata
def txtRelease(metadata: dict, content: dict) -> None: def txtRelease(metadata: dict, content: dict) -> None:
saveFile( saveFile(releaseInfo['txt'], txtSerialize(metadata, content))
releaseInfo['txt'], txtSerialize(metadata, content)
)
def jsonRelease(metadata: dict, content: dict) -> None: def jsonRelease(metadata: dict, content: dict) -> None:
saveFile( saveFile(releaseInfo['json'], jsonSerialize(metadata, content))
releaseInfo['json'], jsonSerialize(metadata, content)
)
def htmlRelease(metadata: dict, content: dict) -> None: def htmlRelease(metadata: dict, content: dict) -> None:
saveFile( saveFile(releaseInfo['calibre'], htmlSerialize(metadata, content))
releaseInfo['calibre'], htmlSerialize(metadata, content)
)
def gitbookRelease(metadata: dict, content: dict) -> None: def gitbookRelease(metadata: dict, content: dict) -> None:
@ -50,17 +45,52 @@ def gitbookRelease(metadata: dict, content: dict) -> None:
+ '{% endhint %}\n\n' + '{% endhint %}\n\n'
cover += '{%% embed url="%s" %%}\n项目地址\n{%% endembed %%}\n' % projectUrl cover += '{%% embed url="%s" %%}\n项目地址\n{%% endembed %%}\n' % projectUrl
saveFile( saveFile(os.path.join(releaseInfo['gitbook'], 'README.md'), cover)
os.path.join(releaseInfo['gitbook'], 'README.md'), cover saveFile(os.path.join(releaseInfo['gitbook'], 'SUMMARY.md'), gitbookSummary(content))
)
saveFile(
os.path.join(releaseInfo['gitbook'], 'SUMMARY.md'), gitbookSummary(content)
)
for (chapterPath, chapterContent) in gitbookChapters(content).items(): for (chapterPath, chapterContent) in gitbookChapters(content).items():
saveFile( saveFile(os.path.join(releaseInfo['gitbook'], chapterPath), chapterContent)
os.path.join(releaseInfo['gitbook'], chapterPath), chapterContent
)
shutil.copy( # gitbook cover shutil.copy( # gitbook cover
os.path.join(rootPath, './assets/cover.jpg'), os.path.join(rootPath, './assets/cover.jpg'),
os.path.join(releaseInfo['gitbook'], './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) + '<hr/>\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

4
src/release/utils/sequence.py

@ -60,8 +60,8 @@ def htmlSerialize(metadata: dict, content: dict) -> str:
def gitbookMetadata(metadata: dict) -> str: def gitbookMetadata(metadata: dict) -> str:
return '---\ndescription: 作者:%s\n---\n\n# %s\n\n' % ( return '---\ndescription: 作者:%s\n---\n\n# %s\n\n' % (
metadata['author'], metadata['name'] metadata['author'], metadata['name']
) + '<figure><img src="%s" alt=""><figcaption><p>%s</p></figcaption></figure>\n\n' % ( ) + '<figure style="%s"><img src="%s" alt=""><figcaption><p>%s</p></figcaption></figure>\n\n' % (
'assets/cover.jpg', metadata['name'] 'text-align:center', 'assets/cover.jpg', metadata['name']
) + '\n>\n'.join(['> %s' % x for x in metadata['desc']]) + '\n\n' ) + '\n>\n'.join(['> %s' % x for x in metadata['desc']]) + '\n\n'

Loading…
Cancel
Save