Dnomd343
2 years ago
6 changed files with 141 additions and 108 deletions
@ -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 += '<figure><img src="assets/cover.jpg" alt=""><figcaption><p>栩栩若生</p></figcaption></figure>\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 += '## 内容 <a href="#content" id="content"></a>\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]) |
@ -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') |
|||
) |
Loading…
Reference in new issue