#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import re import json MarkdownSymbols = [ '\\', '`', '*', '_', '~', '{', '}', '[', ']', '(', ')', '#', '+', '-', '.', '!', '|', ] def markdownTransfer(content: str) -> str: for symbol in MarkdownSymbols: content = content.replace(symbol, '\\' + symbol) # add `\` before symbol return ' ' + content # add chinese indentation def jsonSerialize(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 txtSerialize(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 htmlSerialize(metadata: dict, content: dict) -> str: htmlContent = [ '', '', '
', '%s
' % x for x in metadata['desc']] for (caption, chapter) in content.items(): htmlContent.append('%s
' % x for x in chapter])) htmlContent += ['', ''] return '\n'.join(htmlContent) + '\n' def gitbookMetadata(metadata: dict) -> str: return ''.join([ '# %s\n\n' % metadata['name'], '%s