Browse Source

refactor: release module structure

master
Dnomd343 2 years ago
parent
commit
6d8e7767e5
  1. 30
      src/release/calibre_html.py
  2. 45
      src/release/common.py
  3. 23
      src/release/demo.py
  4. 23
      src/release/json_dumps.py
  5. 30
      src/release/txt_release.py
  6. 10
      src/release/utils/__init__.py
  7. 46
      src/release/utils/common.py
  8. 43
      src/release/utils/sequence.py

30
src/release/calibre_html.py

@ -1,30 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from common import loadData
from common import saveFile
from common import releaseInfo
def formatMetadata(metadata: dict) -> str:
return '<h1>%s</h1>\n' % metadata['name'] + '\n'.join(
['<p>%s</p>' % x for x in metadata['desc']]
)
def htmlRelease(metadata: dict, content: dict) -> str:
result = [formatMetadata(metadata)]
for (title, chapter) in content.items():
result.append(
'<h2>%s</h2>\n' % title + '\n'.join(['<p>%s</p>' % x for x in chapter])
)
return '\n\n'.join(result) + '\n'
if __name__ == '__main__':
data = loadData(sys.argv[1])
saveFile(
releaseInfo['htmlFile'],
htmlRelease(data['metadata'], data['content'])
)

45
src/release/common.py

@ -1,45 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import json
rootPath = os.path.join(
os.path.dirname(os.path.realpath(__file__)), '../../'
)
dataPath = os.path.join(rootPath, './release/')
releasePath = os.path.join(dataPath, './output/')
metadataFile = os.path.join(rootPath, './assets/metadata.json')
releaseInfo = {
'htmlFile': 'xxrs.html',
'jsonFile': 'XXRS.json',
'txtFile': '栩栩若生.txt',
'gitbookDir': 'xxrs-online',
}
releaseInfo = {x: os.path.join(releasePath, y) for (x, y) in releaseInfo.items()}
def loadData(jsonName: str) -> dict: # load book data from json file
content = json.loads(open(
os.path.join(dataPath, '%s.json' % jsonName)
).read())
metadata = json.loads(open(metadataFile).read())
return {
'metadata': metadata,
'content': content,
}
def saveFile(fileName: str, content: str) -> None: # save into file
with open(fileName, 'w') as fileObj:
fileObj.write(content)
def createFolder(folderName: str) -> None: # create folder
if not os.path.exists(folderName):
os.mkdir(folderName)
createFolder(releasePath)

23
src/release/demo.py

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- 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
metadata, content = loadBook('rc-5')
saveFile(
releaseInfo['txt'], txtRelease(metadata, content)
)
saveFile(
releaseInfo['json'], jsonRelease(metadata, content)
)
saveFile(
releaseInfo['calibre'], htmlRelease(metadata, content)
)

23
src/release/json_dumps.py

@ -1,23 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import json
from common import loadData
from common import saveFile
from common import releaseInfo
def jsonRelease(metadata: dict, content: dict) -> str:
return json.dumps({
'metadata': metadata,
'content': content,
}, separators = (',', ':')) # without space
if __name__ == '__main__':
data = loadData(sys.argv[1])
saveFile(
releaseInfo['jsonFile'],
jsonRelease(data['metadata'], data['content'])
)

30
src/release/txt_release.py

@ -1,30 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from common import loadData
from common import saveFile
from common import releaseInfo
def formatMetadata(metadata: dict) -> str:
return '%s\n\n作者:%s\n\n\n%s' % (
metadata['name'],
metadata['author'],
'\n\n'.join(metadata['desc']),
)
def txtRelease(metadata: dict, content: dict) -> str:
result = [formatMetadata(metadata)]
for (title, chapter) in content.items():
result.append('\n\n'.join([title] + chapter))
return '\n\n\n'.join(result) + '\n'
if __name__ == '__main__':
data = loadData(sys.argv[1])
saveFile(
releaseInfo['txtFile'],
txtRelease(data['metadata'], data['content'])
)

10
src/release/utils/__init__.py

@ -0,0 +1,10 @@
#!/usr/bin/env python3
# -*- 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

46
src/release/utils/common.py

@ -0,0 +1,46 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import json
rootPath = os.path.join( # project root directory
os.path.dirname(os.path.realpath(__file__)), '../../../'
)
dataPath = os.path.join(rootPath, './release/')
releasePath = os.path.join(dataPath, './output/')
metadataFile = os.path.join(rootPath, './assets/metadata.json')
releaseInfo = { # release file name
'json': 'XXRS.json',
'txt': '栩栩若生.txt',
'epub': '栩栩若生.epub',
'mobi': '栩栩若生.mobi',
'static': 'XXRS.tar.xz',
'calibre': 'xxrs.html',
'gitbook': 'xxrs-online/',
}
releaseInfo = { # convert to absolute path
key: os.path.join(releasePath, file) for (key, file) in releaseInfo.items()
}
def createFolder(folderName: str) -> None: # create folder
if not os.path.exists(folderName):
os.mkdir(folderName)
def saveFile(fileName: str, content: str) -> None: # save into file
with open(fileName, 'w') as fileObj:
fileObj.write(content)
def loadBook(jsonName: str) -> tuple[dict, dict]: # load book data from json file
content = json.loads(open(
os.path.join(dataPath, '%s.json' % jsonName) # book content
).read())
metadata = json.loads(open(metadataFile).read()) # book metadata
return metadata, content
createFolder(releasePath) # create output folder

43
src/release/utils/sequence.py

@ -0,0 +1,43 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
def jsonRelease(metadata: dict, content: dict) -> str:
return json.dumps({
'metadata': metadata,
'content': content,
}, separators = (',', ':')) # without space
def txtMetadata(metadata: dict) -> str: # txt metadata
return '%s\n\n作者:%s\n\n\n%s' % (
metadata['name'],
metadata['author'],
'\n\n'.join(metadata['desc']),
)
def txtRelease(metadata: dict, content: dict) -> str:
result = [txtMetadata(metadata)]
for (title, chapter) in content.items():
result.append('\n\n'.join([title] + chapter)) # combine txt content
return '\n\n\n'.join(result) + '\n'
def htmlMetadata(metadata: dict) -> str: # html metadata
return '<h1>%s</h1>\n' % metadata['name'] + '\n'.join(
['<p>%s</p>' % x for x in metadata['desc']]
)
def htmlRelease(metadata: dict, content: dict) -> str:
result = [htmlMetadata(metadata)]
for (title, chapter) in content.items():
result.append(
'<h2>%s</h2>\n' % title + '\n'.join(
['<p>%s</p>' % x for x in chapter]
)
)
return '\n\n'.join(result) + '\n'
Loading…
Cancel
Save