Browse Source

feat: gitbook release function

master
Dnomd343 2 years ago
parent
commit
3ef1e334df
  1. 19
      src/release/demo.py
  2. 87
      src/release/gitbook.py
  3. 9
      src/release/utils/__init__.py
  4. 15
      src/release/utils/common.py
  5. 66
      src/release/utils/release.py
  6. 53
      src/release/utils/sequence.py

19
src/release/demo.py

@ -2,22 +2,15 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from utils import loadBook from utils import loadBook
from utils import saveFile
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 releaseInfo from utils import gitbookRelease
metadata, content = loadBook('rc-5')
saveFile(
releaseInfo['txt'], txtRelease(metadata, content)
)
saveFile( metadata, content = loadBook('rc-5')
releaseInfo['json'], jsonRelease(metadata, content)
)
saveFile( txtRelease(metadata, content)
releaseInfo['calibre'], htmlRelease(metadata, content) jsonRelease(metadata, content)
) htmlRelease(metadata, content)
gitbookRelease(metadata, content)

87
src/release/gitbook.py

@ -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])

9
src/release/utils/__init__.py

@ -2,9 +2,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from .common import loadBook from .common import loadBook
from .common import saveFile
from .common import releaseInfo
from .sequence import txtRelease from .release import txtRelease
from .sequence import jsonRelease from .release import jsonRelease
from .sequence import htmlRelease from .release import htmlRelease
from .release import gitbookRelease

15
src/release/utils/common.py

@ -4,6 +4,8 @@
import os import os
import json import json
projectUrl = 'https://github.com/dnomd343/xxrs-crawler'
rootPath = os.path.join( # project root directory rootPath = os.path.join( # project root directory
os.path.dirname(os.path.realpath(__file__)), '../../../' 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() 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 def createFolder(folderName: str) -> None: # create folder
if not os.path.exists(folderName): if not os.path.exists(folderName):

66
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')
)

53
src/release/utils/sequence.py

@ -1,10 +1,24 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import re
import json 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 '&emsp;&emsp;' + content # add chinese indentation
def jsonSerialize(metadata: dict, content: dict) -> str:
return json.dumps({ return json.dumps({
'metadata': metadata, 'metadata': metadata,
'content': content, '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)] result = [txtMetadata(metadata)]
for (title, chapter) in content.items(): for (title, chapter) in content.items():
result.append('\n\n'.join([title] + chapter)) # combine txt content 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)] result = [htmlMetadata(metadata)]
for (title, chapter) in content.items(): for (title, chapter) in content.items():
result.append( result.append(
@ -41,3 +55,36 @@ def htmlRelease(metadata: dict, content: dict) -> str:
) )
) )
return '\n\n'.join(result) + '\n' return '\n\n'.join(result) + '\n'
def gitbookMetadata(metadata: dict) -> str:
return '---\ndescription: 作者:%s\n---\n\n# %s\n\n' % (
metadata['author'], metadata['name']
) + '<figure><img src="%s" alt=""><figcaption><p>%s</p></figcaption></figure>\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 += '## 内容 <a href="#content" id="content"></a>\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

Loading…
Cancel
Save